Message.java

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