1package de.gultsch.chat.entities;
2
3import java.security.interfaces.DSAPublicKey;
4import java.util.ArrayList;
5import java.util.List;
6
7import de.gultsch.chat.crypto.OtrEngine;
8import de.gultsch.chat.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 public Conversation(String name, Account account,
63 String contactJid, int mode) {
64 this(java.util.UUID.randomUUID().toString(), name, null, account.getUuid(), contactJid, System
65 .currentTimeMillis(), STATUS_AVAILABLE,mode);
66 this.account = account;
67 }
68
69 public Conversation(String uuid, String name, String contactUuid,
70 String accountUuid, String contactJid, long created, int status, int mode) {
71 this.uuid = uuid;
72 this.name = name;
73 this.contactUuid = contactUuid;
74 this.accountUuid = accountUuid;
75 this.contactJid = contactJid;
76 this.created = created;
77 this.status = status;
78 this.mode = mode;
79 }
80
81 public List<Message> getMessages() {
82 if (messages == null) this.messages = new ArrayList<Message>(); //prevent null pointer
83
84 //populate with Conversation (this)
85
86 for(Message msg : messages) {
87 msg.setConversation(this);
88 }
89
90 return messages;
91 }
92
93 public boolean isRead() {
94 if ((this.messages == null)||(this.messages.size() == 0)) return true;
95 return this.messages.get(this.messages.size() - 1).isRead();
96 }
97
98 public void markRead() {
99 if (this.messages == null) return;
100 for(int i = this.messages.size() -1; i >= 0; --i) {
101 if (messages.get(i).isRead()) return;
102 this.messages.get(i).markRead();
103 }
104 }
105
106 public Message getLatestMessage() {
107 if ((this.messages == null)||(this.messages.size()==0)) {
108 Message message = new Message(this,"",Message.ENCRYPTION_NONE);
109 message.setTime(0);
110 return message;
111 } else {
112 return this.messages.get(this.messages.size() - 1);
113 }
114 }
115
116 public void setMessages(List<Message> msgs) {
117 this.messages = msgs;
118 }
119
120 public String getName() {
121 if (this.contact!=null) {
122 return this.contact.getDisplayName();
123 } else {
124 return this.name;
125 }
126 }
127
128 public String getProfilePhotoString() {
129 if (this.contact==null) {
130 return null;
131 } else {
132 return this.contact.getProfilePhoto();
133 }
134 }
135
136 public String getAccountUuid() {
137 return this.accountUuid;
138 }
139
140 public Account getAccount() {
141 return this.account;
142 }
143
144 public Contact getContact() {
145 return this.contact;
146 }
147
148 public void setContact(Contact contact) {
149 this.contact = contact;
150 if (contact!=null) {
151 this.contactUuid = contact.getUuid();
152 }
153 }
154
155 public void setAccount(Account account) {
156 this.account = account;
157 }
158
159 public String getContactJid() {
160 return this.contactJid;
161 }
162
163 public Uri getProfilePhotoUri() {
164 if (this.getProfilePhotoString() != null) {
165 return Uri.parse(this.getProfilePhotoString());
166 }
167 return null;
168 }
169
170 public int getStatus() {
171 return this.status;
172 }
173
174 public long getCreated() {
175 return this.created;
176 }
177
178 public ContentValues getContentValues() {
179 ContentValues values = new ContentValues();
180 values.put(UUID, uuid);
181 values.put(NAME, name);
182 values.put(CONTACT, contactUuid);
183 values.put(ACCOUNT, accountUuid);
184 values.put(CONTACTJID, contactJid);
185 values.put(CREATED, created);
186 values.put(STATUS, status);
187 values.put(MODE,mode);
188 return values;
189 }
190
191 public static Conversation fromCursor(Cursor cursor) {
192 return new Conversation(cursor.getString(cursor.getColumnIndex(UUID)),
193 cursor.getString(cursor.getColumnIndex(NAME)),
194 cursor.getString(cursor.getColumnIndex(CONTACT)),
195 cursor.getString(cursor.getColumnIndex(ACCOUNT)),
196 cursor.getString(cursor.getColumnIndex(CONTACTJID)),
197 cursor.getLong(cursor.getColumnIndex(CREATED)),
198 cursor.getInt(cursor.getColumnIndex(STATUS)),
199 cursor.getInt(cursor.getColumnIndex(MODE)));
200 }
201
202 public void setStatus(int status) {
203 this.status = status;
204 }
205
206 public int getMode() {
207 return this.mode;
208 }
209
210 public void setMode(int mode) {
211 this.mode = mode;
212 }
213
214 public void startOtrSession(Context context, String presence) {
215 Log.d("xmppService","starting otr session with "+presence);
216 SessionID sessionId = new SessionID(this.getContactJid(),presence,"xmpp");
217 this.otrSession = new SessionImpl(sessionId, getAccount().getOtrEngine(context));
218 try {
219 this.otrSession.startSession();
220 } catch (OtrException e) {
221 Log.d("xmppServic","couldnt start otr");
222 }
223 }
224
225 public SessionImpl getOtrSession() {
226 return this.otrSession;
227 }
228
229 public void resetOtrSession() {
230 this.otrSession = null;
231 }
232
233 public void endOtrIfNeeded() throws OtrException {
234 if (this.otrSession!=null) {
235 if (this.otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) {
236 this.otrSession.endSession();
237 }
238 }
239 this.resetOtrSession();
240 }
241
242 public boolean hasValidOtrSession() {
243 if (this.otrSession == null) {
244 return false;
245 } else {
246 String foreignPresence = this.otrSession.getSessionID().getUserID();
247 if (!getContact().getPresences().containsKey(foreignPresence)) {
248 this.resetOtrSession();
249 return false;
250 }
251 return true;
252 }
253 }
254
255 public String getOtrFingerprint() {
256 if (this.otrFingerprint == null) {
257 try {
258 DSAPublicKey remotePubKey = (DSAPublicKey) getOtrSession().getRemotePublicKey();
259 StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(remotePubKey));
260 builder.insert(8, " ");
261 builder.insert(17, " ");
262 builder.insert(26, " ");
263 builder.insert(35, " ");
264 this.otrFingerprint = builder.toString();
265 } catch (OtrCryptoException e) {
266
267 }
268 }
269 return this.otrFingerprint;
270 }
271}