Advertisement
Guest User

Bijoy

a guest
Jan 15th, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.10 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package xmpp;
  6.  
  7. import java.io.IOException;
  8. import java.sql.SQLException;
  9. import javax.net.ssl.SSLSocket;
  10. import javax.net.ssl.SSLSocketFactory;
  11. import javax.xml.stream.XMLOutputFactory;
  12. import javax.xml.stream.XMLStreamException;
  13. import javax.xml.stream.XMLStreamWriter;
  14. import sun.misc.BASE64Encoder;
  15.  
  16. /**
  17.  *
  18.  * @author BIJOY
  19.  */
  20. public class XMPP implements Runnable
  21. {
  22.     /**
  23.      * @param args the command line arguments
  24.      */
  25.     // Variables
  26.     public static String XMPPUser;
  27.     private String XMPPPass;
  28.     private String XMPPSer;
  29.     public static String XMPPDom;
  30.     private XMLOutputFactory factory;
  31.     public static XMLStreamWriter writer;
  32.     public static  SSLSocket sslsocket;
  33.     private SSLSocketFactory sslsocketfactory;
  34.     public static Thread th;
  35.     public static String Error;
  36.  
  37.  
  38.     // Functions
  39.     XMPP(String XUser,String XPass,String XServer)
  40.     {
  41.         XMPPUser=XUser;
  42.         XMPPPass=XPass;
  43.         XMPPSer=XServer;
  44.         Error="No_Error";
  45.         if(XServer.equals("talk.google.com"))
  46.         {
  47.             XMPPDom="gmail.com";
  48.         }
  49.         else
  50.         {
  51.             XMPPDom=XServer;
  52.         }
  53.     }
  54.     // To initialize general objects
  55.     void initialize(SSLSocket ssl) throws IOException,javax.xml.stream.XMLStreamException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException
  56.     {
  57.        
  58.             factory = XMLOutputFactory.newInstance();
  59.             writer = factory.createXMLStreamWriter(ssl.getOutputStream()) ;
  60.            
  61.     }
  62.  
  63.    
  64.     @Override
  65.     public void run () {
  66.         // TODO code application logic here
  67.        
  68.         try
  69.         {    
  70.            
  71.            
  72.  
  73.            sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
  74.            sslsocket = (SSLSocket) sslsocketfactory.createSocket(XMPPSer,5223);
  75.            sslsocket.setUseClientMode(true);
  76.            sslsocket.setKeepAlive(true);
  77.            sslsocket.setSoTimeout(0);
  78.            if(sslsocket.isConnected())
  79.            {
  80.                 initialize(sslsocket);
  81.  
  82.            }
  83.            else{
  84.                
  85.                //System.out.println("Failed to connect");
  86.                System.exit(0);
  87.                
  88.                
  89.            }
  90.            writer.writeStartDocument("1.0");
  91.            writer.setPrefix("stream","http://etherx.jabber.org/streams");
  92.            writer.writeStartElement("http://etherx.jabber.org/streams","stream");
  93.            writer.writeAttribute("xmlns","jabber:client");
  94.            writer.writeAttribute("from", XMPPUser);
  95.            writer.writeAttribute("to", XMPPDom);
  96.            writer.writeNamespace("stream", "http://etherx.jabber.org/streams");
  97.            writer.writeAttribute("version", "1.0");
  98.            writer.writeCharacters("\n");
  99.            writer.flush();
  100.          
  101.             // Reading Thread invoking here
  102.                        
  103.  
  104.          
  105.           XMLParser XmlParse = new XMLParser();
  106.           th=new Thread(XmlParse);
  107.           th.start();
  108.           Thread.sleep(2000);
  109.          
  110.          
  111.        //   System.out.println(XMLParser.method);  
  112.          
  113.           //th.sleep(2000);
  114.          
  115.  
  116.           writer.writeStartElement("auth");
  117.           writer.writeAttribute("xmlns","urn:ietf:params:xml:ns:xmpp-sasl");
  118.           writer.writeAttribute("mechanism","PLAIN");
  119.           writer.writeCharacters(new BASE64Encoder().encode(((char)0+XMPPUser+(char)0+XMPPPass).getBytes()));
  120.           writer.writeEndElement();
  121.           writer.flush();
  122.            
  123.            
  124.           //th.sleep(2000);
  125.          
  126.            writer.writeStartDocument("1.0");
  127.            writer.setPrefix("stream","http://etherx.jabber.org/streams");
  128.            writer.writeStartElement("http://etherx.jabber.org/streams","stream");
  129.            writer.writeAttribute("xmlns","jabber:client");
  130.            writer.writeAttribute("from", XMPPUser);
  131.            writer.writeAttribute("to", XMPPDom);
  132.            writer.writeNamespace("stream", "http://etherx.jabber.org/streams");
  133.            writer.writeAttribute("version", "1.0");
  134.            writer.writeCharacters("\n");
  135.            writer.flush();
  136.          
  137.          // th.sleep(2000);
  138.        
  139.            writer.writeStartElement("iq");
  140.            writer.writeAttribute("type","set");
  141.            writer.writeStartElement("bind");
  142.            writer.writeAttribute("xmlns","urn:ietf:params:xml:ns:xmpp-bind");
  143.            writer.writeStartElement("resource");
  144.            writer.writeCharacters("bChat");
  145.            writer.writeEndElement();
  146.            writer.writeEndElement();
  147.            writer.writeEndElement();
  148.            writer.flush();
  149.            
  150.           // th.sleep(2000);
  151.            writer.writeStartElement("iq");
  152.            writer.writeAttribute("type","set");
  153.            writer.writeAttribute("id", "b1h4r9ry");
  154.            writer.writeStartElement("session");
  155.            writer.writeAttribute("xmlns","urn:ietf:params:xml:ns:xmpp-session");
  156.            writer.writeEndElement();
  157.            writer.writeEndElement();
  158.            writer.flush();
  159.            
  160.            
  161.            
  162.           // th.sleep(2000);
  163.            writer.writeStartElement("presence");
  164.            writer.writeStartElement("show");
  165.            writer.writeCharacters("chat");
  166.            writer.writeEndElement();
  167.            writer.writeEndElement();
  168.            writer.flush();
  169.            
  170.            
  171.  
  172.           writer.writeStartElement("iq");
  173.           writer.writeAttribute("type","get");
  174.           writer.writeEmptyElement("query");
  175.           writer.writeAttribute("xmlns", "jabber:iq:roster");
  176.           writer.writeEndElement();
  177.           writer.writeCharacters("\n");
  178.           writer.flush();
  179.        
  180.         }
  181.        
  182.         catch(Exception e){
  183.             System.out.println(e);
  184.             Error=e.toString();
  185.        
  186.         };
  187.        
  188.     }
  189.     public static void main(String[] args) throws XMLStreamException, IOException {
  190.        
  191.         new XMPPClient().setVisible(true);
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement