1package eu.siacs.conversations.entities;
2
3import java.util.HashSet;
4import java.util.Locale;
5import java.util.Set;
6
7import org.json.JSONArray;
8import org.json.JSONException;
9import org.json.JSONObject;
10
11import eu.siacs.conversations.xml.Element;
12import android.content.ContentValues;
13import android.database.Cursor;
14
15public class Contact implements ListItem {
16 public static final String TABLENAME = "contacts";
17
18 public static final String SYSTEMNAME = "systemname";
19 public static final String SERVERNAME = "servername";
20 public static final String JID = "jid";
21 public static final String OPTIONS = "options";
22 public static final String SYSTEMACCOUNT = "systemaccount";
23 public static final String PHOTOURI = "photouri";
24 public static final String KEYS = "pgpkey";
25 public static final String ACCOUNT = "accountUuid";
26
27 protected String accountUuid;
28 protected String systemName;
29 protected String serverName;
30 protected String jid;
31 protected int subscription = 0;
32 protected String systemAccount;
33 protected String photoUri;
34 protected JSONObject keys = new JSONObject();
35 protected Presences presences = new Presences();
36
37 protected Account account;
38
39 protected boolean inRoster = true;
40
41 public Lastseen lastseen = new Lastseen();
42
43 public Contact(String account, String systemName, String serverName,
44 String jid, int subscription, String photoUri,
45 String systemAccount, String keys) {
46 this.accountUuid = account;
47 this.systemName = systemName;
48 this.serverName = serverName;
49 this.jid = jid;
50 this.subscription = subscription;
51 this.photoUri = photoUri;
52 this.systemAccount = systemAccount;
53 if (keys == null) {
54 keys = "";
55 }
56 try {
57 this.keys = new JSONObject(keys);
58 } catch (JSONException e) {
59 this.keys = new JSONObject();
60 }
61 }
62
63 public Contact(String jid) {
64 this.jid = jid;
65 }
66
67 public String getDisplayName() {
68 if (this.systemName != null) {
69 return this.systemName;
70 } else if (this.serverName != null) {
71 return this.serverName;
72 } else {
73 return this.jid.split("@")[0];
74 }
75 }
76
77 public String getProfilePhoto() {
78 return this.photoUri;
79 }
80
81 public String getJid() {
82 return this.jid.toLowerCase(Locale.getDefault());
83 }
84
85 public boolean match(String needle) {
86 return needle == null
87 || jid.contains(needle.toLowerCase())
88 || getDisplayName().toLowerCase()
89 .contains(needle.toLowerCase());
90 }
91
92 public ContentValues getContentValues() {
93 ContentValues values = new ContentValues();
94 values.put(ACCOUNT, accountUuid);
95 values.put(SYSTEMNAME, systemName);
96 values.put(SERVERNAME, serverName);
97 values.put(JID, jid);
98 values.put(OPTIONS, subscription);
99 values.put(SYSTEMACCOUNT, systemAccount);
100 values.put(PHOTOURI, photoUri);
101 values.put(KEYS, keys.toString());
102 return values;
103 }
104
105 public static Contact fromCursor(Cursor cursor) {
106 return new Contact(cursor.getString(cursor.getColumnIndex(ACCOUNT)),
107 cursor.getString(cursor.getColumnIndex(SYSTEMNAME)),
108 cursor.getString(cursor.getColumnIndex(SERVERNAME)),
109 cursor.getString(cursor.getColumnIndex(JID)),
110 cursor.getInt(cursor.getColumnIndex(OPTIONS)),
111 cursor.getString(cursor.getColumnIndex(PHOTOURI)),
112 cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)),
113 cursor.getString(cursor.getColumnIndex(KEYS)));
114 }
115
116 public int getSubscription() {
117 return this.subscription;
118 }
119
120 public void setSystemAccount(String account) {
121 this.systemAccount = account;
122 }
123
124 public void setAccount(Account account) {
125 this.account = account;
126 this.accountUuid = account.getUuid();
127 }
128
129 public Account getAccount() {
130 return this.account;
131 }
132
133 public boolean couldBeMuc() {
134 String[] split = this.getJid().split("@");
135 if (split.length != 2) {
136 return false;
137 } else {
138 String[] domainParts = split[1].split("\\.");
139 if (domainParts.length < 3) {
140 return false;
141 } else {
142 return (domainParts[0].equals("conf")
143 || domainParts[0].equals("conference")
144 || domainParts[0].equals("room")
145 || domainParts[0].equals("muc")
146 || domainParts[0].equals("chat")
147 || domainParts[0].equals("sala") || domainParts[0]
148 .equals("salas"));
149 }
150 }
151 }
152
153 public Presences getPresences() {
154 return this.presences;
155 }
156
157 public void updatePresence(String resource, int status) {
158 this.presences.updatePresence(resource, status);
159 }
160
161 public void removePresence(String resource) {
162 this.presences.removePresence(resource);
163 }
164
165 public void clearPresences() {
166 this.presences.clearPresences();
167 }
168
169 public int getMostAvailableStatus() {
170 return this.presences.getMostAvailableStatus();
171 }
172
173 public void setPresences(Presences pres) {
174 this.presences = pres;
175 }
176
177 public void setPhotoUri(String uri) {
178 this.photoUri = uri;
179 }
180
181 public void setServerName(String serverName) {
182 this.serverName = serverName;
183 }
184
185 public void setSystemName(String systemName) {
186 this.systemName = systemName;
187 }
188
189 public String getSystemAccount() {
190 return systemAccount;
191 }
192
193 public Set<String> getOtrFingerprints() {
194 Set<String> set = new HashSet<String>();
195 try {
196 if (this.keys.has("otr_fingerprints")) {
197 JSONArray fingerprints = this.keys
198 .getJSONArray("otr_fingerprints");
199 for (int i = 0; i < fingerprints.length(); ++i) {
200 set.add(fingerprints.getString(i));
201 }
202 }
203 } catch (JSONException e) {
204 // TODO Auto-generated catch block
205 e.printStackTrace();
206 }
207 return set;
208 }
209
210 public void addOtrFingerprint(String print) {
211 try {
212 JSONArray fingerprints;
213 if (!this.keys.has("otr_fingerprints")) {
214 fingerprints = new JSONArray();
215
216 } else {
217 fingerprints = this.keys.getJSONArray("otr_fingerprints");
218 }
219 fingerprints.put(print);
220 this.keys.put("otr_fingerprints", fingerprints);
221 } catch (JSONException e) {
222
223 }
224 }
225
226 public void setPgpKeyId(long keyId) {
227 try {
228 this.keys.put("pgp_keyid", keyId);
229 } catch (JSONException e) {
230
231 }
232 }
233
234 public long getPgpKeyId() {
235 if (this.keys.has("pgp_keyid")) {
236 try {
237 return this.keys.getLong("pgp_keyid");
238 } catch (JSONException e) {
239 return 0;
240 }
241 } else {
242 return 0;
243 }
244 }
245
246 public void setOption(int option) {
247 this.subscription |= 1 << option;
248 }
249
250 public void resetOption(int option) {
251 this.subscription &= ~(1 << option);
252 }
253
254 public boolean getOption(int option) {
255 return ((this.subscription & (1 << option)) != 0);
256 }
257
258 public boolean showInRoster() {
259 return (this.getOption(Contact.Options.IN_ROSTER) && (!this
260 .getOption(Contact.Options.DIRTY_DELETE)))
261 || (this.getOption(Contact.Options.DIRTY_PUSH));
262 }
263
264 public void parseSubscriptionFromElement(Element item) {
265 String ask = item.getAttribute("ask");
266 String subscription = item.getAttribute("subscription");
267
268 if (subscription != null) {
269 if (subscription.equals("to")) {
270 this.resetOption(Contact.Options.FROM);
271 this.setOption(Contact.Options.TO);
272 } else if (subscription.equals("from")) {
273 this.resetOption(Contact.Options.TO);
274 this.setOption(Contact.Options.FROM);
275 this.resetOption(Contact.Options.PREEMPTIVE_GRANT);
276 } else if (subscription.equals("both")) {
277 this.setOption(Contact.Options.TO);
278 this.setOption(Contact.Options.FROM);
279 this.resetOption(Contact.Options.PREEMPTIVE_GRANT);
280 } else if (subscription.equals("none")) {
281 this.resetOption(Contact.Options.FROM);
282 this.resetOption(Contact.Options.TO);
283 }
284 }
285
286 // do NOT override asking if pending push request
287 if (!this.getOption(Contact.Options.DIRTY_PUSH)) {
288 if ((ask != null) && (ask.equals("subscribe"))) {
289 this.setOption(Contact.Options.ASKING);
290 } else {
291 this.resetOption(Contact.Options.ASKING);
292 }
293 }
294 }
295
296 public Element asElement() {
297 Element item = new Element("item");
298 item.setAttribute("jid", this.jid);
299 if (this.serverName != null) {
300 item.setAttribute("name", this.serverName);
301 }
302 return item;
303 }
304
305 public class Options {
306 public static final int TO = 0;
307 public static final int FROM = 1;
308 public static final int ASKING = 2;
309 public static final int PREEMPTIVE_GRANT = 3;
310 public static final int IN_ROSTER = 4;
311 public static final int PENDING_SUBSCRIPTION_REQUEST = 5;
312 public static final int DIRTY_PUSH = 6;
313 public static final int DIRTY_DELETE = 7;
314 }
315
316 public class Lastseen {
317 public long time = 0;
318 public String presence = null;
319 }
320
321 @Override
322 public int compareTo(ListItem another) {
323 return this.getDisplayName().compareToIgnoreCase(another.getDisplayName());
324 }
325
326 public String getServer() {
327 String[] split = getJid().split("@");
328 if (split.length >= 2) {
329 return split[1];
330 } else {
331 return null;
332 }
333 }
334}