From 81d2760505c2bc153a50238af1a2ea6cd505eec1 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 14 May 2014 12:56:34 +0200 Subject: made message parser non static --- .../siacs/conversations/parser/MessageParser.java | 166 +++++++++++++++++++++ .../services/XmppConnectionService.java | 104 +++++++------ .../siacs/conversations/utils/MessageParser.java | 162 -------------------- 3 files changed, 227 insertions(+), 205 deletions(-) create mode 100644 src/eu/siacs/conversations/parser/MessageParser.java delete mode 100644 src/eu/siacs/conversations/utils/MessageParser.java (limited to 'src') diff --git a/src/eu/siacs/conversations/parser/MessageParser.java b/src/eu/siacs/conversations/parser/MessageParser.java new file mode 100644 index 00000000..26078073 --- /dev/null +++ b/src/eu/siacs/conversations/parser/MessageParser.java @@ -0,0 +1,166 @@ +package eu.siacs.conversations.parser; + +import java.util.List; + +import net.java.otr4j.session.Session; +import net.java.otr4j.session.SessionStatus; +import android.util.Log; +import eu.siacs.conversations.entities.Account; +import eu.siacs.conversations.entities.Conversation; +import eu.siacs.conversations.entities.Message; +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 MessageParser(XmppConnectionService service) { + this.mXmppConnectionService = service; + } + + public Message parsePlainTextChat(MessagePacket packet, Account account) { + String[] fromParts = packet.getFrom().split("/"); + Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, fromParts[0],false); + String body = packet.getBody(); + return new Message(conversation, packet.getFrom(), body, Message.ENCRYPTION_NONE, Message.STATUS_RECIEVED); + } + + public Message parsePgpChat(String pgpBody, MessagePacket packet, Account account) { + String[] fromParts = packet.getFrom().split("/"); + Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, fromParts[0],false); + return new Message(conversation, packet.getFrom(), pgpBody, Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED); + } + + public Message parseOtrChat(MessagePacket packet, Account account) { + boolean properlyAddressed = (packet.getTo().split("/").length == 2) || (account.countPresences() == 1); + String[] fromParts = packet.getFrom().split("/"); + Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, fromParts[0],false); + String body = packet.getBody(); + if (!conversation.hasValidOtrSession()) { + if (properlyAddressed) { + Log.d("xmppService","starting new otr session with "+packet.getFrom()+" because no valid otr session has been found"); + conversation.startOtrSession(mXmppConnectionService.getApplicationContext(), fromParts[1],false); + } else { + Log.d("xmppService",account.getJid()+": ignoring otr session with "+fromParts[0]); + return null; + } + } else { + String foreignPresence = conversation.getOtrSession().getSessionID().getUserID(); + if (!foreignPresence.equals(fromParts[1])) { + conversation.resetOtrSession(); + if (properlyAddressed) { + Log.d("xmppService","replacing otr session with "+packet.getFrom()); + conversation.startOtrSession(mXmppConnectionService.getApplicationContext(), fromParts[1],false); + } else { + return null; + } + } + } + try { + Session otrSession = conversation.getOtrSession(); + SessionStatus before = otrSession + .getSessionStatus(); + body = otrSession.transformReceiving(body); + SessionStatus after = otrSession.getSessionStatus(); + if ((before != after) + && (after == SessionStatus.ENCRYPTED)) { + Log.d(LOGTAG, "otr session etablished"); + List messages = conversation + .getMessages(); + for (int i = 0; i < messages.size(); ++i) { + Message msg = messages.get(i); + if ((msg.getStatus() == Message.STATUS_UNSEND) + && (msg.getEncryption() == Message.ENCRYPTION_OTR)) { + MessagePacket outPacket = mXmppConnectionService.prepareMessagePacket( + account, msg, otrSession); + msg.setStatus(Message.STATUS_SEND); + mXmppConnectionService.databaseBackend.updateMessage(msg); + account.getXmppConnection() + .sendMessagePacket(outPacket); + } + } + 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 + if ((body == null)||(body.isEmpty())) { + return null; + } + return new Message(conversation, packet.getFrom(), body, Message.ENCRYPTION_OTR,Message.STATUS_RECIEVED); + } catch (Exception e) { + conversation.resetOtrSession(); + return null; + } + } + + public Message parseGroupchat(MessagePacket packet, Account account) { + int status; + String[] fromParts = packet.getFrom().split("/"); + Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, fromParts[0],true); + if (packet.hasChild("subject")) { + conversation.getMucOptions().setSubject(packet.findChild("subject").getContent()); + mXmppConnectionService.updateUi(conversation, false); + return null; + } + if ((fromParts.length == 1)) { + return null; + } + String counterPart = fromParts[1]; + if (counterPart.equals(conversation.getMucOptions().getNick())) { + status = Message.STATUS_SEND; + } else { + status = Message.STATUS_RECIEVED; + } + return new Message(conversation, counterPart, packet.getBody(), Message.ENCRYPTION_NONE, status); + } + + public Message parseCarbonMessage(MessagePacket packet,Account account) { + int status; + String fullJid; + Element forwarded; + if (packet.hasChild("received")) { + forwarded = packet.findChild("received").findChild( + "forwarded"); + status = Message.STATUS_RECIEVED; + } else if (packet.hasChild("sent")) { + forwarded = packet.findChild("sent").findChild( + "forwarded"); + status = Message.STATUS_SEND; + } else { + return null; + } + if (forwarded==null) { + return null; + } + Element message = forwarded.findChild("message"); + if ((message == null) || (!message.hasChild("body"))) + return null; // either malformed or boring + if (status == Message.STATUS_RECIEVED) { + fullJid = message.getAttribute("from"); + } else { + fullJid = message.getAttribute("to"); + } + String[] parts = fullJid.split("/"); + Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, parts[0],false); + return new Message(conversation,fullJid, message.findChild("body").getContent(), Message.ENCRYPTION_NONE,status); + } + + public void parseError(MessagePacket packet, Account account) { + String[] fromParts = packet.getFrom().split("/"); + mXmppConnectionService.markMessage(account, fromParts[0], packet.getId(), Message.STATUS_SEND_FAILED); + } + + public String getPgpBody(MessagePacket packet) { + for(Element child : packet.getChildren()) { + if (child.getName().equals("x")&&child.getAttribute("xmlns").equals("jabber:x:encrypted")) { + return child.getContent(); + } + } + return null; + } +} diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index d2742997..df2ee6a0 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -26,6 +26,7 @@ import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.MucOptions; import eu.siacs.conversations.entities.MucOptions.OnRenameListener; import eu.siacs.conversations.entities.Presences; +import eu.siacs.conversations.parser.MessageParser; import eu.siacs.conversations.persistance.DatabaseBackend; import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.persistance.OnPhoneContactsMerged; @@ -34,7 +35,6 @@ import eu.siacs.conversations.ui.OnConversationListChangedListener; import eu.siacs.conversations.ui.OnRosterFetchedListener; import eu.siacs.conversations.ui.UiCallback; import eu.siacs.conversations.utils.ExceptionHelper; -import eu.siacs.conversations.utils.MessageParser; import eu.siacs.conversations.utils.OnPhoneContactsLoadedListener; import eu.siacs.conversations.utils.PhoneHelper; import eu.siacs.conversations.utils.UIHelper; @@ -88,6 +88,8 @@ public class XmppConnectionService extends Service { private static final int CONNECT_TIMEOUT = 60; private static final long CARBON_GRACE_PERIOD = 60000L; + private MessageParser mMessageParser = new MessageParser(this); + private List accounts; private List conversations = null; private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager( @@ -116,8 +118,6 @@ public class XmppConnectionService extends Service { } }; - private XmppConnectionService service = this; - private final IBinder mBinder = new XmppConnectionBinder(); private OnMessagePacketReceived messageListener = new OnMessagePacketReceived() { @@ -132,26 +132,25 @@ public class XmppConnectionService extends Service { } if ((packet.getType() == MessagePacket.TYPE_CHAT)) { - String pgpBody = MessageParser.getPgpBody(packet); + String pgpBody = mMessageParser.getPgpBody(packet); if (pgpBody != null) { - message = MessageParser.parsePgpChat(pgpBody, packet, - account, service); + message = mMessageParser.parsePgpChat(pgpBody, packet, + account); message.markUnread(); } else if ((packet.getBody() != null) && (packet.getBody().startsWith("?OTR"))) { - message = MessageParser.parseOtrChat(packet, account, - service); + message = mMessageParser.parseOtrChat(packet, account); if (message != null) { message.markUnread(); } } else if (packet.hasChild("body")) { - message = MessageParser.parsePlainTextChat(packet, account, - service); + message = mMessageParser + .parsePlainTextChat(packet, account); message.markUnread(); } else if (packet.hasChild("received") || (packet.hasChild("sent"))) { - message = MessageParser.parseCarbonMessage(packet, account, - service); + message = mMessageParser + .parseCarbonMessage(packet, account); if (message != null) { if (message.getStatus() == Message.STATUS_SEND) { lastCarbonMessageReceived = SystemClock @@ -165,8 +164,7 @@ public class XmppConnectionService extends Service { } } else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) { - message = MessageParser - .parseGroupchat(packet, account, service); + message = mMessageParser.parseGroupchat(packet, account); if (message != null) { if (message.getStatus() == Message.STATUS_RECIEVED) { message.markUnread(); @@ -176,7 +174,7 @@ public class XmppConnectionService extends Service { } } } else if (packet.getType() == MessagePacket.TYPE_ERROR) { - MessageParser.parseError(packet, account, service); + mMessageParser.parseError(packet, account); return; } else if (packet.getType() == MessagePacket.TYPE_NORMAL) { if (packet.hasChild("x")) { @@ -325,13 +323,19 @@ public class XmppConnectionService extends Service { } else { msg = ""; } - contact.setPgpKeyId(pgp.fetchKeyId(account,msg,x.getContent())); - Log.d("xmppService",account.getJid()+": fetched key id for "+contact.getJid()+" was:"+contact.getPgpKeyId()); + contact.setPgpKeyId(pgp.fetchKeyId(account, + msg, x.getContent())); + Log.d("xmppService", + account.getJid() + + ": fetched key id for " + + contact.getJid() + + " was:" + + contact.getPgpKeyId()); } } replaceContactInConversation(account, contact.getJid(), contact); - databaseBackend.updateContact(contact, true); + databaseBackend.updateContact(contact, true); } else { // Log.d(LOGTAG,"presence without resource "+packet.toString()); } @@ -378,14 +382,14 @@ public class XmppConnectionService extends Service { @Override public void onIqPacketReceived(Account account, IqPacket packet) { - if (packet.hasChild("query","jabber:iq:roster")) { + if (packet.hasChild("query", "jabber:iq:roster")) { String from = packet.getFrom(); - if ((from==null)||(from.equals(account.getJid()))) { + if ((from == null) || (from.equals(account.getJid()))) { Element query = packet.findChild("query"); processRosterItems(account, query); mergePhoneContactsWithRoster(null); } else { - Log.d(LOGTAG,"unauthorized roster push from: "+from); + Log.d(LOGTAG, "unauthorized roster push from: " + from); } } else if (packet .hasChild("open", "http://jabber.org/protocol/ibb") @@ -393,20 +397,30 @@ public class XmppConnectionService extends Service { .hasChild("data", "http://jabber.org/protocol/ibb")) { XmppConnectionService.this.mJingleConnectionManager .deliverIbbPacket(account, packet); - } else if (packet.hasChild("query","http://jabber.org/protocol/disco#info")) { - IqPacket iqResponse = packet.generateRespone(IqPacket.TYPE_RESULT); - Element query = iqResponse.addChild("query", "http://jabber.org/protocol/disco#info"); - query.addChild("feature").setAttribute("var", "urn:xmpp:jingle:1"); - query.addChild("feature").setAttribute("var", "urn:xmpp:jingle:apps:file-transfer:3"); - query.addChild("feature").setAttribute("var", "urn:xmpp:jingle:transports:s5b:1"); - query.addChild("feature").setAttribute("var", "urn:xmpp:jingle:transports:ibb:1"); + } else if (packet.hasChild("query", + "http://jabber.org/protocol/disco#info")) { + IqPacket iqResponse = packet + .generateRespone(IqPacket.TYPE_RESULT); + Element query = iqResponse.addChild("query", + "http://jabber.org/protocol/disco#info"); + query.addChild("feature").setAttribute("var", + "urn:xmpp:jingle:1"); + query.addChild("feature").setAttribute("var", + "urn:xmpp:jingle:apps:file-transfer:3"); + query.addChild("feature").setAttribute("var", + "urn:xmpp:jingle:transports:s5b:1"); + query.addChild("feature").setAttribute("var", + "urn:xmpp:jingle:transports:ibb:1"); account.getXmppConnection().sendIqPacket(iqResponse, null); } else { - if ((packet.getType() == IqPacket.TYPE_GET)||(packet.getType() == IqPacket.TYPE_SET)) { - IqPacket response = packet.generateRespone(IqPacket.TYPE_ERROR); + if ((packet.getType() == IqPacket.TYPE_GET) + || (packet.getType() == IqPacket.TYPE_SET)) { + IqPacket response = packet + .generateRespone(IqPacket.TYPE_ERROR); Element error = response.addChild("error"); - error.setAttribute("type","cancel"); - error.addChild("feature-not-implemented","urn:ietf:params:xml:ns:xmpp-stanzas"); + error.setAttribute("type", "cancel"); + error.addChild("feature-not-implemented", + "urn:ietf:params:xml:ns:xmpp-stanzas"); account.getXmppConnection().sendIqPacket(response, null); } } @@ -433,7 +447,7 @@ public class XmppConnectionService extends Service { if (this.mPgpEngine == null) { this.mPgpEngine = new PgpEngine(new OpenPgpApi( getApplicationContext(), - pgpServiceConnection.getService()),this); + pgpServiceConnection.getService()), this); } return mPgpEngine; } else { @@ -446,10 +460,12 @@ public class XmppConnectionService extends Service { return this.fileBackend; } - public Message attachImageToConversation(final Conversation conversation, final Uri uri, final UiCallback callback) { + public Message attachImageToConversation(final Conversation conversation, + final Uri uri, final UiCallback callback) { final Message message; if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { - message = new Message(conversation, "",Message.ENCRYPTION_DECRYPTED); + message = new Message(conversation, "", + Message.ENCRYPTION_DECRYPTED); } else { message = new Message(conversation, "", Message.ENCRYPTION_NONE); } @@ -460,8 +476,9 @@ public class XmppConnectionService extends Service { @Override public void run() { - JingleFile file = getFileBackend().copyImageToPrivateStorage(message, uri); - if (file==null) { + JingleFile file = getFileBackend().copyImageToPrivateStorage( + message, uri); + if (file == null) { callback.error(R.string.error_copying_image_file); } else { if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { @@ -474,7 +491,7 @@ public class XmppConnectionService extends Service { }).start(); return message; } - + protected Conversation findMuc(String name, Account account) { for (Conversation conversation : this.conversations) { if (conversation.getContactJid().split("/")[0].equals(name) @@ -639,7 +656,7 @@ public class XmppConnectionService extends Service { @Override public void onDestroy() { - Log.d(LOGTAG,"stopping service"); + Log.d(LOGTAG, "stopping service"); super.onDestroy(); for (Account account : accounts) { if (account.getXmppConnection() != null) { @@ -1214,8 +1231,8 @@ public class XmppConnectionService extends Service { renameListener.onRename(success); } if (success) { - String jid = conversation.getContactJid().split("/")[0] + "/" - + nick; + String jid = conversation.getContactJid().split("/")[0] + + "/" + nick; conversation.setContactJid(jid); databaseBackend.updateConversation(conversation); } @@ -1476,12 +1493,13 @@ public class XmppConnectionService extends Service { return PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); } - + public void updateUi(Conversation conversation, boolean notify) { if (convChangedListener != null) { convChangedListener.onConversationListChanged(); } else { - UIHelper.updateNotification(getApplicationContext(), getConversations(), conversation, notify); + UIHelper.updateNotification(getApplicationContext(), + getConversations(), conversation, notify); } } } diff --git a/src/eu/siacs/conversations/utils/MessageParser.java b/src/eu/siacs/conversations/utils/MessageParser.java deleted file mode 100644 index 52b18f66..00000000 --- a/src/eu/siacs/conversations/utils/MessageParser.java +++ /dev/null @@ -1,162 +0,0 @@ -package eu.siacs.conversations.utils; - -import java.util.List; - -import net.java.otr4j.session.Session; -import net.java.otr4j.session.SessionStatus; -import android.util.Log; -import eu.siacs.conversations.entities.Account; -import eu.siacs.conversations.entities.Conversation; -import eu.siacs.conversations.entities.Message; -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"; - - public static Message parsePlainTextChat(MessagePacket packet, Account account, XmppConnectionService service) { - String[] fromParts = packet.getFrom().split("/"); - Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false); - String body = packet.getBody(); - return new Message(conversation, packet.getFrom(), body, Message.ENCRYPTION_NONE, Message.STATUS_RECIEVED); - } - - public static Message parsePgpChat(String pgpBody, MessagePacket packet, Account account, XmppConnectionService service) { - String[] fromParts = packet.getFrom().split("/"); - Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false); - return new Message(conversation, packet.getFrom(), pgpBody, Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED); - } - - public static Message parseOtrChat(MessagePacket packet, Account account, XmppConnectionService service) { - boolean properlyAddressed = (packet.getTo().split("/").length == 2) || (account.countPresences() == 1); - String[] fromParts = packet.getFrom().split("/"); - Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false); - String body = packet.getBody(); - if (!conversation.hasValidOtrSession()) { - if (properlyAddressed) { - Log.d("xmppService","starting new otr session with "+packet.getFrom()+" because no valid otr session has been found"); - conversation.startOtrSession(service.getApplicationContext(), fromParts[1],false); - } else { - Log.d("xmppService",account.getJid()+": ignoring otr session with "+fromParts[0]); - return null; - } - } else { - String foreignPresence = conversation.getOtrSession().getSessionID().getUserID(); - if (!foreignPresence.equals(fromParts[1])) { - conversation.resetOtrSession(); - if (properlyAddressed) { - Log.d("xmppService","replacing otr session with "+packet.getFrom()); - conversation.startOtrSession(service.getApplicationContext(), fromParts[1],false); - } else { - return null; - } - } - } - try { - Session otrSession = conversation.getOtrSession(); - SessionStatus before = otrSession - .getSessionStatus(); - body = otrSession.transformReceiving(body); - SessionStatus after = otrSession.getSessionStatus(); - if ((before != after) - && (after == SessionStatus.ENCRYPTED)) { - Log.d(LOGTAG, "otr session etablished"); - List messages = conversation - .getMessages(); - for (int i = 0; i < messages.size(); ++i) { - Message msg = messages.get(i); - if ((msg.getStatus() == Message.STATUS_UNSEND) - && (msg.getEncryption() == Message.ENCRYPTION_OTR)) { - MessagePacket outPacket = service.prepareMessagePacket( - account, msg, otrSession); - msg.setStatus(Message.STATUS_SEND); - service.databaseBackend.updateMessage(msg); - account.getXmppConnection() - .sendMessagePacket(outPacket); - } - } - service.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 - if ((body == null)||(body.isEmpty())) { - return null; - } - return new Message(conversation, packet.getFrom(), body, Message.ENCRYPTION_OTR,Message.STATUS_RECIEVED); - } catch (Exception e) { - conversation.resetOtrSession(); - return null; - } - } - - public static Message parseGroupchat(MessagePacket packet, Account account, XmppConnectionService service) { - int status; - String[] fromParts = packet.getFrom().split("/"); - Conversation conversation = service.findOrCreateConversation(account, fromParts[0],true); - if (packet.hasChild("subject")) { - conversation.getMucOptions().setSubject(packet.findChild("subject").getContent()); - service.updateUi(conversation, false); - return null; - } - if ((fromParts.length == 1)) { - return null; - } - String counterPart = fromParts[1]; - if (counterPart.equals(conversation.getMucOptions().getNick())) { - status = Message.STATUS_SEND; - } else { - status = Message.STATUS_RECIEVED; - } - return new Message(conversation, counterPart, packet.getBody(), Message.ENCRYPTION_NONE, status); - } - - public static Message parseCarbonMessage(MessagePacket packet, - Account account, XmppConnectionService service) { - int status; - String fullJid; - Element forwarded; - if (packet.hasChild("received")) { - forwarded = packet.findChild("received").findChild( - "forwarded"); - status = Message.STATUS_RECIEVED; - } else if (packet.hasChild("sent")) { - forwarded = packet.findChild("sent").findChild( - "forwarded"); - status = Message.STATUS_SEND; - } else { - return null; - } - if (forwarded==null) { - return null; - } - Element message = forwarded.findChild("message"); - if ((message == null) || (!message.hasChild("body"))) - return null; // either malformed or boring - if (status == Message.STATUS_RECIEVED) { - fullJid = message.getAttribute("from"); - } else { - fullJid = message.getAttribute("to"); - } - String[] parts = fullJid.split("/"); - Conversation conversation = service.findOrCreateConversation(account, parts[0],false); - return new Message(conversation,fullJid, message.findChild("body").getContent(), Message.ENCRYPTION_NONE,status); - } - - public static void parseError(MessagePacket packet, Account account, XmppConnectionService service) { - String[] fromParts = packet.getFrom().split("/"); - service.markMessage(account, fromParts[0], packet.getId(), Message.STATUS_SEND_FAILED); - } - - public static String getPgpBody(MessagePacket packet) { - for(Element child : packet.getChildren()) { - if (child.getName().equals("x")&&child.getAttribute("xmlns").equals("jabber:x:encrypted")) { - return child.getContent(); - } - } - return null; - } -} -- cgit v1.2.3