Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import org.jivesoftware.smack.Chat;
- import org.jivesoftware.smack.ConnectionConfiguration;
- import org.jivesoftware.smack.MessageListener;
- import org.jivesoftware.smack.Roster;
- import org.jivesoftware.smack.RosterEntry;
- import org.jivesoftware.smack.XMPPConnection;
- import org.jivesoftware.smack.XMPPException;
- import org.jivesoftware.smack.packet.Message;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- import javax.net.SocketFactory;
- import javax.net.ssl.SSLSocketFactory;
- import java.io.IOException;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.security.KeyManagementException;
- import java.security.NoSuchAlgorithmException;
- import java.security.cert.CertificateExpiredException;
- import java.security.cert.CertificateNotYetValidException;
- import java.security.cert.X509Certificate;
- import java.security.cert.CertificateException;
- public class LoLChat implements MessageListener{
- XMPPConnection connection;
- private static String username = "username";
- private static String password = "password";
- public void login() throws XMPPException
- {
- ConnectionConfiguration config = new ConnectionConfiguration("chat.na.lol.riotgames.com",5223, "pvp.net");
- config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
- config.setSocketFactory(new DummySSLSocketFactory());
- connection = new XMPPConnection(config);
- connection.connect();
- connection.login(username, password);
- }
- public void sendMessage(String message, String to) throws XMPPException
- {
- Chat chat = connection.getChatManager().createChat(to, this);
- chat.sendMessage(message);
- }
- public void displayBuddyList()
- {
- Roster roster = connection.getRoster();
- Collection<RosterEntry> entries = roster.getEntries();
- System.out.println("\n\n" + entries.size() + " buddy(ies):");
- for(RosterEntry r:entries)
- {
- System.out.println(r.getUser());
- }
- }
- public void disconnect()
- {
- connection.disconnect();
- }
- public void processMessage(Chat chat, Message message)
- {
- if(message.getType() == Message.Type.chat)
- System.out.println(chat.getParticipant() + " says: " + message.getBody());
- }
- public static void main(String args[]) throws XMPPException, IOException
- {
- // declare variables
- LoLChat c = new LoLChat();
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String msg;
- // turn on the enhanced debugger
- XMPPConnection.DEBUG_ENABLED = true;
- // Enter your login information here
- c.login();
- c.displayBuddyList();
- System.out.println("-----");
- System.out.println("Who do you want to talk to? - Type contacts full email address:");
- String talkTo = br.readLine();
- System.out.println("-----");
- System.out.println("All messages will be sent to " + talkTo);
- System.out.println("Enter your message in the console:");
- System.out.println("-----\n");
- while( !(msg=br.readLine()).equals("bye"))
- {
- c.sendMessage(msg, talkTo);
- }
- c.disconnect();
- System.exit(0);
- }
- }
- class DummySSLSocketFactory extends SSLSocketFactory {
- private SSLSocketFactory factory;
- public DummySSLSocketFactory() {
- try {
- SSLContext sslcontent = SSLContext.getInstance("TLS");
- sslcontent.init(null, // KeyManager not required
- new TrustManager[]{new DummyTrustManager()},
- new java.security.SecureRandom());
- factory = sslcontent.getSocketFactory();
- }
- catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- catch (KeyManagementException e) {
- e.printStackTrace();
- }
- }
- public static SocketFactory getDefault() {
- return new DummySSLSocketFactory();
- }
- public Socket createSocket(Socket socket, String s, int i, boolean flag)
- throws IOException {
- return factory.createSocket(socket, s, i, flag);
- }
- public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr2, int j)
- throws IOException {
- return factory.createSocket(inaddr, i, inaddr2, j);
- }
- public Socket createSocket(InetAddress inaddr, int i) throws IOException {
- return factory.createSocket(inaddr, i);
- }
- public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException {
- return factory.createSocket(s, i, inaddr, j);
- }
- public Socket createSocket(String s, int i) throws IOException {
- return factory.createSocket(s, i);
- }
- public String[] getDefaultCipherSuites() {
- return factory.getSupportedCipherSuites();
- }
- public String[] getSupportedCipherSuites() {
- return factory.getSupportedCipherSuites();
- }
- }
- /**
- * Trust manager which accepts certificates without any validation
- * except date validation.
- */
- class DummyTrustManager implements X509TrustManager {
- public boolean isClientTrusted(X509Certificate[] cert) {
- return true;
- }
- public boolean isServerTrusted(X509Certificate[] cert) {
- try {
- cert[0].checkValidity();
- return true;
- }
- catch (CertificateExpiredException e) {
- return false;
- }
- catch (CertificateNotYetValidException e) {
- return false;
- }
- }
- public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
- // Do nothing for now.
- }
- public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
- // Do nothing for now.
- }
- public X509Certificate[] getAcceptedIssuers() {
- return new X509Certificate[0];
- }
- }
Add Comment
Please, Sign In to add comment