made message parser and presence parser implement interface On*PacketReceived

This commit is contained in:
iNPUTmice 2014-07-12 02:36:37 +02:00
parent 7a26f27212
commit 789383e5cb
3 changed files with 147 additions and 151 deletions

View File

@ -1,5 +1,6 @@
package eu.siacs.conversations.parser; package eu.siacs.conversations.parser;
import android.os.SystemClock;
import net.java.otr4j.session.Session; import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionStatus; import net.java.otr4j.session.SessionStatus;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
@ -8,25 +9,29 @@ import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
import eu.siacs.conversations.xmpp.stanzas.MessagePacket; import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
public class MessageParser extends AbstractParser { public class MessageParser extends AbstractParser implements
OnMessagePacketReceived {
private long lastCarbonMessageReceived = -XmppConnectionService.CARBON_GRACE_PERIOD;
public MessageParser(XmppConnectionService service) { public MessageParser(XmppConnectionService service) {
super(service); super(service);
} }
public Message parseChat(MessagePacket packet, Account account) { private Message parseChat(MessagePacket packet, Account account) {
String[] fromParts = packet.getFrom().split("/"); String[] fromParts = packet.getFrom().split("/");
Conversation conversation = mXmppConnectionService Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account, fromParts[0], false); .findOrCreateConversation(account, fromParts[0], false);
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet)); conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
updateLastseen(packet, account,true); updateLastseen(packet, account, true);
String pgpBody = getPgpBody(packet); String pgpBody = getPgpBody(packet);
Message finishedMessage; Message finishedMessage;
if (pgpBody != null) { if (pgpBody != null) {
finishedMessage = new Message(conversation, packet.getFrom(), pgpBody, finishedMessage = new Message(conversation, packet.getFrom(),
Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED); pgpBody, Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED);
} else { } else {
finishedMessage = new Message(conversation, packet.getFrom(), finishedMessage = new Message(conversation, packet.getFrom(),
packet.getBody(), Message.ENCRYPTION_NONE, packet.getBody(), Message.ENCRYPTION_NONE,
@ -36,13 +41,13 @@ public class MessageParser extends AbstractParser {
return finishedMessage; return finishedMessage;
} }
public Message parseOtrChat(MessagePacket packet, Account account) { private Message parseOtrChat(MessagePacket packet, Account account) {
boolean properlyAddressed = (packet.getTo().split("/").length == 2) boolean properlyAddressed = (packet.getTo().split("/").length == 2)
|| (account.countPresences() == 1); || (account.countPresences() == 1);
String[] fromParts = packet.getFrom().split("/"); String[] fromParts = packet.getFrom().split("/");
Conversation conversation = mXmppConnectionService Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account, fromParts[0], false); .findOrCreateConversation(account, fromParts[0], false);
updateLastseen(packet, account,true); updateLastseen(packet, account, true);
String body = packet.getBody(); String body = packet.getBody();
if (!conversation.hasValidOtrSession()) { if (!conversation.hasValidOtrSession()) {
if (properlyAddressed) { if (properlyAddressed) {
@ -84,22 +89,24 @@ public class MessageParser extends AbstractParser {
conversation.setSymmetricKey(CryptoHelper.hexToBytes(key)); conversation.setSymmetricKey(CryptoHelper.hexToBytes(key));
return null; return null;
} }
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet)); conversation
Message finishedMessage = new Message(conversation, packet.getFrom(), body, .setLatestMarkableMessageId(getMarkableMessageId(packet));
Message.ENCRYPTION_OTR, Message.STATUS_RECIEVED); Message finishedMessage = new Message(conversation,
packet.getFrom(), body, Message.ENCRYPTION_OTR,
Message.STATUS_RECIEVED);
finishedMessage.setTime(getTimestamp(packet)); finishedMessage.setTime(getTimestamp(packet));
return finishedMessage; return finishedMessage;
} catch (Exception e) { } catch (Exception e) {
String receivedId = packet.getId(); String receivedId = packet.getId();
if (receivedId!=null) { if (receivedId != null) {
mXmppConnectionService.replyWithNotAcceptable(account,packet); mXmppConnectionService.replyWithNotAcceptable(account, packet);
} }
conversation.endOtrIfNeeded(); conversation.endOtrIfNeeded();
return null; return null;
} }
} }
public Message parseGroupchat(MessagePacket packet, Account account) { private Message parseGroupchat(MessagePacket packet, Account account) {
int status; int status;
String[] fromParts = packet.getFrom().split("/"); String[] fromParts = packet.getFrom().split("/");
Conversation conversation = mXmppConnectionService Conversation conversation = mXmppConnectionService
@ -128,17 +135,17 @@ public class MessageParser extends AbstractParser {
conversation.setLatestMarkableMessageId(getMarkableMessageId(packet)); conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
Message finishedMessage; Message finishedMessage;
if (pgpBody == null) { if (pgpBody == null) {
finishedMessage = new Message(conversation, counterPart, packet.getBody(), finishedMessage = new Message(conversation, counterPart,
Message.ENCRYPTION_NONE, status); packet.getBody(), Message.ENCRYPTION_NONE, status);
} else { } else {
finishedMessage= new Message(conversation, counterPart, pgpBody, finishedMessage = new Message(conversation, counterPart, pgpBody,
Message.ENCRYPTION_PGP, status); Message.ENCRYPTION_PGP, status);
} }
finishedMessage.setTime(getTimestamp(packet)); finishedMessage.setTime(getTimestamp(packet));
return finishedMessage; return finishedMessage;
} }
public Message parseCarbonMessage(MessagePacket packet, Account account) { private Message parseCarbonMessage(MessagePacket packet, Account account) {
int status; int status;
String fullJid; String fullJid;
Element forwarded; Element forwarded;
@ -163,7 +170,7 @@ public class MessageParser extends AbstractParser {
} }
if (status == Message.STATUS_RECIEVED) { if (status == Message.STATUS_RECIEVED) {
fullJid = message.getAttribute("from"); fullJid = message.getAttribute("from");
updateLastseen(message, account,true); updateLastseen(message, account, true);
} else { } else {
fullJid = message.getAttribute("to"); fullJid = message.getAttribute("to");
} }
@ -174,37 +181,45 @@ public class MessageParser extends AbstractParser {
String pgpBody = getPgpBody(message); String pgpBody = getPgpBody(message);
Message finishedMessage; Message finishedMessage;
if (pgpBody != null) { if (pgpBody != null) {
finishedMessage = new Message(conversation, fullJid, pgpBody,Message.ENCRYPTION_PGP, status); finishedMessage = new Message(conversation, fullJid, pgpBody,
Message.ENCRYPTION_PGP, status);
} else { } else {
String body = message.findChild("body").getContent(); String body = message.findChild("body").getContent();
finishedMessage= new Message(conversation, fullJid, body,Message.ENCRYPTION_NONE, status); finishedMessage = new Message(conversation, fullJid, body,
Message.ENCRYPTION_NONE, status);
} }
finishedMessage.setTime(getTimestamp(message)); finishedMessage.setTime(getTimestamp(message));
return finishedMessage; return finishedMessage;
} }
public void parseError(MessagePacket packet, Account account) { private void parseError(MessagePacket packet, Account account) {
String[] fromParts = packet.getFrom().split("/"); String[] fromParts = packet.getFrom().split("/");
mXmppConnectionService.markMessage(account, fromParts[0], mXmppConnectionService.markMessage(account, fromParts[0],
packet.getId(), Message.STATUS_SEND_FAILED); packet.getId(), Message.STATUS_SEND_FAILED);
} }
public void parseNormal(Element packet, Account account) { private void parseNormal(Element packet, Account account) {
if (packet.hasChild("displayed","urn:xmpp:chat-markers:0")) { if (packet.hasChild("displayed", "urn:xmpp:chat-markers:0")) {
String id = packet.findChild("displayed","urn:xmpp:chat-markers:0").getAttribute("id"); String id = packet
.findChild("displayed", "urn:xmpp:chat-markers:0")
.getAttribute("id");
String[] fromParts = packet.getAttribute("from").split("/"); String[] fromParts = packet.getAttribute("from").split("/");
updateLastseen(packet, account,true); updateLastseen(packet, account, true);
mXmppConnectionService.markMessage(account,fromParts[0], id, Message.STATUS_SEND_DISPLAYED); mXmppConnectionService.markMessage(account, fromParts[0], id,
} else if (packet.hasChild("received","urn:xmpp:chat-markers:0")) { Message.STATUS_SEND_DISPLAYED);
String id = packet.findChild("received","urn:xmpp:chat-markers:0").getAttribute("id"); } else if (packet.hasChild("received", "urn:xmpp:chat-markers:0")) {
String id = packet.findChild("received", "urn:xmpp:chat-markers:0")
.getAttribute("id");
String[] fromParts = packet.getAttribute("from").split("/"); String[] fromParts = packet.getAttribute("from").split("/");
updateLastseen(packet, account,false); updateLastseen(packet, account, false);
mXmppConnectionService.markMessage(account,fromParts[0], id, Message.STATUS_SEND_RECEIVED); mXmppConnectionService.markMessage(account, fromParts[0], id,
Message.STATUS_SEND_RECEIVED);
} else if (packet.hasChild("x")) { } else if (packet.hasChild("x")) {
Element x = packet.findChild("x"); Element x = packet.findChild("x");
if (x.hasChild("invite")) { if (x.hasChild("invite")) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, packet.getAttribute("from"), Conversation conversation = mXmppConnectionService
true); .findOrCreateConversation(account,
packet.getAttribute("from"), true);
mXmppConnectionService.updateUi(conversation, false); mXmppConnectionService.updateUi(conversation, false);
} }
@ -219,7 +234,7 @@ public class MessageParser extends AbstractParser {
return child.getContent(); return child.getContent();
} }
} }
private String getMarkableMessageId(Element message) { private String getMarkableMessageId(Element message) {
if (message.hasChild("markable", "urn:xmpp:chat-markers:0")) { if (message.hasChild("markable", "urn:xmpp:chat-markers:0")) {
return message.getAttribute("id"); return message.getAttribute("id");
@ -228,5 +243,81 @@ public class MessageParser extends AbstractParser {
} }
} }
@Override
public void onMessagePacketReceived(Account account, MessagePacket packet) {
Message message = null;
boolean notify = true;
if (mXmppConnectionService.getPreferences().getBoolean(
"notification_grace_period_after_carbon_received", true)) {
notify = (SystemClock.elapsedRealtime() - lastCarbonMessageReceived) > XmppConnectionService.CARBON_GRACE_PERIOD;
}
if ((packet.getType() == MessagePacket.TYPE_CHAT)) {
if ((packet.getBody() != null)
&& (packet.getBody().startsWith("?OTR"))) {
message = this.parseOtrChat(packet, account);
if (message != null) {
message.markUnread();
}
} else if (packet.hasChild("body")) {
message = this.parseChat(packet, account);
message.markUnread();
} else if (packet.hasChild("received") || (packet.hasChild("sent"))) {
message = this.parseCarbonMessage(packet, account);
if (message != null) {
if (message.getStatus() == Message.STATUS_SEND) {
lastCarbonMessageReceived = SystemClock
.elapsedRealtime();
notify = false;
message.getConversation().markRead();
} else {
message.markUnread();
}
}
}
} else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) {
message = this.parseGroupchat(packet, account);
if (message != null) {
if (message.getStatus() == Message.STATUS_RECIEVED) {
message.markUnread();
} else {
message.getConversation().markRead();
notify = false;
}
}
} else if (packet.getType() == MessagePacket.TYPE_ERROR) {
this.parseError(packet, account);
return;
} else if (packet.getType() == MessagePacket.TYPE_NORMAL) {
this.parseNormal(packet, account);
}
if ((message == null) || (message.getBody() == null)) {
return;
}
if ((mXmppConnectionService.confirmMessages())
&& ((packet.getId() != null))) {
MessagePacket receivedPacket = new MessagePacket();
receivedPacket.setType(MessagePacket.TYPE_NORMAL);
receivedPacket.setTo(message.getCounterpart());
receivedPacket.setFrom(account.getFullJid());
if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
Element received = receivedPacket.addChild("received",
"urn:xmpp:chat-markers:0");
received.setAttribute("id", packet.getId());
account.getXmppConnection().sendMessagePacket(receivedPacket);
} else if (packet.hasChild("request", "urn:xmpp:receipts")) {
Element received = receivedPacket.addChild("received",
"urn:xmpp:receipts");
received.setAttribute("id", packet.getId());
account.getXmppConnection().sendMessagePacket(receivedPacket);
}
}
Conversation conversation = message.getConversation();
conversation.getMessages().add(message);
if (packet.getType() != MessagePacket.TYPE_ERROR) {
mXmppConnectionService.databaseBackend.createMessage(message);
}
mXmppConnectionService.updateUi(conversation, notify);
}
} }

