aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/parser/MessageParser.java
blob: 7870fdbff9549b167b39521d75e1209d320a3599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
package eu.siacs.conversations.parser;

import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionStatus;

import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.http.HttpConnectionManager;
import eu.siacs.conversations.services.MessageArchiveService;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
import eu.siacs.conversations.xmpp.chatstate.ChatState;
import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.pep.Avatar;
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;

public class MessageParser extends AbstractParser implements
		OnMessagePacketReceived {
	public MessageParser(XmppConnectionService service) {
		super(service);
	}

	private boolean extractChatState(Conversation conversation, final Element element) {
		ChatState state = ChatState.parse(element);
		if (state != null && conversation != null) {
			final Account account = conversation.getAccount();
			Jid from = element.getAttributeAsJid("from");
			if (from != null && from.toBareJid().equals(account.getJid().toBareJid())) {
				conversation.setOutgoingChatState(state);
				return false;
			} else {
				return conversation.setIncomingChatState(state);
			}
		}
		return false;
	}

	private Message parseChat(MessagePacket packet, Account account) {
		final Jid jid = packet.getFrom();
		if (jid == null) {
			return null;
		}
		Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid.toBareJid(), false);
		String pgpBody = getPgpBody(packet);
		Message finishedMessage;
		if (pgpBody != null) {
			finishedMessage = new Message(conversation,
					pgpBody, Message.ENCRYPTION_PGP, Message.STATUS_RECEIVED);
		} else {
			finishedMessage = new Message(conversation,
					packet.getBody(), Message.ENCRYPTION_NONE,
					Message.STATUS_RECEIVED);
		}
		finishedMessage.setRemoteMsgId(packet.getId());
		finishedMessage.markable = isMarkable(packet);
		if (conversation.getMode() == Conversation.MODE_MULTI
				&& !jid.isBareJid()) {
			final Jid trueCounterpart = conversation.getMucOptions()
					.getTrueCounterpart(jid.getResourcepart());
			if (trueCounterpart != null) {
				updateLastseen(packet, account, trueCounterpart, false);
			}
			finishedMessage.setType(Message.TYPE_PRIVATE);
			finishedMessage.setTrueCounterpart(trueCounterpart);
			if (conversation.hasDuplicateMessage(finishedMessage)) {
				return null;
			}
		} else {
			updateLastseen(packet, account, true);
		}
		finishedMessage.setCounterpart(jid);
		finishedMessage.setTime(getTimestamp(packet));
		extractChatState(conversation,packet);
		return finishedMessage;
	}

	private Message parseOtrChat(MessagePacket packet, Account account) {
		final Jid to = packet.getTo();
		final Jid from = packet.getFrom();
		if (to == null || from == null) {
			return null;
		}
		boolean properlyAddressed = !to.isBareJid() || account.countPresences() == 1;
		Conversation conversation = mXmppConnectionService
				.findOrCreateConversation(account, from.toBareJid(), false);
		String presence;
		if (from.isBareJid()) {
			presence = "";
		} else {
			presence = from.getResourcepart();
		}
		extractChatState(conversation, packet);
		updateLastseen(packet, account, true);
		String body = packet.getBody();
		if (body.matches("^\\?OTRv\\d{1,2}\\?.*")) {
			conversation.endOtrIfNeeded();
		}
		if (!conversation.hasValidOtrSession()) {
			if (properlyAddressed) {
				conversation.startOtrSession(presence,false);
			} else {
				return null;
			}
		} else {
			String foreignPresence = conversation.getOtrSession()
					.getSessionID().getUserID();
			if (!foreignPresence.equals(presence)) {
				conversation.endOtrIfNeeded();
				if (properlyAddressed) {
					conversation.startOtrSession(presence, false);
				} else {
					return null;
				}
			}
		}
		try {
			conversation.setLastReceivedOtrMessageId(packet.getId());
			Session otrSession = conversation.getOtrSession();
			SessionStatus before = otrSession.getSessionStatus();
			body = otrSession.transformReceiving(body);
			SessionStatus after = otrSession.getSessionStatus();
			if ((before != after) && (after == SessionStatus.ENCRYPTED)) {
				conversation.setNextEncryption(Message.ENCRYPTION_OTR);
				mXmppConnectionService.onOtrSessionEstablished(conversation);
			} else if ((before != after) && (after == SessionStatus.FINISHED)) {
				conversation.setNextEncryption(Message.ENCRYPTION_NONE);
				conversation.resetOtrSession();
				mXmppConnectionService.updateConversationUi();
			}
			if ((body == null) || (body.isEmpty())) {
				return null;
			}
			if (body.startsWith(CryptoHelper.FILETRANSFER)) {
				String key = body.substring(CryptoHelper.FILETRANSFER.length());
				conversation.setSymmetricKey(CryptoHelper.hexToBytes(key));
				return null;
			}
			Message finishedMessage = new Message(conversation, body, Message.ENCRYPTION_OTR,
					Message.STATUS_RECEIVED);
			finishedMessage.setTime(getTimestamp(packet));
			finishedMessage.setRemoteMsgId(packet.getId());
			finishedMessage.markable = isMarkable(packet);
			finishedMessage.setCounterpart(from);
			conversation.setLastReceivedOtrMessageId(null);
			return finishedMessage;
		} catch (Exception e) {
			conversation.resetOtrSession();
			return null;
		}
	}

	private Message parseGroupchat(MessagePacket packet, Account account) {
		int status;
		final Jid from = packet.getFrom();
		if (from == null) {
			return null;
		}
		if (mXmppConnectionService.find(account.pendingConferenceLeaves,
				account, from.toBareJid()) != null) {
			return null;
		}
		Conversation conversation = mXmppConnectionService
				.findOrCreateConversation(account, from.toBareJid(), true);
		final Jid trueCounterpart = conversation.getMucOptions().getTrueCounterpart(from.getResourcepart());
		if (trueCounterpart != null) {
			updateLastseen(packet, account, trueCounterpart, false);
		}
		if (packet.hasChild("subject")) {
			conversation.setHasMessagesLeftOnServer(true);
			conversation.getMucOptions().setSubject(packet.findChild("subject").getContent());
			mXmppConnectionService.updateConversationUi();
			return null;
		}

		final Element x = packet.findChild("x", "http://jabber.org/protocol/muc#user");
		if (from.isBareJid() && (x == null || !x.hasChild("status"))) {
			return null;
		} else if (from.isBareJid() && x.hasChild("status")) {
			for(Element child : x.getChildren()) {
				if (child.getName().equals("status")) {
					String code = child.getAttribute("code");
					if (code.contains(MucOptions.STATUS_CODE_ROOM_CONFIG_CHANGED)) {
						mXmppConnectionService.fetchConferenceConfiguration(conversation);
					}
				}
			}
			return null;
		}

		if (from.getResourcepart().equals(conversation.getMucOptions().getActualNick())) {
			if (mXmppConnectionService.markMessage(conversation,
					packet.getId(), Message.STATUS_SEND_RECEIVED)) {
				return null;
			} else if (packet.getId() == null) {
				Message message = conversation.findSentMessageWithBody(packet.getBody());
				if (message != null) {
					mXmppConnectionService.markMessage(message,Message.STATUS_SEND_RECEIVED);
					return null;
				} else {
					status = Message.STATUS_SEND;
				}
			} else {
				status = Message.STATUS_SEND;
			}
		} else {
			status = Message.STATUS_RECEIVED;
		}
		String pgpBody = getPgpBody(packet);
		Message finishedMessage;
		if (pgpBody == null) {
			finishedMessage = new Message(conversation,
					packet.getBody(), Message.ENCRYPTION_NONE, status);
		} else {
			finishedMessage = new Message(conversation, pgpBody,
					Message.ENCRYPTION_PGP, status);
		}
		finishedMessage.setRemoteMsgId(packet.getId());
		finishedMessage.markable = isMarkable(packet);
		finishedMessage.setCounterpart(from);
		if (status == Message.STATUS_RECEIVED) {
			finishedMessage.setTrueCounterpart(conversation.getMucOptions()
					.getTrueCounterpart(from.getResourcepart()));
		}
		if (packet.hasChild("delay")
				&& conversation.hasDuplicateMessage(finishedMessage)) {
			return null;
		}
		finishedMessage.setTime(getTimestamp(packet));
		return finishedMessage;
	}

	private Message parseCarbonMessage(final MessagePacket packet, final Account account) {
		int status;
		final Jid fullJid;
		Element forwarded;
		if (packet.hasChild("received", "urn:xmpp:carbons:2")) {
			forwarded = packet.findChild("received", "urn:xmpp:carbons:2")
					.findChild("forwarded", "urn:xmpp:forward:0");
			status = Message.STATUS_RECEIVED;
		} else if (packet.hasChild("sent", "urn:xmpp:carbons:2")) {
			forwarded = packet.findChild("sent", "urn:xmpp:carbons:2")
					.findChild("forwarded", "urn:xmpp:forward:0");
			status = Message.STATUS_SEND;
		} else {
			return null;
		}
		if (forwarded == null) {
			return null;
		}
		Element message = forwarded.findChild("message");
		if (message == null) {
			return null;
		}
		if (!message.hasChild("body")) {
			if (status == Message.STATUS_RECEIVED
					&& message.getAttribute("from") != null) {
				parseNonMessage(message, account);
			} else if (status == Message.STATUS_SEND
					&& message.hasChild("displayed", "urn:xmpp:chat-markers:0")) {
				final Jid to = message.getAttributeAsJid("to");
				if (to != null) {
					final Conversation conversation = mXmppConnectionService.find(
							mXmppConnectionService.getConversations(), account,
							to.toBareJid());
					if (conversation != null) {
						mXmppConnectionService.markRead(conversation);
					}
				}
			}
			return null;
		}
		if (status == Message.STATUS_RECEIVED) {
			fullJid = message.getAttributeAsJid("from");
			if (fullJid == null) {
				return null;
			} else {
				updateLastseen(message, account, true);
			}
		} else {
			fullJid = message.getAttributeAsJid("to");
			if (fullJid == null) {
				return null;
			}
		}
		if (message.hasChild("x","http://jabber.org/protocol/muc#user")
				&& "chat".equals(message.getAttribute("type"))) {
			return null;
		}
		Conversation conversation = mXmppConnectionService
				.findOrCreateConversation(account, fullJid.toBareJid(), false);
		String pgpBody = getPgpBody(message);
		Message finishedMessage;
		if (pgpBody != null) {
			finishedMessage = new Message(conversation, pgpBody,
					Message.ENCRYPTION_PGP, status);
		} else {
			String body = message.findChild("body").getContent();
			finishedMessage = new Message(conversation, body,
					Message.ENCRYPTION_NONE, status);
		}
		extractChatState(conversation,message);
		finishedMessage.setTime(getTimestamp(message));
		finishedMessage.setRemoteMsgId(message.getAttribute("id"));
		finishedMessage.markable = isMarkable(message);
		finishedMessage.setCounterpart(fullJid);
		if (conversation.getMode() == Conversation.MODE_MULTI
				&& !fullJid.isBareJid()) {
			finishedMessage.setType(Message.TYPE_PRIVATE);
			finishedMessage.setTrueCounterpart(conversation.getMucOptions()
					.getTrueCounterpart(fullJid.getResourcepart()));
			if (conversation.hasDuplicateMessage(finishedMessage)) {
				return null;
			}
		}
		return finishedMessage;
	}

	private Message parseMamMessage(MessagePacket packet, final Account account) {
		final Element result = packet.findChild("result","urn:xmpp:mam:0");
		if (result == null ) {
			return null;
		}
		final MessageArchiveService.Query query = this.mXmppConnectionService.getMessageArchiveService().findQuery(result.getAttribute("queryid"));
		if (query!=null) {
			query.incrementTotalCount();
		}
		final Element forwarded = result.findChild("forwarded","urn:xmpp:forward:0");
		if (forwarded == null) {
			return null;
		}
		final Element message = forwarded.findChild("message");
		if (message == null) {
			return null;
		}
		final Element body = message.findChild("body");
		if (body == null || message.hasChild("private","urn:xmpp:carbons:2") || message.hasChild("no-copy","urn:xmpp:hints")) {
			return null;
		}
		int encryption;
		String content = getPgpBody(message);
		if (content != null) {
			encryption = Message.ENCRYPTION_PGP;
		} else {
			encryption = Message.ENCRYPTION_NONE;
			content = body.getContent();
		}
		if (content == null) {
			return null;
		}
		final long timestamp = getTimestamp(forwarded);
		final Jid to = message.getAttributeAsJid("to");
		final Jid from = message.getAttributeAsJid("from");
		Jid counterpart;
		int status;
		Conversation conversation;
		if (from!=null && to != null && from.toBareJid().equals(account.getJid().toBareJid())) {
			status = Message.STATUS_SEND;
			conversation = this.mXmppConnectionService.findOrCreateConversation(account,to.toBareJid(),false,query);
			counterpart = to;
		} else if (from !=null && to != null) {
			status = Message.STATUS_RECEIVED;
			conversation = this.mXmppConnectionService.findOrCreateConversation(account,from.toBareJid(),false,query);
			counterpart = from;
		} else {
			return null;
		}
		Message finishedMessage = new Message(conversation,content,encryption,status);
		finishedMessage.setTime(timestamp);
		finishedMessage.setCounterpart(counterpart);
		finishedMessage.setRemoteMsgId(message.getAttribute("id"));
		finishedMessage.setServerMsgId(result.getAttribute("id"));
		if (conversation.hasDuplicateMessage(finishedMessage)) {
			return null;
		}
		if (query!=null) {
			query.incrementMessageCount();
		}
		return finishedMessage;
	}

	private void parseError(final MessagePacket packet, final Account account) {
		final Jid from = packet.getFrom();
		mXmppConnectionService.markMessage(account, from.toBareJid(),
				packet.getId(), Message.STATUS_SEND_FAILED);
	}

	private void parseNonMessage(Element packet, Account account) {
		final Jid from = packet.getAttributeAsJid("from");
		if (account.getJid().equals(from)) {
			return;
		}
		if (extractChatState(from == null ? null : mXmppConnectionService.find(account,from), packet)) {
			mXmppConnectionService.updateConversationUi();
		}
		Invite invite = extractInvite(packet);
		if (invite != null && invite.jid != null) {
			Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, invite.jid, true);
			if (!conversation.getMucOptions().online()) {
				conversation.getMucOptions().setPassword(invite.password);
				mXmppConnectionService.databaseBackend.updateConversation(conversation);
				mXmppConnectionService.joinMuc(conversation);
				mXmppConnectionService.updateConversationUi();
			}
		}
		if (packet.hasChild("event", "http://jabber.org/protocol/pubsub#event")) {
			Element event = packet.findChild("event",
					"http://jabber.org/protocol/pubsub#event");
			parseEvent(event, from, account);
		} else if (from != null && packet.hasChild("displayed", "urn:xmpp:chat-markers:0")) {
			String id = packet
					.findChild("displayed", "urn:xmpp:chat-markers:0")
					.getAttribute("id");
			updateLastseen(packet, account, true);
			final Message displayedMessage = mXmppConnectionService.markMessage(account, from.toBareJid(), id, Message.STATUS_SEND_DISPLAYED);
			Message message = displayedMessage == null ? null :displayedMessage.prev();
			while (message != null
					&& message.getStatus() == Message.STATUS_SEND_RECEIVED
					&& message.getTimeSent() < displayedMessage.getTimeSent()) {
				mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
				message = message.prev();
			}
		} else if (from != null
				&& packet.hasChild("received", "urn:xmpp:chat-markers:0")) {
			String id = packet.findChild("received", "urn:xmpp:chat-markers:0")
					.getAttribute("id");
			updateLastseen(packet, account, false);
			mXmppConnectionService.markMessage(account, from.toBareJid(),
					id, Message.STATUS_SEND_RECEIVED);
		} else if (from != null
				&& packet.hasChild("received", "urn:xmpp:receipts")) {
			String id = packet.findChild("received", "urn:xmpp:receipts")
					.getAttribute("id");
			updateLastseen(packet, account, false);
			mXmppConnectionService.markMessage(account, from.toBareJid(),
					id, Message.STATUS_SEND_RECEIVED);
		}
	}

	private class Invite {
		Jid jid;
		String password;
		Invite(Jid jid, String password) {
			this.jid = jid;
			this.password = password;
		}
	}

	private Invite extractInvite(Element message) {
		Element x = message.findChild("x","http://jabber.org/protocol/muc#user");
		if (x != null) {
			Element invite = x.findChild("invite");
			if (invite != null) {
				Element pw = x.findChild("password");
				return new Invite(message.getAttributeAsJid("from"), pw != null ? pw.getContent(): null);
			}
		} else {
			x = message.findChild("x","jabber:x:conference");
			if (x != null) {
				return new Invite(x.getAttributeAsJid("jid"),x.getAttribute("password"));
			}
		}
		return null;
	}

	private void parseEvent(final Element event, final Jid from, final Account account) {
		Element items = event.findChild("items");
		if (items == null) {
			return;
		}
		String node = items.getAttribute("node");
		if (node == null) {
			return;
		}
		if (node.equals("urn:xmpp:avatar:metadata")) {
			Avatar avatar = Avatar.parseMetadata(items);
			if (avatar != null) {
				avatar.owner = from;
				if (mXmppConnectionService.getFileBackend().isAvatarCached(
						avatar)) {
					if (account.getJid().toBareJid().equals(from)) {
						if (account.setAvatar(avatar.getFilename())) {
							mXmppConnectionService.databaseBackend
									.updateAccount(account);
						}
						mXmppConnectionService.getAvatarService().clear(
								account);
						mXmppConnectionService.updateConversationUi();
						mXmppConnectionService.updateAccountUi();
					} else {
						Contact contact = account.getRoster().getContact(
								from);
						contact.setAvatar(avatar);
						mXmppConnectionService.getAvatarService().clear(
								contact);
						mXmppConnectionService.updateConversationUi();
						mXmppConnectionService.updateRosterUi();
					}
				} else {
					mXmppConnectionService.fetchAvatar(account, avatar);
				}
			}
		} else if (node.equals("http://jabber.org/protocol/nick")) {
			Element item = items.findChild("item");
			if (item != null) {
				Element nick = item.findChild("nick",
						"http://jabber.org/protocol/nick");
				if (nick != null) {
					if (from != null) {
						Contact contact = account.getRoster().getContact(
								from);
						contact.setPresenceName(nick.getContent());
						mXmppConnectionService.getAvatarService().clear(account);
						mXmppConnectionService.updateConversationUi();
						mXmppConnectionService.updateAccountUi();
					}
				}
			}
		}
	}

	private String getPgpBody(Element message) {
		Element child = message.findChild("x", "jabber:x:encrypted");
		if (child == null) {
			return null;
		} else {
			return child.getContent();
		}
	}

	private boolean isMarkable(Element message) {
		return message.hasChild("markable", "urn:xmpp:chat-markers:0");
	}

	@Override
	public void onMessagePacketReceived(Account account, MessagePacket packet) {
		Message message = null;
		this.parseNick(packet, account);
		if ((packet.getType() == MessagePacket.TYPE_CHAT || packet.getType() == MessagePacket.TYPE_NORMAL)) {
			if ((packet.getBody() != null)
					&& (packet.getBody().startsWith("?OTR"))) {
				message = this.parseOtrChat(packet, account);
				if (message != null) {
					message.markUnread();
				}
			} else if (packet.hasChild("body") && extractInvite(packet) == null) {
				message = this.parseChat(packet, account);
				if (message != null) {
					message.markUnread();
				}
			} else if (packet.hasChild("received", "urn:xmpp:carbons:2")
					|| (packet.hasChild("sent", "urn:xmpp:carbons:2"))) {
				message = this.parseCarbonMessage(packet, account);
				if (message != null) {
					if (message.getStatus() == Message.STATUS_SEND) {
						account.activateGracePeriod();
						mXmppConnectionService.markRead(message.getConversation());
					} else {
						message.markUnread();
					}
				}
			} else if (packet.hasChild("result","urn:xmpp:mam:0")) {
				message = parseMamMessage(packet, account);
				if (message != null) {
					Conversation conversation = message.getConversation();
					conversation.add(message);
					mXmppConnectionService.databaseBackend.createMessage(message);
				}
				return;
			} else if (packet.hasChild("fin","urn:xmpp:mam:0")) {
				Element fin = packet.findChild("fin","urn:xmpp:mam:0");
				mXmppConnectionService.getMessageArchiveService().processFin(fin);
			} else {
				parseNonMessage(packet, account);
			}
		} else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) {
			message = this.parseGroupchat(packet, account);
			if (message != null) {
				if (message.getStatus() == Message.STATUS_RECEIVED) {
					message.markUnread();
				} else {
					mXmppConnectionService.markRead(message.getConversation());
					account.activateGracePeriod();
				}
			}
		} else if (packet.getType() == MessagePacket.TYPE_ERROR) {
			this.parseError(packet, account);
			return;
		} else if (packet.getType() == MessagePacket.TYPE_HEADLINE) {
			this.parseHeadline(packet, account);
			return;
		}
		if ((message == null) || (message.getBody() == null)) {
			return;
		}
		if ((mXmppConnectionService.confirmMessages())
				&& ((packet.getId() != null))) {
			if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
				MessagePacket receipt = mXmppConnectionService
						.getMessageGenerator().received(account, packet,
								"urn:xmpp:chat-markers:0");
				mXmppConnectionService.sendMessagePacket(account, receipt);
			}
			if (packet.hasChild("request", "urn:xmpp:receipts")) {
				MessagePacket receipt = mXmppConnectionService
						.getMessageGenerator().received(account, packet,
								"urn:xmpp:receipts");
				mXmppConnectionService.sendMessagePacket(account, receipt);
			}
		}
		Conversation conversation = message.getConversation();
		conversation.add(message);
		if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().advancedStreamFeaturesLoaded()) {
			if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
				mXmppConnectionService.updateConversation(conversation);
			}
		}

		if (message.getStatus() == Message.STATUS_RECEIVED
				&& conversation.getOtrSession() != null
				&& !conversation.getOtrSession().getSessionID().getUserID()
				.equals(message.getCounterpart().getResourcepart())) {
			conversation.endOtrIfNeeded();
		}

		if (packet.getType() != MessagePacket.TYPE_ERROR) {
			if (message.getEncryption() == Message.ENCRYPTION_NONE
					|| mXmppConnectionService.saveEncryptedMessages()) {
				mXmppConnectionService.databaseBackend.createMessage(message);
			}
		}
		final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
		if (message.trusted() && message.bodyContainsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
			manager.createNewConnection(message);
		} else if (!message.isRead()) {
			mXmppConnectionService.getNotificationService().push(message);
		}
		mXmppConnectionService.updateConversationUi();
	}

	private void parseHeadline(MessagePacket packet, Account account) {
		if (packet.hasChild("event", "http://jabber.org/protocol/pubsub#event")) {
			Element event = packet.findChild("event",
					"http://jabber.org/protocol/pubsub#event");
			parseEvent(event, packet.getFrom(), account);
		}
	}

	private void parseNick(MessagePacket packet, Account account) {
		Element nick = packet.findChild("nick",
				"http://jabber.org/protocol/nick");
		if (nick != null) {
			if (packet.getFrom() != null) {
				Contact contact = account.getRoster().getContact(
						packet.getFrom());
				contact.setPresenceName(nick.getContent());
			}
		}
	}
}