Account.java

  1package eu.siacs.conversations.entities;
  2
  3import java.security.interfaces.DSAPublicKey;
  4
  5import net.java.otr4j.crypto.OtrCryptoEngineImpl;
  6import net.java.otr4j.crypto.OtrCryptoException;
  7
  8import org.json.JSONException;
  9import org.json.JSONObject;
 10
 11import eu.siacs.conversations.crypto.OtrEngine;
 12import eu.siacs.conversations.xmpp.XmppConnection;
 13import android.content.ContentValues;
 14import android.content.Context;
 15import android.database.Cursor;
 16
 17public class Account  extends AbstractEntity{
 18
 19	private static final long serialVersionUID = 6174825093869578035L;
 20	
 21	public static final String TABLENAME = "accounts";
 22	
 23	public static final String USERNAME = "username";
 24	public static final String SERVER = "server";
 25	public static final String PASSWORD = "password";
 26	public static final String OPTIONS = "options";
 27	public static final String ROSTERVERSION = "rosterversion";
 28	public static final String KEYS = "keys";
 29	
 30	public static final int OPTION_USETLS = 0;
 31	public static final int OPTION_DISABLED = 1;
 32	public static final int OPTION_REGISTER = 2;
 33	
 34	public static final int STATUS_CONNECTING = 0;
 35	public static final int STATUS_DISABLED = -2;
 36	public static final int STATUS_OFFLINE = -1;
 37	public static final int STATUS_ONLINE = 1;
 38	public static final int STATUS_UNAUTHORIZED = 2;
 39	public static final int STATUS_NO_INTERNET = 3;
 40	public static final int STATUS_TLS_ERROR = 4;
 41	public static final int STATUS_SERVER_NOT_FOUND = 5;
 42
 43	public static final int STATUS_SERVER_REQUIRES_TLS = 6;
 44	
 45	protected String username;
 46	protected String server;
 47	protected String password;
 48	protected int options = 0;
 49	protected String rosterVersion;
 50	protected String resource;
 51	protected int status = -1;
 52	protected JSONObject keys = new JSONObject();
 53	
 54	protected boolean online = false;
 55	
 56	transient OtrEngine otrEngine = null;
 57	transient XmppConnection xmppConnection = null;
 58
 59	private String otrFingerprint;
 60	
 61	public Account() {
 62		this.uuid = "0";
 63	}
 64	
 65	public Account(String username, String server, String password) {
 66		this(java.util.UUID.randomUUID().toString(),username,server,password,0,null,"");
 67	}
 68	public Account(String uuid, String username, String server,String password, int options, String rosterVersion, String keys) {
 69		this.uuid = uuid;
 70		this.username = username;
 71		this.server = server;
 72		this.password = password;
 73		this.options = options;
 74		this.rosterVersion = rosterVersion;
 75		try {
 76			this.keys = new JSONObject(keys);
 77		} catch (JSONException e) {
 78			
 79		}
 80	}
 81	
 82	public boolean isOptionSet(int option) {
 83		return ((options & (1 << option)) != 0);
 84	}
 85	
 86	public void setOption(int option, boolean value) {
 87		if (value) {
 88			this.options |= 1 << option;
 89		} else {
 90			this.options &= ~(1 << option);
 91		}
 92	}
 93	
 94	public String getUsername() {
 95		return username;
 96	}
 97
 98	public void setUsername(String username) {
 99		this.username = username;
100	}
101
102	public String getServer() {
103		return server;
104	}
105
106	public void setServer(String server) {
107		this.server = server;
108	}
109
110	public String getPassword() {
111		return password;
112	}
113
114	public void setPassword(String password) {
115		this.password = password;
116	}
117	
118	public void setStatus(int status) {
119		this.status = status;
120	}
121	
122	public int getStatus() {
123		if (isOptionSet(OPTION_DISABLED)) {
124			return STATUS_DISABLED;
125		} else {
126			return this.status;
127		}
128	}
129	
130	public void setResource(String resource) {
131		this.resource = resource;
132	}
133	
134	public String getJid() {
135		return username+"@"+server;
136	}
137	
138	public JSONObject getKeys() {
139		return keys;
140	}
141	
142	public String getSSLFingerprint() {
143		if (keys.has("ssl_cert")) {
144			try {
145				return keys.getString("ssl_cert");
146			} catch (JSONException e) {
147				return null;
148			}
149		} else {
150			return null;
151		}
152	}
153	
154	public void setSSLCertFingerprint(String fingerprint) {
155		this.setKey("ssl_cert", fingerprint);
156	}
157	
158	public boolean setKey(String keyName, String keyValue) {
159		try {
160			this.keys.put(keyName, keyValue);
161			return true;
162		} catch (JSONException e) {
163			return false;
164		}
165	}
166
167	@Override
168	public ContentValues getContentValues() {
169		ContentValues values = new ContentValues();
170		values.put(UUID,uuid);
171		values.put(USERNAME, username);
172		values.put(SERVER, server);
173		values.put(PASSWORD, password);
174		values.put(OPTIONS,options);
175		values.put(KEYS,this.keys.toString());
176		values.put(ROSTERVERSION,rosterVersion);
177		return values;
178	}
179	
180	public static Account fromCursor(Cursor cursor) {
181		return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
182				cursor.getString(cursor.getColumnIndex(USERNAME)),
183				cursor.getString(cursor.getColumnIndex(SERVER)),
184				cursor.getString(cursor.getColumnIndex(PASSWORD)),
185				cursor.getInt(cursor.getColumnIndex(OPTIONS)),
186				cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
187				cursor.getString(cursor.getColumnIndex(KEYS))
188				);
189	}
190
191	
192	public OtrEngine getOtrEngine(Context context) {
193		if (otrEngine==null) {
194			otrEngine = new OtrEngine(context,this);
195		}
196		return this.otrEngine;
197	}
198
199	public XmppConnection getXmppConnection() {
200		return this.xmppConnection;
201	}
202
203	public void setXmppConnection(XmppConnection connection) {
204		this.xmppConnection = connection;
205	}
206
207	public String getFullJid() {
208		return this.getJid()+"/"+this.resource;
209	}
210	
211	public String getOtrFingerprint() {
212		if (this.otrFingerprint == null) {
213			try {
214				DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine.getPublicKey();
215				StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(pubkey));
216				builder.insert(8, " ");
217				builder.insert(17, " ");
218				builder.insert(26, " ");
219				builder.insert(35, " ");
220				this.otrFingerprint = builder.toString();
221			} catch (OtrCryptoException e) {
222				
223			}
224		}
225		return this.otrFingerprint;
226	}
227
228	public String getRosterVersion() {
229		if (this.rosterVersion==null) {
230			return "";
231		} else {
232			return this.rosterVersion;
233		}
234	}
235	
236	public void setRosterVersion(String version) {
237		this.rosterVersion = version;
238	}
239}