1package eu.siacs.conversations.entities;
2
3import android.annotation.SuppressLint;
4
5import java.util.ArrayList;
6import java.util.Collections;
7import java.util.HashSet;
8import java.util.List;
9import java.util.Set;
10
11import eu.siacs.conversations.R;
12import eu.siacs.conversations.xmpp.forms.Data;
13import eu.siacs.conversations.xmpp.forms.Field;
14import eu.siacs.conversations.xmpp.jid.InvalidJidException;
15import eu.siacs.conversations.xmpp.jid.Jid;
16import eu.siacs.conversations.xmpp.pep.Avatar;
17
18@SuppressLint("DefaultLocale")
19public class MucOptions {
20
21 public Account getAccount() {
22 return this.conversation.getAccount();
23 }
24
25 public void setSelf(User user) {
26 this.self = user;
27 }
28
29 public void changeAffiliation(Jid jid, Affiliation affiliation) {
30 User user = findUserByRealJid(jid);
31 if (user != null && user.getRole() == Role.NONE) {
32 users.remove(user);
33 if (affiliation.ranks(Affiliation.MEMBER)) {
34 user.affiliation = affiliation;
35 users.add(user);
36 }
37 }
38 }
39
40 public enum Affiliation {
41 OWNER("owner", 4, R.string.owner),
42 ADMIN("admin", 3, R.string.admin),
43 MEMBER("member", 2, R.string.member),
44 OUTCAST("outcast", 0, R.string.outcast),
45 NONE("none", 1, R.string.no_affiliation);
46
47 Affiliation(String string, int rank, int resId) {
48 this.string = string;
49 this.resId = resId;
50 this.rank = rank;
51 }
52
53 private String string;
54 private int resId;
55 private int rank;
56
57 public int getResId() {
58 return resId;
59 }
60
61 @Override
62 public String toString() {
63 return this.string;
64 }
65
66 public boolean outranks(Affiliation affiliation) {
67 return rank > affiliation.rank;
68 }
69
70 public boolean ranks(Affiliation affiliation) {
71 return rank >= affiliation.rank;
72 }
73 }
74
75 public enum Role {
76 MODERATOR("moderator", R.string.moderator,3),
77 VISITOR("visitor", R.string.visitor,1),
78 PARTICIPANT("participant", R.string.participant,2),
79 NONE("none", R.string.no_role,0);
80
81 Role(String string, int resId, int rank) {
82 this.string = string;
83 this.resId = resId;
84 this.rank = rank;
85 }
86
87 private String string;
88 private int resId;
89 private int rank;
90
91 public int getResId() {
92 return resId;
93 }
94
95 @Override
96 public String toString() {
97 return this.string;
98 }
99
100 public boolean ranks(Role role) {
101 return rank >= role.rank;
102 }
103 }
104
105 public enum Error {
106 NO_RESPONSE,
107 NONE,
108 NICK_IN_USE,
109 PASSWORD_REQUIRED,
110 BANNED,
111 MEMBERS_ONLY,
112 KICKED,
113 SHUTDOWN,
114 UNKNOWN
115 }
116
117 public static final String STATUS_CODE_ROOM_CONFIG_CHANGED = "104";
118 public static final String STATUS_CODE_SELF_PRESENCE = "110";
119 public static final String STATUS_CODE_BANNED = "301";
120 public static final String STATUS_CODE_CHANGED_NICK = "303";
121 public static final String STATUS_CODE_KICKED = "307";
122 public static final String STATUS_CODE_AFFILIATION_CHANGE = "321";
123 public static final String STATUS_CODE_LOST_MEMBERSHIP = "322";
124 public static final String STATUS_CODE_SHUTDOWN = "332";
125
126 private interface OnEventListener {
127 void onSuccess();
128
129 void onFailure();
130 }
131
132 public interface OnRenameListener extends OnEventListener {
133
134 }
135
136 public static class User implements Comparable<User> {
137 private Role role = Role.NONE;
138 private Affiliation affiliation = Affiliation.NONE;
139 private Jid realJid;
140 private Jid fullJid;
141 private long pgpKeyId = 0;
142 private Avatar avatar;
143 private MucOptions options;
144
145 public User(MucOptions options, Jid from) {
146 this.options = options;
147 this.fullJid = from;
148 }
149
150 public String getName() {
151 return fullJid == null ? null : fullJid.getResourcepart();
152 }
153
154 public void setRealJid(Jid jid) {
155 this.realJid = jid != null ? jid.toBareJid() : null;
156 }
157
158 public Jid getRealJid() {
159 return this.realJid;
160 }
161
162 public Role getRole() {
163 return this.role;
164 }
165
166 public void setRole(String role) {
167 if (role == null) {
168 this.role = Role.NONE;
169 return;
170 }
171 role = role.toLowerCase();
172 switch (role) {
173 case "moderator":
174 this.role = Role.MODERATOR;
175 break;
176 case "participant":
177 this.role = Role.PARTICIPANT;
178 break;
179 case "visitor":
180 this.role = Role.VISITOR;
181 break;
182 default:
183 this.role = Role.NONE;
184 break;
185 }
186 }
187
188 public Affiliation getAffiliation() {
189 return this.affiliation;
190 }
191
192 public void setAffiliation(String affiliation) {
193 if (affiliation == null) {
194 this.affiliation = Affiliation.NONE;
195 return;
196 }
197 affiliation = affiliation.toLowerCase();
198 switch (affiliation) {
199 case "admin":
200 this.affiliation = Affiliation.ADMIN;
201 break;
202 case "owner":
203 this.affiliation = Affiliation.OWNER;
204 break;
205 case "member":
206 this.affiliation = Affiliation.MEMBER;
207 break;
208 case "outcast":
209 this.affiliation = Affiliation.OUTCAST;
210 break;
211 default:
212 this.affiliation = Affiliation.NONE;
213 }
214 }
215
216 public void setPgpKeyId(long id) {
217 this.pgpKeyId = id;
218 }
219
220 public long getPgpKeyId() {
221 return this.pgpKeyId;
222 }
223
224 public Contact getContact() {
225 if (fullJid != null) {
226 return getAccount().getRoster().getContactFromRoster(realJid);
227 } else if (realJid != null){
228 return getAccount().getRoster().getContact(realJid);
229 } else {
230 return null;
231 }
232 }
233
234 public boolean setAvatar(Avatar avatar) {
235 if (this.avatar != null && this.avatar.equals(avatar)) {
236 return false;
237 } else {
238 this.avatar = avatar;
239 return true;
240 }
241 }
242
243 public String getAvatar() {
244 return avatar == null ? null : avatar.getFilename();
245 }
246
247 public Account getAccount() {
248 return options.getAccount();
249 }
250
251 public Jid getFullJid() {
252 return fullJid;
253 }
254
255 @Override
256 public boolean equals(Object o) {
257 if (this == o) return true;
258 if (o == null || getClass() != o.getClass()) return false;
259
260 User user = (User) o;
261
262 if (role != user.role) return false;
263 if (affiliation != user.affiliation) return false;
264 if (realJid != null ? !realJid.equals(user.realJid) : user.realJid != null)
265 return false;
266 return fullJid != null ? fullJid.equals(user.fullJid) : user.fullJid == null;
267
268 }
269
270 @Override
271 public int hashCode() {
272 int result = role != null ? role.hashCode() : 0;
273 result = 31 * result + (affiliation != null ? affiliation.hashCode() : 0);
274 result = 31 * result + (realJid != null ? realJid.hashCode() : 0);
275 result = 31 * result + (fullJid != null ? fullJid.hashCode() : 0);
276 return result;
277 }
278
279 @Override
280 public String toString() {
281 return "[fulljid:"+String.valueOf(fullJid)+",realjid:"+String.valueOf(realJid)+",affiliation"+affiliation.toString()+"]";
282 }
283
284 public boolean realJidMatchesAccount() {
285 return realJid != null && realJid.equals(options.account.getJid().toBareJid());
286 }
287
288 @Override
289 public int compareTo(User another) {
290 Contact ourContact = getContact();
291 Contact anotherContact = another.getContact();
292 if (ourContact != null && anotherContact != null) {
293 return ourContact.compareTo(anotherContact);
294 } else if (ourContact == null && anotherContact != null) {
295 return getName().compareToIgnoreCase(anotherContact.getDisplayName());
296 } else if (ourContact != null) {
297 return ourContact.getDisplayName().compareToIgnoreCase(another.getName());
298 } else {
299 return getName().compareToIgnoreCase(another.getName());
300 }
301 }
302 }
303
304 private Account account;
305 private final Set<User> users = Collections.synchronizedSet(new HashSet<User>());
306 private final List<String> features = new ArrayList<>();
307 private Data form = new Data();
308 private Conversation conversation;
309 private boolean isOnline = false;
310 private Error error = Error.NONE;
311 public OnRenameListener onRenameListener = null;
312 private User self;
313 private String subject = null;
314 private String password = null;
315 public boolean mNickChangingInProgress = false;
316
317 public MucOptions(Conversation conversation) {
318 this.account = conversation.getAccount();
319 this.conversation = conversation;
320 this.self = new User(this,createJoinJid(getProposedNick()));
321 }
322
323 public void updateFeatures(ArrayList<String> features) {
324 this.features.clear();
325 this.features.addAll(features);
326 }
327
328 public void updateFormData(Data form) {
329 this.form = form;
330 }
331
332 public boolean hasFeature(String feature) {
333 return this.features.contains(feature);
334 }
335
336 public boolean canInvite() {
337 Field field = this.form.getFieldByName("muc#roomconfig_allowinvites");
338 return !membersOnly() || self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
339 }
340
341 public boolean canChangeSubject() {
342 Field field = this.form.getFieldByName("muc#roomconfig_changesubject");
343 return self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
344 }
345
346 public boolean participating() {
347 return !online()
348 || self.getRole().ranks(Role.PARTICIPANT)
349 || hasFeature("muc_unmoderated");
350 }
351
352 public boolean membersOnly() {
353 return hasFeature("muc_membersonly");
354 }
355
356 public boolean mamSupport() {
357 // Update with "urn:xmpp:mam:1" once we support it
358 return hasFeature("urn:xmpp:mam:0");
359 }
360
361 public boolean nonanonymous() {
362 return hasFeature("muc_nonanonymous");
363 }
364
365 public boolean persistent() {
366 return hasFeature("muc_persistent");
367 }
368
369 public boolean moderated() {
370 return hasFeature("muc_moderated");
371 }
372
373 public User deleteUser(Jid jid) {
374 User user = findUserByFullJid(jid);
375 if (user != null) {
376 users.remove(user);
377 if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null) {
378 user.role = Role.NONE;
379 user.avatar = null;
380 user.fullJid = null;
381 users.add(user);
382 }
383 }
384 return user;
385 }
386
387 public void addUser(User user) {
388 User old;
389 if (user.fullJid == null && user.realJid != null) {
390 old = findUserByRealJid(user.realJid);
391 if (old != null) {
392 return; //don't add. user already exists
393 }
394 } else if (user.realJid != null) {
395 old = findUserByRealJid(user.realJid);
396 if (old != null && old.fullJid == null) {
397 users.remove(old);
398 }
399 }
400 old = findUserByFullJid(user.getFullJid());
401 if (old != null) {
402 users.remove(old);
403 }
404 this.users.add(user);
405 }
406
407 public User findUserByFullJid(Jid jid) {
408 if (jid == null) {
409 return null;
410 }
411 for(User user : users) {
412 if (jid.equals(user.getFullJid())) {
413 return user;
414 }
415 }
416 return null;
417 }
418
419 public User findUserByRealJid(Jid jid) {
420 if (jid == null) {
421 return null;
422 }
423 for(User user : users) {
424 if (jid.equals(user.getRealJid())) {
425 return user;
426 }
427 }
428 return null;
429 }
430
431 public boolean isUserInRoom(Jid jid) {
432 return findUserByFullJid(jid) != null;
433 }
434
435 public void setError(Error error) {
436 this.isOnline = isOnline && error == Error.NONE;
437 this.error = error;
438 }
439
440 public void setOnline() {
441 this.isOnline = true;
442 }
443
444 public ArrayList<User> getUsers() {
445 return getUsers(true);
446 }
447
448 public ArrayList<User> getUsers(boolean includeOffline) {
449 if (includeOffline) {
450 return new ArrayList<>(users);
451 } else {
452 ArrayList<User> onlineUsers = new ArrayList<>();
453 for(User user : users) {
454 if(user.getRole().ranks(Role.PARTICIPANT)) {
455 onlineUsers.add(user);
456 }
457 }
458 return onlineUsers;
459 }
460 }
461
462 public List<User> getUsers(int max) {
463 ArrayList<User> users = getUsers();
464 return users.subList(0, Math.min(max, users.size()));
465 }
466
467 public int getUserCount() {
468 return this.users.size();
469 }
470
471 public String getProposedNick() {
472 if (conversation.getBookmark() != null
473 && conversation.getBookmark().getNick() != null
474 && !conversation.getBookmark().getNick().isEmpty()) {
475 return conversation.getBookmark().getNick();
476 } else if (!conversation.getJid().isBareJid()) {
477 return conversation.getJid().getResourcepart();
478 } else {
479 return account.getUsername();
480 }
481 }
482
483 public String getActualNick() {
484 if (this.self.getName() != null) {
485 return this.self.getName();
486 } else {
487 return this.getProposedNick();
488 }
489 }
490
491 public boolean online() {
492 return this.isOnline;
493 }
494
495 public Error getError() {
496 return this.error;
497 }
498
499 public void setOnRenameListener(OnRenameListener listener) {
500 this.onRenameListener = listener;
501 }
502
503 public void setOffline() {
504 this.users.clear();
505 this.error = Error.NO_RESPONSE;
506 this.isOnline = false;
507 }
508
509 public User getSelf() {
510 return self;
511 }
512
513 public void setSubject(String content) {
514 this.subject = content;
515 }
516
517 public String getSubject() {
518 return this.subject;
519 }
520
521 public String createNameFromParticipants() {
522 if (users.size() >= 2) {
523 List<String> names = new ArrayList<>();
524 for (User user : getUsers(5)) {
525 Contact contact = user.getContact();
526 if (contact != null && !contact.getDisplayName().isEmpty()) {
527 names.add(contact.getDisplayName().split("\\s+")[0]);
528 } else if (user.getName() != null){
529 names.add(user.getName());
530 }
531 }
532 StringBuilder builder = new StringBuilder();
533 for (int i = 0; i < names.size(); ++i) {
534 builder.append(names.get(i));
535 if (i != names.size() - 1) {
536 builder.append(", ");
537 }
538 }
539 return builder.toString();
540 } else {
541 return null;
542 }
543 }
544
545 public long[] getPgpKeyIds() {
546 List<Long> ids = new ArrayList<>();
547 for (User user : this.users) {
548 if (user.getPgpKeyId() != 0) {
549 ids.add(user.getPgpKeyId());
550 }
551 }
552 ids.add(account.getPgpId());
553 long[] primitiveLongArray = new long[ids.size()];
554 for (int i = 0; i < ids.size(); ++i) {
555 primitiveLongArray[i] = ids.get(i);
556 }
557 return primitiveLongArray;
558 }
559
560 public boolean pgpKeysInUse() {
561 for (User user : this.users) {
562 if (user.getPgpKeyId() != 0) {
563 return true;
564 }
565 }
566 return false;
567 }
568
569 public boolean everybodyHasKeys() {
570 for (User user : this.users) {
571 if (user.getPgpKeyId() == 0) {
572 return false;
573 }
574 }
575 return true;
576 }
577
578 public Jid createJoinJid(String nick) {
579 try {
580 return Jid.fromString(this.conversation.getJid().toBareJid().toString() + "/" + nick);
581 } catch (final InvalidJidException e) {
582 return null;
583 }
584 }
585
586 public Jid getTrueCounterpart(Jid jid) {
587 if (jid.equals(getSelf().getFullJid())) {
588 return account.getJid().toBareJid();
589 }
590 User user = findUserByFullJid(jid);
591 return user == null ? null : user.getRealJid();
592 }
593
594 public String getPassword() {
595 this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
596 if (this.password == null && conversation.getBookmark() != null
597 && conversation.getBookmark().getPassword() != null) {
598 return conversation.getBookmark().getPassword();
599 } else {
600 return this.password;
601 }
602 }
603
604 public void setPassword(String password) {
605 if (conversation.getBookmark() != null) {
606 conversation.getBookmark().setPassword(password);
607 } else {
608 this.password = password;
609 }
610 conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
611 }
612
613 public Conversation getConversation() {
614 return this.conversation;
615 }
616
617 public List<Jid> getMembers() {
618 ArrayList<Jid> members = new ArrayList<>();
619 for(User user : users) {
620 if (user.affiliation.ranks(Affiliation.MEMBER) && user.getRealJid() != null) {
621 members.add(user.getRealJid());
622 }
623 }
624 return members;
625 }
626}