Account.java

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