final XMLEvent event = (XMLEvent) e.getMessage();
switch (status) {
case CONNECT:
if (event.isStartElement()) {
final StartElement element = event.asStartElement();
if (STREAM_NAME.equals(element.getName()) && XMPPNamespaces.ACCEPT.equals(element.getNamespaceURI(null))) {
if (!serverName.equals(element.getAttributeByName(new QName("from")).getValue())) {
throw new Exception("server name mismatch");
}
streamID = element.getAttributeByName(new QName("id")).getValue();
status = Status.AUTHENTICATE;
Channels.write(ctx.getChannel(), ChannelBuffers.copiedBuffer("<handshake>" + Hashing.sha1().hashString(streamID + secret, CharsetUtil.UTF_8).toString() + "</handshake>", CharsetUtil.UTF_8));
}
} else {
throw new Exception("Expected stream:stream element");
}
break;
case AUTHENTICATE:
case READY:
if (event.isEndElement()) {
final EndElement element = event.asEndElement();
if (STREAM_NAME.equals(element.getName())) {
Channels.disconnect(ctx.getChannel());
return;
}
}
break;
case DISCONNECTED:
throw new Exception("received DISCONNECTED");
}
}
else if (e.getMessage() instanceof XMLElement) {
final XMLElement element = (XMLElement) e.getMessage();
switch (status) {
case AUTHENTICATE:
if (!"handshake".equals(element.getTagName()))
throw new Exception("expected handshake");
status = Status.READY;
System.out.println("logged in");
ctx.getPipeline().get(XMPPStreamHandler.class).loggedIn();