View File

@ -7,9 +7,11 @@ import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket; import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
public class PresenceParser extends AbstractParser { public class PresenceParser extends AbstractParser implements
OnPresencePacketReceived {
public PresenceParser(XmppConnectionService service) { public PresenceParser(XmppConnectionService service) {
super(service); super(service);
@ -37,7 +39,7 @@ public class PresenceParser extends AbstractParser {
} }
public void parseContactPresence(PresencePacket packet, Account account) { public void parseContactPresence(PresencePacket packet, Account account) {
if (packet.getFrom()==null) { if (packet.getFrom() == null) {
return; return;
} }
String[] fromParts = packet.getFrom().split("/"); String[] fromParts = packet.getFrom().split("/");
@ -75,9 +77,9 @@ public class PresenceParser extends AbstractParser {
} }
} }
boolean online = sizeBefore < contact.getPresences().size(); boolean online = sizeBefore < contact.getPresences().size();
updateLastseen(packet, account,true); updateLastseen(packet, account, true);
mXmppConnectionService.onContactStatusChanged mXmppConnectionService.onContactStatusChanged
.onContactStatusChanged(contact,online); .onContactStatusChanged(contact, online);
} }
} else if (type.equals("unavailable")) { } else if (type.equals("unavailable")) {
if (fromParts.length != 2) { if (fromParts.length != 2) {
@ -86,7 +88,7 @@ public class PresenceParser extends AbstractParser {
contact.removePresence(fromParts[1]); contact.removePresence(fromParts[1]);
} }
mXmppConnectionService.onContactStatusChanged mXmppConnectionService.onContactStatusChanged
.onContactStatusChanged(contact,false); .onContactStatusChanged(contact, false);
} else if (type.equals("subscribe")) { } else if (type.equals("subscribe")) {
if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) { if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
mXmppConnectionService.sendPresenceUpdatesTo(contact); mXmppConnectionService.sendPresenceUpdatesTo(contact);
@ -102,4 +104,15 @@ public class PresenceParser extends AbstractParser {
} }
} }
@Override
public void onPresencePacketReceived(Account account, PresencePacket packet) {
if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) {
this.parseConferencePresence(packet, account);
} else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
this.parseConferencePresence(packet, account);
} else {
this.parseContactPresence(packet, account);
}
}
} }

