2014-05-14 06:56:34 -04:00
|
|
|
package eu.siacs.conversations.parser;
|
2014-02-18 19:35:23 -05:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
import android.os.SystemClock;
|
2014-10-02 15:07:11 -04:00
|
|
|
import android.util.Log;
|
2014-02-18 19:35:23 -05:00
|
|
|
import net.java.otr4j.session.Session;
|
|
|
|
import net.java.otr4j.session.SessionStatus;
|
2014-08-31 10:28:21 -04:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-02-28 12:46:01 -05:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
2014-08-05 16:58:46 -04:00
|
|
|
import eu.siacs.conversations.entities.Contact;
|
2014-02-28 12:46:01 -05:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.Message;
|
2014-09-28 10:33:25 -04:00
|
|
|
import eu.siacs.conversations.services.NotificationService;
|
2014-02-28 12:46:01 -05:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2014-06-20 11:30:19 -04:00
|
|
|
import eu.siacs.conversations.utils.CryptoHelper;
|
2014-02-28 12:46:01 -05:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2014-07-11 20:36:37 -04:00
|
|
|
import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
|
2014-08-05 16:58:46 -04:00
|
|
|
import eu.siacs.conversations.xmpp.pep.Avatar;
|
2014-03-10 14:22:13 -04:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
|
2014-02-18 19:35:23 -05:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
public class MessageParser extends AbstractParser implements
|
|
|
|
OnMessagePacketReceived {
|
|
|
|
|
2014-08-31 10:28:21 -04:00
|
|
|
private long lastCarbonMessageReceived = -(Config.CARBON_GRACE_PERIOD * 1000);
|
2014-06-04 06:31:19 -04:00
|
|
|
|
2014-05-14 06:56:34 -04:00
|
|
|
public MessageParser(XmppConnectionService service) {
|
2014-06-06 12:26:40 -04:00
|
|
|
super(service);
|
2014-05-14 06:56:34 -04:00
|
|
|
}
|
2014-06-04 06:31:19 -04:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
private Message parseChat(MessagePacket packet, Account account) {
|
2014-09-23 05:48:48 -04:00
|
|
|
String[] fromParts = packet.getFrom().split("/", 2);
|
2014-06-04 06:31:19 -04:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, fromParts[0], false);
|
2014-06-04 12:44:15 -04:00
|
|
|
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
2014-07-11 20:36:37 -04:00
|
|
|
updateLastseen(packet, account, true);
|
2014-06-04 05:55:38 -04:00
|
|
|
String pgpBody = getPgpBody(packet);
|
2014-06-22 12:21:04 -04:00
|
|
|
Message finishedMessage;
|
2014-06-04 06:31:19 -04:00
|
|
|
if (pgpBody != null) {
|
2014-07-11 20:36:37 -04:00
|
|
|
finishedMessage = new Message(conversation, packet.getFrom(),
|
2014-08-28 05:01:24 -04:00
|
|
|
pgpBody, Message.ENCRYPTION_PGP, Message.STATUS_RECEIVED);
|
2014-06-04 05:55:38 -04:00
|
|
|
} else {
|
2014-06-22 12:21:04 -04:00
|
|
|
finishedMessage = new Message(conversation, packet.getFrom(),
|
2014-06-04 06:31:19 -04:00
|
|
|
packet.getBody(), Message.ENCRYPTION_NONE,
|
2014-08-28 05:01:24 -04:00
|
|
|
Message.STATUS_RECEIVED);
|
2014-06-04 05:55:38 -04:00
|
|
|
}
|
2014-08-25 13:20:53 -04:00
|
|
|
finishedMessage.setRemoteMsgId(packet.getId());
|
2014-08-10 09:27:44 -04:00
|
|
|
if (conversation.getMode() == Conversation.MODE_MULTI
|
|
|
|
&& fromParts.length >= 2) {
|
|
|
|
finishedMessage.setType(Message.TYPE_PRIVATE);
|
|
|
|
finishedMessage.setPresence(fromParts[1]);
|
|
|
|
finishedMessage.setTrueCounterpart(conversation.getMucOptions()
|
|
|
|
.getTrueCounterpart(fromParts[1]));
|
2014-08-25 13:20:53 -04:00
|
|
|
if (conversation.hasDuplicateMessage(finishedMessage)) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-10 09:27:44 -04:00
|
|
|
|
|
|
|
}
|
2014-06-22 12:21:04 -04:00
|
|
|
finishedMessage.setTime(getTimestamp(packet));
|
|
|
|
return finishedMessage;
|
2014-02-27 18:22:56 -05:00
|
|
|
}
|
2014-06-04 05:55:38 -04:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
private Message parseOtrChat(MessagePacket packet, Account account) {
|
2014-09-23 05:48:48 -04:00
|
|
|
boolean properlyAddressed = (packet.getTo().split("/", 2).length == 2)
|
2014-06-04 06:31:19 -04:00
|
|
|
|| (account.countPresences() == 1);
|
2014-09-23 05:48:48 -04:00
|
|
|
String[] fromParts = packet.getFrom().split("/", 2);
|
2014-06-04 06:31:19 -04:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, fromParts[0], false);
|
2014-08-11 07:46:32 -04:00
|
|
|
String presence;
|
|
|
|
if (fromParts.length >= 2) {
|
|
|
|
presence = fromParts[1];
|
|
|
|
} else {
|
|
|
|
presence = "";
|
|
|
|
}
|
2014-07-11 20:36:37 -04:00
|
|
|
updateLastseen(packet, account, true);
|
2014-02-18 19:35:23 -05:00
|
|
|
String body = packet.getBody();
|
2014-09-06 12:21:31 -04:00
|
|
|
if (body.matches("^\\?OTRv\\d*\\?")) {
|
2014-10-05 05:14:50 -04:00
|
|
|
conversation.endOtrIfNeeded();
|
2014-09-06 12:21:31 -04:00
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
if (!conversation.hasValidOtrSession()) {
|
2014-03-19 11:16:40 -04:00
|
|
|
if (properlyAddressed) {
|
2014-09-08 17:58:37 -04:00
|
|
|
conversation.startOtrSession(mXmppConnectionService, presence,
|
|
|
|
false);
|
2014-03-19 11:16:40 -04:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-07 18:48:52 -05:00
|
|
|
} else {
|
2014-06-04 06:31:19 -04:00
|
|
|
String foreignPresence = conversation.getOtrSession()
|
|
|
|
.getSessionID().getUserID();
|
2014-08-11 07:46:32 -04:00
|
|
|
if (!foreignPresence.equals(presence)) {
|
2014-06-25 10:55:47 -04:00
|
|
|
conversation.endOtrIfNeeded();
|
2014-03-19 11:16:40 -04:00
|
|
|
if (properlyAddressed) {
|
2014-09-08 17:58:37 -04:00
|
|
|
conversation.startOtrSession(mXmppConnectionService,
|
2014-08-11 07:46:32 -04:00
|
|
|
presence, false);
|
2014-03-19 11:16:40 -04:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-07 18:48:52 -05:00
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
Session otrSession = conversation.getOtrSession();
|
2014-06-04 06:31:19 -04:00
|
|
|
SessionStatus before = otrSession.getSessionStatus();
|
2014-02-18 19:35:23 -05:00
|
|
|
body = otrSession.transformReceiving(body);
|
|
|
|
SessionStatus after = otrSession.getSessionStatus();
|
2014-06-04 06:31:19 -04:00
|
|
|
if ((before != after) && (after == SessionStatus.ENCRYPTED)) {
|
2014-06-11 15:53:25 -04:00
|
|
|
mXmppConnectionService.onOtrSessionEstablished(conversation);
|
2014-02-18 19:35:23 -05:00
|
|
|
} else if ((before != after) && (after == SessionStatus.FINISHED)) {
|
|
|
|
conversation.resetOtrSession();
|
2014-07-20 06:36:57 -04:00
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-02-18 19:35:23 -05:00
|
|
|
}
|
2014-06-04 06:31:19 -04:00
|
|
|
if ((body == null) || (body.isEmpty())) {
|
2014-03-23 09:15:14 -04:00
|
|
|
return null;
|
|
|
|
}
|
2014-06-20 11:30:19 -04:00
|
|
|
if (body.startsWith(CryptoHelper.FILETRANSFER)) {
|
|
|
|
String key = body.substring(CryptoHelper.FILETRANSFER.length());
|
|
|
|
conversation.setSymmetricKey(CryptoHelper.hexToBytes(key));
|
|
|
|
return null;
|
|
|
|
}
|
2014-07-11 20:36:37 -04:00
|
|
|
conversation
|
|
|
|
.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
|
|
|
Message finishedMessage = new Message(conversation,
|
|
|
|
packet.getFrom(), body, Message.ENCRYPTION_OTR,
|
2014-08-28 05:01:24 -04:00
|
|
|
Message.STATUS_RECEIVED);
|
2014-06-06 12:26:40 -04:00
|
|
|
finishedMessage.setTime(getTimestamp(packet));
|
2014-08-23 09:57:39 -04:00
|
|
|
finishedMessage.setRemoteMsgId(packet.getId());
|
2014-06-06 12:26:40 -04:00
|
|
|
return finishedMessage;
|
2014-02-18 19:35:23 -05:00
|
|
|
} catch (Exception e) {
|
2014-06-25 10:55:47 -04:00
|
|
|
String receivedId = packet.getId();
|
2014-07-11 20:36:37 -04:00
|
|
|
if (receivedId != null) {
|
|
|
|
mXmppConnectionService.replyWithNotAcceptable(account, packet);
|
2014-06-25 10:55:47 -04:00
|
|
|
}
|
2014-09-06 12:21:31 -04:00
|
|
|
conversation.resetOtrSession();
|
2014-02-18 19:35:23 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 06:31:19 -04:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
private Message parseGroupchat(MessagePacket packet, Account account) {
|
2014-02-18 19:35:23 -05:00
|
|
|
int status;
|
2014-09-23 05:48:48 -04:00
|
|
|
String[] fromParts = packet.getFrom().split("/", 2);
|
2014-08-10 09:27:44 -04:00
|
|
|
if (mXmppConnectionService.find(account.pendingConferenceLeaves,
|
|
|
|
account, fromParts[0]) != null) {
|
2014-07-19 20:26:23 -04:00
|
|
|
return null;
|
|
|
|
}
|
2014-06-04 06:31:19 -04:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, fromParts[0], true);
|
2014-03-14 17:40:56 -04:00
|
|
|
if (packet.hasChild("subject")) {
|
2014-06-04 06:31:19 -04:00
|
|
|
conversation.getMucOptions().setSubject(
|
|
|
|
packet.findChild("subject").getContent());
|
2014-07-12 07:42:17 -04:00
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-03-14 17:40:56 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ((fromParts.length == 1)) {
|
2014-02-18 19:35:23 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String counterPart = fromParts[1];
|
2014-07-15 15:54:11 -04:00
|
|
|
if (counterPart.equals(conversation.getMucOptions().getActualNick())) {
|
2014-06-04 06:31:19 -04:00
|
|
|
if (mXmppConnectionService.markMessage(conversation,
|
|
|
|
packet.getId(), Message.STATUS_SEND)) {
|
2014-05-16 16:46:15 -04:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
} else {
|
2014-08-28 05:01:24 -04:00
|
|
|
status = Message.STATUS_RECEIVED;
|
2014-02-18 19:35:23 -05:00
|
|
|
}
|
2014-05-22 10:17:51 -04:00
|
|
|
String pgpBody = getPgpBody(packet);
|
2014-06-04 12:44:15 -04:00
|
|
|
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
2014-06-06 12:26:40 -04:00
|
|
|
Message finishedMessage;
|
2014-06-04 06:31:19 -04:00
|
|
|
if (pgpBody == null) {
|
2014-07-11 20:36:37 -04:00
|
|
|
finishedMessage = new Message(conversation, counterPart,
|
|
|
|
packet.getBody(), Message.ENCRYPTION_NONE, status);
|
2014-05-22 10:17:51 -04:00
|
|
|
} else {
|
2014-07-11 20:36:37 -04:00
|
|
|
finishedMessage = new Message(conversation, counterPart, pgpBody,
|
2014-06-04 06:31:19 -04:00
|
|
|
Message.ENCRYPTION_PGP, status);
|
2014-05-22 10:17:51 -04:00
|
|
|
}
|
2014-08-23 09:57:39 -04:00
|
|
|
finishedMessage.setRemoteMsgId(packet.getId());
|
2014-08-28 05:01:24 -04:00
|
|
|
if (status == Message.STATUS_RECEIVED) {
|
2014-08-10 09:27:44 -04:00
|
|
|
finishedMessage.setTrueCounterpart(conversation.getMucOptions()
|
|
|
|
.getTrueCounterpart(counterPart));
|
2014-07-29 08:42:17 -04:00
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
if (packet.hasChild("delay")
|
|
|
|
&& conversation.hasDuplicateMessage(finishedMessage)) {
|
2014-08-23 09:57:39 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
finishedMessage.setTime(getTimestamp(packet));
|
2014-06-06 12:26:40 -04:00
|
|
|
return finishedMessage;
|
2014-02-18 19:35:23 -05:00
|
|
|
}
|
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
private Message parseCarbonMessage(MessagePacket packet, Account account) {
|
2014-02-18 19:35:23 -05:00
|
|
|
int status;
|
|
|
|
String fullJid;
|
|
|
|
Element forwarded;
|
2014-09-12 14:14:19 -04:00
|
|
|
if (packet.hasChild("received", "urn:xmpp:carbons:2")) {
|
|
|
|
forwarded = packet.findChild("received", "urn:xmpp:carbons:2")
|
2014-09-23 05:48:48 -04:00
|
|
|
.findChild("forwarded", "urn:xmpp:forward:0");
|
2014-08-28 05:01:24 -04:00
|
|
|
status = Message.STATUS_RECEIVED;
|
2014-09-12 14:14:19 -04:00
|
|
|
} else if (packet.hasChild("sent", "urn:xmpp:carbons:2")) {
|
|
|
|
forwarded = packet.findChild("sent", "urn:xmpp:carbons:2")
|
2014-09-23 05:48:48 -04:00
|
|
|
.findChild("forwarded", "urn:xmpp:forward:0");
|
2014-02-18 19:35:23 -05:00
|
|
|
status = Message.STATUS_SEND;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-06-04 06:31:19 -04:00
|
|
|
if (forwarded == null) {
|
2014-03-31 03:45:39 -04:00
|
|
|
return null;
|
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
Element message = forwarded.findChild("message");
|
2014-10-02 15:07:11 -04:00
|
|
|
if (message == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!message.hasChild("body")) {
|
2014-08-31 10:28:21 -04:00
|
|
|
if (status == Message.STATUS_RECEIVED
|
|
|
|
&& message.getAttribute("from") != null) {
|
2014-09-11 07:18:29 -04:00
|
|
|
parseNonMessage(message, account);
|
2014-10-02 15:07:11 -04:00
|
|
|
} else if (status == Message.STATUS_SEND
|
|
|
|
&& message.hasChild("displayed", "urn:xmpp:chat-markers:0")) {
|
|
|
|
String to = message.getAttribute("to");
|
|
|
|
if (to != null) {
|
|
|
|
Conversation conversation = mXmppConnectionService.find(
|
|
|
|
mXmppConnectionService.getConversations(), account,
|
|
|
|
to.split("/")[0]);
|
|
|
|
if (conversation != null) {
|
|
|
|
mXmppConnectionService.markRead(conversation, false);
|
|
|
|
}
|
|
|
|
}
|
2014-07-11 08:49:06 -04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-28 05:01:24 -04:00
|
|
|
if (status == Message.STATUS_RECEIVED) {
|
2014-02-18 19:35:23 -05:00
|
|
|
fullJid = message.getAttribute("from");
|
2014-08-10 09:27:44 -04:00
|
|
|
if (fullJid == null) {
|
2014-07-27 16:31:33 -04:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
updateLastseen(message, account, true);
|
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
} else {
|
|
|
|
fullJid = message.getAttribute("to");
|
2014-07-27 16:31:33 -04:00
|
|
|
if (fullJid == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-07-16 06:42:44 -04:00
|
|
|
}
|
2014-09-23 05:48:48 -04:00
|
|
|
String[] parts = fullJid.split("/", 2);
|
2014-06-04 06:31:19 -04:00
|
|
|
Conversation conversation = mXmppConnectionService
|
|
|
|
.findOrCreateConversation(account, parts[0], false);
|
2014-06-04 12:44:15 -04:00
|
|
|
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-06-04 06:31:19 -04:00
|
|
|
String pgpBody = getPgpBody(message);
|
2014-06-06 12:26:40 -04:00
|
|
|
Message finishedMessage;
|
2014-06-04 06:31:19 -04:00
|
|
|
if (pgpBody != null) {
|
2014-07-11 20:36:37 -04:00
|
|
|
finishedMessage = new Message(conversation, fullJid, pgpBody,
|
|
|
|
Message.ENCRYPTION_PGP, status);
|
2014-06-04 05:55:38 -04:00
|
|
|
} else {
|
2014-06-04 06:31:19 -04:00
|
|
|
String body = message.findChild("body").getContent();
|
2014-07-11 20:36:37 -04:00
|
|
|
finishedMessage = new Message(conversation, fullJid, body,
|
|
|
|
Message.ENCRYPTION_NONE, status);
|
2014-06-04 05:55:38 -04:00
|
|
|
}
|
2014-06-06 12:26:40 -04:00
|
|
|
finishedMessage.setTime(getTimestamp(message));
|
2014-08-23 09:57:39 -04:00
|
|
|
finishedMessage.setRemoteMsgId(message.getAttribute("id"));
|
2014-08-10 09:27:44 -04:00
|
|
|
if (conversation.getMode() == Conversation.MODE_MULTI
|
|
|
|
&& parts.length >= 2) {
|
|
|
|
finishedMessage.setType(Message.TYPE_PRIVATE);
|
|
|
|
finishedMessage.setPresence(parts[1]);
|
|
|
|
finishedMessage.setTrueCounterpart(conversation.getMucOptions()
|
|
|
|
.getTrueCounterpart(parts[1]));
|
2014-08-25 13:20:53 -04:00
|
|
|
if (conversation.hasDuplicateMessage(finishedMessage)) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-10 09:27:44 -04:00
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-06-06 12:26:40 -04:00
|
|
|
return finishedMessage;
|
2014-02-18 19:35:23 -05:00
|
|
|
}
|
2014-02-20 11:00:50 -05:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
private void parseError(MessagePacket packet, Account account) {
|
2014-09-23 05:48:48 -04:00
|
|
|
String[] fromParts = packet.getFrom().split("/", 2);
|
2014-06-04 06:31:19 -04:00
|
|
|
mXmppConnectionService.markMessage(account, fromParts[0],
|
|
|
|
packet.getId(), Message.STATUS_SEND_FAILED);
|
2014-02-20 11:00:50 -05:00
|
|
|
}
|
2014-07-11 20:36:37 -04:00
|
|
|
|
2014-09-11 07:18:29 -04:00
|
|
|
private void parseNonMessage(Element packet, Account account) {
|
2014-09-23 05:48:48 -04:00
|
|
|
String from = packet.getAttribute("from");
|
2014-08-10 09:27:44 -04:00
|
|
|
if (packet.hasChild("event", "http://jabber.org/protocol/pubsub#event")) {
|
|
|
|
Element event = packet.findChild("event",
|
|
|
|
"http://jabber.org/protocol/pubsub#event");
|
|
|
|
parseEvent(event, packet.getAttribute("from"), account);
|
2014-09-23 05:48:48 -04:00
|
|
|
} else if (from != null
|
|
|
|
&& packet.hasChild("displayed", "urn:xmpp:chat-markers:0")) {
|
2014-07-11 20:36:37 -04:00
|
|
|
String id = packet
|
|
|
|
.findChild("displayed", "urn:xmpp:chat-markers:0")
|
|
|
|
.getAttribute("id");
|
|
|
|
updateLastseen(packet, account, true);
|
2014-09-23 05:48:48 -04:00
|
|
|
mXmppConnectionService.markMessage(account, from.split("/", 2)[0],
|
|
|
|
id, Message.STATUS_SEND_DISPLAYED);
|
|
|
|
} else if (from != null
|
|
|
|
&& packet.hasChild("received", "urn:xmpp:chat-markers:0")) {
|
2014-07-11 20:36:37 -04:00
|
|
|
String id = packet.findChild("received", "urn:xmpp:chat-markers:0")
|
|
|
|
.getAttribute("id");
|
|
|
|
updateLastseen(packet, account, false);
|
2014-09-23 05:48:48 -04:00
|
|
|
mXmppConnectionService.markMessage(account, from.split("/", 2)[0],
|
|
|
|
id, Message.STATUS_SEND_RECEIVED);
|
|
|
|
} else if (from != null
|
|
|
|
&& packet.hasChild("received", "urn:xmpp:receipts")) {
|
2014-09-20 09:49:25 -04:00
|
|
|
String id = packet.findChild("received", "urn:xmpp:receipts")
|
|
|
|
.getAttribute("id");
|
|
|
|
updateLastseen(packet, account, false);
|
2014-09-23 05:48:48 -04:00
|
|
|
mXmppConnectionService.markMessage(account, from.split("/", 2)[0],
|
|
|
|
id, Message.STATUS_SEND_RECEIVED);
|
2014-08-10 09:27:44 -04:00
|
|
|
} else if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) {
|
|
|
|
Element x = packet.findChild("x",
|
|
|
|
"http://jabber.org/protocol/muc#user");
|
2014-06-06 12:49:35 -04:00
|
|
|
if (x.hasChild("invite")) {
|
2014-07-16 18:03:37 -04:00
|
|
|
Conversation conversation = mXmppConnectionService
|
2014-07-11 20:36:37 -04:00
|
|
|
.findOrCreateConversation(account,
|
|
|
|
packet.getAttribute("from"), true);
|
2014-07-16 18:03:37 -04:00
|
|
|
if (!conversation.getMucOptions().online()) {
|
2014-09-05 16:07:35 -04:00
|
|
|
if (x.hasChild("password")) {
|
|
|
|
Element password = x.findChild("password");
|
2014-09-08 17:58:37 -04:00
|
|
|
conversation.getMucOptions().setPassword(
|
|
|
|
password.getContent());
|
2014-09-28 09:21:56 -04:00
|
|
|
mXmppConnectionService.databaseBackend
|
|
|
|
.updateConversation(conversation);
|
2014-09-05 16:07:35 -04:00
|
|
|
}
|
2014-07-16 18:03:37 -04:00
|
|
|
mXmppConnectionService.joinMuc(conversation);
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-08-10 09:27:44 -04:00
|
|
|
}
|
2014-06-06 12:49:35 -04:00
|
|
|
}
|
2014-07-21 10:04:53 -04:00
|
|
|
} else if (packet.hasChild("x", "jabber:x:conference")) {
|
|
|
|
Element x = packet.findChild("x", "jabber:x:conference");
|
|
|
|
String jid = x.getAttribute("jid");
|
2014-09-05 16:07:35 -04:00
|
|
|
String password = x.getAttribute("password");
|
2014-08-10 09:27:44 -04:00
|
|
|
if (jid != null) {
|
2014-07-21 10:04:53 -04:00
|
|
|
Conversation conversation = mXmppConnectionService
|
2014-08-10 09:27:44 -04:00
|
|
|
.findOrCreateConversation(account, jid, true);
|
2014-07-21 10:04:53 -04:00
|
|
|
if (!conversation.getMucOptions().online()) {
|
2014-09-05 16:07:35 -04:00
|
|
|
if (password != null) {
|
|
|
|
conversation.getMucOptions().setPassword(password);
|
2014-09-28 09:21:56 -04:00
|
|
|
mXmppConnectionService.databaseBackend
|
|
|
|
.updateConversation(conversation);
|
2014-09-05 16:07:35 -04:00
|
|
|
}
|
2014-07-21 10:04:53 -04:00
|
|
|
mXmppConnectionService.joinMuc(conversation);
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
}
|
|
|
|
}
|
2014-06-06 12:49:35 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-27 18:22:56 -05:00
|
|
|
|
2014-08-04 19:36:17 -04:00
|
|
|
private void parseEvent(Element event, String from, Account account) {
|
|
|
|
Element items = event.findChild("items");
|
|
|
|
String node = items.getAttribute("node");
|
2014-08-10 09:27:44 -04:00
|
|
|
if (node != null) {
|
2014-08-05 16:58:46 -04:00
|
|
|
if (node.equals("urn:xmpp:avatar:metadata")) {
|
|
|
|
Avatar avatar = Avatar.parseMetadata(items);
|
2014-08-31 10:28:21 -04:00
|
|
|
if (avatar != null) {
|
2014-08-15 07:11:33 -04:00
|
|
|
avatar.owner = from;
|
|
|
|
if (mXmppConnectionService.getFileBackend().isAvatarCached(
|
|
|
|
avatar)) {
|
|
|
|
if (account.getJid().equals(from)) {
|
2014-08-21 06:32:50 -04:00
|
|
|
if (account.setAvatar(avatar.getFilename())) {
|
2014-08-31 10:28:21 -04:00
|
|
|
mXmppConnectionService.databaseBackend
|
|
|
|
.updateAccount(account);
|
2014-08-21 06:32:50 -04:00
|
|
|
}
|
2014-08-15 07:11:33 -04:00
|
|
|
} else {
|
2014-08-31 10:28:21 -04:00
|
|
|
Contact contact = account.getRoster().getContact(
|
|
|
|
from);
|
2014-08-15 07:11:33 -04:00
|
|
|
contact.setAvatar(avatar.getFilename());
|
|
|
|
}
|
2014-08-05 16:58:46 -04:00
|
|
|
} else {
|
2014-08-15 07:11:33 -04:00
|
|
|
mXmppConnectionService.fetchAvatar(account, avatar);
|
2014-08-05 16:58:46 -04:00
|
|
|
}
|
|
|
|
}
|
2014-09-04 04:50:06 -04:00
|
|
|
} 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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 16:58:46 -04:00
|
|
|
}
|
2014-08-04 19:36:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 12:44:15 -04:00
|
|
|
private String getPgpBody(Element message) {
|
|
|
|
Element child = message.findChild("x", "jabber:x:encrypted");
|
2014-06-04 06:31:19 -04:00
|
|
|
if (child == null) {
|
2014-05-22 10:17:51 -04:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return child.getContent();
|
2014-02-27 18:22:56 -05:00
|
|
|
}
|
|
|
|
}
|
2014-07-11 20:36:37 -04:00
|
|
|
|
2014-06-04 12:44:15 -04:00
|
|
|
private String getMarkableMessageId(Element message) {
|
|
|
|
if (message.hasChild("markable", "urn:xmpp:chat-markers:0")) {
|
|
|
|
return message.getAttribute("id");
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-06-06 12:49:35 -04:00
|
|
|
|
2014-07-11 20:36:37 -04:00
|
|
|
@Override
|
|
|
|
public void onMessagePacketReceived(Account account, MessagePacket packet) {
|
|
|
|
Message message = null;
|
2014-09-28 10:33:25 -04:00
|
|
|
boolean notify = mXmppConnectionService.getPreferences().getBoolean(
|
|
|
|
"show_notification", true);
|
|
|
|
notify = notify
|
|
|
|
&& (SystemClock.elapsedRealtime() - lastCarbonMessageReceived) > (Config.CARBON_GRACE_PERIOD * 1000);
|
|
|
|
boolean alwaysNotifyInConference = notify
|
|
|
|
&& mXmppConnectionService.getPreferences().getBoolean(
|
|
|
|
"always_notify_in_conference", false);
|
2014-07-11 20:36:37 -04:00
|
|
|
|
2014-09-04 04:50:06 -04:00
|
|
|
this.parseNick(packet, account);
|
|
|
|
|
2014-09-11 07:18:29 -04:00
|
|
|
if ((packet.getType() == MessagePacket.TYPE_CHAT || packet.getType() == MessagePacket.TYPE_NORMAL)) {
|
2014-07-11 20:36:37 -04:00
|
|
|
if ((packet.getBody() != null)
|
|
|
|
&& (packet.getBody().startsWith("?OTR"))) {
|
|
|
|
message = this.parseOtrChat(packet, account);
|
|
|
|
if (message != null) {
|
|
|
|
message.markUnread();
|
|
|
|
}
|
2014-09-29 11:59:53 -04:00
|
|
|
} else if (packet.hasChild("body")
|
|
|
|
&& !(packet.hasChild("x",
|
|
|
|
"http://jabber.org/protocol/muc#user"))) {
|
2014-07-11 20:36:37 -04:00
|
|
|
message = this.parseChat(packet, account);
|
2014-08-25 13:20:53 -04:00
|
|
|
if (message != null) {
|
|
|
|
message.markUnread();
|
|
|
|
}
|
2014-09-12 14:14:19 -04:00
|
|
|
} else if (packet.hasChild("received", "urn:xmpp:carbons:2")
|
|
|
|
|| (packet.hasChild("sent", "urn:xmpp:carbons:2"))) {
|
2014-07-11 20:36:37 -04:00
|
|
|
message = this.parseCarbonMessage(packet, account);
|
|
|
|
if (message != null) {
|
|
|
|
if (message.getStatus() == Message.STATUS_SEND) {
|
|
|
|
lastCarbonMessageReceived = SystemClock
|
|
|
|
.elapsedRealtime();
|
|
|
|
notify = false;
|
2014-10-02 15:07:11 -04:00
|
|
|
mXmppConnectionService.markRead(
|
|
|
|
message.getConversation(), false);
|
2014-07-11 20:36:37 -04:00
|
|
|
} else {
|
|
|
|
message.markUnread();
|
|
|
|
}
|
|
|
|
}
|
2014-07-23 08:30:27 -04:00
|
|
|
} else {
|
2014-09-11 07:18:29 -04:00
|
|
|
parseNonMessage(packet, account);
|
2014-07-11 20:36:37 -04:00
|
|
|
}
|
|
|
|
} else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) {
|
|
|
|
message = this.parseGroupchat(packet, account);
|
|
|
|
if (message != null) {
|
2014-08-28 05:01:24 -04:00
|
|
|
if (message.getStatus() == Message.STATUS_RECEIVED) {
|
2014-07-11 20:36:37 -04:00
|
|
|
message.markUnread();
|
2014-09-28 10:33:25 -04:00
|
|
|
notify = alwaysNotifyInConference
|
|
|
|
|| NotificationService
|
|
|
|
.wasHighlightedOrPrivate(message);
|
2014-07-11 20:36:37 -04:00
|
|
|
} else {
|
2014-10-02 15:07:11 -04:00
|
|
|
mXmppConnectionService.markRead(message.getConversation(),
|
|
|
|
false);
|
2014-08-10 09:27:44 -04:00
|
|
|
lastCarbonMessageReceived = SystemClock.elapsedRealtime();
|
2014-07-11 20:36:37 -04:00
|
|
|
notify = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (packet.getType() == MessagePacket.TYPE_ERROR) {
|
|
|
|
this.parseError(packet, account);
|
|
|
|
return;
|
2014-08-04 19:36:17 -04:00
|
|
|
} else if (packet.getType() == MessagePacket.TYPE_HEADLINE) {
|
|
|
|
this.parseHeadline(packet, account);
|
|
|
|
return;
|
2014-07-11 20:36:37 -04:00
|
|
|
}
|
|
|
|
if ((message == null) || (message.getBody() == null)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((mXmppConnectionService.confirmMessages())
|
|
|
|
&& ((packet.getId() != null))) {
|
|
|
|
if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
|
2014-08-10 09:27:44 -04:00
|
|
|
MessagePacket receipt = mXmppConnectionService
|
|
|
|
.getMessageGenerator().received(account, packet,
|
|
|
|
"urn:xmpp:chat-markers:0");
|
2014-07-23 19:04:25 -04:00
|
|
|
mXmppConnectionService.sendMessagePacket(account, receipt);
|
|
|
|
}
|
|
|
|
if (packet.hasChild("request", "urn:xmpp:receipts")) {
|
2014-08-10 09:27:44 -04:00
|
|
|
MessagePacket receipt = mXmppConnectionService
|
|
|
|
.getMessageGenerator().received(account, packet,
|
|
|
|
"urn:xmpp:receipts");
|
2014-07-23 19:04:25 -04:00
|
|
|
mXmppConnectionService.sendMessagePacket(account, receipt);
|
2014-07-11 20:36:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Conversation conversation = message.getConversation();
|
|
|
|
conversation.getMessages().add(message);
|
|
|
|
if (packet.getType() != MessagePacket.TYPE_ERROR) {
|
2014-09-08 17:58:37 -04:00
|
|
|
if (message.getEncryption() == Message.ENCRYPTION_NONE
|
|
|
|
|| mXmppConnectionService.saveEncryptedMessages()) {
|
2014-09-08 07:37:22 -04:00
|
|
|
mXmppConnectionService.databaseBackend.createMessage(message);
|
|
|
|
}
|
2014-07-11 20:36:37 -04:00
|
|
|
}
|
2014-09-03 06:36:54 -04:00
|
|
|
notify = notify && !conversation.isMuted();
|
2014-09-28 09:21:56 -04:00
|
|
|
if (notify) {
|
2014-09-29 12:28:13 -04:00
|
|
|
mXmppConnectionService.getNotificationService().push(message);
|
2014-09-28 09:21:56 -04:00
|
|
|
}
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-07-11 20:36:37 -04:00
|
|
|
}
|
2014-08-04 19:36:17 -04:00
|
|
|
|
|
|
|
private void parseHeadline(MessagePacket packet, Account account) {
|
2014-08-10 09:27:44 -04:00
|
|
|
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);
|
2014-08-04 19:36:17 -04:00
|
|
|
}
|
|
|
|
}
|
2014-09-04 04:50:06 -04:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
}
|