1package eu.siacs.conversations.parser;
2
3import android.text.Html;
4import android.util.Log;
5import android.util.Pair;
6
7import net.java.otr4j.session.Session;
8import net.java.otr4j.session.SessionStatus;
9
10import java.util.ArrayList;
11import java.util.Arrays;
12import java.util.List;
13import java.util.Locale;
14import java.util.Set;
15import java.util.UUID;
16
17import eu.siacs.conversations.Config;
18import eu.siacs.conversations.crypto.OtrService;
19import eu.siacs.conversations.crypto.axolotl.AxolotlService;
20import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
21import eu.siacs.conversations.entities.Account;
22import eu.siacs.conversations.entities.Bookmark;
23import eu.siacs.conversations.entities.Contact;
24import eu.siacs.conversations.entities.Conversation;
25import eu.siacs.conversations.entities.Message;
26import eu.siacs.conversations.entities.MucOptions;
27import eu.siacs.conversations.entities.Presence;
28import eu.siacs.conversations.entities.ServiceDiscoveryResult;
29import eu.siacs.conversations.http.HttpConnectionManager;
30import eu.siacs.conversations.services.MessageArchiveService;
31import eu.siacs.conversations.services.XmppConnectionService;
32import eu.siacs.conversations.utils.CryptoHelper;
33import eu.siacs.conversations.xml.Element;
34import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
35import eu.siacs.conversations.xmpp.chatstate.ChatState;
36import eu.siacs.conversations.xmpp.jid.Jid;
37import eu.siacs.conversations.xmpp.pep.Avatar;
38import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
39
40public class MessageParser extends AbstractParser implements OnMessagePacketReceived {
41
42 private static final List<String> CLIENTS_SENDING_HTML_IN_OTR = Arrays.asList(new String[]{"Pidgin","Adium"});
43
44 public MessageParser(XmppConnectionService service) {
45 super(service);
46 }
47
48 private boolean extractChatState(Conversation conversation, final MessagePacket packet) {
49 ChatState state = ChatState.parse(packet);
50 if (state != null && conversation != null) {
51 final Account account = conversation.getAccount();
52 Jid from = packet.getFrom();
53 if (from.toBareJid().equals(account.getJid().toBareJid())) {
54 conversation.setOutgoingChatState(state);
55 if (state == ChatState.ACTIVE || state == ChatState.COMPOSING) {
56 mXmppConnectionService.markRead(conversation);
57 account.activateGracePeriod();
58 }
59 return false;
60 } else {
61 return conversation.setIncomingChatState(state);
62 }
63 }
64 return false;
65 }
66
67 private Message parseOtrChat(String body, Jid from, String id, Conversation conversation) {
68 String presence;
69 if (from.isBareJid()) {
70 presence = "";
71 } else {
72 presence = from.getResourcepart();
73 }
74 if (body.matches("^\\?OTRv\\d{1,2}\\?.*")) {
75 conversation.endOtrIfNeeded();
76 }
77 if (!conversation.hasValidOtrSession()) {
78 conversation.startOtrSession(presence,false);
79 } else {
80 String foreignPresence = conversation.getOtrSession().getSessionID().getUserID();
81 if (!foreignPresence.equals(presence)) {
82 conversation.endOtrIfNeeded();
83 conversation.startOtrSession(presence, false);
84 }
85 }
86 try {
87 conversation.setLastReceivedOtrMessageId(id);
88 Session otrSession = conversation.getOtrSession();
89 body = otrSession.transformReceiving(body);
90 SessionStatus status = otrSession.getSessionStatus();
91 if (body == null && status == SessionStatus.ENCRYPTED) {
92 mXmppConnectionService.onOtrSessionEstablished(conversation);
93 return null;
94 } else if (body == null && status == SessionStatus.FINISHED) {
95 conversation.resetOtrSession();
96 mXmppConnectionService.updateConversationUi();
97 return null;
98 } else if (body == null || (body.isEmpty())) {
99 return null;
100 }
101 if (body.startsWith(CryptoHelper.FILETRANSFER)) {
102 String key = body.substring(CryptoHelper.FILETRANSFER.length());
103 conversation.setSymmetricKey(CryptoHelper.hexToBytes(key));
104 return null;
105 }
106 if (clientMightSendHtml(conversation.getAccount(), from)) {
107 Log.d(Config.LOGTAG,conversation.getAccount().getJid().toBareJid()+": received OTR message from bad behaving client. escaping HTML…");
108 body = Html.fromHtml(body).toString();
109 }
110
111 final OtrService otrService = conversation.getAccount().getOtrService();
112 Message finishedMessage = new Message(conversation, body, Message.ENCRYPTION_OTR, Message.STATUS_RECEIVED);
113 finishedMessage.setFingerprint(otrService.getFingerprint(otrSession.getRemotePublicKey()));
114 conversation.setLastReceivedOtrMessageId(null);
115
116 return finishedMessage;
117 } catch (Exception e) {
118 conversation.resetOtrSession();
119 return null;
120 }
121 }
122
123 private static boolean clientMightSendHtml(Account account, Jid from) {
124 String resource = from.getResourcepart();
125 if (resource == null) {
126 return false;
127 }
128 Presence presence = account.getRoster().getContact(from).getPresences().getPresences().get(resource);
129 ServiceDiscoveryResult disco = presence == null ? null : presence.getServiceDiscoveryResult();
130 if (disco == null) {
131 return false;
132 }
133 return hasIdentityKnowForSendingHtml(disco.getIdentities());
134 }
135
136 private static boolean hasIdentityKnowForSendingHtml(List<ServiceDiscoveryResult.Identity> identities) {
137 for(ServiceDiscoveryResult.Identity identity : identities) {
138 if (identity.getName() != null) {
139 if (CLIENTS_SENDING_HTML_IN_OTR.contains(identity.getName())) {
140 return true;
141 }
142 }
143 }
144 return false;
145 }
146
147 private Message parseAxolotlChat(Element axolotlMessage, Jid from, Conversation conversation, int status) {
148 Message finishedMessage = null;
149 AxolotlService service = conversation.getAccount().getAxolotlService();
150 XmppAxolotlMessage xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlMessage, from.toBareJid());
151 XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = service.processReceivingPayloadMessage(xmppAxolotlMessage);
152 if(plaintextMessage != null) {
153 finishedMessage = new Message(conversation, plaintextMessage.getPlaintext(), Message.ENCRYPTION_AXOLOTL, status);
154 finishedMessage.setFingerprint(plaintextMessage.getFingerprint());
155 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(finishedMessage.getConversation().getAccount())+" Received Message with session fingerprint: "+plaintextMessage.getFingerprint());
156 }
157
158 return finishedMessage;
159 }
160
161 private class Invite {
162 Jid jid;
163 String password;
164 Invite(Jid jid, String password) {
165 this.jid = jid;
166 this.password = password;
167 }
168
169 public boolean execute(Account account) {
170 if (jid != null) {
171 Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid, true);
172 if (!conversation.getMucOptions().online()) {
173 conversation.getMucOptions().setPassword(password);
174 mXmppConnectionService.databaseBackend.updateConversation(conversation);
175 mXmppConnectionService.joinMuc(conversation);
176 mXmppConnectionService.updateConversationUi();
177 }
178 return true;
179 }
180 return false;
181 }
182 }
183
184 private Invite extractInvite(Element message) {
185 Element x = message.findChild("x", "http://jabber.org/protocol/muc#user");
186 if (x != null) {
187 Element invite = x.findChild("invite");
188 if (invite != null) {
189 Element pw = x.findChild("password");
190 return new Invite(message.getAttributeAsJid("from"), pw != null ? pw.getContent(): null);
191 }
192 } else {
193 x = message.findChild("x","jabber:x:conference");
194 if (x != null) {
195 return new Invite(x.getAttributeAsJid("jid"),x.getAttribute("password"));
196 }
197 }
198 return null;
199 }
200
201 private static String extractStanzaId(Element packet, Jid by) {
202 for(Element child : packet.getChildren()) {
203 if (child.getName().equals("stanza-id")
204 && "urn:xmpp:sid:0".equals(child.getNamespace())
205 && by.equals(child.getAttributeAsJid("by"))) {
206 return child.getAttribute("id");
207 }
208 }
209 return null;
210 }
211
212 private void parseEvent(final Element event, final Jid from, final Account account) {
213 Element items = event.findChild("items");
214 String node = items == null ? null : items.getAttribute("node");
215 if ("urn:xmpp:avatar:metadata".equals(node)) {
216 Avatar avatar = Avatar.parseMetadata(items);
217 if (avatar != null) {
218 avatar.owner = from.toBareJid();
219 if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
220 if (account.getJid().toBareJid().equals(from)) {
221 if (account.setAvatar(avatar.getFilename())) {
222 mXmppConnectionService.databaseBackend.updateAccount(account);
223 }
224 mXmppConnectionService.getAvatarService().clear(account);
225 mXmppConnectionService.updateConversationUi();
226 mXmppConnectionService.updateAccountUi();
227 } else {
228 Contact contact = account.getRoster().getContact(from);
229 contact.setAvatar(avatar);
230 mXmppConnectionService.getAvatarService().clear(contact);
231 mXmppConnectionService.updateConversationUi();
232 mXmppConnectionService.updateRosterUi();
233 }
234 } else {
235 mXmppConnectionService.fetchAvatar(account, avatar);
236 }
237 }
238 } else if ("http://jabber.org/protocol/nick".equals(node)) {
239 Element i = items.findChild("item");
240 Element nick = i == null ? null : i.findChild("nick", "http://jabber.org/protocol/nick");
241 if (nick != null && nick.getContent() != null) {
242 Contact contact = account.getRoster().getContact(from);
243 contact.setPresenceName(nick.getContent());
244 mXmppConnectionService.getAvatarService().clear(account);
245 mXmppConnectionService.updateConversationUi();
246 mXmppConnectionService.updateAccountUi();
247 }
248 } else if (AxolotlService.PEP_DEVICE_LIST.equals(node)) {
249 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Received PEP device list update from "+ from + ", processing...");
250 Element item = items.findChild("item");
251 Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
252 AxolotlService axolotlService = account.getAxolotlService();
253 axolotlService.registerDevices(from, deviceIds);
254 mXmppConnectionService.updateAccountUi();
255 }
256 }
257
258 private boolean handleErrorMessage(Account account, MessagePacket packet) {
259 if (packet.getType() == MessagePacket.TYPE_ERROR) {
260 Jid from = packet.getFrom();
261 if (from != null) {
262 Element error = packet.findChild("error");
263 String text = error == null ? null : error.findChildContent("text");
264 if (text != null) {
265 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": sending message to "+ from+ " failed - " + text);
266 } else if (error != null) {
267 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": sending message to "+ from+ " failed - " + error);
268 }
269 Message message = mXmppConnectionService.markMessage(account,
270 from.toBareJid(),
271 packet.getId(),
272 Message.STATUS_SEND_FAILED);
273 if (message != null && message.getEncryption() == Message.ENCRYPTION_OTR) {
274 message.getConversation().endOtrIfNeeded();
275 }
276 }
277 return true;
278 }
279 return false;
280 }
281
282 @Override
283 public void onMessagePacketReceived(Account account, MessagePacket original) {
284 if (handleErrorMessage(account, original)) {
285 return;
286 }
287 final MessagePacket packet;
288 Long timestamp = null;
289 final boolean isForwarded;
290 boolean isCarbon = false;
291 String serverMsgId = null;
292 final Element fin = original.findChild("fin", "urn:xmpp:mam:0");
293 if (fin != null) {
294 mXmppConnectionService.getMessageArchiveService().processFin(fin,original.getFrom());
295 return;
296 }
297 final Element result = original.findChild("result","urn:xmpp:mam:0");
298 final MessageArchiveService.Query query = result == null ? null : mXmppConnectionService.getMessageArchiveService().findQuery(result.getAttribute("queryid"));
299 if (query != null && query.validFrom(original.getFrom())) {
300 Pair<MessagePacket, Long> f = original.getForwardedMessagePacket("result", "urn:xmpp:mam:0");
301 if (f == null) {
302 return;
303 }
304 timestamp = f.second;
305 packet = f.first;
306 isForwarded = true;
307 serverMsgId = result.getAttribute("id");
308 query.incrementMessageCount();
309 } else if (query != null) {
310 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received mam result from invalid sender");
311 return;
312 } else if (original.fromServer(account)) {
313 Pair<MessagePacket, Long> f;
314 f = original.getForwardedMessagePacket("received", "urn:xmpp:carbons:2");
315 f = f == null ? original.getForwardedMessagePacket("sent", "urn:xmpp:carbons:2") : f;
316 packet = f != null ? f.first : original;
317 if (handleErrorMessage(account, packet)) {
318 return;
319 }
320 timestamp = f != null ? f.second : null;
321 isCarbon = f != null;
322 isForwarded = isCarbon;
323 } else {
324 packet = original;
325 isForwarded = false;
326 }
327
328 if (timestamp == null) {
329 timestamp = AbstractParser.getTimestamp(packet, System.currentTimeMillis());
330 }
331 final String body = packet.getBody();
332 final Element mucUserElement = packet.findChild("x", "http://jabber.org/protocol/muc#user");
333 final String pgpEncrypted = packet.findChildContent("x", "jabber:x:encrypted");
334 final Element replaceElement = packet.findChild("replace", "urn:xmpp:message-correct:0");
335 final Element oob = packet.findChild("x", "jabber:x:oob");
336 final boolean isOob = oob!= null && body != null && body.equals(oob.findChildContent("url"));
337 final String replacementId = replaceElement == null ? null : replaceElement.getAttribute("id");
338 final Element axolotlEncrypted = packet.findChild(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX);
339 int status;
340 final Jid counterpart;
341 final Jid to = packet.getTo();
342 final Jid from = packet.getFrom();
343 final String remoteMsgId = packet.getId();
344
345 if (from == null) {
346 Log.d(Config.LOGTAG,"no from in: "+packet.toString());
347 return;
348 }
349
350 boolean isTypeGroupChat = packet.getType() == MessagePacket.TYPE_GROUPCHAT;
351 boolean isProperlyAddressed = (to != null ) && (!to.isBareJid() || account.countPresences() == 0);
352 boolean isMucStatusMessage = from.isBareJid() && mucUserElement != null && mucUserElement.hasChild("status");
353 if (packet.fromAccount(account)) {
354 status = Message.STATUS_SEND;
355 counterpart = to != null ? to : account.getJid();
356 } else {
357 status = Message.STATUS_RECEIVED;
358 counterpart = from;
359 }
360
361 Invite invite = extractInvite(packet);
362 if (invite != null && invite.execute(account)) {
363 return;
364 }
365
366 if (extractChatState(mXmppConnectionService.find(account, counterpart.toBareJid()), packet)) {
367 mXmppConnectionService.updateConversationUi();
368 }
369
370 if ((body != null || pgpEncrypted != null || axolotlEncrypted != null) && !isMucStatusMessage) {
371 Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.toBareJid(), isTypeGroupChat, query);
372 if (isTypeGroupChat) {
373 if (counterpart.getResourcepart().equals(conversation.getMucOptions().getActualNick())) {
374 status = Message.STATUS_SEND_RECEIVED;
375 isCarbon = true; //not really carbon but received from another resource
376 if (mXmppConnectionService.markMessage(conversation, remoteMsgId, status)) {
377 return;
378 } else if (remoteMsgId == null || Config.IGNORE_ID_REWRITE_IN_MUC) {
379 Message message = conversation.findSentMessageWithBody(packet.getBody());
380 if (message != null) {
381 mXmppConnectionService.markMessage(message, status);
382 return;
383 }
384 }
385 } else {
386 status = Message.STATUS_RECEIVED;
387 }
388 }
389 Message message;
390 if (body != null && body.startsWith("?OTR") && Config.supportOtr()) {
391 if (!isForwarded && !isTypeGroupChat && isProperlyAddressed) {
392 message = parseOtrChat(body, from, remoteMsgId, conversation);
393 if (message == null) {
394 return;
395 }
396 } else {
397 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": ignoring OTR message from "+from+" isForwarded="+Boolean.toString(isForwarded)+", isProperlyAddressed="+Boolean.valueOf(isProperlyAddressed));
398 message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
399 }
400 } else if (pgpEncrypted != null && Config.supportOpenPgp()) {
401 message = new Message(conversation, pgpEncrypted, Message.ENCRYPTION_PGP, status);
402 } else if (axolotlEncrypted != null && Config.supportOmemo()) {
403 Jid origin;
404 if (conversation.getMode() == Conversation.MODE_MULTI) {
405 origin = conversation.getMucOptions().getTrueCounterpart(counterpart.getResourcepart());
406 if (origin == null) {
407 Log.d(Config.LOGTAG,"axolotl message in non anonymous conference received");
408 return;
409 }
410 } else {
411 origin = from;
412 }
413 message = parseAxolotlChat(axolotlEncrypted, origin, conversation, status);
414 if (message == null) {
415 return;
416 }
417 } else {
418 message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
419 }
420
421 if (serverMsgId == null) {
422 serverMsgId = extractStanzaId(packet, isTypeGroupChat ? conversation.getJid().toBareJid() : account.getServer());
423 }
424
425 message.setCounterpart(counterpart);
426 message.setRemoteMsgId(remoteMsgId);
427 message.setServerMsgId(serverMsgId);
428 message.setCarbon(isCarbon);
429 message.setTime(timestamp);
430 message.setOob(isOob);
431 message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
432 if (conversation.getMode() == Conversation.MODE_MULTI) {
433 Jid trueCounterpart = conversation.getMucOptions().getTrueCounterpart(counterpart.getResourcepart());
434 message.setTrueCounterpart(trueCounterpart);
435 if (!isTypeGroupChat) {
436 message.setType(Message.TYPE_PRIVATE);
437 }
438 } else {
439 updateLastseen(timestamp, account, from);
440 }
441
442 if (replacementId != null && mXmppConnectionService.allowMessageCorrection()) {
443 Message replacedMessage = conversation.findMessageWithRemoteIdAndCounterpart(replacementId,
444 counterpart,
445 message.getStatus() == Message.STATUS_RECEIVED,
446 message.isCarbon());
447 if (replacedMessage != null) {
448 final boolean fingerprintsMatch = replacedMessage.getFingerprint() == null
449 || replacedMessage.getFingerprint().equals(message.getFingerprint());
450 final boolean trueCountersMatch = replacedMessage.getTrueCounterpart() != null
451 && replacedMessage.getTrueCounterpart().equals(message.getTrueCounterpart());
452 if (fingerprintsMatch && (trueCountersMatch || conversation.getMode() == Conversation.MODE_SINGLE)) {
453 Log.d(Config.LOGTAG, "replaced message '" + replacedMessage.getBody() + "' with '" + message.getBody() + "'");
454 final String uuid = replacedMessage.getUuid();
455 replacedMessage.setUuid(UUID.randomUUID().toString());
456 replacedMessage.setBody(message.getBody());
457 replacedMessage.setEdited(replacedMessage.getRemoteMsgId());
458 replacedMessage.setRemoteMsgId(remoteMsgId);
459 replacedMessage.setEncryption(message.getEncryption());
460 if (replacedMessage.getStatus() == Message.STATUS_RECEIVED) {
461 replacedMessage.markUnread();
462 }
463 mXmppConnectionService.updateMessage(replacedMessage, uuid);
464 mXmppConnectionService.getNotificationService().updateNotification(false);
465 if (mXmppConnectionService.confirmMessages() && remoteMsgId != null && !isForwarded && !isTypeGroupChat) {
466 sendMessageReceipts(account, packet);
467 }
468 if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) {
469 conversation.getAccount().getPgpDecryptionService().add(replacedMessage);
470 }
471 return;
472 } else {
473 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received message correction but verification didn't check out");
474 }
475 }
476 }
477
478 boolean checkForDuplicates = query != null
479 || (isTypeGroupChat && packet.hasChild("delay","urn:xmpp:delay"))
480 || message.getType() == Message.TYPE_PRIVATE;
481 if (checkForDuplicates && conversation.hasDuplicateMessage(message)) {
482 Log.d(Config.LOGTAG,"skipping duplicate message from "+message.getCounterpart().toString()+" "+message.getBody());
483 return;
484 }
485
486 if (query != null && query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
487 conversation.prepend(message);
488 } else {
489 conversation.add(message);
490 }
491
492 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
493 conversation.getAccount().getPgpDecryptionService().add(message);
494 }
495
496 if (query == null || query.getWith() == null) { //either no mam or catchup
497 if (status == Message.STATUS_SEND || status == Message.STATUS_SEND_RECEIVED) {
498 mXmppConnectionService.markRead(conversation);
499 if (query == null) {
500 account.activateGracePeriod();
501 }
502 } else {
503 message.markUnread();
504 }
505 }
506
507 if (query == null) {
508 mXmppConnectionService.updateConversationUi();
509 }
510
511 if (mXmppConnectionService.confirmMessages() && remoteMsgId != null && !isForwarded && !isTypeGroupChat) {
512 sendMessageReceipts(account, packet);
513 }
514
515 if (message.getStatus() == Message.STATUS_RECEIVED
516 && conversation.getOtrSession() != null
517 && !conversation.getOtrSession().getSessionID().getUserID()
518 .equals(message.getCounterpart().getResourcepart())) {
519 conversation.endOtrIfNeeded();
520 }
521
522 if (message.getEncryption() == Message.ENCRYPTION_NONE || mXmppConnectionService.saveEncryptedMessages()) {
523 mXmppConnectionService.databaseBackend.createMessage(message);
524 }
525 final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
526 if (message.trusted() && message.treatAsDownloadable() != Message.Decision.NEVER && manager.getAutoAcceptFileSize() > 0) {
527 manager.createNewDownloadConnection(message);
528 } else if (!message.isRead()) {
529 if (query == null) {
530 mXmppConnectionService.getNotificationService().push(message);
531 } else if (query.getWith() == null) { // mam catchup
532 mXmppConnectionService.getNotificationService().pushFromBacklog(message);
533 }
534 }
535 } else if (!packet.hasChild("body")){ //no body
536 if (isTypeGroupChat) {
537 Conversation conversation = mXmppConnectionService.find(account, from.toBareJid());
538 if (packet.hasChild("subject")) {
539 if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
540 conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0);
541 String subject = packet.findChildContent("subject");
542 conversation.getMucOptions().setSubject(subject);
543 final Bookmark bookmark = conversation.getBookmark();
544 if (bookmark != null && bookmark.getBookmarkName() == null) {
545 if (bookmark.setBookmarkName(subject)) {
546 mXmppConnectionService.pushBookmarks(account);
547 }
548 }
549 mXmppConnectionService.updateConversationUi();
550 return;
551 }
552 }
553
554 if (conversation != null && isMucStatusMessage) {
555 for (Element child : mucUserElement.getChildren()) {
556 if (child.getName().equals("status")
557 && MucOptions.STATUS_CODE_ROOM_CONFIG_CHANGED.equals(child.getAttribute("code"))) {
558 mXmppConnectionService.fetchConferenceConfiguration(conversation);
559 }
560 }
561 }
562 }
563 }
564
565 Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
566 if (received == null) {
567 received = packet.findChild("received", "urn:xmpp:receipts");
568 }
569 if (received != null && !packet.fromAccount(account)) {
570 mXmppConnectionService.markMessage(account, from.toBareJid(), received.getAttribute("id"), Message.STATUS_SEND_RECEIVED);
571 }
572 Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
573 if (displayed != null) {
574 if (packet.fromAccount(account)) {
575 Conversation conversation = mXmppConnectionService.find(account,counterpart.toBareJid());
576 if (conversation != null) {
577 mXmppConnectionService.markRead(conversation);
578 }
579 } else {
580 updateLastseen(timestamp, account, from);
581 final Message displayedMessage = mXmppConnectionService.markMessage(account, from.toBareJid(), displayed.getAttribute("id"), Message.STATUS_SEND_DISPLAYED);
582 Message message = displayedMessage == null ? null : displayedMessage.prev();
583 while (message != null
584 && message.getStatus() == Message.STATUS_SEND_RECEIVED
585 && message.getTimeSent() < displayedMessage.getTimeSent()) {
586 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
587 message = message.prev();
588 }
589 }
590 }
591
592 Element event = packet.findChild("event", "http://jabber.org/protocol/pubsub#event");
593 if (event != null) {
594 parseEvent(event, from, account);
595 }
596
597 String nick = packet.findChildContent("nick", "http://jabber.org/protocol/nick");
598 if (nick != null) {
599 Contact contact = account.getRoster().getContact(from);
600 contact.setPresenceName(nick);
601 }
602 }
603
604 private void sendMessageReceipts(Account account, MessagePacket packet) {
605 ArrayList<String> receiptsNamespaces = new ArrayList<>();
606 if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
607 receiptsNamespaces.add("urn:xmpp:chat-markers:0");
608 }
609 if (packet.hasChild("request", "urn:xmpp:receipts")) {
610 receiptsNamespaces.add("urn:xmpp:receipts");
611 }
612 if (receiptsNamespaces.size() > 0) {
613 MessagePacket receipt = mXmppConnectionService.getMessageGenerator().received(account,
614 packet,
615 receiptsNamespaces,
616 packet.getType());
617 mXmppConnectionService.sendMessagePacket(account, receipt);
618 }
619 }
620}