View File

@ -43,8 +43,6 @@ import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnBindListener; import eu.siacs.conversations.xmpp.OnBindListener;
import eu.siacs.conversations.xmpp.OnContactStatusChanged; import eu.siacs.conversations.xmpp.OnContactStatusChanged;
import eu.siacs.conversations.xmpp.OnIqPacketReceived; import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
import eu.siacs.conversations.xmpp.OnStatusChanged; import eu.siacs.conversations.xmpp.OnStatusChanged;
import eu.siacs.conversations.xmpp.OnTLSExceptionReceived; import eu.siacs.conversations.xmpp.OnTLSExceptionReceived;
import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.XmppConnection;
@ -87,7 +85,7 @@ public class XmppConnectionService extends Service {
private static final int PING_MIN_INTERVAL = 30; private static final int PING_MIN_INTERVAL = 30;
private static final int PING_TIMEOUT = 10; private static final int PING_TIMEOUT = 10;
private static final int CONNECT_TIMEOUT = 90; private static final int CONNECT_TIMEOUT = 90;
private static final long CARBON_GRACE_PERIOD = 60000L; public static final long CARBON_GRACE_PERIOD = 60000L;
private static String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts"; private static String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts";
@ -125,8 +123,6 @@ public class XmppConnectionService extends Service {
private SecureRandom mRandom; private SecureRandom mRandom;
private long lastCarbonMessageReceived = -CARBON_GRACE_PERIOD;
private ContentObserver contactObserver = new ContentObserver(null) { private ContentObserver contactObserver = new ContentObserver(null) {
@Override @Override
public void onChange(boolean selfChange) { public void onChange(boolean selfChange) {
@ -139,95 +135,6 @@ public class XmppConnectionService extends Service {
}; };
private final IBinder mBinder = new XmppConnectionBinder(); private final IBinder mBinder = new XmppConnectionBinder();
private OnMessagePacketReceived messageListener = new OnMessagePacketReceived() {
@Override
public void onMessagePacketReceived(Account account,
MessagePacket packet) {
Message message = null;
boolean notify = true;
if (getPreferences().getBoolean(
"notification_grace_period_after_carbon_received", true)) {
notify = (SystemClock.elapsedRealtime() - lastCarbonMessageReceived) > CARBON_GRACE_PERIOD;
}
if ((packet.getType() == MessagePacket.TYPE_CHAT)) {
if ((packet.getBody() != null)
&& (packet.getBody().startsWith("?OTR"))) {
message = mMessageParser.parseOtrChat(packet, account);
if (message != null) {
message.markUnread();
}
} else if (packet.hasChild("body")) {
message = mMessageParser.parseChat(packet, account);
message.markUnread();
} else if (packet.hasChild("received")
|| (packet.hasChild("sent"))) {
message = mMessageParser
.parseCarbonMessage(packet, account);
if (message != null) {
if (message.getStatus() == Message.STATUS_SEND) {
lastCarbonMessageReceived = SystemClock
.elapsedRealtime();
notify = false;
message.getConversation().markRead();
} else {
message.markUnread();
}
}
}
} else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) {
message = mMessageParser.parseGroupchat(packet, account);
if (message != null) {
if (message.getStatus() == Message.STATUS_RECIEVED) {
message.markUnread();
} else {
message.getConversation().markRead();
notify = false;
}
}
} else if (packet.getType() == MessagePacket.TYPE_ERROR) {
mMessageParser.parseError(packet, account);
return;
} else if (packet.getType() == MessagePacket.TYPE_NORMAL) {
mMessageParser.parseNormal(packet, account);
}
if ((message == null) || (message.getBody() == null)) {
return;
}
if ((confirmMessages()) && ((packet.getId() != null))) {
MessagePacket receivedPacket = new MessagePacket();
receivedPacket.setType(MessagePacket.TYPE_NORMAL);
receivedPacket.setTo(message.getCounterpart());
receivedPacket.setFrom(account.getFullJid());
if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
Element received = receivedPacket.addChild("received",
"urn:xmpp:chat-markers:0");
received.setAttribute("id", packet.getId());
account.getXmppConnection().sendMessagePacket(
receivedPacket);
} else if (packet.hasChild("request", "urn:xmpp:receipts")) {
Element received = receivedPacket.addChild("received",
"urn:xmpp:receipts");
received.setAttribute("id", packet.getId());
account.getXmppConnection().sendMessagePacket(
receivedPacket);
}
}
Conversation conversation = message.getConversation();
conversation.getMessages().add(message);
if (packet.getType() != MessagePacket.TYPE_ERROR) {
databaseBackend.createMessage(message);
}
if (convChangedListener != null) {
convChangedListener.onConversationListChanged();
} else {
UIHelper.updateNotification(getApplicationContext(),
getConversations(), message.getConversation(), notify);
}
}
};
private OnStatusChanged statusListener = new OnStatusChanged() { private OnStatusChanged statusListener = new OnStatusChanged() {
@Override @Override
@ -270,21 +177,6 @@ public class XmppConnectionService extends Service {
} }
}; };
private OnPresencePacketReceived presenceListener = new OnPresencePacketReceived() {
@Override
public void onPresencePacketReceived(final Account account,
PresencePacket packet) {
if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) {
mPresenceParser.parseConferencePresence(packet, account);
} else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
mPresenceParser.parseConferencePresence(packet, account);
} else {
mPresenceParser.parseContactPresence(packet, account);
}
}
};
private OnIqPacketReceived unknownIqListener = new OnIqPacketReceived() { private OnIqPacketReceived unknownIqListener = new OnIqPacketReceived() {
@Override @Override
@ -624,9 +516,9 @@ public class XmppConnectionService extends Service {
account.setResource(sharedPref.getString("resource", "mobile") account.setResource(sharedPref.getString("resource", "mobile")
.toLowerCase(Locale.getDefault())); .toLowerCase(Locale.getDefault()));
XmppConnection connection = new XmppConnection(account, this); XmppConnection connection = new XmppConnection(account, this);
connection.setOnMessagePacketReceivedListener(this.messageListener); connection.setOnMessagePacketReceivedListener(this.mMessageParser);
connection.setOnStatusChangedListener(this.statusListener); connection.setOnStatusChangedListener(this.statusListener);
connection.setOnPresencePacketReceivedListener(this.presenceListener); connection.setOnPresencePacketReceivedListener(this.mPresenceParser);
connection connection
.setOnUnregisteredIqPacketReceivedListener(this.unknownIqListener); .setOnUnregisteredIqPacketReceivedListener(this.unknownIqListener);
connection.setOnJinglePacketReceivedListener(this.jingleListener); connection.setOnJinglePacketReceivedListener(this.jingleListener);