Conversation.java

  1package eu.siacs.conversations.entities;
  2
  3import java.security.interfaces.DSAPublicKey;
  4import java.util.ArrayList;
  5import java.util.List;
  6
  7import net.java.otr4j.OtrException;
  8import net.java.otr4j.crypto.OtrCryptoEngineImpl;
  9import net.java.otr4j.crypto.OtrCryptoException;
 10import net.java.otr4j.session.SessionID;
 11import net.java.otr4j.session.SessionImpl;
 12import net.java.otr4j.session.SessionStatus;
 13import android.content.ContentValues;
 14import android.content.Context;
 15import android.database.Cursor;
 16import android.net.Uri;
 17
 18public class Conversation extends AbstractEntity {
 19	public static final String TABLENAME = "conversations";
 20
 21	public static final int STATUS_AVAILABLE = 0;
 22	public static final int STATUS_ARCHIVED = 1;
 23	public static final int STATUS_DELETED = 2;
 24
 25	public static final int MODE_MULTI = 1;
 26	public static final int MODE_SINGLE = 0;
 27
 28	public static final String NAME = "name";
 29	public static final String ACCOUNT = "accountUuid";
 30	public static final String CONTACT = "contactUuid";
 31	public static final String CONTACTJID = "contactJid";
 32	public static final String STATUS = "status";
 33	public static final String CREATED = "created";
 34	public static final String MODE = "mode";
 35
 36	private String name;
 37	private String contactUuid;
 38	private String accountUuid;
 39	private String contactJid;
 40	private int status;
 41	private long created;
 42	private int mode;
 43
 44	private String nextPresence;
 45
 46	private transient List<Message> messages = null;
 47	private transient Account account = null;
 48
 49	private transient SessionImpl otrSession;
 50
 51	private transient String otrFingerprint = null;
 52
 53	private int nextMessageEncryption = -1;
 54	private String nextMessage;
 55
 56	private transient MucOptions mucOptions = null;
 57
 58	private transient String latestMarkableMessageId;
 59
 60	private byte[] symmetricKey;
 61
 62	private boolean otrSessionNeedsStarting = false;
 63
 64	private Bookmark bookmark;
 65
 66	public Conversation(String name, Account account, String contactJid,
 67			int mode) {
 68		this(java.util.UUID.randomUUID().toString(), name, null, account
 69				.getUuid(), contactJid, System.currentTimeMillis(),
 70				STATUS_AVAILABLE, mode);
 71		this.account = account;
 72	}
 73
 74	public Conversation(String uuid, String name, String contactUuid,
 75			String accountUuid, String contactJid, long created, int status,
 76			int mode) {
 77		this.uuid = uuid;
 78		this.name = name;
 79		this.contactUuid = contactUuid;
 80		this.accountUuid = accountUuid;
 81		this.contactJid = contactJid;
 82		this.created = created;
 83		this.status = status;
 84		this.mode = mode;
 85	}
 86
 87	public List<Message> getMessages() {
 88		if (messages == null)
 89			this.messages = new ArrayList<Message>(); // prevent null pointer
 90
 91		// populate with Conversation (this)
 92
 93		for (Message msg : messages) {
 94			msg.setConversation(this);
 95		}
 96
 97		return messages;
 98	}
 99
100	public boolean isRead() {
101		if ((this.messages == null) || (this.messages.size() == 0))
102			return true;
103		return this.messages.get(this.messages.size() - 1).isRead();
104	}
105
106	public void markRead() {
107		if (this.messages == null) {
108			return;
109		}
110		for (int i = this.messages.size() - 1; i >= 0; --i) {
111			if (messages.get(i).isRead()) {
112				break;
113			}
114			this.messages.get(i).markRead();
115		}
116	}
117
118	public String popLatestMarkableMessageId() {
119		String id = this.latestMarkableMessageId;
120		this.latestMarkableMessageId = null;
121		return id;
122	}
123
124	public Message getLatestMessage() {
125		if ((this.messages == null) || (this.messages.size() == 0)) {
126			Message message = new Message(this, "", Message.ENCRYPTION_NONE);
127			message.setTime(getCreated());
128			return message;
129		} else {
130			Message message = this.messages.get(this.messages.size() - 1);
131			message.setConversation(this);
132			return message;
133		}
134	}
135
136	public void setMessages(List<Message> msgs) {
137		this.messages = msgs;
138	}
139
140	public String getName(boolean useSubject) {
141		if ((getMode() == MODE_MULTI) && (getMucOptions().getSubject() != null)
142				&& useSubject) {
143			return getMucOptions().getSubject();
144		} else if (getMode() == MODE_MULTI && bookmark != null
145				&& bookmark.getName() != null) {
146			return bookmark.getName();
147		} else {
148			return this.getContact().getDisplayName();
149		}
150	}
151
152	public String getProfilePhotoString() {
153		return this.getContact().getProfilePhoto();
154	}
155
156	public String getAccountUuid() {
157		return this.accountUuid;
158	}
159
160	public Account getAccount() {
161		return this.account;
162	}
163
164	public Contact getContact() {
165		return this.account.getRoster().getContact(this.contactJid);
166	}
167
168	public void setAccount(Account account) {
169		this.account = account;
170	}
171
172	public String getContactJid() {
173		return this.contactJid;
174	}
175
176	public Uri getProfilePhotoUri() {
177		if (this.getProfilePhotoString() != null) {
178			return Uri.parse(this.getProfilePhotoString());
179		}
180		return null;
181	}
182
183	public int getStatus() {
184		return this.status;
185	}
186
187	public long getCreated() {
188		return this.created;
189	}
190
191	public ContentValues getContentValues() {
192		ContentValues values = new ContentValues();
193		values.put(UUID, uuid);
194		values.put(NAME, name);
195		values.put(CONTACT, contactUuid);
196		values.put(ACCOUNT, accountUuid);
197		values.put(CONTACTJID, contactJid);
198		values.put(CREATED, created);
199		values.put(STATUS, status);
200		values.put(MODE, mode);
201		return values;
202	}
203
204	public static Conversation fromCursor(Cursor cursor) {
205		return new Conversation(cursor.getString(cursor.getColumnIndex(UUID)),
206				cursor.getString(cursor.getColumnIndex(NAME)),
207				cursor.getString(cursor.getColumnIndex(CONTACT)),
208				cursor.getString(cursor.getColumnIndex(ACCOUNT)),
209				cursor.getString(cursor.getColumnIndex(CONTACTJID)),
210				cursor.getLong(cursor.getColumnIndex(CREATED)),
211				cursor.getInt(cursor.getColumnIndex(STATUS)),
212				cursor.getInt(cursor.getColumnIndex(MODE)));
213	}
214
215	public void setStatus(int status) {
216		this.status = status;
217	}
218
219	public int getMode() {
220		return this.mode;
221	}
222
223	public void setMode(int mode) {
224		this.mode = mode;
225	}
226
227	public SessionImpl startOtrSession(Context context, String presence,
228			boolean sendStart) {
229		if (this.otrSession != null) {
230			return this.otrSession;
231		} else {
232			SessionID sessionId = new SessionID(this.getContactJid(), presence,
233					"xmpp");
234			this.otrSession = new SessionImpl(sessionId, getAccount()
235					.getOtrEngine(context));
236			try {
237				if (sendStart) {
238					this.otrSession.startSession();
239					this.otrSessionNeedsStarting = false;
240					return this.otrSession;
241				} else {
242					this.otrSessionNeedsStarting = true;
243				}
244				return this.otrSession;
245			} catch (OtrException e) {
246				return null;
247			}
248		}
249
250	}
251
252	public SessionImpl getOtrSession() {
253		return this.otrSession;
254	}
255
256	public void resetOtrSession() {
257		this.otrFingerprint = null;
258		this.otrSessionNeedsStarting = false;
259		this.otrSession = null;
260	}
261
262	public void startOtrIfNeeded() {
263		if (this.otrSession != null && this.otrSessionNeedsStarting) {
264			try {
265				this.otrSession.startSession();
266			} catch (OtrException e) {
267				this.resetOtrSession();
268			}
269		}
270	}
271
272	public void endOtrIfNeeded() {
273		if (this.otrSession != null) {
274			if (this.otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) {
275				try {
276					this.otrSession.endSession();
277					this.resetOtrSession();
278				} catch (OtrException e) {
279					this.resetOtrSession();
280				}
281			} else {
282				this.resetOtrSession();
283			}
284		}
285	}
286
287	public boolean hasValidOtrSession() {
288		return this.otrSession != null;
289	}
290
291	public String getOtrFingerprint() {
292		if (this.otrFingerprint == null) {
293			try {
294				DSAPublicKey remotePubKey = (DSAPublicKey) getOtrSession()
295						.getRemotePublicKey();
296				StringBuilder builder = new StringBuilder(
297						new OtrCryptoEngineImpl().getFingerprint(remotePubKey));
298				builder.insert(8, " ");
299				builder.insert(17, " ");
300				builder.insert(26, " ");
301				builder.insert(35, " ");
302				this.otrFingerprint = builder.toString();
303			} catch (OtrCryptoException e) {
304
305			}
306		}
307		return this.otrFingerprint;
308	}
309
310	public synchronized MucOptions getMucOptions() {
311		if (this.mucOptions == null) {
312			this.mucOptions = new MucOptions(this.getAccount());
313		}
314		this.mucOptions.setConversation(this);
315		return this.mucOptions;
316	}
317
318	public void resetMucOptions() {
319		this.mucOptions = null;
320	}
321
322	public void setContactJid(String jid) {
323		this.contactJid = jid;
324	}
325
326	public void setNextPresence(String presence) {
327		this.nextPresence = presence;
328	}
329
330	public String getNextPresence() {
331		return this.nextPresence;
332	}
333
334	public int getLatestEncryption() {
335		int latestEncryption = this.getLatestMessage().getEncryption();
336		if ((latestEncryption == Message.ENCRYPTION_DECRYPTED)
337				|| (latestEncryption == Message.ENCRYPTION_DECRYPTION_FAILED)) {
338			return Message.ENCRYPTION_PGP;
339		} else {
340			return latestEncryption;
341		}
342	}
343
344	public int getNextEncryption() {
345		if (this.nextMessageEncryption == -1) {
346			return this.getLatestEncryption();
347		}
348		return this.nextMessageEncryption;
349	}
350
351	public void setNextEncryption(int encryption) {
352		this.nextMessageEncryption = encryption;
353	}
354
355	public String getNextMessage() {
356		if (this.nextMessage == null) {
357			return "";
358		} else {
359			return this.nextMessage;
360		}
361	}
362
363	public void setNextMessage(String message) {
364		this.nextMessage = message;
365	}
366
367	public void setLatestMarkableMessageId(String id) {
368		if (id != null) {
369			this.latestMarkableMessageId = id;
370		}
371	}
372
373	public void setSymmetricKey(byte[] key) {
374		this.symmetricKey = key;
375	}
376
377	public byte[] getSymmetricKey() {
378		return this.symmetricKey;
379	}
380
381	public void setBookmark(Bookmark bookmark) {
382		this.bookmark = bookmark;
383		this.bookmark.setConversation(this);
384	}
385
386	public void deregisterWithBookmark() {
387		if (this.bookmark != null) {
388			this.bookmark.setConversation(null);
389		}
390	}
391
392	public Bookmark getBookmark() {
393		return this.bookmark;
394	}
395
396	public void failWaitingOtrMessages() {
397		for (Message message : this.messages) {
398			if (message.getEncryption() == Message.ENCRYPTION_OTR
399					&& message.getStatus() == Message.STATUS_WAITING) {
400				message.setStatus(Message.STATUS_SEND_FAILED);
401			}
402		}
403	}
404}