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_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				&& !message.bodyContainsDownloadable()
332				&& !this.bodyContainsDownloadable());
333	}
334
335	public String getMergedBody() {
336		Message next = this.next();
337		if (this.mergable(next)) {
338			return body.trim() + '\n' + next.getMergedBody();
339		}
340		return body.trim();
341	}
342
343	public int getMergedStatus() {
344		Message next = this.next();
345		if (this.mergable(next)) {
346			return next.getMergedStatus();
347		} else {
348			return getStatus();
349		}
350	}
351
352	public long getMergedTimeSent() {
353		Message next = this.next();
354		if (this.mergable(next)) {
355			return next.getMergedTimeSent();
356		} else {
357			return getTimeSent();
358		}
359	}
360
361	public boolean wasMergedIntoPrevious() {
362		Message prev = this.prev();
363		if (prev == null) {
364			return false;
365		} else {
366			return prev.mergable(this);
367		}
368	}
369	
370	public boolean trusted() {
371		Contact contact = this.getContact();
372		return (status > STATUS_RECEIVED || (contact != null && contact.trusted()));
373	}
374
375	public boolean bodyContainsDownloadable() {
376		try {
377			URL url = new URL(this.getBody());
378			if (!url.getProtocol().equalsIgnoreCase("http")
379					&& !url.getProtocol().equalsIgnoreCase("https")) {
380				return false;
381			}
382			if (url.getPath() == null) {
383				return false;
384			}
385			String[] pathParts = url.getPath().split("/");
386			String filename = pathParts[pathParts.length - 1];
387			String[] extensionParts = filename.split("\\.");
388			if (extensionParts.length == 2
389					&& Arrays.asList(Downloadable.VALID_EXTENSIONS).contains(
390							extensionParts[extensionParts.length - 1])) {
391				return true;
392			} else if (extensionParts.length == 3
393					&& Arrays
394							.asList(Downloadable.VALID_CRYPTO_EXTENSIONS)
395							.contains(extensionParts[extensionParts.length - 1])
396					&& Arrays.asList(Downloadable.VALID_EXTENSIONS).contains(
397							extensionParts[extensionParts.length - 2])) {
398				return true;
399			} else {
400				return false;
401			}
402		} catch (MalformedURLException e) {
403			return false;
404		}
405	}
406
407	public ImageParams getImageParams() {
408		ImageParams params = getLegacyImageParams();
409		if (params!=null) {
410			return params;
411		}
412		params = new ImageParams();
413		if (this.downloadable != null) {
414			params.size = this.downloadable.getFileSize();
415		}
416		if (body == null) {
417			return params;
418		}
419		String parts[] = body.split("\\|");
420		if (parts.length == 1) {
421			try {
422				params.size = Long.parseLong(parts[0]);
423			} catch (NumberFormatException e) {
424				params.origin = parts[0];
425				try {
426					params.url = new URL(parts[0]);
427				} catch (MalformedURLException e1) {
428					params.url = null;
429				}
430			}
431		} else if (parts.length == 3) {
432			try {
433				params.size = Long.parseLong(parts[0]);
434			} catch (NumberFormatException e) {
435				params.size = 0;
436			}
437			try {
438				params.width = Integer.parseInt(parts[1]);
439			} catch (NumberFormatException e) {
440				params.width = 0;
441			}
442			try {
443				params.height = Integer.parseInt(parts[2]);
444			} catch (NumberFormatException e) {
445				params.height = 0;
446			}
447		} else if (parts.length == 4) {
448			params.origin = parts[0];
449			try {
450				params.url = new URL(parts[0]);
451			} catch (MalformedURLException e1) {
452				params.url = null;
453			}
454			try {
455				params.size = Long.parseLong(parts[1]);
456			} catch (NumberFormatException e) {
457				params.size = 0;
458			}
459			try {
460				params.width = Integer.parseInt(parts[2]);
461			} catch (NumberFormatException e) {
462				params.width = 0;
463			}
464			try {
465				params.height = Integer.parseInt(parts[3]);
466			} catch (NumberFormatException e) {
467				params.height = 0;
468			}
469		}
470		return params;
471	}
472	
473	public ImageParams getLegacyImageParams() {
474		ImageParams params = new ImageParams();
475		if (body == null) {
476			return params;
477		}
478		String parts[] = body.split(",");
479		if (parts.length == 3) {
480			try {
481				params.size = Long.parseLong(parts[0]);
482			} catch (NumberFormatException e) {
483				return null;
484			}
485			try {
486				params.width = Integer.parseInt(parts[1]);
487			} catch (NumberFormatException e) {
488				return null;
489			}
490			try {
491				params.height = Integer.parseInt(parts[2]);
492			} catch (NumberFormatException e) {
493				return null;
494			}
495			return params;
496		} else {
497			return null;
498		}
499	}
500
501	public class ImageParams {
502		public URL url;
503		public long size = 0;
504		public int width = 0;
505		public int height = 0;
506		public String origin;
507	}
508}