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