1package eu.siacs.conversations.entities;
2
3import java.net.MalformedURLException;
4import java.net.URL;
5import java.util.Arrays;
6
7import eu.siacs.conversations.Config;
8import android.content.ContentValues;
9import android.database.Cursor;
10
11public class Message extends AbstractEntity {
12
13 public static final String TABLENAME = "messages";
14
15 public static final int STATUS_RECEIVED = 0;
16 public static final int STATUS_UNSEND = 1;
17 public static final int STATUS_SEND = 2;
18 public static final int STATUS_SEND_FAILED = 3;
19 public static final int STATUS_SEND_REJECTED = 4;
20 public static final int STATUS_WAITING = 5;
21 public static final int STATUS_OFFERED = 6;
22 public static final int STATUS_SEND_RECEIVED = 7;
23 public static final int STATUS_SEND_DISPLAYED = 8;
24
25 public static final int ENCRYPTION_NONE = 0;
26 public static final int ENCRYPTION_PGP = 1;
27 public static final int ENCRYPTION_OTR = 2;
28 public static final int ENCRYPTION_DECRYPTED = 3;
29 public static final int ENCRYPTION_DECRYPTION_FAILED = 4;
30
31 public static final int TYPE_TEXT = 0;
32 public static final int TYPE_IMAGE = 1;
33 public static final int TYPE_AUDIO = 2;
34 public static final int TYPE_STATUS = 3;
35 public static final int TYPE_PRIVATE = 4;
36
37 public static String CONVERSATION = "conversationUuid";
38 public static String COUNTERPART = "counterpart";
39 public static String TRUE_COUNTERPART = "trueCounterpart";
40 public static String BODY = "body";
41 public static String TIME_SENT = "timeSent";
42 public static String ENCRYPTION = "encryption";
43 public static String STATUS = "status";
44 public static String TYPE = "type";
45 public static String REMOTE_MSG_ID = "remoteMsgId";
46
47 protected String conversationUuid;
48 protected String counterpart;
49 protected String trueCounterpart;
50 protected String body;
51 protected String encryptedBody;
52 protected long timeSent;
53 protected int encryption;
54 protected int status;
55 protected int type;
56 protected boolean read = true;
57 protected String remoteMsgId = null;
58
59 protected Conversation conversation = null;
60 protected Downloadable downloadable = null;
61 public boolean markable = false;
62
63 private Message mNextMessage = null;
64 private Message mPreviousMessage = null;
65
66 private Message() {
67
68 }
69
70 public Message(Conversation conversation, String body, int encryption) {
71 this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),
72 conversation.getContactJid(), null, body, System
73 .currentTimeMillis(), encryption,
74 Message.STATUS_UNSEND, TYPE_TEXT, null);
75 this.conversation = conversation;
76 }
77
78 public Message(Conversation conversation, String counterpart, String body,
79 int encryption, int status) {
80 this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),
81 counterpart, null, body, System.currentTimeMillis(),
82 encryption, status, TYPE_TEXT, null);
83 this.conversation = conversation;
84 }
85
86 public Message(String uuid, String conversationUUid, String counterpart,
87 String trueCounterpart, String body, long timeSent, int encryption,
88 int status, int type, String remoteMsgId) {
89 this.uuid = uuid;
90 this.conversationUuid = conversationUUid;
91 this.counterpart = counterpart;
92 this.trueCounterpart = trueCounterpart;
93 this.body = body;
94 this.timeSent = timeSent;
95 this.encryption = encryption;
96 this.status = status;
97 this.type = type;
98 this.remoteMsgId = remoteMsgId;
99 }
100
101 @Override
102 public ContentValues getContentValues() {
103 ContentValues values = new ContentValues();
104 values.put(UUID, uuid);
105 values.put(CONVERSATION, conversationUuid);
106 values.put(COUNTERPART, counterpart);
107 values.put(TRUE_COUNTERPART, trueCounterpart);
108 values.put(BODY, body);
109 values.put(TIME_SENT, timeSent);
110 values.put(ENCRYPTION, encryption);
111 values.put(STATUS, status);
112 values.put(TYPE, type);
113 values.put(REMOTE_MSG_ID, remoteMsgId);
114 return values;
115 }
116
117 public String getConversationUuid() {
118 return conversationUuid;
119 }
120
121 public Conversation getConversation() {
122 return this.conversation;
123 }
124
125 public String getCounterpart() {
126 return counterpart;
127 }
128
129 public Contact getContact() {
130 if (this.conversation.getMode() == Conversation.MODE_SINGLE) {
131 return this.conversation.getContact();
132 } else {
133 if (this.trueCounterpart == null) {
134 return null;
135 } else {
136 return this.conversation.getAccount().getRoster()
137 .getContactFromRoster(this.trueCounterpart);
138 }
139 }
140 }
141
142 public String getBody() {
143 return body;
144 }
145
146 public long getTimeSent() {
147 return timeSent;
148 }
149
150 public int getEncryption() {
151 return encryption;
152 }
153
154 public int getStatus() {
155 return status;
156 }
157
158 public String getRemoteMsgId() {
159 return this.remoteMsgId;
160 }
161
162 public void setRemoteMsgId(String id) {
163 this.remoteMsgId = id;
164 }
165
166 public static Message fromCursor(Cursor cursor) {
167 return new Message(cursor.getString(cursor.getColumnIndex(UUID)),
168 cursor.getString(cursor.getColumnIndex(CONVERSATION)),
169 cursor.getString(cursor.getColumnIndex(COUNTERPART)),
170 cursor.getString(cursor.getColumnIndex(TRUE_COUNTERPART)),
171 cursor.getString(cursor.getColumnIndex(BODY)),
172 cursor.getLong(cursor.getColumnIndex(TIME_SENT)),
173 cursor.getInt(cursor.getColumnIndex(ENCRYPTION)),
174 cursor.getInt(cursor.getColumnIndex(STATUS)),
175 cursor.getInt(cursor.getColumnIndex(TYPE)),
176 cursor.getString(cursor.getColumnIndex(REMOTE_MSG_ID)));
177 }
178
179 public void setConversation(Conversation conv) {
180 this.conversation = conv;
181 }
182
183 public void setStatus(int status) {
184 this.status = status;
185 }
186
187 public boolean isRead() {
188 return this.read;
189 }
190
191 public void markRead() {
192 this.read = true;
193 }
194
195 public void markUnread() {
196 this.read = false;
197 }
198
199 public void setTime(long time) {
200 this.timeSent = time;
201 }
202
203 public void setEncryption(int encryption) {
204 this.encryption = encryption;
205 }
206
207 public void setBody(String body) {
208 this.body = body;
209 }
210
211 public String getEncryptedBody() {
212 return this.encryptedBody;
213 }
214
215 public void setEncryptedBody(String body) {
216 this.encryptedBody = body;
217 }
218
219 public void setType(int type) {
220 this.type = type;
221 }
222
223 public int getType() {
224 return this.type;
225 }
226
227 public void setPresence(String presence) {
228 if (presence == null) {
229 this.counterpart = this.counterpart.split("/", 2)[0];
230 } else {
231 this.counterpart = this.counterpart.split("/", 2)[0] + "/"
232 + presence;
233 }
234 }
235
236 public void setTrueCounterpart(String trueCounterpart) {
237 this.trueCounterpart = trueCounterpart;
238 }
239
240 public String getPresence() {
241 String[] counterparts = this.counterpart.split("/", 2);
242 if (counterparts.length == 2) {
243 return counterparts[1];
244 } else {
245 if (this.counterpart.contains("/")) {
246 return "";
247 } else {
248 return null;
249 }
250 }
251 }
252
253 public void setDownloadable(Downloadable downloadable) {
254 this.downloadable = downloadable;
255 }
256
257 public Downloadable getDownloadable() {
258 return this.downloadable;
259 }
260
261 public static Message createStatusMessage(Conversation conversation) {
262 Message message = new Message();
263 message.setType(Message.TYPE_STATUS);
264 message.setConversation(conversation);
265 return message;
266 }
267
268 public void setCounterpart(String counterpart) {
269 this.counterpart = counterpart;
270 }
271
272 public boolean equals(Message message) {
273 if ((this.remoteMsgId != null) && (this.body != null)
274 && (this.counterpart != null)) {
275 return this.remoteMsgId.equals(message.getRemoteMsgId())
276 && this.body.equals(message.getBody())
277 && this.counterpart.equals(message.getCounterpart());
278 } else {
279 return false;
280 }
281 }
282
283 public Message next() {
284 if (this.mNextMessage == null) {
285 synchronized (this.conversation.messages) {
286 int index = this.conversation.messages.indexOf(this);
287 if (index < 0
288 || index >= this.conversation.getMessages().size() - 1) {
289 this.mNextMessage = null;
290 } else {
291 this.mNextMessage = this.conversation.messages
292 .get(index + 1);
293 }
294 }
295 }
296 return this.mNextMessage;
297 }
298
299 public Message prev() {
300 if (this.mPreviousMessage == null) {
301 synchronized (this.conversation.messages) {
302 int index = this.conversation.messages.indexOf(this);
303 if (index <= 0 || index > this.conversation.messages.size()) {
304 this.mPreviousMessage = null;
305 } else {
306 this.mPreviousMessage = this.conversation.messages
307 .get(index - 1);
308 }
309 }
310 }
311 return this.mPreviousMessage;
312 }
313
314 public boolean mergable(Message message) {
315 if (message == null) {
316 return false;
317 }
318 return (message.getType() == Message.TYPE_TEXT
319 && this.getDownloadable() == null
320 && message.getDownloadable() == null
321 && message.getEncryption() != Message.ENCRYPTION_PGP
322 && this.getType() == message.getType()
323 && this.getEncryption() == message.getEncryption()
324 && this.getCounterpart().equals(message.getCounterpart())
325 && (message.getTimeSent() - this.getTimeSent()) <= (Config.MESSAGE_MERGE_WINDOW * 1000) && ((this
326 .getStatus() == message.getStatus() || ((this.getStatus() == Message.STATUS_SEND || this
327 .getStatus() == Message.STATUS_SEND_RECEIVED) && (message
328 .getStatus() == Message.STATUS_UNSEND
329 || message.getStatus() == Message.STATUS_SEND || message
330 .getStatus() == Message.STATUS_SEND_DISPLAYED)))));
331 }
332
333 public String getMergedBody() {
334 Message next = this.next();
335 if (this.mergable(next)) {
336 return body.trim() + '\n' + next.getMergedBody();
337 }
338 return body.trim();
339 }
340
341 public int getMergedStatus() {
342 Message next = this.next();
343 if (this.mergable(next)) {
344 return next.getMergedStatus();
345 } else {
346 return getStatus();
347 }
348 }
349
350 public long getMergedTimeSent() {
351 Message next = this.next();
352 if (this.mergable(next)) {
353 return next.getMergedTimeSent();
354 } else {
355 return getTimeSent();
356 }
357 }
358
359 public boolean wasMergedIntoPrevious() {
360 Message prev = this.prev();
361 if (prev == null) {
362 return false;
363 } else {
364 return prev.mergable(this);
365 }
366 }
367
368 public boolean bodyContainsDownloadable() {
369 Contact contact = this.getContact();
370 if (status <= STATUS_RECEIVED
371 && (contact == null || !contact.trusted())) {
372 return false;
373 }
374 try {
375 URL url = new URL(this.getBody());
376 if (!url.getProtocol().equalsIgnoreCase("http")
377 && !url.getProtocol().equalsIgnoreCase("https")) {
378 return false;
379 }
380 if (url.getPath() == null) {
381 return false;
382 }
383 String[] pathParts = url.getPath().split("/");
384 String filename = pathParts[pathParts.length - 1];
385 String[] extensionParts = filename.split("\\.");
386 if (extensionParts.length == 2
387 && Arrays.asList(Downloadable.VALID_EXTENSIONS).contains(
388 extensionParts[extensionParts.length - 1])) {
389 return true;
390 } else if (extensionParts.length == 3
391 && Arrays
392 .asList(Downloadable.VALID_CRYPTO_EXTENSIONS)
393 .contains(extensionParts[extensionParts.length - 1])
394 && Arrays.asList(Downloadable.VALID_EXTENSIONS).contains(
395 extensionParts[extensionParts.length - 2])) {
396 return true;
397 } else {
398 return false;
399 }
400 } catch (MalformedURLException e) {
401 return false;
402 }
403 }
404
405 public ImageParams getImageParams() {
406 ImageParams params = new ImageParams();
407 if (this.downloadable != null) {
408 params.size = this.downloadable.getFileSize();
409 }
410 if (body == null) {
411 return params;
412 }
413 String parts[] = body.split(",");
414 if (parts.length == 1) {
415 try {
416 params.size = Long.parseLong(parts[0]);
417 } catch (NumberFormatException e) {
418 params.origin = parts[0];
419 try {
420 params.url = new URL(parts[0]);
421 } catch (MalformedURLException e1) {
422 params.url = null;
423 }
424 }
425 } else if (parts.length == 3) {
426 try {
427 params.size = Long.parseLong(parts[0]);
428 } catch (NumberFormatException e) {
429 params.size = 0;
430 }
431 try {
432 params.width = Integer.parseInt(parts[1]);
433 } catch (NumberFormatException e) {
434 params.width = 0;
435 }
436 try {
437 params.height = Integer.parseInt(parts[2]);
438 } catch (NumberFormatException e) {
439 params.height = 0;
440 }
441 } else if (parts.length == 4) {
442 params.origin = parts[0];
443 try {
444 params.url = new URL(parts[0]);
445 } catch (MalformedURLException e1) {
446 params.url = null;
447 }
448 try {
449 params.size = Long.parseLong(parts[1]);
450 } catch (NumberFormatException e) {
451 params.size = 0;
452 }
453 try {
454 params.width = Integer.parseInt(parts[2]);
455 } catch (NumberFormatException e) {
456 params.width = 0;
457 }
458 try {
459 params.height = Integer.parseInt(parts[3]);
460 } catch (NumberFormatException e) {
461 params.height = 0;
462 }
463 }
464 return params;
465 }
466
467 public class ImageParams {
468 public URL url;
469 public long size = 0;
470 public int width = 0;
471 public int height = 0;
472 public String origin;
473 }
474}