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