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