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