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 if (getOtrSession()== null) {
295 return "";
296 }
297 DSAPublicKey remotePubKey = (DSAPublicKey) getOtrSession()
298 .getRemotePublicKey();
299 StringBuilder builder = new StringBuilder(
300 new OtrCryptoEngineImpl().getFingerprint(remotePubKey));
301 builder.insert(8, " ");
302 builder.insert(17, " ");
303 builder.insert(26, " ");
304 builder.insert(35, " ");
305 this.otrFingerprint = builder.toString();
306 } catch (OtrCryptoException e) {
307
308 }
309 }
310 return this.otrFingerprint;
311 }
312
313 public synchronized MucOptions getMucOptions() {
314 if (this.mucOptions == null) {
315 this.mucOptions = new MucOptions(this.getAccount());
316 }
317 this.mucOptions.setConversation(this);
318 return this.mucOptions;
319 }
320
321 public void resetMucOptions() {
322 this.mucOptions = null;
323 }
324
325 public void setContactJid(String jid) {
326 this.contactJid = jid;
327 }
328
329 public void setNextPresence(String presence) {
330 this.nextPresence = presence;
331 }
332
333 public String getNextPresence() {
334 return this.nextPresence;
335 }
336
337 public int getLatestEncryption() {
338 int latestEncryption = this.getLatestMessage().getEncryption();
339 if ((latestEncryption == Message.ENCRYPTION_DECRYPTED)
340 || (latestEncryption == Message.ENCRYPTION_DECRYPTION_FAILED)) {
341 return Message.ENCRYPTION_PGP;
342 } else {
343 return latestEncryption;
344 }
345 }
346
347 public int getNextEncryption() {
348 if (this.nextMessageEncryption == -1) {
349 return this.getLatestEncryption();
350 }
351 return this.nextMessageEncryption;
352 }
353
354 public void setNextEncryption(int encryption) {
355 this.nextMessageEncryption = encryption;
356 }
357
358 public String getNextMessage() {
359 if (this.nextMessage == null) {
360 return "";
361 } else {
362 return this.nextMessage;
363 }
364 }
365
366 public void setNextMessage(String message) {
367 this.nextMessage = message;
368 }
369
370 public void setLatestMarkableMessageId(String id) {
371 if (id != null) {
372 this.latestMarkableMessageId = id;
373 }
374 }
375
376 public void setSymmetricKey(byte[] key) {
377 this.symmetricKey = key;
378 }
379
380 public byte[] getSymmetricKey() {
381 return this.symmetricKey;
382 }
383
384 public void setBookmark(Bookmark bookmark) {
385 this.bookmark = bookmark;
386 this.bookmark.setConversation(this);
387 }
388
389 public void deregisterWithBookmark() {
390 if (this.bookmark != null) {
391 this.bookmark.setConversation(null);
392 }
393 }
394
395 public Bookmark getBookmark() {
396 return this.bookmark;
397 }
398
399 public void failWaitingOtrMessages() {
400 for (Message message : this.messages) {
401 if (message.getEncryption() == Message.ENCRYPTION_OTR
402 && message.getStatus() == Message.STATUS_WAITING) {
403 message.setStatus(Message.STATUS_SEND_FAILED);
404 }
405 }
406 }
407}