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