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