1package eu.siacs.conversations.entities;
2
3import java.security.interfaces.DSAPublicKey;
4import java.util.List;
5import java.util.Locale;
6import java.util.concurrent.CopyOnWriteArrayList;
7
8import net.java.otr4j.crypto.OtrCryptoEngineImpl;
9import net.java.otr4j.crypto.OtrCryptoException;
10
11import org.json.JSONException;
12import org.json.JSONObject;
13
14import eu.siacs.conversations.crypto.OtrEngine;
15import eu.siacs.conversations.persistance.FileBackend;
16import eu.siacs.conversations.utils.UIHelper;
17import eu.siacs.conversations.xmpp.XmppConnection;
18import android.content.ContentValues;
19import android.content.Context;
20import android.database.Cursor;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23
24public class Account extends AbstractEntity{
25
26 public static final String TABLENAME = "accounts";
27
28 public static final String USERNAME = "username";
29 public static final String SERVER = "server";
30 public static final String PASSWORD = "password";
31 public static final String OPTIONS = "options";
32 public static final String ROSTERVERSION = "rosterversion";
33 public static final String KEYS = "keys";
34
35 public static final int OPTION_USETLS = 0;
36 public static final int OPTION_DISABLED = 1;
37 public static final int OPTION_REGISTER = 2;
38 public static final int OPTION_USECOMPRESSION = 3;
39
40 public static final int STATUS_CONNECTING = 0;
41 public static final int STATUS_DISABLED = -2;
42 public static final int STATUS_OFFLINE = -1;
43 public static final int STATUS_ONLINE = 1;
44 public static final int STATUS_NO_INTERNET = 2;
45 public static final int STATUS_UNAUTHORIZED = 3;
46 public static final int STATUS_SERVER_NOT_FOUND = 5;
47
48 public static final int STATUS_SERVER_REQUIRES_TLS = 6;
49
50 public static final int STATUS_REGISTRATION_FAILED = 7;
51 public static final int STATUS_REGISTRATION_CONFLICT = 8;
52 public static final int STATUS_REGISTRATION_SUCCESSFULL = 9;
53 public static final int STATUS_REGISTRATION_NOT_SUPPORTED = 10;
54
55 protected String username;
56 protected String server;
57 protected String password;
58 protected int options = 0;
59 protected String rosterVersion;
60 protected String resource = "mobile";
61 protected int status = -1;
62 protected JSONObject keys = new JSONObject();
63 protected String avatar;
64
65 protected boolean online = false;
66
67 transient OtrEngine otrEngine = null;
68 transient XmppConnection xmppConnection = null;
69 transient protected Presences presences = new Presences();
70
71 private String otrFingerprint;
72
73 private Roster roster = null;
74
75 private List<Bookmark> bookmarks = new CopyOnWriteArrayList<Bookmark>();
76
77 public List<Conversation> pendingConferenceJoins = new CopyOnWriteArrayList<Conversation>();
78 public List<Conversation> pendingConferenceLeaves = new CopyOnWriteArrayList<Conversation>();
79
80 public Account() {
81 this.uuid = "0";
82 }
83
84 public Account(String username, String server, String password) {
85 this(java.util.UUID.randomUUID().toString(),username,server,password,0,null,"");
86 }
87 public Account(String uuid, String username, String server,String password, int options, String rosterVersion, String keys) {
88 this.uuid = uuid;
89 this.username = username;
90 this.server = server;
91 this.password = password;
92 this.options = options;
93 this.rosterVersion = rosterVersion;
94 try {
95 this.keys = new JSONObject(keys);
96 } catch (JSONException e) {
97
98 }
99 }
100
101 public boolean isOptionSet(int option) {
102 return ((options & (1 << option)) != 0);
103 }
104
105 public void setOption(int option, boolean value) {
106 if (value) {
107 this.options |= 1 << option;
108 } else {
109 this.options &= ~(1 << option);
110 }
111 }
112
113 public String getUsername() {
114 return username;
115 }
116
117 public void setUsername(String username) {
118 this.username = username;
119 }
120
121 public String getServer() {
122 return server;
123 }
124
125 public void setServer(String server) {
126 this.server = server;
127 }
128
129 public String getPassword() {
130 return password;
131 }
132
133 public void setPassword(String password) {
134 this.password = password;
135 }
136
137 public void setStatus(int status) {
138 this.status = status;
139 }
140
141 public int getStatus() {
142 if (isOptionSet(OPTION_DISABLED)) {
143 return STATUS_DISABLED;
144 } else {
145 return this.status;
146 }
147 }
148
149 public boolean hasErrorStatus() {
150 return getStatus() > STATUS_NO_INTERNET && (getXmppConnection().getAttempt() >= 2);
151 }
152
153 public void setResource(String resource) {
154 this.resource = resource;
155 }
156
157 public String getResource() {
158 return this.resource;
159 }
160
161 public String getJid() {
162 return username.toLowerCase(Locale.getDefault())+"@"+server.toLowerCase(Locale.getDefault());
163 }
164
165 public JSONObject getKeys() {
166 return keys;
167 }
168
169 public String getSSLFingerprint() {
170 if (keys.has("ssl_cert")) {
171 try {
172 return keys.getString("ssl_cert");
173 } catch (JSONException e) {
174 return null;
175 }
176 } else {
177 return null;
178 }
179 }
180
181 public void setSSLCertFingerprint(String fingerprint) {
182 this.setKey("ssl_cert", fingerprint);
183 }
184
185 public boolean setKey(String keyName, String keyValue) {
186 try {
187 this.keys.put(keyName, keyValue);
188 return true;
189 } catch (JSONException e) {
190 return false;
191 }
192 }
193
194 @Override
195 public ContentValues getContentValues() {
196 ContentValues values = new ContentValues();
197 values.put(UUID,uuid);
198 values.put(USERNAME, username);
199 values.put(SERVER, server);
200 values.put(PASSWORD, password);
201 values.put(OPTIONS,options);
202 values.put(KEYS,this.keys.toString());
203 values.put(ROSTERVERSION,rosterVersion);
204 return values;
205 }
206
207 public static Account fromCursor(Cursor cursor) {
208 return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
209 cursor.getString(cursor.getColumnIndex(USERNAME)),
210 cursor.getString(cursor.getColumnIndex(SERVER)),
211 cursor.getString(cursor.getColumnIndex(PASSWORD)),
212 cursor.getInt(cursor.getColumnIndex(OPTIONS)),
213 cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
214 cursor.getString(cursor.getColumnIndex(KEYS))
215 );
216 }
217
218
219 public OtrEngine getOtrEngine(Context context) {
220 if (otrEngine==null) {
221 otrEngine = new OtrEngine(context,this);
222 }
223 return this.otrEngine;
224 }
225
226 public XmppConnection getXmppConnection() {
227 return this.xmppConnection;
228 }
229
230 public void setXmppConnection(XmppConnection connection) {
231 this.xmppConnection = connection;
232 }
233
234 public String getFullJid() {
235 return this.getJid()+"/"+this.resource;
236 }
237
238 public String getOtrFingerprint() {
239 if (this.otrFingerprint == null) {
240 try {
241 DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine.getPublicKey();
242 if (pubkey == null) {
243 return null;
244 }
245 StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(pubkey));
246 builder.insert(8, " ");
247 builder.insert(17, " ");
248 builder.insert(26, " ");
249 builder.insert(35, " ");
250 this.otrFingerprint = builder.toString();
251 } catch (OtrCryptoException e) {
252
253 }
254 }
255 return this.otrFingerprint;
256 }
257
258 public String getRosterVersion() {
259 if (this.rosterVersion==null) {
260 return "";
261 } else {
262 return this.rosterVersion;
263 }
264 }
265
266 public void setRosterVersion(String version) {
267 this.rosterVersion = version;
268 }
269
270 public String getOtrFingerprint(Context applicationContext) {
271 this.getOtrEngine(applicationContext);
272 return this.getOtrFingerprint();
273 }
274
275 public void updatePresence(String resource, int status) {
276 this.presences.updatePresence(resource, status);
277 }
278
279 public void removePresence(String resource) {
280 this.presences.removePresence(resource);
281 }
282
283 public void clearPresences() {
284 this.presences = new Presences();
285 }
286
287 public int countPresences() {
288 return this.presences.size();
289 }
290
291 public String getPgpSignature() {
292 if (keys.has("pgp_signature")) {
293 try {
294 return keys.getString("pgp_signature");
295 } catch (JSONException e) {
296 return null;
297 }
298 } else {
299 return null;
300 }
301 }
302
303 public Roster getRoster() {
304 if (this.roster==null) {
305 this.roster = new Roster(this);
306 }
307 return this.roster;
308 }
309
310 public void setBookmarks(List<Bookmark> bookmarks) {
311 this.bookmarks = bookmarks;
312 }
313
314 public List<Bookmark> getBookmarks() {
315 return this.bookmarks;
316 }
317
318 public boolean hasBookmarkFor(String conferenceJid) {
319 for(Bookmark bmark : this.bookmarks) {
320 if (bmark.getJid().equals(conferenceJid)) {
321 return true;
322 }
323 }
324 return false;
325 }
326
327 public Bitmap getImage(Context context, int size) {
328 if (this.avatar!=null) {
329 Bitmap bm = BitmapFactory.decodeFile(FileBackend.getAvatarPath(context, avatar));
330 if (bm==null) {
331 return UIHelper.getContactPicture(getJid(), size, context, false);
332 } else {
333 return bm;
334 }
335 } else {
336 return UIHelper.getContactPicture(getJid(), size, context, false);
337 }
338 }
339
340 public void setAvatar(String filename) {
341 this.avatar = filename;
342 }
343
344 public String getAvatar() {
345 return this.avatar;
346 }
347}