1package eu.siacs.conversations.entities;
2
3import androidx.annotation.NonNull;
4import androidx.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 eu.siacs.conversations.xmpp.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 final 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_CHAT_STATE;
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 boolean stableId() {
215 return getFeatures().contains("http://jabber.org/protocol/muc#stable_id");
216 }
217
218 public User deleteUser(Jid jid) {
219 User user = findUserByFullJid(jid);
220 if (user != null) {
221 synchronized (users) {
222 users.remove(user);
223 boolean realJidInMuc = false;
224 for (User u : users) {
225 if (user.realJid != null && user.realJid.equals(u.realJid)) {
226 realJidInMuc = true;
227 break;
228 }
229 }
230 boolean self = user.realJid != null && user.realJid.equals(account.getJid().asBareJid());
231 if (membersOnly()
232 && nonanonymous()
233 && user.affiliation.ranks(Affiliation.MEMBER)
234 && user.realJid != null
235 && !realJidInMuc
236 && !self) {
237 user.role = Role.NONE;
238 user.avatar = null;
239 user.fullJid = null;
240 users.add(user);
241 }
242 }
243 }
244 return user;
245 }
246
247 //returns true if real jid was new;
248 public boolean updateUser(User user) {
249 User old;
250 boolean realJidFound = false;
251 if (user.fullJid == null && user.realJid != null) {
252 old = findUserByRealJid(user.realJid);
253 realJidFound = old != null;
254 if (old != null) {
255 if (old.fullJid != null) {
256 return false; //don't add. user already exists
257 } else {
258 synchronized (users) {
259 users.remove(old);
260 }
261 }
262 }
263 } else if (user.realJid != null) {
264 old = findUserByRealJid(user.realJid);
265 realJidFound = old != null;
266 synchronized (users) {
267 if (old != null && (old.fullJid == null || old.role == Role.NONE)) {
268 users.remove(old);
269 }
270 }
271 }
272 old = findUserByFullJid(user.getFullJid());
273
274 synchronized (this.users) {
275 if (old != null) {
276 users.remove(old);
277 }
278 boolean fullJidIsSelf = isOnline && user.getFullJid() != null && user.getFullJid().equals(self.getFullJid());
279 if ((!membersOnly() || user.getAffiliation().ranks(Affiliation.MEMBER))
280 && user.getAffiliation().outranks(Affiliation.OUTCAST)
281 && !fullJidIsSelf) {
282 this.users.add(user);
283 return !realJidFound && user.realJid != null;
284 }
285 }
286 return false;
287 }
288
289 public User findUserByFullJid(Jid jid) {
290 if (jid == null) {
291 return null;
292 }
293 synchronized (users) {
294 for (User user : users) {
295 if (jid.equals(user.getFullJid())) {
296 return user;
297 }
298 }
299 }
300 return null;
301 }
302
303 public User findUserByRealJid(Jid jid) {
304 if (jid == null) {
305 return null;
306 }
307 synchronized (users) {
308 for (User user : users) {
309 if (jid.equals(user.realJid)) {
310 return user;
311 }
312 }
313 }
314 return null;
315 }
316
317 public User findOrCreateUserByRealJid(Jid jid, Jid fullJid) {
318 User user = findUserByRealJid(jid);
319 if (user == null) {
320 user = new User(this, fullJid);
321 user.setRealJid(jid);
322 }
323 return user;
324 }
325
326 public User findUser(ReadByMarker readByMarker) {
327 if (readByMarker.getRealJid() != null) {
328 return findOrCreateUserByRealJid(readByMarker.getRealJid().asBareJid(), readByMarker.getFullJid());
329 } else if (readByMarker.getFullJid() != null) {
330 return findUserByFullJid(readByMarker.getFullJid());
331 } else {
332 return null;
333 }
334 }
335
336 public boolean isContactInRoom(Contact contact) {
337 return contact != null && findUserByRealJid(contact.getJid().asBareJid()) != null;
338 }
339
340 public boolean isUserInRoom(Jid jid) {
341 return findUserByFullJid(jid) != null;
342 }
343
344 public boolean setOnline() {
345 boolean before = this.isOnline;
346 this.isOnline = true;
347 return !before;
348 }
349
350 public ArrayList<User> getUsers() {
351 return getUsers(true);
352 }
353
354 public ArrayList<User> getUsers(boolean includeOffline) {
355 synchronized (users) {
356 ArrayList<User> users = new ArrayList<>();
357 for (User user : this.users) {
358 if (!user.isDomain() && (includeOffline || user.getRole().ranks(Role.PARTICIPANT))) {
359 users.add(user);
360 }
361 }
362 return users;
363 }
364 }
365
366 public ArrayList<User> getUsersWithChatState(ChatState state, int max) {
367 synchronized (users) {
368 ArrayList<User> list = new ArrayList<>();
369 for (User user : users) {
370 if (user.chatState == state) {
371 list.add(user);
372 if (list.size() >= max) {
373 break;
374 }
375 }
376 }
377 return list;
378 }
379 }
380
381 public List<User> getUsers(int max) {
382 ArrayList<User> subset = new ArrayList<>();
383 HashSet<Jid> jids = new HashSet<>();
384 jids.add(account.getJid().asBareJid());
385 synchronized (users) {
386 for (User user : users) {
387 if (user.getRealJid() == null || (user.getRealJid().getLocal() != null && jids.add(user.getRealJid()))) {
388 subset.add(user);
389 }
390 if (subset.size() >= max) {
391 break;
392 }
393 }
394 }
395 return subset;
396 }
397
398 public static List<User> sub(List<User> users, int max) {
399 ArrayList<User> subset = new ArrayList<>();
400 HashSet<Jid> jids = new HashSet<>();
401 for (User user : users) {
402 jids.add(user.getAccount().getJid().asBareJid());
403 if (user.getRealJid() == null || (user.getRealJid().getLocal() != null && jids.add(user.getRealJid()))) {
404 subset.add(user);
405 }
406 if (subset.size() >= max) {
407 break;
408 }
409 }
410 return subset;
411 }
412
413 public int getUserCount() {
414 synchronized (users) {
415 return users.size();
416 }
417 }
418
419 public String getProposedNick() {
420 final Bookmark bookmark = this.conversation.getBookmark();
421 final String bookmarkedNick = normalize(account.getJid(), bookmark == null ? null : bookmark.getNick());
422 if (bookmarkedNick != null) {
423 this.tookProposedNickFromBookmark = true;
424 return bookmarkedNick;
425 } else if (!conversation.getJid().isBareJid()) {
426 return conversation.getJid().getResource();
427 } else {
428 return defaultNick(account);
429 }
430 }
431
432 public static String defaultNick(final Account account) {
433 final String displayName = normalize(account.getJid(), account.getDisplayName());
434 if (displayName == null) {
435 return JidHelper.localPartOrFallback(account.getJid());
436 } else {
437 return displayName;
438 }
439 }
440
441 private static String normalize(Jid account, String nick) {
442 if (account == null || TextUtils.isEmpty(nick)) {
443 return null;
444 }
445 try {
446 return account.withResource(nick).getResource();
447 } catch (IllegalArgumentException e) {
448 return null;
449 }
450
451 }
452
453 public String getActualNick() {
454 if (this.self.getName() != null) {
455 return this.self.getName();
456 } else {
457 return this.getProposedNick();
458 }
459 }
460
461 public boolean online() {
462 return this.isOnline;
463 }
464
465 public Error getError() {
466 return this.error;
467 }
468
469 public void setError(Error error) {
470 this.isOnline = isOnline && error == Error.NONE;
471 this.error = error;
472 }
473
474 public void setOnRenameListener(OnRenameListener listener) {
475 this.onRenameListener = listener;
476 }
477
478 public void setOffline() {
479 synchronized (users) {
480 this.users.clear();
481 }
482 this.error = Error.NO_RESPONSE;
483 this.isOnline = false;
484 }
485
486 public User getSelf() {
487 return self;
488 }
489
490 public boolean setSubject(String subject) {
491 return this.conversation.setAttribute("subject", subject);
492 }
493
494 public String getSubject() {
495 return this.conversation.getAttribute("subject");
496 }
497
498 public String getName() {
499 return this.conversation.getAttribute("muc_name");
500 }
501
502 private List<User> getFallbackUsersFromCryptoTargets() {
503 List<User> users = new ArrayList<>();
504 for (Jid jid : conversation.getAcceptedCryptoTargets()) {
505 User user = new User(this, null);
506 user.setRealJid(jid);
507 users.add(user);
508 }
509 return users;
510 }
511
512 public List<User> getUsersRelevantForNameAndAvatar() {
513 final List<User> users;
514 if (isOnline) {
515 users = getUsers(5);
516 } else {
517 users = getFallbackUsersFromCryptoTargets();
518 }
519 return users;
520 }
521
522 String createNameFromParticipants() {
523 List<User> users = getUsersRelevantForNameAndAvatar();
524 if (users.size() >= 2) {
525 StringBuilder builder = new StringBuilder();
526 for (User user : users) {
527 if (builder.length() != 0) {
528 builder.append(", ");
529 }
530 String name = UIHelper.getDisplayName(user);
531 if (name != null) {
532 builder.append(name.split("\\s+")[0]);
533 }
534 }
535 return builder.toString();
536 } else {
537 return null;
538 }
539 }
540
541 public long[] getPgpKeyIds() {
542 List<Long> ids = new ArrayList<>();
543 for (User user : this.users) {
544 if (user.getPgpKeyId() != 0) {
545 ids.add(user.getPgpKeyId());
546 }
547 }
548 ids.add(account.getPgpId());
549 long[] primitiveLongArray = new long[ids.size()];
550 for (int i = 0; i < ids.size(); ++i) {
551 primitiveLongArray[i] = ids.get(i);
552 }
553 return primitiveLongArray;
554 }
555
556 public boolean pgpKeysInUse() {
557 synchronized (users) {
558 for (User user : users) {
559 if (user.getPgpKeyId() != 0) {
560 return true;
561 }
562 }
563 }
564 return false;
565 }
566
567 public boolean everybodyHasKeys() {
568 synchronized (users) {
569 for (User user : users) {
570 if (user.getPgpKeyId() == 0) {
571 return false;
572 }
573 }
574 }
575 return true;
576 }
577
578 public Jid createJoinJid(String nick) {
579 try {
580 return conversation.getJid().withResource(nick);
581 } catch (final IllegalArgumentException e) {
582 return null;
583 }
584 }
585
586 public Jid getTrueCounterpart(Jid jid) {
587 if (jid.equals(getSelf().getFullJid())) {
588 return account.getJid().asBareJid();
589 }
590 User user = findUserByFullJid(jid);
591 return user == null ? null : user.realJid;
592 }
593
594 public String getPassword() {
595 this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
596 if (this.password == null && conversation.getBookmark() != null
597 && conversation.getBookmark().getPassword() != null) {
598 return conversation.getBookmark().getPassword();
599 } else {
600 return this.password;
601 }
602 }
603
604 public void setPassword(String password) {
605 if (conversation.getBookmark() != null) {
606 conversation.getBookmark().setPassword(password);
607 } else {
608 this.password = password;
609 }
610 conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
611 }
612
613 public Conversation getConversation() {
614 return this.conversation;
615 }
616
617 public List<Jid> getMembers(final boolean includeDomains) {
618 ArrayList<Jid> members = new ArrayList<>();
619 synchronized (users) {
620 for (User user : users) {
621 if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null && !user.realJid.asBareJid().equals(conversation.account.getJid().asBareJid()) && (!user.isDomain() || includeDomains)) {
622 members.add(user.realJid);
623 }
624 }
625 }
626 return members;
627 }
628
629 public enum Affiliation {
630 OWNER(4, R.string.owner),
631 ADMIN(3, R.string.admin),
632 MEMBER(2, R.string.member),
633 OUTCAST(0, R.string.outcast),
634 NONE(1, R.string.no_affiliation);
635
636 private int resId;
637 private int rank;
638
639 Affiliation(int rank, int resId) {
640 this.resId = resId;
641 this.rank = rank;
642 }
643
644 public static Affiliation of(@Nullable String value) {
645 if (value == null) {
646 return NONE;
647 }
648 try {
649 return Affiliation.valueOf(value.toUpperCase(Locale.US));
650 } catch (IllegalArgumentException e) {
651 return NONE;
652 }
653 }
654
655 public int getResId() {
656 return resId;
657 }
658
659 @Override
660 public String toString() {
661 return name().toLowerCase(Locale.US);
662 }
663
664 public boolean outranks(Affiliation affiliation) {
665 return rank > affiliation.rank;
666 }
667
668 public boolean ranks(Affiliation affiliation) {
669 return rank >= affiliation.rank;
670 }
671 }
672
673 public enum Role {
674 MODERATOR(R.string.moderator, 3),
675 VISITOR(R.string.visitor, 1),
676 PARTICIPANT(R.string.participant, 2),
677 NONE(R.string.no_role, 0);
678
679 private int resId;
680 private int rank;
681
682 Role(int resId, int rank) {
683 this.resId = resId;
684 this.rank = rank;
685 }
686
687 public static Role of(@Nullable String value) {
688 if (value == null) {
689 return NONE;
690 }
691 try {
692 return Role.valueOf(value.toUpperCase(Locale.US));
693 } catch (IllegalArgumentException e) {
694 return NONE;
695 }
696 }
697
698 public int getResId() {
699 return resId;
700 }
701
702 @Override
703 public String toString() {
704 return name().toLowerCase(Locale.US);
705 }
706
707 public boolean ranks(Role role) {
708 return rank >= role.rank;
709 }
710 }
711
712 public enum Error {
713 NO_RESPONSE,
714 SERVER_NOT_FOUND,
715 REMOTE_SERVER_TIMEOUT,
716 NONE,
717 NICK_IN_USE,
718 PASSWORD_REQUIRED,
719 BANNED,
720 MEMBERS_ONLY,
721 RESOURCE_CONSTRAINT,
722 KICKED,
723 SHUTDOWN,
724 DESTROYED,
725 INVALID_NICK,
726 UNKNOWN,
727 NON_ANONYMOUS
728 }
729
730 private interface OnEventListener {
731 void onSuccess();
732
733 void onFailure();
734 }
735
736 public interface OnRenameListener extends OnEventListener {
737
738 }
739
740 public static class User implements Comparable<User>, AvatarService.Avatarable {
741 private Role role = Role.NONE;
742 private Affiliation affiliation = Affiliation.NONE;
743 private Jid realJid;
744 private Jid fullJid;
745 private long pgpKeyId = 0;
746 private Avatar avatar;
747 private MucOptions options;
748 private ChatState chatState = Config.DEFAULT_CHAT_STATE;
749
750 public User(MucOptions options, Jid fullJid) {
751 this.options = options;
752 this.fullJid = fullJid;
753 }
754
755 public String getName() {
756 return fullJid == null ? null : fullJid.getResource();
757 }
758
759 public Role getRole() {
760 return this.role;
761 }
762
763 public void setRole(String role) {
764 this.role = Role.of(role);
765 }
766
767 public Affiliation getAffiliation() {
768 return this.affiliation;
769 }
770
771 public void setAffiliation(String affiliation) {
772 this.affiliation = Affiliation.of(affiliation);
773 }
774
775 public long getPgpKeyId() {
776 if (this.pgpKeyId != 0) {
777 return this.pgpKeyId;
778 } else if (realJid != null) {
779 return getAccount().getRoster().getContact(realJid).getPgpKeyId();
780 } else {
781 return 0;
782 }
783 }
784
785 public void setPgpKeyId(long id) {
786 this.pgpKeyId = id;
787 }
788
789 public Contact getContact() {
790 if (fullJid != null) {
791 return getAccount().getRoster().getContactFromContactList(realJid);
792 } else if (realJid != null) {
793 return getAccount().getRoster().getContact(realJid);
794 } else {
795 return null;
796 }
797 }
798
799 public boolean setAvatar(Avatar avatar) {
800 if (this.avatar != null && this.avatar.equals(avatar)) {
801 return false;
802 } else {
803 this.avatar = avatar;
804 return true;
805 }
806 }
807
808 public String getAvatar() {
809 if (avatar != null) {
810 return avatar.getFilename();
811 }
812 Avatar avatar = realJid != null ? getAccount().getRoster().getContact(realJid).getAvatar() : null;
813 return avatar == null ? null : avatar.getFilename();
814 }
815
816 public Account getAccount() {
817 return options.getAccount();
818 }
819
820 public Conversation getConversation() {
821 return options.getConversation();
822 }
823
824 public Jid getFullJid() {
825 return fullJid;
826 }
827
828 @Override
829 public boolean equals(Object o) {
830 if (this == o) return true;
831 if (o == null || getClass() != o.getClass()) return false;
832
833 User user = (User) o;
834
835 if (role != user.role) return false;
836 if (affiliation != user.affiliation) return false;
837 if (realJid != null ? !realJid.equals(user.realJid) : user.realJid != null)
838 return false;
839 return fullJid != null ? fullJid.equals(user.fullJid) : user.fullJid == null;
840
841 }
842
843 public boolean isDomain() {
844 return realJid != null && realJid.getLocal() == null && role == Role.NONE;
845 }
846
847 @Override
848 public int hashCode() {
849 int result = role != null ? role.hashCode() : 0;
850 result = 31 * result + (affiliation != null ? affiliation.hashCode() : 0);
851 result = 31 * result + (realJid != null ? realJid.hashCode() : 0);
852 result = 31 * result + (fullJid != null ? fullJid.hashCode() : 0);
853 return result;
854 }
855
856 @Override
857 public String toString() {
858 return "[fulljid:" + String.valueOf(fullJid) + ",realjid:" + String.valueOf(realJid) + ",affiliation" + affiliation.toString() + "]";
859 }
860
861 public boolean realJidMatchesAccount() {
862 return realJid != null && realJid.equals(options.account.getJid().asBareJid());
863 }
864
865 @Override
866 public int compareTo(@NonNull User another) {
867 if (another.getAffiliation().outranks(getAffiliation())) {
868 return 1;
869 } else if (getAffiliation().outranks(another.getAffiliation())) {
870 return -1;
871 } else {
872 return getComparableName().compareToIgnoreCase(another.getComparableName());
873 }
874 }
875
876 public String getComparableName() {
877 Contact contact = getContact();
878 if (contact != null) {
879 return contact.getDisplayName();
880 } else {
881 String name = getName();
882 return name == null ? "" : name;
883 }
884 }
885
886 public Jid getRealJid() {
887 return realJid;
888 }
889
890 public void setRealJid(Jid jid) {
891 this.realJid = jid != null ? jid.asBareJid() : null;
892 }
893
894 public boolean setChatState(ChatState chatState) {
895 if (this.chatState == chatState) {
896 return false;
897 }
898 this.chatState = chatState;
899 return true;
900 }
901
902 @Override
903 public int getAvatarBackgroundColor() {
904 final String seed = realJid != null ? realJid.asBareJid().toString() : null;
905 return UIHelper.getColorForName(seed == null ? getName() : seed);
906 }
907
908 @Override
909 public String getAvatarName() {
910 return getConversation().getName().toString();
911 }
912 }
913}