Guest User

Untitled

a guest
Dec 11th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.96 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. import org.jivesoftware.smack.Chat;
  5. import org.jivesoftware.smack.ConnectionConfiguration;
  6. import org.jivesoftware.smack.MessageListener;
  7. import org.jivesoftware.smack.Roster;
  8. import org.jivesoftware.smack.RosterEntry;
  9. import org.jivesoftware.smack.XMPPConnection;
  10. import org.jivesoftware.smack.XMPPException;
  11. import org.jivesoftware.smack.packet.Message;
  12.  
  13.  
  14. import javax.net.ssl.SSLContext;
  15. import javax.net.ssl.TrustManager;
  16. import javax.net.ssl.X509TrustManager;
  17. import javax.net.SocketFactory;
  18. import javax.net.ssl.SSLSocketFactory;
  19. import java.io.IOException;
  20. import java.net.InetAddress;
  21. import java.net.Socket;
  22. import java.security.KeyManagementException;
  23. import java.security.NoSuchAlgorithmException;
  24. import java.security.cert.CertificateExpiredException;
  25. import java.security.cert.CertificateNotYetValidException;
  26. import java.security.cert.X509Certificate;
  27. import java.security.cert.CertificateException;
  28.  
  29. public class LoLChat implements MessageListener{
  30.  
  31.     XMPPConnection connection;
  32.     private static String username = "username";
  33.     private static String password = "password";
  34.     public void login() throws XMPPException
  35.     {
  36.     ConnectionConfiguration config = new ConnectionConfiguration("chat.na.lol.riotgames.com",5223, "pvp.net");
  37.     config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
  38.     config.setSocketFactory(new DummySSLSocketFactory());
  39.     connection = new XMPPConnection(config);
  40.  
  41.     connection.connect();
  42.     connection.login(username, password);
  43.     }
  44.  
  45.     public void sendMessage(String message, String to) throws XMPPException
  46.     {
  47.     Chat chat = connection.getChatManager().createChat(to, this);
  48.     chat.sendMessage(message);
  49.     }
  50.  
  51.     public void displayBuddyList()
  52.     {
  53.     Roster roster = connection.getRoster();
  54.     Collection<RosterEntry> entries = roster.getEntries();
  55.  
  56.     System.out.println("\n\n" + entries.size() + " buddy(ies):");
  57.     for(RosterEntry r:entries)
  58.     {
  59.     System.out.println(r.getUser());
  60.     }
  61.     }
  62.  
  63.     public void disconnect()
  64.     {
  65.     connection.disconnect();
  66.     }
  67.  
  68.     public void processMessage(Chat chat, Message message)
  69.     {
  70.     if(message.getType() == Message.Type.chat)
  71.     System.out.println(chat.getParticipant() + " says: " + message.getBody());
  72.     }
  73.  
  74.     public static void main(String args[]) throws XMPPException, IOException
  75.     {
  76.     // declare variables
  77.     LoLChat c = new LoLChat();
  78.     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  79.     String msg;
  80.  
  81.  
  82.     // turn on the enhanced debugger
  83.     XMPPConnection.DEBUG_ENABLED = true;
  84.  
  85.  
  86.     // Enter your login information here
  87.     c.login();
  88.  
  89.     c.displayBuddyList();
  90.  
  91.     System.out.println("-----");
  92.  
  93.     System.out.println("Who do you want to talk to? - Type contacts full email address:");
  94.     String talkTo = br.readLine();
  95.  
  96.     System.out.println("-----");
  97.     System.out.println("All messages will be sent to " + talkTo);
  98.     System.out.println("Enter your message in the console:");
  99.     System.out.println("-----\n");
  100.  
  101.     while( !(msg=br.readLine()).equals("bye"))
  102.     {
  103.         c.sendMessage(msg, talkTo);
  104.     }
  105.  
  106.     c.disconnect();
  107.     System.exit(0);
  108.     }
  109. }
  110.  
  111. class DummySSLSocketFactory extends SSLSocketFactory {
  112.  
  113.     private SSLSocketFactory factory;
  114.  
  115.     public DummySSLSocketFactory() {
  116.  
  117.         try {
  118.             SSLContext sslcontent = SSLContext.getInstance("TLS");
  119.             sslcontent.init(null, // KeyManager not required
  120.                     new TrustManager[]{new DummyTrustManager()},
  121.                     new java.security.SecureRandom());
  122.             factory = sslcontent.getSocketFactory();
  123.         }
  124.         catch (NoSuchAlgorithmException e) {
  125.             e.printStackTrace();
  126.         }
  127.         catch (KeyManagementException e) {
  128.             e.printStackTrace();
  129.         }
  130.     }
  131.  
  132.     public static SocketFactory getDefault() {
  133.         return new DummySSLSocketFactory();
  134.     }
  135.  
  136.     public Socket createSocket(Socket socket, String s, int i, boolean flag)
  137.             throws IOException {
  138.         return factory.createSocket(socket, s, i, flag);
  139.     }
  140.  
  141.     public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr2, int j)
  142.             throws IOException {
  143.         return factory.createSocket(inaddr, i, inaddr2, j);
  144.     }
  145.  
  146.     public Socket createSocket(InetAddress inaddr, int i) throws IOException {
  147.         return factory.createSocket(inaddr, i);
  148.     }
  149.  
  150.     public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException {
  151.         return factory.createSocket(s, i, inaddr, j);
  152.     }
  153.  
  154.     public Socket createSocket(String s, int i) throws IOException {
  155.         return factory.createSocket(s, i);
  156.     }
  157.  
  158.     public String[] getDefaultCipherSuites() {
  159.         return factory.getSupportedCipherSuites();
  160.     }
  161.  
  162.     public String[] getSupportedCipherSuites() {
  163.         return factory.getSupportedCipherSuites();
  164.     }
  165. }
  166.  
  167. /**
  168.  * Trust manager which accepts certificates without any validation
  169.  * except date validation.
  170.  */
  171. class DummyTrustManager implements X509TrustManager {
  172.  
  173.     public boolean isClientTrusted(X509Certificate[] cert) {
  174.         return true;
  175.     }
  176.  
  177.     public boolean isServerTrusted(X509Certificate[] cert) {
  178.         try {
  179.             cert[0].checkValidity();
  180.             return true;
  181.         }
  182.         catch (CertificateExpiredException e) {
  183.             return false;
  184.         }
  185.         catch (CertificateNotYetValidException e) {
  186.             return false;
  187.         }
  188.     }
  189.  
  190.     public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
  191.         // Do nothing for now.
  192.     }
  193.  
  194.     public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
  195.         // Do nothing for now.
  196.     }
  197.  
  198.     public X509Certificate[] getAcceptedIssuers() {
  199.         return new X509Certificate[0];
  200.     }
  201. }
Add Comment
Please, Sign In to add comment