1package eu.siacs.conversations.entities;
2
3import java.security.interfaces.DSAPublicKey;
4import java.util.List;
5import java.util.Locale;
6import java.util.concurrent.CopyOnWriteArrayList;
7
8import net.java.otr4j.crypto.OtrCryptoEngineImpl;
9import net.java.otr4j.crypto.OtrCryptoException;
10
11import org.json.JSONException;
12import org.json.JSONObject;
13
14import eu.siacs.conversations.crypto.OtrEngine;
15import eu.siacs.conversations.persistance.FileBackend;
16import eu.siacs.conversations.utils.UIHelper;
17import eu.siacs.conversations.xmpp.XmppConnection;
18import android.content.ContentValues;
19import android.content.Context;
20import android.database.Cursor;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23
24public class Account extends AbstractEntity{
25
26 public static final String TABLENAME = "accounts";
27
28 public static final String USERNAME = "username";
29 public static final String SERVER = "server";
30 public static final String PASSWORD = "password";
31 public static final String OPTIONS = "options";
32 public static final String ROSTERVERSION = "rosterversion";
33 public static final String KEYS = "keys";
34
35 public static final int OPTION_USETLS = 0;
36 public static final int OPTION_DISABLED = 1;
37 public static final int OPTION_REGISTER = 2;
38 public static final int OPTION_USECOMPRESSION = 3;
39
40 public static final int STATUS_CONNECTING = 0;
41 public static final int STATUS_DISABLED = -2;
42 public static final int STATUS_OFFLINE = -1;
43 public static final int STATUS_ONLINE = 1;
44 public static final int STATUS_NO_INTERNET = 2;
45 public static final int STATUS_UNAUTHORIZED = 3;
46 public static final int STATUS_SERVER_NOT_FOUND = 5;
47
48 public static final int STATUS_REGISTRATION_FAILED = 7;
49 public static final int STATUS_REGISTRATION_CONFLICT = 8;
50 public static final int STATUS_REGISTRATION_SUCCESSFULL = 9;
51 public static final int STATUS_REGISTRATION_NOT_SUPPORTED = 10;
52
53 protected String username;
54 protected String server;
55 protected String password;
56 protected int options = 0;
57 protected String rosterVersion;
58 protected String resource = "mobile";
59 protected int status = -1;
60 protected JSONObject keys = new JSONObject();
61 protected String avatar;
62
63 protected boolean online = false;
64
65 transient OtrEngine otrEngine = null;
66 transient XmppConnection xmppConnection = null;
67 transient protected Presences presences = new Presences();
68
69 private String otrFingerprint;
70
71 private Roster roster = null;
72
73 private List<Bookmark> bookmarks = new CopyOnWriteArrayList<Bookmark>();
74
75 public List<Conversation> pendingConferenceJoins = new CopyOnWriteArrayList<Conversation>();
76 public List<Conversation> pendingConferenceLeaves = new CopyOnWriteArrayList<Conversation>();
77
78 public Account() {
79 this.uuid = "0";
80 }
81
82 public Account(String username, String server, String password) {
83 this(java.util.UUID.randomUUID().toString(),username,server,password,0,null,"");
84 }
85 public Account(String uuid, String username, String server,String password, int options, String rosterVersion, String keys) {
86 this.uuid = uuid;
87 this.username = username;
88 this.server = server;
89 this.password = password;
90 this.options = options;
91 this.rosterVersion = rosterVersion;
92 try {
93 this.keys = new JSONObject(keys);
94 } catch (JSONException e) {
95
96 }
97 }
98
99 public boolean isOptionSet(int option) {
100 return ((options & (1 << option)) != 0);
101 }
102
103 public void setOption(int option, boolean value) {
104 if (value) {
105 this.options |= 1 << option;
106 } else {
107 this.options &= ~(1 << option);
108 }
109 }
110
111 public String getUsername() {
112 return username;
113 }
114
115 public void setUsername(String username) {
116 this.username = username;
117 }
118
119 public String getServer() {
120 return server;
121 }
122
123 public void setServer(String server) {
124 this.server = server;
125 }
126
127 public String getPassword() {
128 return password;
129 }
130
131 public void setPassword(String password) {
132 this.password = password;
133 }
134
135 public void setStatus(int status) {
136 this.status = status;
137 }
138
139 public int getStatus() {
140 if (isOptionSet(OPTION_DISABLED)) {
141 return STATUS_DISABLED;
142 } else {
143 return this.status;
144 }
145 }
146
147 public boolean errorStatus() {
148 int s = getStatus();
149 return (s == STATUS_OFFLINE || s == STATUS_SERVER_NOT_FOUND || s == STATUS_UNAUTHORIZED);
150 }
151
152 public boolean hasErrorStatus() {
153 return getStatus() > STATUS_NO_INTERNET && (getXmppConnection().getAttempt() >= 2);
154 }
155
156 public void setResource(String resource) {
157 this.resource = resource;
158 }
159
160 public String getResource() {
161 return this.resource;
162 }
163
164 public String getJid() {
165 return username.toLowerCase(Locale.getDefault())+"@"+server.toLowerCase(Locale.getDefault());
166 }
167
168 public JSONObject getKeys() {
169 return keys;
170 }
171
172 public String getSSLFingerprint() {
173 if (keys.has("ssl_cert")) {
174 try {
175 return keys.getString("ssl_cert");
176 } catch (JSONException e) {
177 return null;
178 }
179 } else {
180 return null;
181 }
182 }
183
184 public void setSSLCertFingerprint(String fingerprint) {
185 this.setKey("ssl_cert", fingerprint);
186 }
187
188 public boolean setKey(String keyName, String keyValue) {
189 try {
190 this.keys.put(keyName, keyValue);
191 return true;
192 } catch (JSONException e) {
193 return false;
194 }
195 }
196
197 @Override
198 public ContentValues getContentValues() {
199 ContentValues values = new ContentValues();
200 values.put(UUID,uuid);
201 values.put(USERNAME, username);
202 values.put(SERVER, server);
203 values.put(PASSWORD, password);
204 values.put(OPTIONS,options);
205 values.put(KEYS,this.keys.toString());
206 values.put(ROSTERVERSION,rosterVersion);
207 return values;
208 }
209
210 public static Account fromCursor(Cursor cursor) {
211 return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
212 cursor.getString(cursor.getColumnIndex(USERNAME)),
213 cursor.getString(cursor.getColumnIndex(SERVER)),
214 cursor.getString(cursor.getColumnIndex(PASSWORD)),
215 cursor.getInt(cursor.getColumnIndex(OPTIONS)),
216 cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
217 cursor.getString(cursor.getColumnIndex(KEYS))
218 );
219 }
220
221
222 public OtrEngine getOtrEngine(Context context) {
223 if (otrEngine==null) {
224 otrEngine = new OtrEngine(context,this);
225 }
226 return this.otrEngine;
227 }
228
229 public XmppConnection getXmppConnection() {
230 return this.xmppConnection;
231 }
232
233 public void setXmppConnection(XmppConnection connection) {
234 this.xmppConnection = connection;
235 }
236
237 public String getFullJid() {
238 return this.getJid()+"/"+this.resource;
239 }
240
241 public String getOtrFingerprint() {
242 if (this.otrFingerprint == null) {
243 try {
244 DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine.getPublicKey();
245 if (pubkey == null) {
246 return null;
247 }
248 StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(pubkey));
249 builder.insert(8, " ");
250 builder.insert(17, " ");
251 builder.insert(26, " ");
252 builder.insert(35, " ");
253 this.otrFingerprint = builder.toString();
254 } catch (OtrCryptoException e) {
255
256 }
257 }
258 return this.otrFingerprint;
259 }
260
261 public String getRosterVersion() {
262 if (this.rosterVersion==null) {
263 return "";
264 } else {
265 return this.rosterVersion;
266 }
267 }
268
269 public void setRosterVersion(String version) {
270 this.rosterVersion = version;
271 }
272
273 public String getOtrFingerprint(Context applicationContext) {
274 this.getOtrEngine(applicationContext);
275 return this.getOtrFingerprint();
276 }
277
278 public void updatePresence(String resource, int status) {
279 this.presences.updatePresence(resource, status);
280 }
281
282 public void removePresence(String resource) {
283 this.presences.removePresence(resource);
284 }
285
286 public void clearPresences() {
287 this.presences = new Presences();
288 }
289
290 public int countPresences() {
291 return this.presences.size();
292 }
293
294 public String getPgpSignature() {
295 if (keys.has("pgp_signature")) {
296 try {
297 return keys.getString("pgp_signature");
298 } catch (JSONException e) {
299 return null;
300 }
301 } else {
302 return null;
303 }
304 }
305
306 public Roster getRoster() {
307 if (this.roster==null) {
308 this.roster = new Roster(this);
309 }
310 return this.roster;
311 }
312
313 public void setBookmarks(List<Bookmark> bookmarks) {
314 this.bookmarks = bookmarks;
315 }
316
317 public List<Bookmark> getBookmarks() {
318 return this.bookmarks;
319 }
320
321 public boolean hasBookmarkFor(String conferenceJid) {
322 for(Bookmark bmark : this.bookmarks) {
323 if (bmark.getJid().equals(conferenceJid)) {
324 return true;
325 }
326 }
327 return false;
328 }
329
330 public Bitmap getImage(Context context, int size) {
331 if (this.avatar!=null) {
332 Bitmap bm = FileBackend.getAvatar(this.avatar, size, context);
333 if (bm==null) {
334 return UIHelper.getContactPicture(getJid(), size, context, false);
335 } else {
336 return bm;
337 }
338 } else {
339 return UIHelper.getContactPicture(getJid(), size, context, false);
340 }
341 }
342
343 public void setAvatar(String filename) {
344 this.avatar = filename;
345 }
346
347 public String getAvatar() {
348 return this.avatar;
349 }
350}