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