1package eu.siacs.conversations.entities;
2
3import java.security.interfaces.DSAPublicKey;
4import java.util.ArrayList;
5import java.util.List;
6
7import eu.siacs.conversations.crypto.OtrEngine;
8import eu.siacs.conversations.xmpp.XmppConnection;
9
10import net.java.otr4j.OtrException;
11import net.java.otr4j.crypto.OtrCryptoEngineImpl;
12import net.java.otr4j.crypto.OtrCryptoException;
13import net.java.otr4j.session.SessionID;
14import net.java.otr4j.session.SessionImpl;
15import net.java.otr4j.session.SessionStatus;
16
17import android.content.ContentValues;
18import android.content.Context;
19import android.database.Cursor;
20import android.net.Uri;
21import android.util.Log;
22
23public class Conversation extends AbstractEntity {
24
25 private static final long serialVersionUID = -6727528868973996739L;
26
27 public static final String TABLENAME = "conversations";
28
29 public static final int STATUS_AVAILABLE = 0;
30 public static final int STATUS_ARCHIVED = 1;
31 public static final int STATUS_DELETED = 2;
32
33 public static final int MODE_MULTI = 1;
34 public static final int MODE_SINGLE = 0;
35
36 public static final String NAME = "name";
37 public static final String ACCOUNT = "accountUuid";
38 public static final String CONTACT = "contactUuid";
39 public static final String CONTACTJID = "contactJid";
40 public static final String STATUS = "status";
41 public static final String CREATED = "created";
42 public static final String MODE = "mode";
43
44 private String name;
45 private String contactUuid;
46 private String accountUuid;
47 private String contactJid;
48 private int status;
49 private long created;
50 private int mode;
51
52 private transient List<Message> messages = null;
53 private transient Account account = null;
54 private transient Contact contact;
55
56 private transient SessionImpl otrSession;
57
58 private transient String otrFingerprint = null;
59
60 public int nextMessageEncryption = Message.ENCRYPTION_NONE;
61
62 private transient MucOptions mucOptions = null;
63
64 public Conversation(String name, Account account,
65 String contactJid, int mode) {
66 this(java.util.UUID.randomUUID().toString(), name, null, account.getUuid(), contactJid, System
67 .currentTimeMillis(), STATUS_AVAILABLE,mode);
68 this.account = account;
69 }
70
71 public Conversation(String uuid, String name, String contactUuid,
72 String accountUuid, String contactJid, long created, int status, int mode) {
73 this.uuid = uuid;
74 this.name = name;
75 this.contactUuid = contactUuid;
76 this.accountUuid = accountUuid;
77 this.contactJid = contactJid;
78 this.created = created;
79 this.status = status;
80 this.mode = mode;
81 }
82
83 public List<Message> getMessages() {
84 if (messages == null) this.messages = new ArrayList<Message>(); //prevent null pointer
85
86 //populate with Conversation (this)
87
88 for(Message msg : messages) {
89 msg.setConversation(this);
90 }
91
92 return messages;
93 }
94
95 public boolean isRead() {
96 if ((this.messages == null)||(this.messages.size() == 0)) return true;
97 return this.messages.get(this.messages.size() - 1).isRead();
98 }
99
100 public void markRead() {
101 if (this.messages == null) return;
102 for(int i = this.messages.size() -1; i >= 0; --i) {
103 if (messages.get(i).isRead()) return;
104 this.messages.get(i).markRead();
105 }
106 }
107
108 public Message getLatestMessage() {
109 if ((this.messages == null)||(this.messages.size()==0)) {
110 Message message = new Message(this,"",Message.ENCRYPTION_NONE);
111 message.setTime(getCreated());
112 return message;
113 } else {
114 return this.messages.get(this.messages.size() - 1);
115 }
116 }
117
118 public void setMessages(List<Message> msgs) {
119 this.messages = msgs;
120 }
121
122 public String getName() {
123 if (this.contact!=null) {
124 return this.contact.getDisplayName();
125 } else {
126 return this.name;
127 }
128 }
129
130 public String getProfilePhotoString() {
131 if (this.contact==null) {
132 return null;
133 } else {
134 return this.contact.getProfilePhoto();
135 }
136 }
137
138 public String getAccountUuid() {
139 return this.accountUuid;
140 }
141
142 public Account getAccount() {
143 return this.account;
144 }
145
146 public Contact getContact() {
147 return this.contact;
148 }
149
150 public void setContact(Contact contact) {
151 this.contact = contact;
152 if (contact!=null) {
153 this.contactUuid = contact.getUuid();
154 }
155 }
156
157 public void setAccount(Account account) {
158 this.account = account;
159 }
160
161 public String getContactJid() {
162 return this.contactJid;
163 }
164
165 public Uri getProfilePhotoUri() {
166 if (this.getProfilePhotoString() != null) {
167 return Uri.parse(this.getProfilePhotoString());
168 }
169 return null;
170 }
171
172 public int getStatus() {
173 return this.status;
174 }
175
176 public long getCreated() {
177 return this.created;
178 }
179
180 public ContentValues getContentValues() {
181 ContentValues values = new ContentValues();
182 values.put(UUID, uuid);
183 values.put(NAME, name);
184 values.put(CONTACT, contactUuid);
185 values.put(ACCOUNT, accountUuid);
186 values.put(CONTACTJID, contactJid);
187 values.put(CREATED, created);
188 values.put(STATUS, status);
189 values.put(MODE,mode);
190 return values;
191 }
192
193 public static Conversation fromCursor(Cursor cursor) {
194 return new Conversation(cursor.getString(cursor.getColumnIndex(UUID)),
195 cursor.getString(cursor.getColumnIndex(NAME)),
196 cursor.getString(cursor.getColumnIndex(CONTACT)),
197 cursor.getString(cursor.getColumnIndex(ACCOUNT)),
198 cursor.getString(cursor.getColumnIndex(CONTACTJID)),
199 cursor.getLong(cursor.getColumnIndex(CREATED)),
200 cursor.getInt(cursor.getColumnIndex(STATUS)),
201 cursor.getInt(cursor.getColumnIndex(MODE)));
202 }
203
204 public void setStatus(int status) {
205 this.status = status;
206 }
207
208 public int getMode() {
209 return this.mode;
210 }
211
212 public void setMode(int mode) {
213 this.mode = mode;
214 }
215
216 public void startOtrSession(Context context, String presence) {
217 Log.d("xmppService","starting otr session with "+presence);
218 SessionID sessionId = new SessionID(this.getContactJid(),presence,"xmpp");
219 this.otrSession = new SessionImpl(sessionId, getAccount().getOtrEngine(context));
220 try {
221 this.otrSession.startSession();
222 } catch (OtrException e) {
223 Log.d("xmppServic","couldnt start otr");
224 }
225 }
226
227 public SessionImpl getOtrSession() {
228 return this.otrSession;
229 }
230
231 public void resetOtrSession() {
232 this.otrSession = null;
233 }
234
235 public void endOtrIfNeeded() {
236 if (this.otrSession!=null) {
237 if (this.otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) {
238 try {
239 this.otrSession.endSession();
240 this.resetOtrSession();
241 } catch (OtrException e) {
242 this.resetOtrSession();
243 }
244 }
245 }
246 }
247
248 public boolean hasValidOtrSession() {
249 if (this.otrSession == null) {
250 return false;
251 } else {
252 String foreignPresence = this.otrSession.getSessionID().getUserID();
253 if (!getContact().getPresences().containsKey(foreignPresence)) {
254 this.resetOtrSession();
255 return false;
256 }
257 return true;
258 }
259 }
260
261 public String getOtrFingerprint() {
262 if (this.otrFingerprint == null) {
263 try {
264 DSAPublicKey remotePubKey = (DSAPublicKey) getOtrSession().getRemotePublicKey();
265 StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(remotePubKey));
266 builder.insert(8, " ");
267 builder.insert(17, " ");
268 builder.insert(26, " ");
269 builder.insert(35, " ");
270 this.otrFingerprint = builder.toString();
271 } catch (OtrCryptoException e) {
272
273 }
274 }
275 return this.otrFingerprint;
276 }
277
278 public MucOptions getMucOptions() {
279 if (this.mucOptions == null) {
280 this.mucOptions = new MucOptions();
281 }
282 this.mucOptions.setConversation(this);
283 return this.mucOptions ;
284 }
285
286 public void resetMucOptions() {
287 this.mucOptions = null;
288 }
289
290 public void setContactJid(String jid) {
291 this.contactJid = jid;
292 }
293}