MessageParser.java

  1package eu.siacs.conversations.parser;
  2
  3import android.util.Log;
  4
  5import net.java.otr4j.session.Session;
  6import net.java.otr4j.session.SessionStatus;
  7
  8import eu.siacs.conversations.Config;
  9import eu.siacs.conversations.entities.Account;
 10import eu.siacs.conversations.entities.Contact;
 11import eu.siacs.conversations.entities.Conversation;
 12import eu.siacs.conversations.entities.Message;
 13import eu.siacs.conversations.entities.MucOptions;
 14import eu.siacs.conversations.http.HttpConnectionManager;
 15import eu.siacs.conversations.services.MessageArchiveService;
 16import eu.siacs.conversations.services.XmppConnectionService;
 17import eu.siacs.conversations.utils.CryptoHelper;
 18import eu.siacs.conversations.xml.Element;
 19import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
 20import eu.siacs.conversations.xmpp.chatstate.ChatState;
 21import eu.siacs.conversations.xmpp.jid.Jid;
 22import eu.siacs.conversations.xmpp.pep.Avatar;
 23import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 24
 25public class MessageParser extends AbstractParser implements
 26		OnMessagePacketReceived {
 27	public MessageParser(XmppConnectionService service) {
 28		super(service);
 29	}
 30
 31	private boolean extractChatState(Conversation conversation, final Element element) {
 32		ChatState state = ChatState.parse(element);
 33		if (state != null && conversation != null) {
 34			final Account account = conversation.getAccount();
 35			Jid from = element.getAttributeAsJid("from");
 36			if (from != null && from.toBareJid().equals(account.getJid().toBareJid())) {
 37				conversation.setOutgoingChatState(state);
 38				return false;
 39			} else {
 40				return conversation.setIncomingChatState(state);
 41			}
 42		}
 43		return false;
 44	}
 45
 46	private Message parseChat(MessagePacket packet, Account account) {
 47        final Jid jid = packet.getFrom();
 48		if (jid == null) {
 49			return null;
 50		}
 51		Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid.toBareJid(), false);
 52		updateLastseen(packet, account, true);
 53		String pgpBody = getPgpBody(packet);
 54		Message finishedMessage;
 55		if (pgpBody != null) {
 56			finishedMessage = new Message(conversation,
 57					pgpBody, Message.ENCRYPTION_PGP, Message.STATUS_RECEIVED);
 58		} else {
 59			finishedMessage = new Message(conversation,
 60					packet.getBody(), Message.ENCRYPTION_NONE,
 61					Message.STATUS_RECEIVED);
 62		}
 63		finishedMessage.setRemoteMsgId(packet.getId());
 64		finishedMessage.markable = isMarkable(packet);
 65		if (conversation.getMode() == Conversation.MODE_MULTI
 66				&& !jid.isBareJid()) {
 67			finishedMessage.setType(Message.TYPE_PRIVATE);
 68			finishedMessage.setTrueCounterpart(conversation.getMucOptions()
 69					.getTrueCounterpart(jid.getResourcepart()));
 70			if (conversation.hasDuplicateMessage(finishedMessage)) {
 71				return null;
 72			}
 73
 74		}
 75		finishedMessage.setCounterpart(jid);
 76		finishedMessage.setTime(getTimestamp(packet));
 77		extractChatState(conversation,packet);
 78		return finishedMessage;
 79	}
 80
 81	private Message parseOtrChat(MessagePacket packet, Account account) {
 82		final Jid to = packet.getTo();
 83		final Jid from = packet.getFrom();
 84		if (to == null || from == null) {
 85			return null;
 86		}
 87		boolean properlyAddressed = !to.isBareJid() || account.countPresences() == 1;
 88		Conversation conversation = mXmppConnectionService
 89				.findOrCreateConversation(account, from.toBareJid(), false);
 90		String presence;
 91		if (from.isBareJid()) {
 92            presence = "";
 93		} else {
 94			presence = from.getResourcepart();
 95		}
 96		updateLastseen(packet, account, true);
 97		String body = packet.getBody();
 98		if (body.matches("^\\?OTRv\\d{1,2}\\?.*")) {
 99			conversation.endOtrIfNeeded();
100		}
101		if (!conversation.hasValidOtrSession()) {
102			if (properlyAddressed) {
103				conversation.startOtrSession(presence,false);
104			} else {
105				return null;
106			}
107		} else {
108			String foreignPresence = conversation.getOtrSession()
109					.getSessionID().getUserID();
110			if (!foreignPresence.equals(presence)) {
111				conversation.endOtrIfNeeded();
112				if (properlyAddressed) {
113					conversation.startOtrSession(presence, false);
114				} else {
115					return null;
116				}
117			}
118		}
119		try {
120			Session otrSession = conversation.getOtrSession();
121			SessionStatus before = otrSession.getSessionStatus();
122			body = otrSession.transformReceiving(body);
123			SessionStatus after = otrSession.getSessionStatus();
124			if ((before != after) && (after == SessionStatus.ENCRYPTED)) {
125				conversation.setNextEncryption(Message.ENCRYPTION_OTR);
126				mXmppConnectionService.onOtrSessionEstablished(conversation);
127			} else if ((before != after) && (after == SessionStatus.FINISHED)) {
128				conversation.setNextEncryption(Message.ENCRYPTION_NONE);
129				conversation.resetOtrSession();
130				mXmppConnectionService.updateConversationUi();
131			}
132			if ((body == null) || (body.isEmpty())) {
133				return null;
134			}
135			if (body.startsWith(CryptoHelper.FILETRANSFER)) {
136				String key = body.substring(CryptoHelper.FILETRANSFER.length());
137				conversation.setSymmetricKey(CryptoHelper.hexToBytes(key));
138				return null;
139			}
140			Message finishedMessage = new Message(conversation, body, Message.ENCRYPTION_OTR,
141					Message.STATUS_RECEIVED);
142			finishedMessage.setTime(getTimestamp(packet));
143			finishedMessage.setRemoteMsgId(packet.getId());
144			finishedMessage.markable = isMarkable(packet);
145			finishedMessage.setCounterpart(from);
146			extractChatState(conversation,packet);
147			return finishedMessage;
148		} catch (Exception e) {
149			conversation.resetOtrSession();
150			return null;
151		}
152	}
153
154	private Message parseGroupchat(MessagePacket packet, Account account) {
155		int status;
156        final Jid from = packet.getFrom();
157		if (from == null) {
158			return null;
159		}
160		if (mXmppConnectionService.find(account.pendingConferenceLeaves,
161				account, from.toBareJid()) != null) {
162			return null;
163		}
164		Conversation conversation = mXmppConnectionService
165				.findOrCreateConversation(account, from.toBareJid(), true);
166		if (packet.hasChild("subject")) {
167			conversation.setHasMessagesLeftOnServer(true);
168			conversation.getMucOptions().setSubject(packet.findChild("subject").getContent());
169			mXmppConnectionService.updateConversationUi();
170			return null;
171		}
172
173		final Element x = packet.findChild("x", "http://jabber.org/protocol/muc#user");
174		if (from.isBareJid() && (x == null || !x.hasChild("status"))) {
175			return null;
176		} else if (from.isBareJid() && x.hasChild("status")) {
177			for(Element child : x.getChildren()) {
178				if (child.getName().equals("status")) {
179					String code = child.getAttribute("code");
180					if (code.contains(MucOptions.STATUS_CODE_ROOM_CONFIG_CHANGED)) {
181						mXmppConnectionService.fetchConferenceConfiguration(conversation);
182					}
183				}
184			}
185			return null;
186		}
187
188		if (from.getResourcepart().equals(conversation.getMucOptions().getActualNick())) {
189			if (mXmppConnectionService.markMessage(conversation,
190					packet.getId(), Message.STATUS_SEND_RECEIVED)) {
191				return null;
192			} else if (packet.getId() == null) {
193				Message message = conversation.findSentMessageWithBody(packet.getBody());
194				if (message != null) {
195					mXmppConnectionService.markMessage(message,Message.STATUS_SEND_RECEIVED);
196					return null;
197				} else {
198					status = Message.STATUS_SEND;
199				}
200			} else {
201				status = Message.STATUS_SEND;
202			}
203		} else {
204			status = Message.STATUS_RECEIVED;
205		}
206		String pgpBody = getPgpBody(packet);
207		Message finishedMessage;
208		if (pgpBody == null) {
209			finishedMessage = new Message(conversation,
210					packet.getBody(), Message.ENCRYPTION_NONE, status);
211		} else {
212			finishedMessage = new Message(conversation, pgpBody,
213					Message.ENCRYPTION_PGP, status);
214		}
215		finishedMessage.setRemoteMsgId(packet.getId());
216		finishedMessage.markable = isMarkable(packet);
217		finishedMessage.setCounterpart(from);
218		if (status == Message.STATUS_RECEIVED) {
219			finishedMessage.setTrueCounterpart(conversation.getMucOptions()
220					.getTrueCounterpart(from.getResourcepart()));
221		}
222		if (packet.hasChild("delay")
223				&& conversation.hasDuplicateMessage(finishedMessage)) {
224			return null;
225		}
226		finishedMessage.setTime(getTimestamp(packet));
227		return finishedMessage;
228	}
229
230	private Message parseCarbonMessage(final MessagePacket packet, final Account account) {
231		int status;
232		final Jid fullJid;
233		Element forwarded;
234		if (packet.hasChild("received", "urn:xmpp:carbons:2")) {
235			forwarded = packet.findChild("received", "urn:xmpp:carbons:2")
236					.findChild("forwarded", "urn:xmpp:forward:0");
237			status = Message.STATUS_RECEIVED;
238		} else if (packet.hasChild("sent", "urn:xmpp:carbons:2")) {
239			forwarded = packet.findChild("sent", "urn:xmpp:carbons:2")
240					.findChild("forwarded", "urn:xmpp:forward:0");
241			status = Message.STATUS_SEND;
242		} else {
243			return null;
244		}
245		if (forwarded == null) {
246			return null;
247		}
248		Element message = forwarded.findChild("message");
249		if (message == null) {
250			return null;
251		}
252		if (!message.hasChild("body")) {
253			if (status == Message.STATUS_RECEIVED
254					&& message.getAttribute("from") != null) {
255				parseNonMessage(message, account);
256			} else if (status == Message.STATUS_SEND
257					&& message.hasChild("displayed", "urn:xmpp:chat-markers:0")) {
258				final Jid to = message.getAttributeAsJid("to");
259				if (to != null) {
260					final Conversation conversation = mXmppConnectionService.find(
261							mXmppConnectionService.getConversations(), account,
262							to.toBareJid());
263					if (conversation != null) {
264						mXmppConnectionService.markRead(conversation);
265					}
266				}
267			}
268			return null;
269		}
270		if (status == Message.STATUS_RECEIVED) {
271			fullJid = message.getAttributeAsJid("from");
272			if (fullJid == null) {
273				return null;
274			} else {
275				updateLastseen(message, account, true);
276			}
277		} else {
278			fullJid = message.getAttributeAsJid("to");
279			if (fullJid == null) {
280				return null;
281			}
282		}
283		if (message.hasChild("x","http://jabber.org/protocol/muc#user")
284				&& "chat".equals(message.getAttribute("type"))) {
285			return null;
286		}
287		Conversation conversation = mXmppConnectionService
288				.findOrCreateConversation(account, fullJid.toBareJid(), false);
289		String pgpBody = getPgpBody(message);
290		Message finishedMessage;
291		if (pgpBody != null) {
292			finishedMessage = new Message(conversation, pgpBody,
293					Message.ENCRYPTION_PGP, status);
294		} else {
295			String body = message.findChild("body").getContent();
296			finishedMessage = new Message(conversation, body,
297					Message.ENCRYPTION_NONE, status);
298		}
299		extractChatState(conversation,message);
300		finishedMessage.setTime(getTimestamp(message));
301		finishedMessage.setRemoteMsgId(message.getAttribute("id"));
302		finishedMessage.markable = isMarkable(message);
303		finishedMessage.setCounterpart(fullJid);
304		if (conversation.getMode() == Conversation.MODE_MULTI
305				&& !fullJid.isBareJid()) {
306			finishedMessage.setType(Message.TYPE_PRIVATE);
307			finishedMessage.setTrueCounterpart(conversation.getMucOptions()
308					.getTrueCounterpart(fullJid.getResourcepart()));
309			if (conversation.hasDuplicateMessage(finishedMessage)) {
310				return null;
311			}
312		}
313		return finishedMessage;
314	}
315
316	private Message parseMamMessage(MessagePacket packet, final Account account) {
317		final Element result = packet.findChild("result","urn:xmpp:mam:0");
318		if (result == null ) {
319			return null;
320		}
321		final MessageArchiveService.Query query = this.mXmppConnectionService.getMessageArchiveService().findQuery(result.getAttribute("queryid"));
322		if (query!=null) {
323			query.incrementTotalCount();
324		}
325		final Element forwarded = result.findChild("forwarded","urn:xmpp:forward:0");
326		if (forwarded == null) {
327			return null;
328		}
329		final Element message = forwarded.findChild("message");
330		if (message == null) {
331			return null;
332		}
333		final Element body = message.findChild("body");
334		if (body == null || message.hasChild("private","urn:xmpp:carbons:2") || message.hasChild("no-copy","urn:xmpp:hints")) {
335			return null;
336		}
337		int encryption;
338		String content = getPgpBody(message);
339		if (content != null) {
340			encryption = Message.ENCRYPTION_PGP;
341		} else {
342			encryption = Message.ENCRYPTION_NONE;
343			content = body.getContent();
344		}
345		if (content == null) {
346			return null;
347		}
348		final long timestamp = getTimestamp(forwarded);
349		final Jid to = message.getAttributeAsJid("to");
350		final Jid from = message.getAttributeAsJid("from");
351		Jid counterpart;
352		int status;
353		Conversation conversation;
354		if (from!=null && to != null && from.toBareJid().equals(account.getJid().toBareJid())) {
355			status = Message.STATUS_SEND;
356			conversation = this.mXmppConnectionService.findOrCreateConversation(account,to.toBareJid(),false,query);
357			counterpart = to;
358		} else if (from !=null && to != null) {
359			status = Message.STATUS_RECEIVED;
360			conversation = this.mXmppConnectionService.findOrCreateConversation(account,from.toBareJid(),false,query);
361			counterpart = from;
362		} else {
363			return null;
364		}
365		Message finishedMessage = new Message(conversation,content,encryption,status);
366		finishedMessage.setTime(timestamp);
367		finishedMessage.setCounterpart(counterpart);
368		finishedMessage.setRemoteMsgId(message.getAttribute("id"));
369		finishedMessage.setServerMsgId(result.getAttribute("id"));
370		if (conversation.hasDuplicateMessage(finishedMessage)) {
371			return null;
372		}
373		if (query!=null) {
374			query.incrementMessageCount();
375		}
376		return finishedMessage;
377	}
378
379	private void parseError(final MessagePacket packet, final Account account) {
380		final Jid from = packet.getFrom();
381		mXmppConnectionService.markMessage(account, from.toBareJid(),
382				packet.getId(), Message.STATUS_SEND_FAILED);
383	}
384
385	private void parseNonMessage(Element packet, Account account) {
386		final Jid from = packet.getAttributeAsJid("from");
387		if (extractChatState(from == null ? null : mXmppConnectionService.find(account,from), packet)) {
388			mXmppConnectionService.updateConversationUi();
389		}
390		Element invite = extractInvite(packet);
391		if (invite != null) {
392			Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, from, true);
393			if (!conversation.getMucOptions().online()) {
394				Element password = invite.findChild("password");
395				conversation.getMucOptions().setPassword(password == null ? null : password.getContent());
396				mXmppConnectionService.databaseBackend.updateConversation(conversation);
397				mXmppConnectionService.joinMuc(conversation);
398				mXmppConnectionService.updateConversationUi();
399			}
400		}
401		if (packet.hasChild("event", "http://jabber.org/protocol/pubsub#event")) {
402			Element event = packet.findChild("event",
403					"http://jabber.org/protocol/pubsub#event");
404			parseEvent(event, from, account);
405		} else if (from != null
406				&& packet.hasChild("displayed", "urn:xmpp:chat-markers:0")) {
407			String id = packet
408					.findChild("displayed", "urn:xmpp:chat-markers:0")
409					.getAttribute("id");
410			updateLastseen(packet, account, true);
411			mXmppConnectionService.markMessage(account, from.toBareJid(),
412					id, Message.STATUS_SEND_DISPLAYED);
413		} else if (from != null
414				&& packet.hasChild("received", "urn:xmpp:chat-markers:0")) {
415			String id = packet.findChild("received", "urn:xmpp:chat-markers:0")
416					.getAttribute("id");
417			updateLastseen(packet, account, false);
418			mXmppConnectionService.markMessage(account, from.toBareJid(),
419					id, Message.STATUS_SEND_RECEIVED);
420		} else if (from != null
421				&& packet.hasChild("received", "urn:xmpp:receipts")) {
422			String id = packet.findChild("received", "urn:xmpp:receipts")
423					.getAttribute("id");
424			updateLastseen(packet, account, false);
425			mXmppConnectionService.markMessage(account, from.toBareJid(),
426					id, Message.STATUS_SEND_RECEIVED);
427		}
428	}
429
430	private Element extractInvite(Element message) {
431		Element x = message.findChild("x","http://jabber.org/protocol/muc#user");
432		if (x == null) {
433			x = message.findChild("x","jabber:x:conference");
434		}
435		if (x != null && x.hasChild("invite")) {
436			return x;
437		} else {
438			return null;
439		}
440	}
441
442	private void parseEvent(final Element event, final Jid from, final Account account) {
443		Element items = event.findChild("items");
444		if (items == null) {
445			return;
446		}
447		String node = items.getAttribute("node");
448		if (node == null) {
449			return;
450		}
451		if (node.equals("urn:xmpp:avatar:metadata")) {
452			Avatar avatar = Avatar.parseMetadata(items);
453			if (avatar != null) {
454				avatar.owner = from;
455				if (mXmppConnectionService.getFileBackend().isAvatarCached(
456						avatar)) {
457					if (account.getJid().toBareJid().equals(from)) {
458						if (account.setAvatar(avatar.getFilename())) {
459							mXmppConnectionService.databaseBackend
460									.updateAccount(account);
461						}
462						mXmppConnectionService.getAvatarService().clear(
463								account);
464						mXmppConnectionService.updateConversationUi();
465						mXmppConnectionService.updateAccountUi();
466					} else {
467						Contact contact = account.getRoster().getContact(
468								from);
469						contact.setAvatar(avatar.getFilename());
470						mXmppConnectionService.getAvatarService().clear(
471								contact);
472						mXmppConnectionService.updateConversationUi();
473						mXmppConnectionService.updateRosterUi();
474					}
475				} else {
476					mXmppConnectionService.fetchAvatar(account, avatar);
477				}
478			}
479		} else if (node.equals("http://jabber.org/protocol/nick")) {
480			Element item = items.findChild("item");
481			if (item != null) {
482				Element nick = item.findChild("nick",
483						"http://jabber.org/protocol/nick");
484				if (nick != null) {
485					if (from != null) {
486						Contact contact = account.getRoster().getContact(
487								from);
488						contact.setPresenceName(nick.getContent());
489						mXmppConnectionService.getAvatarService().clear(account);
490						mXmppConnectionService.updateConversationUi();
491						mXmppConnectionService.updateAccountUi();
492					}
493				}
494			}
495		}
496	}
497
498	private String getPgpBody(Element message) {
499		Element child = message.findChild("x", "jabber:x:encrypted");
500		if (child == null) {
501			return null;
502		} else {
503			return child.getContent();
504		}
505	}
506
507	private boolean isMarkable(Element message) {
508		return message.hasChild("markable", "urn:xmpp:chat-markers:0");
509	}
510
511	@Override
512	public void onMessagePacketReceived(Account account, MessagePacket packet) {
513		Message message = null;
514		this.parseNick(packet, account);
515		if ((packet.getType() == MessagePacket.TYPE_CHAT || packet.getType() == MessagePacket.TYPE_NORMAL)) {
516			if ((packet.getBody() != null)
517					&& (packet.getBody().startsWith("?OTR"))) {
518				message = this.parseOtrChat(packet, account);
519				if (message != null) {
520					message.markUnread();
521				}
522			} else if (packet.hasChild("body") && extractInvite(packet) == null) {
523				message = this.parseChat(packet, account);
524				if (message != null) {
525					message.markUnread();
526				}
527			} else if (packet.hasChild("received", "urn:xmpp:carbons:2")
528					|| (packet.hasChild("sent", "urn:xmpp:carbons:2"))) {
529				message = this.parseCarbonMessage(packet, account);
530				if (message != null) {
531					if (message.getStatus() == Message.STATUS_SEND) {
532						account.activateGracePeriod();
533						mXmppConnectionService.markRead(message.getConversation());
534					} else {
535						message.markUnread();
536					}
537				}
538			} else if (packet.hasChild("result","urn:xmpp:mam:0")) {
539				message = parseMamMessage(packet, account);
540				if (message != null) {
541					Conversation conversation = message.getConversation();
542					conversation.add(message);
543					mXmppConnectionService.databaseBackend.createMessage(message);
544				}
545				return;
546			} else if (packet.hasChild("fin","urn:xmpp:mam:0")) {
547				Element fin = packet.findChild("fin","urn:xmpp:mam:0");
548				mXmppConnectionService.getMessageArchiveService().processFin(fin);
549			} else {
550				parseNonMessage(packet, account);
551			}
552		} else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) {
553			message = this.parseGroupchat(packet, account);
554			if (message != null) {
555				if (message.getStatus() == Message.STATUS_RECEIVED) {
556					message.markUnread();
557				} else {
558					mXmppConnectionService.markRead(message.getConversation());
559					account.activateGracePeriod();
560				}
561			}
562		} else if (packet.getType() == MessagePacket.TYPE_ERROR) {
563			this.parseError(packet, account);
564			return;
565		} else if (packet.getType() == MessagePacket.TYPE_HEADLINE) {
566			this.parseHeadline(packet, account);
567			return;
568		}
569		if ((message == null) || (message.getBody() == null)) {
570			return;
571		}
572		if ((mXmppConnectionService.confirmMessages())
573				&& ((packet.getId() != null))) {
574			if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
575				MessagePacket receipt = mXmppConnectionService
576						.getMessageGenerator().received(account, packet,
577								"urn:xmpp:chat-markers:0");
578				mXmppConnectionService.sendMessagePacket(account, receipt);
579			}
580			if (packet.hasChild("request", "urn:xmpp:receipts")) {
581				MessagePacket receipt = mXmppConnectionService
582						.getMessageGenerator().received(account, packet,
583								"urn:xmpp:receipts");
584				mXmppConnectionService.sendMessagePacket(account, receipt);
585			}
586		}
587		Conversation conversation = message.getConversation();
588		conversation.add(message);
589		if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().advancedStreamFeaturesLoaded()) {
590			if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
591				mXmppConnectionService.updateConversation(conversation);
592			}
593		}
594
595		if (message.getStatus() == Message.STATUS_RECEIVED
596				&& conversation.getOtrSession() != null
597				&& !conversation.getOtrSession().getSessionID().getUserID()
598				.equals(message.getCounterpart().getResourcepart())) {
599			conversation.endOtrIfNeeded();
600		}
601
602		if (packet.getType() != MessagePacket.TYPE_ERROR) {
603			if (message.getEncryption() == Message.ENCRYPTION_NONE
604					|| mXmppConnectionService.saveEncryptedMessages()) {
605				mXmppConnectionService.databaseBackend.createMessage(message);
606			}
607		}
608		final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
609		if (message.trusted() && message.bodyContainsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
610			manager.createNewConnection(message);
611		} else if (!message.isRead()) {
612			mXmppConnectionService.getNotificationService().push(message);
613		}
614		mXmppConnectionService.updateConversationUi();
615	}
616
617	private void parseHeadline(MessagePacket packet, Account account) {
618		if (packet.hasChild("event", "http://jabber.org/protocol/pubsub#event")) {
619			Element event = packet.findChild("event",
620					"http://jabber.org/protocol/pubsub#event");
621			parseEvent(event, packet.getFrom(), account);
622		}
623	}
624
625	private void parseNick(MessagePacket packet, Account account) {
626		Element nick = packet.findChild("nick",
627				"http://jabber.org/protocol/nick");
628		if (nick != null) {
629			if (packet.getFrom() != null) {
630				Contact contact = account.getRoster().getContact(
631						packet.getFrom());
632				contact.setPresenceName(nick.getContent());
633			}
634		}
635	}
636}