diff options
author | Daniel Gultsch <daniel@gultsch.de> | 2014-06-06 18:26:40 +0200 |
---|---|---|
committer | Daniel Gultsch <daniel@gultsch.de> | 2014-06-06 18:26:40 +0200 |
commit | 476ab656ef0a50b081d92750b8b4251652ef4624 (patch) | |
tree | f59188e0258832beaa0057ef6f337b1c4e47dfa9 /src/eu/siacs/conversations/parser | |
parent | 5cd750cb5e7ce5e9b0ddcab8e0089b964d1de70d (diff) |
refactored presence parsing into dedicated class
Diffstat (limited to 'src/eu/siacs/conversations/parser')
-rw-r--r-- | src/eu/siacs/conversations/parser/AbstractParser.java | 53 | ||||
-rw-r--r-- | src/eu/siacs/conversations/parser/MessageParser.java | 48 | ||||
-rw-r--r-- | src/eu/siacs/conversations/parser/PresenceParser.java | 103 |
3 files changed, 171 insertions, 33 deletions
diff --git a/src/eu/siacs/conversations/parser/AbstractParser.java b/src/eu/siacs/conversations/parser/AbstractParser.java new file mode 100644 index 00000000..8c6ad2ab --- /dev/null +++ b/src/eu/siacs/conversations/parser/AbstractParser.java @@ -0,0 +1,53 @@ +package eu.siacs.conversations.parser; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import eu.siacs.conversations.entities.Account; +import eu.siacs.conversations.entities.Contact; +import eu.siacs.conversations.services.XmppConnectionService; +import eu.siacs.conversations.xml.Element; + +public abstract class AbstractParser { + + protected XmppConnectionService mXmppConnectionService; + + protected AbstractParser(XmppConnectionService service) { + this.mXmppConnectionService = service; + } + + protected long getTimestamp(Element packet) { + if (packet.hasChild("delay")) { + try { + String stamp = packet.findChild("delay").getAttribute( + "stamp"); + stamp = stamp.replace("Z", "+0000"); + Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") + .parse(stamp); + return date.getTime(); + } catch (ParseException e) { + return System.currentTimeMillis(); + } + } else { + return System.currentTimeMillis(); + } + } + + protected void updateLastseen(Element packet, Account account) { + String[] fromParts = packet.getAttribute("from").split("/"); + String from = fromParts[0]; + String presence = null; + if (fromParts.length >= 2) { + presence = fromParts[1]; + } + Contact contact = account.getRoster().getContact(from); + long timestamp = getTimestamp(packet); + if (timestamp >= contact.lastseen.time) { + contact.lastseen.time = timestamp; + if (presence!=null) { + contact.lastseen.presence = presence; + } + } + } +} diff --git a/src/eu/siacs/conversations/parser/MessageParser.java b/src/eu/siacs/conversations/parser/MessageParser.java index ab75d676..12e2aa16 100644 --- a/src/eu/siacs/conversations/parser/MessageParser.java +++ b/src/eu/siacs/conversations/parser/MessageParser.java @@ -13,13 +13,10 @@ import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.stanzas.MessagePacket; -public class MessageParser { - - protected static final String LOGTAG = "xmppService"; - private XmppConnectionService mXmppConnectionService; +public class MessageParser extends AbstractParser { public MessageParser(XmppConnectionService service) { - this.mXmppConnectionService = service; + super(service); } public Message parseChat(MessagePacket packet, Account account) { @@ -83,7 +80,6 @@ public class MessageParser { body = otrSession.transformReceiving(body); SessionStatus after = otrSession.getSessionStatus(); if ((before != after) && (after == SessionStatus.ENCRYPTED)) { - Log.d(LOGTAG, "otr session etablished"); List<Message> messages = conversation.getMessages(); for (int i = 0; i < messages.size(); ++i) { Message msg = messages.get(i); @@ -101,7 +97,6 @@ public class MessageParser { mXmppConnectionService.updateUi(conversation, false); } else if ((before != after) && (after == SessionStatus.FINISHED)) { conversation.resetOtrSession(); - Log.d(LOGTAG, "otr session stoped"); } // isEmpty is a work around for some weird clients which send emtpty // strings over otr @@ -109,8 +104,10 @@ public class MessageParser { return null; } conversation.setLatestMarkableMessageId(getMarkableMessageId(packet)); - return new Message(conversation, packet.getFrom(), body, + Message finishedMessage = new Message(conversation, packet.getFrom(), body, Message.ENCRYPTION_OTR, Message.STATUS_RECIEVED); + finishedMessage.setTime(getTimestamp(packet)); + return finishedMessage; } catch (Exception e) { conversation.resetOtrSession(); return null; @@ -144,13 +141,16 @@ public class MessageParser { } String pgpBody = getPgpBody(packet); conversation.setLatestMarkableMessageId(getMarkableMessageId(packet)); + Message finishedMessage; if (pgpBody == null) { - return new Message(conversation, counterPart, packet.getBody(), + finishedMessage = new Message(conversation, counterPart, packet.getBody(), Message.ENCRYPTION_NONE, status); } else { - return new Message(conversation, counterPart, pgpBody, + finishedMessage= new Message(conversation, counterPart, pgpBody, Message.ENCRYPTION_PGP, status); } + finishedMessage.setTime(getTimestamp(packet)); + return finishedMessage; } public Message parseCarbonMessage(MessagePacket packet, Account account) { @@ -183,14 +183,15 @@ public class MessageParser { .findOrCreateConversation(account, parts[0], false); conversation.setLatestMarkableMessageId(getMarkableMessageId(packet)); String pgpBody = getPgpBody(message); + Message finishedMessage; if (pgpBody != null) { - return new Message(conversation, fullJid, pgpBody, - Message.ENCRYPTION_PGP, status); + finishedMessage = new Message(conversation, fullJid, pgpBody,Message.ENCRYPTION_PGP, status); } else { String body = message.findChild("body").getContent(); - return new Message(conversation, fullJid, body, - Message.ENCRYPTION_NONE, status); + finishedMessage= new Message(conversation, fullJid, body,Message.ENCRYPTION_NONE, status); } + finishedMessage.setTime(getTimestamp(message)); + return finishedMessage; } public void parseError(MessagePacket packet, Account account) { @@ -215,23 +216,4 @@ public class MessageParser { return null; } } - - private void updateLastseen(Element message, Account account) { - String[] fromParts = message.getAttribute("from").split("/"); - String from = fromParts[0]; - String presence = null; - if (fromParts.length >= 2) { - presence = fromParts[1]; - } - Contact contact = account.getRoster().getContact(from); - if (presence!=null) { - contact.lastseen.presence = presence; - contact.lastseen.time = System.currentTimeMillis(); - } else if ((contact.getPresences().size() == 1)&&(contact.getPresences().containsKey(contact.lastseen.presence))) { - contact.lastseen.time = System.currentTimeMillis(); - } else { - contact.lastseen.presence = null; - contact.lastseen.time = System.currentTimeMillis(); - } - } } diff --git a/src/eu/siacs/conversations/parser/PresenceParser.java b/src/eu/siacs/conversations/parser/PresenceParser.java new file mode 100644 index 00000000..25a23423 --- /dev/null +++ b/src/eu/siacs/conversations/parser/PresenceParser.java @@ -0,0 +1,103 @@ +package eu.siacs.conversations.parser; + +import android.util.Log; +import eu.siacs.conversations.crypto.PgpEngine; +import eu.siacs.conversations.entities.Account; +import eu.siacs.conversations.entities.Contact; +import eu.siacs.conversations.entities.Conversation; +import eu.siacs.conversations.entities.Presences; +import eu.siacs.conversations.services.XmppConnectionService; +import eu.siacs.conversations.xml.Element; +import eu.siacs.conversations.xmpp.stanzas.PresencePacket; + +public class PresenceParser extends AbstractParser { + + public PresenceParser(XmppConnectionService service) { + super(service); + } + + public void parseConferencePresence(PresencePacket packet, Account account) { + PgpEngine mPgpEngine = mXmppConnectionService.getPgpEngine(); + if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) { + Conversation muc = mXmppConnectionService.findMuc(packet + .getAttribute("from").split("/")[0], account); + if (muc != null) { + muc.getMucOptions().processPacket(packet, mPgpEngine); + } + } else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) { + Conversation muc = mXmppConnectionService.findMuc(packet + .getAttribute("from").split("/")[0], account); + if (muc != null) { + int error = muc.getMucOptions().getError(); + muc.getMucOptions().processPacket(packet, mPgpEngine); + if (muc.getMucOptions().getError() != error) { + mXmppConnectionService.updateUi(muc, false); + } + } + } + } + + public void parseContactPresence(PresencePacket packet, Account account) { + String[] fromParts = packet.getAttribute("from").split("/"); + String type = packet.getAttribute("type"); + if (fromParts[0].equals(account.getJid())) { + if (fromParts.length == 2) { + if (type == null) { + account.updatePresence(fromParts[1], + Presences.parseShow(packet.findChild("show"))); + } else if (type.equals("unavailable")) { + account.removePresence(fromParts[1]); + } + } + + } else { + Contact contact = account.getRoster().getContact(packet.getFrom()); + if (type == null) { + if (fromParts.length == 2) { + contact.updatePresence(fromParts[1], + Presences.parseShow(packet.findChild("show"))); + PgpEngine pgp = mXmppConnectionService.getPgpEngine(); + if (pgp != null) { + Element x = packet.findChild("x", "jabber:x:signed"); + if (x != null) { + Element status = packet.findChild("status"); + String msg; + if (status != null) { + msg = status.getContent(); + } else { + msg = ""; + } + contact.setPgpKeyId(pgp.fetchKeyId(account, msg, + x.getContent())); + } + } + mXmppConnectionService.onContactStatusChanged + .onContactStatusChanged(contact); + updateLastseen(packet, account); + } + } else if (type.equals("unavailable")) { + if (fromParts.length != 2) { + contact.clearPresences(); + } else { + contact.removePresence(fromParts[1]); + } + mXmppConnectionService.onContactStatusChanged + .onContactStatusChanged(contact); + } else if (type.equals("subscribe")) { + if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) { + mXmppConnectionService.sendPresenceUpdatesTo(contact); + contact.setOption(Contact.Options.FROM); + contact.resetOption(Contact.Options.PREEMPTIVE_GRANT); + if ((contact.getOption(Contact.Options.ASKING)) + && (!contact.getOption(Contact.Options.TO))) { + mXmppConnectionService + .requestPresenceUpdatesFrom(contact); + } + } else { + contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST); + } + } + } + } + +} |