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