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;
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 getJid() {
141 return username+"@"+server;
142 }
143
144 public JSONObject getKeys() {
145 return keys;
146 }
147
148 public String getSSLFingerprint() {
149 if (keys.has("ssl_cert")) {
150 try {
151 return keys.getString("ssl_cert");
152 } catch (JSONException e) {
153 return null;
154 }
155 } else {
156 return null;
157 }
158 }
159
160 public void setSSLCertFingerprint(String fingerprint) {
161 this.setKey("ssl_cert", fingerprint);
162 }
163
164 public boolean setKey(String keyName, String keyValue) {
165 try {
166 this.keys.put(keyName, keyValue);
167 return true;
168 } catch (JSONException e) {
169 return false;
170 }
171 }
172
173 @Override
174 public ContentValues getContentValues() {
175 ContentValues values = new ContentValues();
176 values.put(UUID,uuid);
177 values.put(USERNAME, username);
178 values.put(SERVER, server);
179 values.put(PASSWORD, password);
180 values.put(OPTIONS,options);
181 values.put(KEYS,this.keys.toString());
182 values.put(ROSTERVERSION,rosterVersion);
183 return values;
184 }
185
186 public static Account fromCursor(Cursor cursor) {
187 return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
188 cursor.getString(cursor.getColumnIndex(USERNAME)),
189 cursor.getString(cursor.getColumnIndex(SERVER)),
190 cursor.getString(cursor.getColumnIndex(PASSWORD)),
191 cursor.getInt(cursor.getColumnIndex(OPTIONS)),
192 cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
193 cursor.getString(cursor.getColumnIndex(KEYS))
194 );
195 }
196
197
198 public OtrEngine getOtrEngine(Context context) {
199 if (otrEngine==null) {
200 otrEngine = new OtrEngine(context,this);
201 }
202 return this.otrEngine;
203 }
204
205 public XmppConnection getXmppConnection() {
206 return this.xmppConnection;
207 }
208
209 public void setXmppConnection(XmppConnection connection) {
210 this.xmppConnection = connection;
211 }
212
213 public String getFullJid() {
214 return this.getJid()+"/"+this.resource;
215 }
216
217 public String getOtrFingerprint() {
218 if (this.otrFingerprint == null) {
219 try {
220 DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine.getPublicKey();
221 if (pubkey == null) {
222 return null;
223 }
224 StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(pubkey));
225 builder.insert(8, " ");
226 builder.insert(17, " ");
227 builder.insert(26, " ");
228 builder.insert(35, " ");
229 this.otrFingerprint = builder.toString();
230 } catch (OtrCryptoException e) {
231
232 }
233 }
234 return this.otrFingerprint;
235 }
236
237 public String getRosterVersion() {
238 if (this.rosterVersion==null) {
239 return "";
240 } else {
241 return this.rosterVersion;
242 }
243 }
244
245 public void setRosterVersion(String version) {
246 this.rosterVersion = version;
247 }
248
249 public String getOtrFingerprint(Context applicationContext) {
250 this.getOtrEngine(applicationContext);
251 return this.getOtrFingerprint();
252 }
253
254 public void updatePresence(String resource, int status) {
255 this.presences.updatePresence(resource, status);
256 }
257
258 public void removePresence(String resource) {
259 this.presences.removePresence(resource);
260 }
261
262 public void clearPresences() {
263 this.presences = new Presences();
264 }
265
266 public int countPresences() {
267 return this.presences.size();
268 }
269}