1package eu.siacs.conversations.entities;
2
3import android.net.Uri;
4import android.text.TextUtils;
5
6import androidx.annotation.NonNull;
7import androidx.annotation.Nullable;
8
9import java.util.ArrayList;
10import java.util.Collections;
11import java.util.HashSet;
12import java.util.List;
13import java.util.Locale;
14import java.util.Set;
15
16import io.ipfs.cid.Cid;
17
18import eu.siacs.conversations.Config;
19import eu.siacs.conversations.R;
20import eu.siacs.conversations.services.AvatarService;
21import eu.siacs.conversations.services.MessageArchiveService;
22import eu.siacs.conversations.utils.JidHelper;
23import eu.siacs.conversations.utils.UIHelper;
24import eu.siacs.conversations.xmpp.Jid;
25import eu.siacs.conversations.xmpp.chatstate.ChatState;
26import eu.siacs.conversations.xmpp.forms.Data;
27import eu.siacs.conversations.xmpp.forms.Field;
28import eu.siacs.conversations.xmpp.pep.Avatar;
29import eu.siacs.conversations.xml.Element;
30
31public class MucOptions {
32
33 public static final String STATUS_CODE_SELF_PRESENCE = "110";
34 public static final String STATUS_CODE_ROOM_CREATED = "201";
35 public static final String STATUS_CODE_BANNED = "301";
36 public static final String STATUS_CODE_CHANGED_NICK = "303";
37 public static final String STATUS_CODE_KICKED = "307";
38 public static final String STATUS_CODE_AFFILIATION_CHANGE = "321";
39 public static final String STATUS_CODE_LOST_MEMBERSHIP = "322";
40 public static final String STATUS_CODE_SHUTDOWN = "332";
41 public static final String STATUS_CODE_TECHNICAL_REASONS = "333";
42 private final Set<User> users = new HashSet<>();
43 private final Conversation conversation;
44 public OnRenameListener onRenameListener = null;
45 private boolean mAutoPushConfiguration = true;
46 private final Account account;
47 private ServiceDiscoveryResult serviceDiscoveryResult;
48 private boolean isOnline = false;
49 private Error error = Error.NONE;
50 private User self;
51 private String password = null;
52
53 private boolean tookProposedNickFromBookmark = false;
54
55 public MucOptions(Conversation conversation) {
56 this.account = conversation.getAccount();
57 this.conversation = conversation;
58 final String nick = getProposedNick(conversation.getAttribute("mucNick"));
59 this.self = new User(this, createJoinJid(nick), null, nick, new HashSet<>());
60 this.self.affiliation = Affiliation.of(conversation.getAttribute("affiliation"));
61 this.self.role = Role.of(conversation.getAttribute("role"));
62 }
63
64 public Account getAccount() {
65 return this.conversation.getAccount();
66 }
67
68 public boolean setSelf(User user) {
69 this.self = user;
70 final boolean roleChanged = this.conversation.setAttribute("role", user.role.toString());
71 final boolean affiliationChanged = this.conversation.setAttribute("affiliation", user.affiliation.toString());
72 this.conversation.setAttribute("mucNick", user.getNick());
73 return roleChanged || affiliationChanged;
74 }
75
76 public void changeAffiliation(Jid jid, Affiliation affiliation) {
77 User user = findUserByRealJid(jid);
78 synchronized (users) {
79 if (user != null && user.getRole() == Role.NONE) {
80 users.remove(user);
81 if (affiliation.ranks(Affiliation.MEMBER)) {
82 user.affiliation = affiliation;
83 users.add(user);
84 }
85 }
86 }
87 }
88
89 public void flagNoAutoPushConfiguration() {
90 mAutoPushConfiguration = false;
91 }
92
93 public boolean autoPushConfiguration() {
94 return mAutoPushConfiguration;
95 }
96
97 public boolean isSelf(Jid counterpart) {
98 return counterpart.equals(self.getFullJid());
99 }
100
101 public void resetChatState() {
102 synchronized (users) {
103 for (User user : users) {
104 user.chatState = Config.DEFAULT_CHAT_STATE;
105 }
106 }
107 }
108
109 public boolean isTookProposedNickFromBookmark() {
110 return tookProposedNickFromBookmark;
111 }
112
113 void notifyOfBookmarkNick(final String nick) {
114 final String normalized = normalize(account.getJid(),nick);
115 if (normalized != null && normalized.equals(getSelf().getNick())) {
116 this.tookProposedNickFromBookmark = true;
117 }
118 }
119
120 public boolean mamSupport() {
121 return MessageArchiveService.Version.has(getFeatures());
122 }
123
124 public boolean updateConfiguration(ServiceDiscoveryResult serviceDiscoveryResult) {
125 this.serviceDiscoveryResult = serviceDiscoveryResult;
126 String name;
127 Field roomConfigName = getRoomInfoForm().getFieldByName("muc#roomconfig_roomname");
128 if (roomConfigName != null) {
129 name = roomConfigName.getValue();
130 } else {
131 List<ServiceDiscoveryResult.Identity> identities = serviceDiscoveryResult.getIdentities();
132 String identityName = identities.size() > 0 ? identities.get(0).getName() : null;
133 final Jid jid = conversation.getJid();
134 if (identityName != null && !identityName.equals(jid == null ? null : jid.getEscapedLocal())) {
135 name = identityName;
136 } else {
137 name = null;
138 }
139 }
140 boolean changed = conversation.setAttribute("muc_name", name);
141 changed |= conversation.setAttribute(Conversation.ATTRIBUTE_MEMBERS_ONLY, this.hasFeature("muc_membersonly"));
142 changed |= conversation.setAttribute(Conversation.ATTRIBUTE_MODERATED, this.hasFeature("muc_moderated"));
143 changed |= conversation.setAttribute(Conversation.ATTRIBUTE_NON_ANONYMOUS, this.hasFeature("muc_nonanonymous"));
144 return changed;
145 }
146
147 private Data getRoomInfoForm() {
148 final List<Data> forms = serviceDiscoveryResult == null ? Collections.emptyList() : serviceDiscoveryResult.forms;
149 return forms.size() == 0 ? new Data() : forms.get(0);
150 }
151
152 public String getAvatar() {
153 return account.getRoster().getContact(conversation.getJid()).getAvatarFilename();
154 }
155
156 public boolean hasFeature(String feature) {
157 return this.serviceDiscoveryResult != null && this.serviceDiscoveryResult.features.contains(feature);
158 }
159
160 public boolean hasVCards() {
161 return hasFeature("vcard-temp");
162 }
163
164 public boolean canInvite() {
165 final boolean hasPermission = !membersOnly() || self.getRole().ranks(Role.MODERATOR) || allowInvites();
166 return hasPermission && online();
167 }
168
169 public boolean allowInvites() {
170 final Field field = getRoomInfoForm().getFieldByName("muc#roomconfig_allowinvites");
171 return field != null && "1".equals(field.getValue());
172 }
173
174 public boolean canChangeSubject() {
175 return self.getRole().ranks(Role.MODERATOR) || participantsCanChangeSubject();
176 }
177
178 public boolean participantsCanChangeSubject() {
179 final Field configField = getRoomInfoForm().getFieldByName("muc#roomconfig_changesubject");
180 final Field infoField = getRoomInfoForm().getFieldByName("muc#roominfo_changesubject");
181 final Field field = configField != null ? configField : infoField;
182 return field != null && "1".equals(field.getValue());
183 }
184
185 public boolean allowPm() {
186 final Field field = getRoomInfoForm().getFieldByName("muc#roomconfig_allowpm");
187 if (field == null) {
188 return true; //fall back if field does not exists
189 }
190 if ("anyone".equals(field.getValue())) {
191 return true;
192 } else if ("participants".equals(field.getValue())) {
193 return self.getRole().ranks(Role.PARTICIPANT);
194 } else if ("moderators".equals(field.getValue())) {
195 return self.getRole().ranks(Role.MODERATOR);
196 } else {
197 return false;
198 }
199 }
200
201 public boolean participating() {
202 return self.getRole().ranks(Role.PARTICIPANT) || !moderated();
203 }
204
205 public boolean membersOnly() {
206 return conversation.getBooleanAttribute(Conversation.ATTRIBUTE_MEMBERS_ONLY, false);
207 }
208
209 public List<String> getFeatures() {
210 return this.serviceDiscoveryResult != null ? this.serviceDiscoveryResult.features : Collections.emptyList();
211 }
212
213 public boolean nonanonymous() {
214 return conversation.getBooleanAttribute(Conversation.ATTRIBUTE_NON_ANONYMOUS, false);
215 }
216
217 public boolean isPrivateAndNonAnonymous() {
218 return membersOnly() && nonanonymous();
219 }
220
221 public boolean moderated() {
222 return conversation.getBooleanAttribute(Conversation.ATTRIBUTE_MODERATED, false);
223 }
224
225 public boolean stableId() {
226 return getFeatures().contains("http://jabber.org/protocol/muc#stable_id");
227 }
228
229 public User deleteUser(Jid jid) {
230 User user = findUserByFullJid(jid);
231 if (user != null) {
232 synchronized (users) {
233 users.remove(user);
234 boolean realJidInMuc = false;
235 for (User u : users) {
236 if (user.realJid != null && user.realJid.equals(u.realJid)) {
237 realJidInMuc = true;
238 break;
239 }
240 }
241 boolean self = user.realJid != null && user.realJid.equals(account.getJid().asBareJid());
242 if (membersOnly()
243 && nonanonymous()
244 && user.affiliation.ranks(Affiliation.MEMBER)
245 && user.realJid != null
246 && !realJidInMuc
247 && !self) {
248 user.role = Role.NONE;
249 user.avatar = null;
250 user.fullJid = null;
251 users.add(user);
252 }
253 }
254 }
255 return user;
256 }
257
258 //returns true if real jid was new;
259 public boolean updateUser(User user) {
260 User old;
261 boolean realJidFound = false;
262 if (user.fullJid == null && user.realJid != null) {
263 old = findUserByRealJid(user.realJid);
264 realJidFound = old != null;
265 if (old != null) {
266 if (old.fullJid != null) {
267 return false; //don't add. user already exists
268 } else {
269 synchronized (users) {
270 users.remove(old);
271 }
272 }
273 }
274 } else if (user.realJid != null) {
275 old = findUserByRealJid(user.realJid);
276 realJidFound = old != null;
277 synchronized (users) {
278 if (old != null && (old.fullJid == null || old.role == Role.NONE)) {
279 users.remove(old);
280 }
281 }
282 }
283 old = findUserByFullJid(user.getFullJid());
284
285 synchronized (this.users) {
286 if (old != null) {
287 users.remove(old);
288 if (old.nick != null && user.nick == null && old.getName().equals(user.getName())) user.nick = old.nick;
289 if (old.hats != null && user.hats == null) user.hats = old.hats;
290 if (old.avatar != null && user.avatar == null) user.avatar = old.avatar;
291 }
292 boolean fullJidIsSelf = isOnline && user.getFullJid() != null && user.getFullJid().equals(self.getFullJid());
293 if ((!membersOnly() || user.getAffiliation().ranks(Affiliation.MEMBER))
294 && user.getAffiliation().outranks(Affiliation.OUTCAST)
295 && !fullJidIsSelf) {
296 this.users.add(user);
297 return !realJidFound && user.realJid != null;
298 }
299 }
300 return false;
301 }
302
303 public User findUserByName(final String name) {
304 if (name == null) {
305 return null;
306 }
307 synchronized (users) {
308 for (User user : users) {
309 if (name.equals(user.getName())) {
310 return user;
311 }
312 }
313 }
314 return null;
315 }
316
317 public User findUserByFullJid(Jid jid) {
318 if (jid == null) {
319 return null;
320 }
321 synchronized (users) {
322 for (User user : users) {
323 if (jid.equals(user.getFullJid())) {
324 return user;
325 }
326 }
327 }
328 return null;
329 }
330
331 public User findUserByRealJid(Jid jid) {
332 if (jid == null) {
333 return null;
334 }
335 synchronized (users) {
336 for (User user : users) {
337 if (jid.asBareJid().equals(user.realJid)) {
338 return user;
339 }
340 }
341 }
342 return null;
343 }
344
345 public User findUserByOccupantId(final String id) {
346 if (id == null) {
347 return null;
348 }
349 synchronized (users) {
350 for (User user : users) {
351 if (id.equals(user.getOccupantId())) {
352 return user;
353 }
354 }
355 }
356 return new User(this, null, id, null, new HashSet<>());
357 }
358
359 public User findOrCreateUserByRealJid(Jid jid, Jid fullJid) {
360 User user = findUserByRealJid(jid);
361 if (user == null) {
362 user = new User(this, fullJid, null, null, new HashSet<>());
363 user.setRealJid(jid);
364 }
365 return user;
366 }
367
368 public User findUser(ReadByMarker readByMarker) {
369 if (readByMarker.getRealJid() != null) {
370 return findOrCreateUserByRealJid(readByMarker.getRealJid().asBareJid(), readByMarker.getFullJid());
371 } else if (readByMarker.getFullJid() != null) {
372 return findUserByFullJid(readByMarker.getFullJid());
373 } else {
374 return null;
375 }
376 }
377
378 public boolean isContactInRoom(Contact contact) {
379 return contact != null && findUserByRealJid(contact.getJid().asBareJid()) != null;
380 }
381
382 public boolean isUserInRoom(Jid jid) {
383 return findUserByFullJid(jid) != null;
384 }
385
386 public boolean setOnline() {
387 boolean before = this.isOnline;
388 this.isOnline = true;
389 return !before;
390 }
391
392 public ArrayList<User> getUsers() {
393 return getUsers(true);
394 }
395
396 public ArrayList<User> getUsers(boolean includeOffline) {
397 synchronized (users) {
398 ArrayList<User> users = new ArrayList<>();
399 for (User user : this.users) {
400 if (!user.isDomain() && (includeOffline || user.getRole().ranks(Role.PARTICIPANT))) {
401 users.add(user);
402 }
403 }
404 return users;
405 }
406 }
407
408 public ArrayList<User> getUsersWithChatState(ChatState state, int max) {
409 synchronized (users) {
410 ArrayList<User> list = new ArrayList<>();
411 for (User user : users) {
412 if (user.chatState == state) {
413 list.add(user);
414 if (list.size() >= max) {
415 break;
416 }
417 }
418 }
419 return list;
420 }
421 }
422
423 public List<User> getUsers(int max) {
424 ArrayList<User> subset = new ArrayList<>();
425 HashSet<Jid> jids = new HashSet<>();
426 jids.add(account.getJid().asBareJid());
427 synchronized (users) {
428 for (User user : users) {
429 if (user.getRealJid() == null || (user.getRealJid().getLocal() != null && jids.add(user.getRealJid()))) {
430 subset.add(user);
431 }
432 if (subset.size() >= max) {
433 break;
434 }
435 }
436 }
437 return subset;
438 }
439
440 public static List<User> sub(List<User> users, int max) {
441 ArrayList<User> subset = new ArrayList<>();
442 HashSet<Jid> jids = new HashSet<>();
443 for (User user : users) {
444 jids.add(user.getAccount().getJid().asBareJid());
445 if (user.getRealJid() == null || (user.getRealJid().getLocal() != null && jids.add(user.getRealJid()))) {
446 subset.add(user);
447 }
448 if (subset.size() >= max) {
449 break;
450 }
451 }
452 return subset;
453 }
454
455 public int getUserCount() {
456 synchronized (users) {
457 return users.size();
458 }
459 }
460
461 public String getProposedNick() {
462 return getProposedNick(null);
463 }
464
465 public String getProposedNick(final String mucNick) {
466 final Bookmark bookmark = this.conversation.getBookmark();
467 final String bookmarkedNick = normalize(account.getJid(), bookmark == null ? null : bookmark.getNick());
468 if (bookmarkedNick != null) {
469 this.tookProposedNickFromBookmark = true;
470 return bookmarkedNick;
471 } else if (mucNick != null) {
472 return mucNick;
473 } else if (!conversation.getJid().isBareJid()) {
474 return conversation.getJid().getResource();
475 } else {
476 return defaultNick(account);
477 }
478 }
479
480 public static String defaultNick(final Account account) {
481 final String displayName = normalize(account.getJid(), account.getDisplayName());
482 if (displayName == null) {
483 return JidHelper.localPartOrFallback(account.getJid());
484 } else {
485 return displayName;
486 }
487 }
488
489 private static String normalize(Jid account, String nick) {
490 if (account == null || TextUtils.isEmpty(nick)) {
491 return null;
492 }
493
494 try {
495 return account.withResource(nick).getResource();
496 } catch (IllegalArgumentException e) {
497 return nick;
498 }
499 }
500
501 public String getActualNick() {
502 if (this.self.getNick() != null) {
503 return this.self.getNick();
504 } else {
505 return this.getProposedNick();
506 }
507 }
508
509 public String getActualName() {
510 if (this.self.getName() != null) {
511 return this.self.getName();
512 } else {
513 return this.getProposedNick();
514 }
515 }
516
517 public boolean online() {
518 return this.isOnline;
519 }
520
521 public Error getError() {
522 return this.error;
523 }
524
525 public void setError(Error error) {
526 this.isOnline = isOnline && error == Error.NONE;
527 this.error = error;
528 }
529
530 public void setOnRenameListener(OnRenameListener listener) {
531 this.onRenameListener = listener;
532 }
533
534 public void setOffline() {
535 synchronized (users) {
536 this.users.clear();
537 }
538 this.error = Error.NO_RESPONSE;
539 this.isOnline = false;
540 }
541
542 public User getSelf() {
543 return self;
544 }
545
546 public boolean setSubject(String subject) {
547 return this.conversation.setAttribute("subject", subject);
548 }
549
550 public String getSubject() {
551 return this.conversation.getAttribute("subject");
552 }
553
554 public String getName() {
555 return this.conversation.getAttribute("muc_name");
556 }
557
558 private List<User> getFallbackUsersFromCryptoTargets() {
559 List<User> users = new ArrayList<>();
560 for (Jid jid : conversation.getAcceptedCryptoTargets()) {
561 User user = new User(this, null, null, null, new HashSet<>());
562 user.setRealJid(jid);
563 users.add(user);
564 }
565 return users;
566 }
567
568 public List<User> getUsersRelevantForNameAndAvatar() {
569 final List<User> users;
570 if (isOnline) {
571 users = getUsers(5);
572 } else {
573 users = getFallbackUsersFromCryptoTargets();
574 }
575 return users;
576 }
577
578 String createNameFromParticipants() {
579 List<User> users = getUsersRelevantForNameAndAvatar();
580 if (users.size() >= 2) {
581 StringBuilder builder = new StringBuilder();
582 for (User user : users) {
583 if (builder.length() != 0) {
584 builder.append(", ");
585 }
586 String name = UIHelper.getDisplayName(user);
587 if (name != null) {
588 builder.append(name.split("\\s+")[0]);
589 }
590 }
591 return builder.toString();
592 } else {
593 return null;
594 }
595 }
596
597 public long[] getPgpKeyIds() {
598 List<Long> ids = new ArrayList<>();
599 for (User user : this.users) {
600 if (user.getPgpKeyId() != 0) {
601 ids.add(user.getPgpKeyId());
602 }
603 }
604 ids.add(account.getPgpId());
605 long[] primitiveLongArray = new long[ids.size()];
606 for (int i = 0; i < ids.size(); ++i) {
607 primitiveLongArray[i] = ids.get(i);
608 }
609 return primitiveLongArray;
610 }
611
612 public boolean pgpKeysInUse() {
613 synchronized (users) {
614 for (User user : users) {
615 if (user.getPgpKeyId() != 0) {
616 return true;
617 }
618 }
619 }
620 return false;
621 }
622
623 public boolean everybodyHasKeys() {
624 synchronized (users) {
625 for (User user : users) {
626 if (user.getPgpKeyId() == 0) {
627 return false;
628 }
629 }
630 }
631 return true;
632 }
633
634 public Jid createJoinJid(String nick) {
635 return createJoinJid(nick, true);
636 }
637
638 private Jid createJoinJid(String nick, boolean tryFix) {
639 try {
640 return conversation.getJid().withResource(nick);
641 } catch (final IllegalArgumentException e) {
642 try {
643 return tryFix ? createJoinJid(gnu.inet.encoding.Punycode.encode(nick), false) : null;
644 } catch (final Exception e2) {
645 return null;
646 }
647 }
648 }
649
650 public Jid getTrueCounterpart(Jid jid) {
651 if (jid.equals(getSelf().getFullJid())) {
652 return account.getJid().asBareJid();
653 }
654 User user = findUserByFullJid(jid);
655 return user == null ? null : user.realJid;
656 }
657
658 public String getPassword() {
659 this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
660 if (this.password == null && conversation.getBookmark() != null
661 && conversation.getBookmark().getPassword() != null) {
662 return conversation.getBookmark().getPassword();
663 } else {
664 return this.password;
665 }
666 }
667
668 public void setPassword(String password) {
669 if (conversation.getBookmark() != null) {
670 conversation.getBookmark().setPassword(password);
671 } else {
672 this.password = password;
673 }
674 conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
675 }
676
677 public Conversation getConversation() {
678 return this.conversation;
679 }
680
681 public List<Jid> getMembers(final boolean includeDomains) {
682 ArrayList<Jid> members = new ArrayList<>();
683 synchronized (users) {
684 for (User user : users) {
685 if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null && !user.realJid.asBareJid().equals(conversation.account.getJid().asBareJid()) && (!user.isDomain() || includeDomains)) {
686 members.add(user.realJid);
687 }
688 }
689 }
690 return members;
691 }
692
693 public enum Affiliation {
694 OWNER(4, R.string.owner),
695 ADMIN(3, R.string.admin),
696 MEMBER(2, R.string.member),
697 OUTCAST(0, R.string.outcast),
698 NONE(1, R.string.no_affiliation);
699
700 private final int resId;
701 private final int rank;
702
703 Affiliation(int rank, int resId) {
704 this.resId = resId;
705 this.rank = rank;
706 }
707
708 public static Affiliation of(@Nullable String value) {
709 if (value == null) {
710 return NONE;
711 }
712 try {
713 return Affiliation.valueOf(value.toUpperCase(Locale.US));
714 } catch (IllegalArgumentException e) {
715 return NONE;
716 }
717 }
718
719 public int getResId() {
720 return resId;
721 }
722
723 @Override
724 public String toString() {
725 return name().toLowerCase(Locale.US);
726 }
727
728 public boolean outranks(Affiliation affiliation) {
729 return rank > affiliation.rank;
730 }
731
732 public boolean ranks(Affiliation affiliation) {
733 return rank >= affiliation.rank;
734 }
735 }
736
737 public enum Role {
738 MODERATOR(R.string.moderator, 3),
739 VISITOR(R.string.visitor, 1),
740 PARTICIPANT(R.string.participant, 2),
741 NONE(R.string.no_role, 0);
742
743 private final int resId;
744 private final int rank;
745
746 Role(int resId, int rank) {
747 this.resId = resId;
748 this.rank = rank;
749 }
750
751 public static Role of(@Nullable String value) {
752 if (value == null) {
753 return NONE;
754 }
755 try {
756 return Role.valueOf(value.toUpperCase(Locale.US));
757 } catch (IllegalArgumentException e) {
758 return NONE;
759 }
760 }
761
762 public int getResId() {
763 return resId;
764 }
765
766 @Override
767 public String toString() {
768 return name().toLowerCase(Locale.US);
769 }
770
771 public boolean ranks(Role role) {
772 return rank >= role.rank;
773 }
774 }
775
776 public enum Error {
777 NO_RESPONSE,
778 SERVER_NOT_FOUND,
779 REMOTE_SERVER_TIMEOUT,
780 NONE,
781 NICK_IN_USE,
782 PASSWORD_REQUIRED,
783 BANNED,
784 MEMBERS_ONLY,
785 RESOURCE_CONSTRAINT,
786 KICKED,
787 SHUTDOWN,
788 DESTROYED,
789 INVALID_NICK,
790 TECHNICAL_PROBLEMS,
791 UNKNOWN,
792 NON_ANONYMOUS
793 }
794
795 private interface OnEventListener {
796 void onSuccess();
797
798 void onFailure();
799 }
800
801 public interface OnRenameListener extends OnEventListener {
802
803 }
804
805 public static class Hat implements Comparable<Hat> {
806 private final Uri uri;
807 private final String title;
808
809 public Hat(final Element el) {
810 Uri parseUri = null; // null hat uri is invaild per spec
811 try {
812 parseUri = Uri.parse(el.getAttribute("uri"));
813 } catch (final Exception e) { }
814 uri = parseUri;
815
816 title = el.getAttribute("title");
817 }
818
819 public Hat(final Uri uri, final String title) {
820 this.uri = uri;
821 this.title = title;
822 }
823
824 public String toString() {
825 return title;
826 }
827
828 public int getColor() {
829 return UIHelper.getColorForName(uri == null ? title : uri.toString());
830 }
831
832 @Override
833 public int compareTo(@NonNull Hat another) {
834 return title.compareTo(another.title);
835 }
836 }
837
838 public static class User implements Comparable<User>, AvatarService.Avatarable {
839 private Role role = Role.NONE;
840 private Affiliation affiliation = Affiliation.NONE;
841 private Jid realJid;
842 private Jid fullJid;
843 protected String nick;
844 private long pgpKeyId = 0;
845 protected Avatar avatar;
846 private final MucOptions options;
847 private ChatState chatState = Config.DEFAULT_CHAT_STATE;
848 protected Set<Hat> hats;
849 protected String occupantId;
850
851 public User(MucOptions options, Jid fullJid, final String occupantId, final String nick, final Set<Hat> hats) {
852 this.options = options;
853 this.fullJid = fullJid;
854 this.occupantId = occupantId;
855 this.nick = nick;
856 this.hats = hats;
857 }
858
859 public String getName() {
860 return fullJid == null ? null : fullJid.getResource();
861 }
862
863 public Jid getMuc() {
864 return fullJid == null ? (options.getConversation().getJid().asBareJid()) : fullJid.asBareJid();
865 }
866
867 public String getOccupantId() {
868 return occupantId;
869 }
870
871 public String getNick() {
872 return nick == null ? getName() : nick;
873 }
874
875 public Role getRole() {
876 return this.role;
877 }
878
879 public void setRole(String role) {
880 this.role = Role.of(role);
881 }
882
883 public Affiliation getAffiliation() {
884 return this.affiliation;
885 }
886
887 public void setAffiliation(String affiliation) {
888 this.affiliation = Affiliation.of(affiliation);
889 }
890
891 public Set<Hat> getHats() {
892 return this.hats == null ? new HashSet<>() : hats;
893 }
894
895 public long getPgpKeyId() {
896 if (this.pgpKeyId != 0) {
897 return this.pgpKeyId;
898 } else if (realJid != null) {
899 return getAccount().getRoster().getContact(realJid).getPgpKeyId();
900 } else {
901 return 0;
902 }
903 }
904
905 public void setPgpKeyId(long id) {
906 this.pgpKeyId = id;
907 }
908
909 public Contact getContact() {
910 if (fullJid != null) {
911 return getAccount().getRoster().getContactFromContactList(realJid);
912 } else if (realJid != null) {
913 return getAccount().getRoster().getContact(realJid);
914 } else {
915 return null;
916 }
917 }
918
919 public boolean setAvatar(Avatar avatar) {
920 if (this.avatar != null && this.avatar.equals(avatar)) {
921 return false;
922 } else {
923 this.avatar = avatar;
924 return true;
925 }
926 }
927
928 public String getAvatar() {
929 if (avatar != null) {
930 return avatar.getFilename();
931 }
932 Avatar avatar = realJid != null ? getAccount().getRoster().getContact(realJid).getAvatar() : null;
933 return avatar == null ? null : avatar.getFilename();
934 }
935
936 public Cid getAvatarCid() {
937 if (avatar != null) {
938 return avatar.cid();
939 }
940 Avatar avatar = realJid != null ? getAccount().getRoster().getContact(realJid).getAvatar() : null;
941 return avatar == null ? null : avatar.cid();
942 }
943
944 public Account getAccount() {
945 return options.getAccount();
946 }
947
948 public Conversation getConversation() {
949 return options.getConversation();
950 }
951
952 public Jid getFullJid() {
953 return fullJid;
954 }
955
956 @Override
957 public boolean equals(Object o) {
958 if (this == o) return true;
959 if (o == null || getClass() != o.getClass()) return false;
960
961 User user = (User) o;
962
963 if (role != user.role) return false;
964 if (affiliation != user.affiliation) return false;
965 if (realJid != null ? !realJid.equals(user.realJid) : user.realJid != null)
966 return false;
967 return fullJid != null ? fullJid.equals(user.fullJid) : user.fullJid == null;
968
969 }
970
971 public boolean isDomain() {
972 return realJid != null && realJid.getLocal() == null && role == Role.NONE;
973 }
974
975 @Override
976 public int hashCode() {
977 int result = role != null ? role.hashCode() : 0;
978 result = 31 * result + (affiliation != null ? affiliation.hashCode() : 0);
979 result = 31 * result + (realJid != null ? realJid.hashCode() : 0);
980 result = 31 * result + (fullJid != null ? fullJid.hashCode() : 0);
981 return result;
982 }
983
984 @Override
985 public String toString() {
986 return "[fulljid:" + fullJid + ",realjid:" + realJid + ",nick:" + nick + ",affiliation" + affiliation.toString() + "]";
987 }
988
989 public boolean realJidMatchesAccount() {
990 return realJid != null && realJid.equals(options.account.getJid().asBareJid());
991 }
992
993 @Override
994 public int compareTo(@NonNull User another) {
995 if (another.getAffiliation().outranks(getAffiliation())) {
996 return 1;
997 } else if (getAffiliation().outranks(another.getAffiliation())) {
998 return -1;
999 } else {
1000 return getComparableName().compareToIgnoreCase(another.getComparableName());
1001 }
1002 }
1003
1004 public String getComparableName() {
1005 Contact contact = getContact();
1006 if (contact != null) {
1007 return contact.getDisplayName();
1008 } else {
1009 String name = getName();
1010 return name == null ? "" : name;
1011 }
1012 }
1013
1014 public Jid getRealJid() {
1015 return realJid;
1016 }
1017
1018 public void setRealJid(Jid jid) {
1019 this.realJid = jid != null ? jid.asBareJid() : null;
1020 }
1021
1022 public boolean setChatState(ChatState chatState) {
1023 if (this.chatState == chatState) {
1024 return false;
1025 }
1026 this.chatState = chatState;
1027 return true;
1028 }
1029
1030 @Override
1031 public int getAvatarBackgroundColor() {
1032 final String seed = realJid != null ? realJid.asBareJid().toString() : null;
1033 return UIHelper.getColorForName(seed == null ? getName() : seed);
1034 }
1035
1036 @Override
1037 public String getAvatarName() {
1038 return getConversation().getName().toString();
1039 }
1040 }
1041}