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