made message parser non static

This commit is contained in:
Daniel Gultsch 2014-05-14 12:56:34 +02:00
parent 1f600be4dd
commit 81d2760505
2 changed files with 86 additions and 64 deletions

View file

@ -1,4 +1,4 @@
package eu.siacs.conversations.utils; package eu.siacs.conversations.parser;
import java.util.List; import java.util.List;
@ -15,29 +15,34 @@ import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
public class MessageParser { public class MessageParser {
protected static final String LOGTAG = "xmppService"; 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("/"); 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(); String body = packet.getBody();
return new Message(conversation, packet.getFrom(), body, Message.ENCRYPTION_NONE, Message.STATUS_RECIEVED); 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("/"); 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); 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); boolean properlyAddressed = (packet.getTo().split("/").length == 2) || (account.countPresences() == 1);
String[] fromParts = packet.getFrom().split("/"); 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(); String body = packet.getBody();
if (!conversation.hasValidOtrSession()) { if (!conversation.hasValidOtrSession()) {
if (properlyAddressed) { if (properlyAddressed) {
Log.d("xmppService","starting new otr session with "+packet.getFrom()+" because no valid otr session has been found"); 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 { } else {
Log.d("xmppService",account.getJid()+": ignoring otr session with "+fromParts[0]); Log.d("xmppService",account.getJid()+": ignoring otr session with "+fromParts[0]);
return null; return null;
@ -48,7 +53,7 @@ public class MessageParser {
conversation.resetOtrSession(); conversation.resetOtrSession();
if (properlyAddressed) { if (properlyAddressed) {
Log.d("xmppService","replacing otr session with "+packet.getFrom()); Log.d("xmppService","replacing otr session with "+packet.getFrom());
conversation.startOtrSession(service.getApplicationContext(), fromParts[1],false); conversation.startOtrSession(mXmppConnectionService.getApplicationContext(), fromParts[1],false);
} else { } else {
return null; return null;
} }
@ -69,15 +74,15 @@ public class MessageParser {
Message msg = messages.get(i); Message msg = messages.get(i);
if ((msg.getStatus() == Message.STATUS_UNSEND) if ((msg.getStatus() == Message.STATUS_UNSEND)
&& (msg.getEncryption() == Message.ENCRYPTION_OTR)) { && (msg.getEncryption() == Message.ENCRYPTION_OTR)) {
MessagePacket outPacket = service.prepareMessagePacket( MessagePacket outPacket = mXmppConnectionService.prepareMessagePacket(
account, msg, otrSession); account, msg, otrSession);
msg.setStatus(Message.STATUS_SEND); msg.setStatus(Message.STATUS_SEND);
service.databaseBackend.updateMessage(msg); mXmppConnectionService.databaseBackend.updateMessage(msg);
account.getXmppConnection() account.getXmppConnection()
.sendMessagePacket(outPacket); .sendMessagePacket(outPacket);
} }
} }
service.updateUi(conversation, false); mXmppConnectionService.updateUi(conversation, false);
} else if ((before != after) && (after == SessionStatus.FINISHED)) { } else if ((before != after) && (after == SessionStatus.FINISHED)) {
conversation.resetOtrSession(); conversation.resetOtrSession();
Log.d(LOGTAG,"otr session stoped"); 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; int status;
String[] fromParts = packet.getFrom().split("/"); 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")) { if (packet.hasChild("subject")) {
conversation.getMucOptions().setSubject(packet.findChild("subject").getContent()); conversation.getMucOptions().setSubject(packet.findChild("subject").getContent());
service.updateUi(conversation, false); mXmppConnectionService.updateUi(conversation, false);
return null; return null;
} }
if ((fromParts.length == 1)) { if ((fromParts.length == 1)) {
@ -114,8 +119,7 @@ public class MessageParser {
return new Message(conversation, counterPart, packet.getBody(), Message.ENCRYPTION_NONE, status); return new Message(conversation, counterPart, packet.getBody(), Message.ENCRYPTION_NONE, status);
} }
public static Message parseCarbonMessage(MessagePacket packet, public Message parseCarbonMessage(MessagePacket packet,Account account) {
Account account, XmppConnectionService service) {
int status; int status;
String fullJid; String fullJid;
Element forwarded; Element forwarded;
@ -142,16 +146,16 @@ public class MessageParser {
fullJid = message.getAttribute("to"); fullJid = message.getAttribute("to");
} }
String[] parts = fullJid.split("/"); 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); 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("/"); 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()) { for(Element child : packet.getChildren()) {
if (child.getName().equals("x")&&child.getAttribute("xmlns").equals("jabber:x:encrypted")) { if (child.getName().equals("x")&&child.getAttribute("xmlns").equals("jabber:x:encrypted")) {
return child.getContent(); return child.getContent();

View file

@ -26,6 +26,7 @@ import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.MucOptions; import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.MucOptions.OnRenameListener; import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.parser.MessageParser;
import eu.siacs.conversations.persistance.DatabaseBackend; import eu.siacs.conversations.persistance.DatabaseBackend;
import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.persistance.OnPhoneContactsMerged; 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.OnRosterFetchedListener;
import eu.siacs.conversations.ui.UiCallback; import eu.siacs.conversations.ui.UiCallback;
import eu.siacs.conversations.utils.ExceptionHelper; import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.utils.MessageParser;
import eu.siacs.conversations.utils.OnPhoneContactsLoadedListener; import eu.siacs.conversations.utils.OnPhoneContactsLoadedListener;
import eu.siacs.conversations.utils.PhoneHelper; import eu.siacs.conversations.utils.PhoneHelper;
import eu.siacs.conversations.utils.UIHelper; 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 int CONNECT_TIMEOUT = 60;
private static final long CARBON_GRACE_PERIOD = 60000L; private static final long CARBON_GRACE_PERIOD = 60000L;
private MessageParser mMessageParser = new MessageParser(this);
private List<Account> accounts; private List<Account> accounts;
private List<Conversation> conversations = null; private List<Conversation> conversations = null;
private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager( 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 final IBinder mBinder = new XmppConnectionBinder();
private OnMessagePacketReceived messageListener = new OnMessagePacketReceived() { private OnMessagePacketReceived messageListener = new OnMessagePacketReceived() {
@ -132,26 +132,25 @@ public class XmppConnectionService extends Service {
} }
if ((packet.getType() == MessagePacket.TYPE_CHAT)) { if ((packet.getType() == MessagePacket.TYPE_CHAT)) {
String pgpBody = MessageParser.getPgpBody(packet); String pgpBody = mMessageParser.getPgpBody(packet);
if (pgpBody != null) { if (pgpBody != null) {
message = MessageParser.parsePgpChat(pgpBody, packet, message = mMessageParser.parsePgpChat(pgpBody, packet,
account, service); account);
message.markUnread(); message.markUnread();
} else if ((packet.getBody() != null) } else if ((packet.getBody() != null)
&& (packet.getBody().startsWith("?OTR"))) { && (packet.getBody().startsWith("?OTR"))) {
message = MessageParser.parseOtrChat(packet, account, message = mMessageParser.parseOtrChat(packet, account);
service);
if (message != null) { if (message != null) {
message.markUnread(); message.markUnread();
} }
} else if (packet.hasChild("body")) { } else if (packet.hasChild("body")) {
message = MessageParser.parsePlainTextChat(packet, account, message = mMessageParser
service); .parsePlainTextChat(packet, account);
message.markUnread(); message.markUnread();
} else if (packet.hasChild("received") } else if (packet.hasChild("received")
|| (packet.hasChild("sent"))) { || (packet.hasChild("sent"))) {
message = MessageParser.parseCarbonMessage(packet, account, message = mMessageParser
service); .parseCarbonMessage(packet, account);
if (message != null) { if (message != null) {
if (message.getStatus() == Message.STATUS_SEND) { if (message.getStatus() == Message.STATUS_SEND) {
lastCarbonMessageReceived = SystemClock lastCarbonMessageReceived = SystemClock
@ -165,8 +164,7 @@ public class XmppConnectionService extends Service {
} }
} else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) { } else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) {
message = MessageParser message = mMessageParser.parseGroupchat(packet, account);
.parseGroupchat(packet, account, service);
if (message != null) { if (message != null) {
if (message.getStatus() == Message.STATUS_RECIEVED) { if (message.getStatus() == Message.STATUS_RECIEVED) {
message.markUnread(); message.markUnread();
@ -176,7 +174,7 @@ public class XmppConnectionService extends Service {
} }
} }
} else if (packet.getType() == MessagePacket.TYPE_ERROR) { } else if (packet.getType() == MessagePacket.TYPE_ERROR) {
MessageParser.parseError(packet, account, service); mMessageParser.parseError(packet, account);
return; return;
} else if (packet.getType() == MessagePacket.TYPE_NORMAL) { } else if (packet.getType() == MessagePacket.TYPE_NORMAL) {
if (packet.hasChild("x")) { if (packet.hasChild("x")) {
@ -325,8 +323,14 @@ public class XmppConnectionService extends Service {
} else { } else {
msg = ""; msg = "";
} }
contact.setPgpKeyId(pgp.fetchKeyId(account,msg,x.getContent())); contact.setPgpKeyId(pgp.fetchKeyId(account,
Log.d("xmppService",account.getJid()+": fetched key id for "+contact.getJid()+" was:"+contact.getPgpKeyId()); msg, x.getContent()));
Log.d("xmppService",
account.getJid()
+ ": fetched key id for "
+ contact.getJid()
+ " was:"
+ contact.getPgpKeyId());
} }
} }
replaceContactInConversation(account, replaceContactInConversation(account,
@ -393,20 +397,30 @@ public class XmppConnectionService extends Service {
.hasChild("data", "http://jabber.org/protocol/ibb")) { .hasChild("data", "http://jabber.org/protocol/ibb")) {
XmppConnectionService.this.mJingleConnectionManager XmppConnectionService.this.mJingleConnectionManager
.deliverIbbPacket(account, packet); .deliverIbbPacket(account, packet);
} else if (packet.hasChild("query","http://jabber.org/protocol/disco#info")) { } else if (packet.hasChild("query",
IqPacket iqResponse = packet.generateRespone(IqPacket.TYPE_RESULT); "http://jabber.org/protocol/disco#info")) {
Element query = iqResponse.addChild("query", "http://jabber.org/protocol/disco#info"); IqPacket iqResponse = packet
query.addChild("feature").setAttribute("var", "urn:xmpp:jingle:1"); .generateRespone(IqPacket.TYPE_RESULT);
query.addChild("feature").setAttribute("var", "urn:xmpp:jingle:apps:file-transfer:3"); Element query = iqResponse.addChild("query",
query.addChild("feature").setAttribute("var", "urn:xmpp:jingle:transports:s5b:1"); "http://jabber.org/protocol/disco#info");
query.addChild("feature").setAttribute("var", "urn:xmpp:jingle:transports:ibb:1"); 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); account.getXmppConnection().sendIqPacket(iqResponse, null);
} else { } else {
if ((packet.getType() == IqPacket.TYPE_GET)||(packet.getType() == IqPacket.TYPE_SET)) { if ((packet.getType() == IqPacket.TYPE_GET)
IqPacket response = packet.generateRespone(IqPacket.TYPE_ERROR); || (packet.getType() == IqPacket.TYPE_SET)) {
IqPacket response = packet
.generateRespone(IqPacket.TYPE_ERROR);
Element error = response.addChild("error"); Element error = response.addChild("error");
error.setAttribute("type", "cancel"); error.setAttribute("type", "cancel");
error.addChild("feature-not-implemented","urn:ietf:params:xml:ns:xmpp-stanzas"); error.addChild("feature-not-implemented",
"urn:ietf:params:xml:ns:xmpp-stanzas");
account.getXmppConnection().sendIqPacket(response, null); account.getXmppConnection().sendIqPacket(response, null);
} }
} }
@ -446,10 +460,12 @@ public class XmppConnectionService extends Service {
return this.fileBackend; 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; final Message message;
if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
message = new Message(conversation, "",Message.ENCRYPTION_DECRYPTED); message = new Message(conversation, "",
Message.ENCRYPTION_DECRYPTED);
} else { } else {
message = new Message(conversation, "", Message.ENCRYPTION_NONE); message = new Message(conversation, "", Message.ENCRYPTION_NONE);
} }
@ -460,7 +476,8 @@ public class XmppConnectionService extends Service {
@Override @Override
public void run() { public void run() {
JingleFile file = getFileBackend().copyImageToPrivateStorage(message, uri); JingleFile file = getFileBackend().copyImageToPrivateStorage(
message, uri);
if (file == null) { if (file == null) {
callback.error(R.string.error_copying_image_file); callback.error(R.string.error_copying_image_file);
} else { } else {
@ -1214,8 +1231,8 @@ public class XmppConnectionService extends Service {
renameListener.onRename(success); renameListener.onRename(success);
} }
if (success) { if (success) {
String jid = conversation.getContactJid().split("/")[0] + "/" String jid = conversation.getContactJid().split("/")[0]
+ nick; + "/" + nick;
conversation.setContactJid(jid); conversation.setContactJid(jid);
databaseBackend.updateConversation(conversation); databaseBackend.updateConversation(conversation);
} }
@ -1481,7 +1498,8 @@ public class XmppConnectionService extends Service {
if (convChangedListener != null) { if (convChangedListener != null) {
convChangedListener.onConversationListChanged(); convChangedListener.onConversationListChanged();
} else { } else {
UIHelper.updateNotification(getApplicationContext(), getConversations(), conversation, notify); UIHelper.updateNotification(getApplicationContext(),
getConversations(), conversation, notify);
} }
} }
} }