aboutsummaryrefslogtreecommitdiffstats
path: root/src/de/gultsch/chat/utils
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/de/gultsch/chat/utils/MessageParser.java119
-rw-r--r--src/de/gultsch/chat/utils/PhoneHelper.java11
2 files changed, 127 insertions, 3 deletions
diff --git a/src/de/gultsch/chat/utils/MessageParser.java b/src/de/gultsch/chat/utils/MessageParser.java
new file mode 100644
index 00000000..aec492c8
--- /dev/null
+++ b/src/de/gultsch/chat/utils/MessageParser.java
@@ -0,0 +1,119 @@
+package de.gultsch.chat.utils;
+
+import java.util.List;
+
+import net.java.otr4j.session.Session;
+import net.java.otr4j.session.SessionStatus;
+import android.util.Log;
+import de.gultsch.chat.entities.Account;
+import de.gultsch.chat.entities.Conversation;
+import de.gultsch.chat.entities.Message;
+import de.gultsch.chat.services.XmppConnectionService;
+import de.gultsch.chat.xml.Element;
+import de.gultsch.chat.xmpp.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 parseOtrChat(MessagePacket packet, Account account, XmppConnectionService service) {
+ String[] fromParts = packet.getFrom().split("/");
+ Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false);
+ String body = packet.getBody();
+ if (!conversation.hasValidOtrSession()) {
+ conversation.startOtrSession(service.getApplicationContext(), fromParts[1]);
+ }
+ 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<Message> 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);
+ }
+ }
+ if (service.convChangedListener!=null) {
+ service.convChangedListener.onConversationListChanged();
+ }
+ } else if ((before != after) && (after == SessionStatus.FINISHED)) {
+ conversation.resetOtrSession();
+ Log.d(LOGTAG,"otr session stoped");
+ }
+ } catch (Exception e) {
+ Log.d(LOGTAG, "error receiving otr. resetting");
+ conversation.resetOtrSession();
+ return null;
+ }
+ if (body == null) {
+ return null;
+ }
+ return new Message(conversation, packet.getFrom(), body, Message.ENCRYPTION_OTR,Message.STATUS_RECIEVED);
+ }
+
+ 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 ((fromParts.length == 1) || (packet.hasChild("subject"))) {
+ return null;
+ }
+ String counterPart = fromParts[1];
+ if (counterPart.equals(account.getUsername())) {
+ 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) {
+ // TODO Auto-generated method stub
+ 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;
+ }
+ 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);
+ }
+}
diff --git a/src/de/gultsch/chat/utils/PhoneHelper.java b/src/de/gultsch/chat/utils/PhoneHelper.java
index 3a53c08f..14773caa 100644
--- a/src/de/gultsch/chat/utils/PhoneHelper.java
+++ b/src/de/gultsch/chat/utils/PhoneHelper.java
@@ -1,6 +1,5 @@
package de.gultsch.chat.utils;
-import java.util.ArrayList;
import java.util.Hashtable;
import android.app.Activity;
@@ -11,14 +10,19 @@ import android.content.Loader.OnLoadCompleteListener;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
+import android.os.Looper;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Profile;
public class PhoneHelper {
public static void loadPhoneContacts(Context context, final OnPhoneContactsLoadedListener listener) {
+ if (Looper.myLooper()==null) {
+ Looper.prepare();
+ }
+ final Looper mLooper = Looper.myLooper();
final Hashtable<String, Bundle> phoneContacts = new Hashtable<String, Bundle>();
-
+
final String[] PROJECTION = new String[] {
ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME,
@@ -31,7 +35,7 @@ public class PhoneHelper {
+ "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL
+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
+ "\")";
-
+
CursorLoader mCursorLoader = new CursorLoader(context,
ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
null);
@@ -61,6 +65,7 @@ public class PhoneHelper {
if (listener!=null) {
listener.onPhoneContactsLoaded(phoneContacts);
}
+ mLooper.quit();
}
});
mCursorLoader.startLoading();