1package eu.siacs.conversations.parser;
2
3import java.util.List;
4
5import net.java.otr4j.session.Session;
6import net.java.otr4j.session.SessionStatus;
7import android.util.Log;
8import eu.siacs.conversations.entities.Account;
9import eu.siacs.conversations.entities.Conversation;
10import eu.siacs.conversations.entities.Message;
11import eu.siacs.conversations.services.XmppConnectionService;
12import eu.siacs.conversations.xml.Element;
13import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
14
15public class MessageParser {
16
17 protected static final String LOGTAG = "xmppService";
18 private XmppConnectionService mXmppConnectionService;
19
20 public MessageParser(XmppConnectionService service) {
21 this.mXmppConnectionService = service;
22 }
23
24 public Message parseChat(MessagePacket packet, Account account) {
25 String[] fromParts = packet.getFrom().split("/");
26 Conversation conversation = mXmppConnectionService
27 .findOrCreateConversation(account, fromParts[0], false);
28 conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
29 String pgpBody = getPgpBody(packet);
30 if (pgpBody != null) {
31 return new Message(conversation, packet.getFrom(), pgpBody,
32 Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED);
33 } else {
34 return new Message(conversation, packet.getFrom(),
35 packet.getBody(), Message.ENCRYPTION_NONE,
36 Message.STATUS_RECIEVED);
37 }
38 }
39
40 public Message parseOtrChat(MessagePacket packet, Account account) {
41 boolean properlyAddressed = (packet.getTo().split("/").length == 2)
42 || (account.countPresences() == 1);
43 String[] fromParts = packet.getFrom().split("/");
44 Conversation conversation = mXmppConnectionService
45 .findOrCreateConversation(account, fromParts[0], false);
46 String body = packet.getBody();
47 if (!conversation.hasValidOtrSession()) {
48 if (properlyAddressed) {
49 Log.d("xmppService",
50 "starting new otr session with "
51 + packet.getFrom()
52 + " because no valid otr session has been found");
53 conversation.startOtrSession(
54 mXmppConnectionService.getApplicationContext(),
55 fromParts[1], false);
56 } else {
57 Log.d("xmppService", account.getJid()
58 + ": ignoring otr session with " + fromParts[0]);
59 return null;
60 }
61 } else {
62 String foreignPresence = conversation.getOtrSession()
63 .getSessionID().getUserID();
64 if (!foreignPresence.equals(fromParts[1])) {
65 conversation.resetOtrSession();
66 if (properlyAddressed) {
67 Log.d("xmppService",
68 "replacing otr session with " + packet.getFrom());
69 conversation.startOtrSession(
70 mXmppConnectionService.getApplicationContext(),
71 fromParts[1], false);
72 } else {
73 return null;
74 }
75 }
76 }
77 try {
78 Session otrSession = conversation.getOtrSession();
79 SessionStatus before = otrSession.getSessionStatus();
80 body = otrSession.transformReceiving(body);
81 SessionStatus after = otrSession.getSessionStatus();
82 if ((before != after) && (after == SessionStatus.ENCRYPTED)) {
83 Log.d(LOGTAG, "otr session etablished");
84 List<Message> messages = conversation.getMessages();
85 for (int i = 0; i < messages.size(); ++i) {
86 Message msg = messages.get(i);
87 if ((msg.getStatus() == Message.STATUS_UNSEND)
88 && (msg.getEncryption() == Message.ENCRYPTION_OTR)) {
89 MessagePacket outPacket = mXmppConnectionService
90 .prepareMessagePacket(account, msg, otrSession);
91 msg.setStatus(Message.STATUS_SEND);
92 mXmppConnectionService.databaseBackend
93 .updateMessage(msg);
94 account.getXmppConnection()
95 .sendMessagePacket(outPacket);
96 }
97 }
98 mXmppConnectionService.updateUi(conversation, false);
99 } else if ((before != after) && (after == SessionStatus.FINISHED)) {
100 conversation.resetOtrSession();
101 Log.d(LOGTAG, "otr session stoped");
102 }
103 // isEmpty is a work around for some weird clients which send emtpty
104 // strings over otr
105 if ((body == null) || (body.isEmpty())) {
106 return null;
107 }
108 conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
109 return new Message(conversation, packet.getFrom(), body,
110 Message.ENCRYPTION_OTR, Message.STATUS_RECIEVED);
111 } catch (Exception e) {
112 conversation.resetOtrSession();
113 return null;
114 }
115 }
116
117 public Message parseGroupchat(MessagePacket packet, Account account) {
118 int status;
119 String[] fromParts = packet.getFrom().split("/");
120 Conversation conversation = mXmppConnectionService
121 .findOrCreateConversation(account, fromParts[0], true);
122 if (packet.hasChild("subject")) {
123 conversation.getMucOptions().setSubject(
124 packet.findChild("subject").getContent());
125 mXmppConnectionService.updateUi(conversation, false);
126 return null;
127 }
128 if ((fromParts.length == 1)) {
129 return null;
130 }
131 String counterPart = fromParts[1];
132 if (counterPart.equals(conversation.getMucOptions().getNick())) {
133 if (mXmppConnectionService.markMessage(conversation,
134 packet.getId(), Message.STATUS_SEND)) {
135 return null;
136 } else {
137 status = Message.STATUS_SEND;
138 }
139 } else {
140 status = Message.STATUS_RECIEVED;
141 }
142 String pgpBody = getPgpBody(packet);
143 conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
144 if (pgpBody == null) {
145 return new Message(conversation, counterPart, packet.getBody(),
146 Message.ENCRYPTION_NONE, status);
147 } else {
148 return new Message(conversation, counterPart, pgpBody,
149 Message.ENCRYPTION_PGP, status);
150 }
151 }
152
153 public Message parseCarbonMessage(MessagePacket packet, Account account) {
154 int status;
155 String fullJid;
156 Element forwarded;
157 if (packet.hasChild("received")) {
158 forwarded = packet.findChild("received").findChild("forwarded");
159 status = Message.STATUS_RECIEVED;
160 } else if (packet.hasChild("sent")) {
161 forwarded = packet.findChild("sent").findChild("forwarded");
162 status = Message.STATUS_SEND;
163 } else {
164 return null;
165 }
166 if (forwarded == null) {
167 return null;
168 }
169 Element message = forwarded.findChild("message");
170 if ((message == null) || (!message.hasChild("body")))
171 return null; // either malformed or boring
172 if (status == Message.STATUS_RECIEVED) {
173 fullJid = message.getAttribute("from");
174 } else {
175 fullJid = message.getAttribute("to");
176 }
177 String[] parts = fullJid.split("/");
178 Conversation conversation = mXmppConnectionService
179 .findOrCreateConversation(account, parts[0], false);
180 conversation.setLatestMarkableMessageId(getMarkableMessageId(packet));
181 String pgpBody = getPgpBody(message);
182 if (pgpBody != null) {
183 return new Message(conversation, fullJid, pgpBody,
184 Message.ENCRYPTION_PGP, status);
185 } else {
186 String body = message.findChild("body").getContent();
187 return new Message(conversation, fullJid, body,
188 Message.ENCRYPTION_NONE, status);
189 }
190 }
191
192 public void parseError(MessagePacket packet, Account account) {
193 String[] fromParts = packet.getFrom().split("/");
194 mXmppConnectionService.markMessage(account, fromParts[0],
195 packet.getId(), Message.STATUS_SEND_FAILED);
196 }
197
198 private String getPgpBody(Element message) {
199 Element child = message.findChild("x", "jabber:x:encrypted");
200 if (child == null) {
201 return null;
202 } else {
203 return child.getContent();
204 }
205 }
206
207 private String getMarkableMessageId(Element message) {
208 if (message.hasChild("markable", "urn:xmpp:chat-markers:0")) {
209 return message.getAttribute("id");
210 } else {
211 return null;
212 }
213 }
214}