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