diff options
-rw-r--r-- | src/eu/siacs/conversations/parser/MessageParser.java (renamed from src/eu/siacs/conversations/utils/MessageParser.java) | 46 | ||||
-rw-r--r-- | src/eu/siacs/conversations/services/XmppConnectionService.java | 104 |
2 files changed, 86 insertions, 64 deletions
diff --git a/src/eu/siacs/conversations/utils/MessageParser.java b/src/eu/siacs/conversations/parser/MessageParser.java index 52b18f66..26078073 100644 --- a/src/eu/siacs/conversations/utils/MessageParser.java +++ b/src/eu/siacs/conversations/parser/MessageParser.java @@ -1,4 +1,4 @@ -package eu.siacs.conversations.utils; +package eu.siacs.conversations.parser; import java.util.List; @@ -15,29 +15,34 @@ import eu.siacs.conversations.xmpp.stanzas.MessagePacket; public class MessageParser { protected static final String LOGTAG = "xmppService"; + private XmppConnectionService mXmppConnectionService; - public static Message parsePlainTextChat(MessagePacket packet, Account account, XmppConnectionService service) { + public MessageParser(XmppConnectionService service) { + this.mXmppConnectionService = service; + } + + public Message parsePlainTextChat(MessagePacket packet, Account account) { String[] fromParts = packet.getFrom().split("/"); - Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false); + 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 static Message parsePgpChat(String pgpBody, MessagePacket packet, Account account, XmppConnectionService service) { + public Message parsePgpChat(String pgpBody, MessagePacket packet, Account account) { String[] fromParts = packet.getFrom().split("/"); - Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false); + Conversation conversation = mXmppConnectionService.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) { + public Message parseOtrChat(MessagePacket packet, Account account) { boolean properlyAddressed = (packet.getTo().split("/").length == 2) || (account.countPresences() == 1); String[] fromParts = packet.getFrom().split("/"); - Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false); + 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(service.getApplicationContext(), fromParts[1],false); + conversation.startOtrSession(mXmppConnectionService.getApplicationContext(), fromParts[1],false); } else { Log.d("xmppService",account.getJid()+": ignoring otr session with "+fromParts[0]); return null; @@ -48,7 +53,7 @@ public class MessageParser { conversation.resetOtrSession(); if (properlyAddressed) { Log.d("xmppService","replacing otr session with "+packet.getFrom()); - conversation.startOtrSession(service.getApplicationContext(), fromParts[1],false); + conversation.startOtrSession(mXmppConnectionService.getApplicationContext(), fromParts[1],false); } else { return null; } @@ -69,15 +74,15 @@ public class MessageParser { Message msg = messages.get(i); if ((msg.getStatus() == Message.STATUS_UNSEND) && (msg.getEncryption() == Message.ENCRYPTION_OTR)) { - MessagePacket outPacket = service.prepareMessagePacket( + MessagePacket outPacket = mXmppConnectionService.prepareMessagePacket( account, msg, otrSession); msg.setStatus(Message.STATUS_SEND); - service.databaseBackend.updateMessage(msg); + mXmppConnectionService.databaseBackend.updateMessage(msg); account.getXmppConnection() .sendMessagePacket(outPacket); } } - service.updateUi(conversation, false); + mXmppConnectionService.updateUi(conversation, false); } else if ((before != after) && (after == SessionStatus.FINISHED)) { conversation.resetOtrSession(); Log.d(LOGTAG,"otr session stoped"); @@ -93,13 +98,13 @@ public class MessageParser { } } - public static Message parseGroupchat(MessagePacket packet, Account account, XmppConnectionService service) { + public Message parseGroupchat(MessagePacket packet, Account account) { int status; String[] fromParts = packet.getFrom().split("/"); - Conversation conversation = service.findOrCreateConversation(account, fromParts[0],true); + Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, fromParts[0],true); if (packet.hasChild("subject")) { conversation.getMucOptions().setSubject(packet.findChild("subject").getContent()); - service.updateUi(conversation, false); + mXmppConnectionService.updateUi(conversation, false); return null; } if ((fromParts.length == 1)) { @@ -114,8 +119,7 @@ public class MessageParser { return new Message(conversation, counterPart, packet.getBody(), Message.ENCRYPTION_NONE, status); } - public static Message parseCarbonMessage(MessagePacket packet, - Account account, XmppConnectionService service) { + public Message parseCarbonMessage(MessagePacket packet,Account account) { int status; String fullJid; Element forwarded; @@ -142,16 +146,16 @@ public class MessageParser { fullJid = message.getAttribute("to"); } String[] parts = fullJid.split("/"); - Conversation conversation = service.findOrCreateConversation(account, parts[0],false); + Conversation conversation = mXmppConnectionService.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) { + public void parseError(MessagePacket packet, Account account) { String[] fromParts = packet.getFrom().split("/"); - service.markMessage(account, fromParts[0], packet.getId(), Message.STATUS_SEND_FAILED); + mXmppConnectionService.markMessage(account, fromParts[0], packet.getId(), Message.STATUS_SEND_FAILED); } - public static String getPgpBody(MessagePacket packet) { + 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(); 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<Account> accounts; private List<Conversation> 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); } } } |