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