//System.out.println("+readMESSAGE+");
Message message = new Message(_node); // takes the subject&body of the message
String threadId = "";
Conversation conversation = null; // conversation corresponding to the message
Jid rosterFrom;
if (_node.getChild("thread") != null) {
threadId = _node.getChild("thread").text;
} else {
// _node has no "thread" child: server message?
if (message.from.equalsIgnoreCase(Datas.hostname) || Datas.isGateway(message.from))
{
System.out.println("server message");
conversation = new Conversation(message.from);
conversation.appendToMe(message);
jabberListener.newMessageEvent(conversation, -1);
return;
}
}
//groupchat invitation management
if ((stanzaType == null || stanzaType.equals("normal")) && _node.getChild("x", "xmlns", "jabber:x:conference") != null) {
String jidfrom;
String room;
//check if server uses new MUC protocol
if (_node.getChild("x", "xmlns", "http://jabber.org/protocol/muc#user") != null) {
Node invite = _node.getChild("x", "xmlns", "http://jabber.org/protocol/muc#user");
if (invite.getChild("invite") != null) {
jidfrom = invite.getChild("invite").getValue("from");
room = stanzaFrom;
jabberListener.newInvitationEvent(jidfrom, room);
return;
}
}//check if server uses old protocol
else if (_node.getChild("body") != null && _node.getChild("body").text.startsWith("You have been invited")) {
Node invite = _node.getChild("x", "xmlns", "jabber:x:conference");
jidfrom = stanzaFrom;
room = invite.getValue("jid");
jabberListener.newInvitationEvent(jidfrom, room);
return;
}
}
//Composing management
else if (stanzaType.equals("chat") && _node.getChild("composing", "xmlns", "http://jabber.org/protocol/chatstates") != null) {
Vector conversations = Datas.conversations;
Conversation convers = null;
// finds out the conversation, if already exists
for(int i=0; i<conversations.size(); i++) {
convers = (Conversation) conversations.elementAt(i);
if (convers.match(_node)) {
convers.composing = Jid.getUsername(convers.name) + Contents.composing;
jabberListener.newComposingEvent(convers);
break;
}
}
return;
}
else if (stanzaType.equals("chat") && _node.getChild("inactive", "xmlns", "http://jabber.org/protocol/chatstates") != null) {
Vector conversations = Datas.conversations;
Conversation convers = null;
// finds out the conversation, if already exists
for(int i=0; i<conversations.size(); i++) {
convers = (Conversation) conversations.elementAt(i);
if (convers.match(_node)) {
convers.composing = Jid.getUsername(convers.name) + Contents.inactive;
jabberListener.newComposingEvent(convers);
break;
}
}
return;
}
else if (stanzaType.equals("chat") && _node.getChild("x", "xmlns", "jabber:x:event") != null && _node.getChild("body") == null) {
Node x = _node.getChild("x", "xmlns", "jabber:x:event");
if (x.getChild("composing") != null) {
Vector conversations = Datas.conversations;
Conversation convers = null;
for(int i=0; i<conversations.size(); i++) {
convers = (Conversation) conversations.elementAt(i);
if (convers.match(_node)) {
convers.composing = Jid.getUsername(convers.name) + Contents.composing;
jabberListener.newComposingEvent(convers);
break;
}
}
return;
}
}
Vector conversations = Datas.conversations;
// finds out the conversation, if already exists
int i=0;
boolean found=false;
while ((i<conversations.size()) && !found) {
conversation = (Conversation) conversations.elementAt(i);
found = conversation.match(_node);
i++;
}
// default stanza type if not specified.
if (stanzaType == null) {
stanzaType = "normal";
}
if (found == false) { // no conversation with this roster is running
if (stanzaType.equals("error")) {
message.addError(_node.getChild("error").text);
jabberListener.notifyError(null, message);
}
else if (stanzaType.equals("normal")
|| stanzaType.equals("chat") ) {
rosterFrom = (Jid) Datas.roster.get(Jid.getLittleJid(stanzaFrom));
if (rosterFrom == null) {
// the roster is not known
Jid newjid = new Jid(stanzaFrom, "online");
rosterFrom = newjid;
Datas.registerRoster(newjid);
}
// normal: default message type. reply expected. no history.
// chat: peer to peer communication. with history.
SingleChat chat = new SingleChat(rosterFrom, stanzaType, threadId);
chat.appendToMe(message);
// registers this new conversation
conversations.addElement(chat);
jabberListener.newConversationEvent(chat);
}
else if (stanzaType.equals("groupchat") && (stanzaFrom.indexOf("/") == -1)) {
System.out.println("My message");
return;
/*Conversation conversation = ChatHelper.createChat(partners, littleFrom, nick);
conversation.appendToMe(new Message("", "Added to this room"));
conversation.isMulti = true;
Datas.conversations.addElement(conversation);
jabberListener.newConversationEvent(conversation);
return;*/
}
else if (stanzaType.equals("headline")) {
rosterFrom = (Jid) Datas.roster.get(stanzaFrom);
if (rosterFrom == null) {
// the roster is not known
Jid newjid = new Jid(stanzaFrom);
rosterFrom = newjid;
if (stanzaFrom.indexOf("@") != -1) //is user
{
Datas.registerRoster(newjid);