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