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 final boolean conversationMultiMode = conversation.getMode() == Conversation.MODE_MULTI;
373 if (isTypeGroupChat) {
374 if (counterpart.getResourcepart().equals(conversation.getMucOptions().getActualNick())) {
375 status = Message.STATUS_SEND_RECEIVED;
376 isCarbon = true; //not really carbon but received from another resource
377 if (mXmppConnectionService.markMessage(conversation, remoteMsgId, status)) {
378 return;
379 } else if (remoteMsgId == null || Config.IGNORE_ID_REWRITE_IN_MUC) {
380 Message message = conversation.findSentMessageWithBody(packet.getBody());
381 if (message != null) {
382 mXmppConnectionService.markMessage(message, status);
383 return;
384 }
385 }
386 } else {
387 status = Message.STATUS_RECEIVED;
388 }
389 }
390 Message message;
391 if (body != null && body.startsWith("?OTR") && Config.supportOtr()) {
392 if (!isForwarded && !isTypeGroupChat && isProperlyAddressed && !conversationMultiMode) {
393 message = parseOtrChat(body, from, remoteMsgId, conversation);
394 if (message == null) {
395 return;
396 }
397 } else {
398 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": ignoring OTR message from "+from+" isForwarded="+Boolean.toString(isForwarded)+", isProperlyAddressed="+Boolean.valueOf(isProperlyAddressed));
399 message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
400 }
401 } else if (pgpEncrypted != null && Config.supportOpenPgp()) {
402 message = new Message(conversation, pgpEncrypted, Message.ENCRYPTION_PGP, status);
403 } else if (axolotlEncrypted != null && Config.supportOmemo()) {
404 Jid origin;
405 if (conversationMultiMode) {
406 origin = conversation.getMucOptions().getTrueCounterpart(counterpart);
407 if (origin == null) {
408 Log.d(Config.LOGTAG,"axolotl message in non anonymous conference received");
409 return;
410 }
411 } else {
412 origin = from;
413 }
414 message = parseAxolotlChat(axolotlEncrypted, origin, conversation, status);
415 if (message == null) {
416 return;
417 }
418 } else {
419 message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
420 }
421
422 if (serverMsgId == null) {
423 serverMsgId = extractStanzaId(packet, isTypeGroupChat ? conversation.getJid().toBareJid() : account.getServer());
424 }
425
426 message.setCounterpart(counterpart);
427 message.setRemoteMsgId(remoteMsgId);
428 message.setServerMsgId(serverMsgId);
429 message.setCarbon(isCarbon);
430 message.setTime(timestamp);
431 message.setOob(isOob);
432 message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
433 if (conversationMultiMode) {
434 Jid trueCounterpart = conversation.getMucOptions().getTrueCounterpart(counterpart);
435 message.setTrueCounterpart(trueCounterpart);
436 if (!isTypeGroupChat) {
437 message.setType(Message.TYPE_PRIVATE);
438 }
439 } else {
440 updateLastseen(timestamp, account, from);
441 }
442
443 if (replacementId != null && mXmppConnectionService.allowMessageCorrection()) {
444 Message replacedMessage = conversation.findMessageWithRemoteIdAndCounterpart(replacementId,
445 counterpart,
446 message.getStatus() == Message.STATUS_RECEIVED,
447 message.isCarbon());
448 if (replacedMessage != null) {
449 final boolean fingerprintsMatch = replacedMessage.getFingerprint() == null
450 || replacedMessage.getFingerprint().equals(message.getFingerprint());
451 final boolean trueCountersMatch = replacedMessage.getTrueCounterpart() != null
452 && replacedMessage.getTrueCounterpart().equals(message.getTrueCounterpart());
453 if (fingerprintsMatch && (trueCountersMatch || !conversationMultiMode)) {
454 Log.d(Config.LOGTAG, "replaced message '" + replacedMessage.getBody() + "' with '" + message.getBody() + "'");
455 final String uuid = replacedMessage.getUuid();
456 replacedMessage.setUuid(UUID.randomUUID().toString());
457 replacedMessage.setBody(message.getBody());
458 replacedMessage.setEdited(replacedMessage.getRemoteMsgId());
459 replacedMessage.setRemoteMsgId(remoteMsgId);
460 replacedMessage.setEncryption(message.getEncryption());
461 if (replacedMessage.getStatus() == Message.STATUS_RECEIVED) {
462 replacedMessage.markUnread();
463 }
464 mXmppConnectionService.updateMessage(replacedMessage, uuid);
465 mXmppConnectionService.getNotificationService().updateNotification(false);
466 if (mXmppConnectionService.confirmMessages() && remoteMsgId != null && !isForwarded && !isTypeGroupChat) {
467 sendMessageReceipts(account, packet);
468 }
469 if (replacedMessage.getEncryption() == Message.ENCRYPTION_PGP) {
470 conversation.getAccount().getPgpDecryptionService().add(replacedMessage);
471 }
472 return;
473 } else {
474 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received message correction but verification didn't check out");
475 }
476 }
477 }
478
479 boolean checkForDuplicates = query != null
480 || (isTypeGroupChat && packet.hasChild("delay","urn:xmpp:delay"))
481 || message.getType() == Message.TYPE_PRIVATE;
482 if (checkForDuplicates && conversation.hasDuplicateMessage(message)) {
483 Log.d(Config.LOGTAG,"skipping duplicate message from "+message.getCounterpart().toString()+" "+message.getBody());
484 return;
485 }
486
487 if (query != null && query.getPagingOrder() == MessageArchiveService.PagingOrder.REVERSE) {
488 conversation.prepend(message);
489 } else {
490 conversation.add(message);
491 }
492
493 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
494 conversation.getAccount().getPgpDecryptionService().add(message);
495 }
496
497 if (query == null || query.getWith() == null) { //either no mam or catchup
498 if (status == Message.STATUS_SEND || status == Message.STATUS_SEND_RECEIVED) {
499 mXmppConnectionService.markRead(conversation);
500 if (query == null) {
501 account.activateGracePeriod();
502 }
503 } else {
504 message.markUnread();
505 }
506 }
507
508 if (query == null) {
509 mXmppConnectionService.updateConversationUi();
510 }
511
512 if (mXmppConnectionService.confirmMessages() && remoteMsgId != null && !isForwarded && !isTypeGroupChat) {
513 sendMessageReceipts(account, packet);
514 }
515
516 if (message.getStatus() == Message.STATUS_RECEIVED
517 && conversation.getOtrSession() != null
518 && !conversation.getOtrSession().getSessionID().getUserID()
519 .equals(message.getCounterpart().getResourcepart())) {
520 conversation.endOtrIfNeeded();
521 }
522
523 if (message.getEncryption() == Message.ENCRYPTION_NONE || mXmppConnectionService.saveEncryptedMessages()) {
524 mXmppConnectionService.databaseBackend.createMessage(message);
525 }
526 final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
527 if (message.trusted() && message.treatAsDownloadable() != Message.Decision.NEVER && manager.getAutoAcceptFileSize() > 0) {
528 manager.createNewDownloadConnection(message);
529 } else if (!message.isRead()) {
530 if (query == null) {
531 mXmppConnectionService.getNotificationService().push(message);
532 } else if (query.getWith() == null) { // mam catchup
533 mXmppConnectionService.getNotificationService().pushFromBacklog(message);
534 }
535 }
536 } else if (!packet.hasChild("body")){ //no body
537 if (Config.BACKGROUND_STANZA_LOGGING && mXmppConnectionService.checkListeners()) {
538 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": " + packet+ " (carbon="+Boolean.toString(isCarbon)+")");
539 }
540 Conversation conversation = mXmppConnectionService.find(account, from.toBareJid());
541 if (isTypeGroupChat) {
542 if (packet.hasChild("subject")) {
543 if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
544 conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0);
545 String subject = packet.findChildContent("subject");
546 conversation.getMucOptions().setSubject(subject);
547 final Bookmark bookmark = conversation.getBookmark();
548 if (bookmark != null && bookmark.getBookmarkName() == null) {
549 if (bookmark.setBookmarkName(subject)) {
550 mXmppConnectionService.pushBookmarks(account);
551 }
552 }
553 mXmppConnectionService.updateConversationUi();
554 return;
555 }
556 }
557 }
558 if (conversation != null && mucUserElement != null && from.isBareJid()) {
559 if (mucUserElement.hasChild("status")) {
560 for (Element child : mucUserElement.getChildren()) {
561 if (child.getName().equals("status")
562 && MucOptions.STATUS_CODE_ROOM_CONFIG_CHANGED.equals(child.getAttribute("code"))) {
563 mXmppConnectionService.fetchConferenceConfiguration(conversation);
564 }
565 }
566 } else if (mucUserElement.hasChild("item")) {
567 for(Element child : mucUserElement.getChildren()) {
568 if ("item".equals(child.getName())) {
569 MucOptions.User user = AbstractParser.parseItem(conversation,child);
570 Log.d(Config.LOGTAG,account.getJid()+": changing affiliation for "
571 +user.getRealJid()+" to "+user.getAffiliation()+" in "
572 +conversation.getJid().toBareJid());
573 if (!user.realJidMatchesAccount()) {
574 conversation.getMucOptions().addUser(user);
575 mXmppConnectionService.getAvatarService().clear(conversation);
576 mXmppConnectionService.updateMucRosterUi();
577 mXmppConnectionService.updateConversationUi();
578 }
579 }
580 }
581 }
582 }
583 }
584
585
586
587 Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
588 if (received == null) {
589 received = packet.findChild("received", "urn:xmpp:receipts");
590 }
591 if (received != null && !packet.fromAccount(account)) {
592 mXmppConnectionService.markMessage(account, from.toBareJid(), received.getAttribute("id"), Message.STATUS_SEND_RECEIVED);
593 }
594 Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
595 if (displayed != null) {
596 if (packet.fromAccount(account)) {
597 Conversation conversation = mXmppConnectionService.find(account,counterpart.toBareJid());
598 if (conversation != null) {
599 mXmppConnectionService.markRead(conversation);
600 }
601 } else {
602 updateLastseen(timestamp, account, from);
603 final Message displayedMessage = mXmppConnectionService.markMessage(account, from.toBareJid(), displayed.getAttribute("id"), Message.STATUS_SEND_DISPLAYED);
604 Message message = displayedMessage == null ? null : displayedMessage.prev();
605 while (message != null
606 && message.getStatus() == Message.STATUS_SEND_RECEIVED
607 && message.getTimeSent() < displayedMessage.getTimeSent()) {
608 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
609 message = message.prev();
610 }
611 }
612 }
613
614 Element event = packet.findChild("event", "http://jabber.org/protocol/pubsub#event");
615 if (event != null) {
616 parseEvent(event, from, account);
617 }
618
619 String nick = packet.findChildContent("nick", "http://jabber.org/protocol/nick");
620 if (nick != null) {
621 Contact contact = account.getRoster().getContact(from);
622 contact.setPresenceName(nick);
623 }
624 }
625
626 private void sendMessageReceipts(Account account, MessagePacket packet) {
627 ArrayList<String> receiptsNamespaces = new ArrayList<>();
628 if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
629 receiptsNamespaces.add("urn:xmpp:chat-markers:0");
630 }
631 if (packet.hasChild("request", "urn:xmpp:receipts")) {
632 receiptsNamespaces.add("urn:xmpp:receipts");
633 }
634 if (receiptsNamespaces.size() > 0) {
635 MessagePacket receipt = mXmppConnectionService.getMessageGenerator().received(account,
636 packet,
637 receiptsNamespaces,
638 packet.getType());
639 mXmppConnectionService.sendMessagePacket(account, receipt);
640 }
641 }
642}