/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package xmpp;
import java.io.IOException;
import java.sql.SQLException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import sun.misc.BASE64Encoder;
/**
*
* @author BIJOY
*/
public class XMPP implements Runnable
{
/**
* @param args the command line arguments
*/
// Variables
public static String XMPPUser;
private String XMPPPass;
private String XMPPSer;
public static String XMPPDom;
private XMLOutputFactory factory;
public static XMLStreamWriter writer;
public static SSLSocket sslsocket;
private SSLSocketFactory sslsocketfactory;
public static Thread th;
public static String Error;
// Functions
XMPP(String XUser,String XPass,String XServer)
{
XMPPUser=XUser;
XMPPPass=XPass;
XMPPSer=XServer;
Error="No_Error";
if(XServer.equals("talk.google.com"))
{
XMPPDom="gmail.com";
}
else
{
XMPPDom=XServer;
}
}
// To initialize general objects
void initialize(SSLSocket ssl) throws IOException,javax.xml.stream.XMLStreamException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException
{
factory = XMLOutputFactory.newInstance();
writer = factory.createXMLStreamWriter(ssl.getOutputStream()) ;
}
@Override
public void run () {
// TODO code application logic here
try
{
sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
sslsocket = (SSLSocket) sslsocketfactory.createSocket(XMPPSer,5223);
sslsocket.setUseClientMode(true);
sslsocket.setKeepAlive(true);
sslsocket.setSoTimeout(0);
if(sslsocket.isConnected())
{
initialize(sslsocket);
}
else{
//System.out.println("Failed to connect");
System.exit(0);
}
writer.writeStartDocument("1.0");
writer.setPrefix("stream","http://etherx.jabber.org/streams");
writer.writeStartElement("http://etherx.jabber.org/streams","stream");
writer.writeAttribute("xmlns","jabber:client");
writer.writeAttribute("from", XMPPUser);
writer.writeAttribute("to", XMPPDom);
writer.writeNamespace("stream", "http://etherx.jabber.org/streams");
writer.writeAttribute("version", "1.0");
writer.writeCharacters("\n");
writer.flush();
// Reading Thread invoking here
XMLParser XmlParse = new XMLParser();
th=new Thread(XmlParse);
th.start();
Thread.sleep(2000);
// System.out.println(XMLParser.method);
//th.sleep(2000);
writer.writeStartElement("auth");
writer.writeAttribute("xmlns","urn:ietf:params:xml:ns:xmpp-sasl");
writer.writeAttribute("mechanism","PLAIN");
writer.writeCharacters(new BASE64Encoder().encode(((char)0+XMPPUser+(char)0+XMPPPass).getBytes()));
writer.writeEndElement();
writer.flush();
//th.sleep(2000);
writer.writeStartDocument("1.0");
writer.setPrefix("stream","http://etherx.jabber.org/streams");
writer.writeStartElement("http://etherx.jabber.org/streams","stream");
writer.writeAttribute("xmlns","jabber:client");
writer.writeAttribute("from", XMPPUser);
writer.writeAttribute("to", XMPPDom);
writer.writeNamespace("stream", "http://etherx.jabber.org/streams");
writer.writeAttribute("version", "1.0");
writer.writeCharacters("\n");
writer.flush();
// th.sleep(2000);
writer.writeStartElement("iq");
writer.writeAttribute("type","set");
writer.writeStartElement("bind");
writer.writeAttribute("xmlns","urn:ietf:params:xml:ns:xmpp-bind");
writer.writeStartElement("resource");
writer.writeCharacters("bChat");
writer.writeEndElement();
writer.writeEndElement();
writer.writeEndElement();
writer.flush();
// th.sleep(2000);
writer.writeStartElement("iq");
writer.writeAttribute("type","set");
writer.writeAttribute("id", "b1h4r9ry");
writer.writeStartElement("session");
writer.writeAttribute("xmlns","urn:ietf:params:xml:ns:xmpp-session");
writer.writeEndElement();
writer.writeEndElement();
writer.flush();
// th.sleep(2000);
writer.writeStartElement("presence");
writer.writeStartElement("show");
writer.writeCharacters("chat");
writer.writeEndElement();
writer.writeEndElement();
writer.flush();
writer.writeStartElement("iq");
writer.writeAttribute("type","get");
writer.writeEmptyElement("query");
writer.writeAttribute("xmlns", "jabber:iq:roster");
writer.writeEndElement();
writer.writeCharacters("\n");
writer.flush();
}
catch(Exception e){
System.out.println(e);
Error=e.toString();
};
}
public static void main(String[] args) throws XMLStreamException, IOException {
new XMPPClient().setVisible(true);
}
}