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