MucOptions.java

  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 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 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 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 contact != null && 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    public 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            return defaultNick(account);
425        }
426    }
427
428    public static String defaultNick(final Account account) {
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    private static String normalize(Jid account, String nick) {
438        if (account == null || TextUtils.isEmpty(nick)) {
439            return null;
440        }
441        try {
442            return account.withResource(nick).getResource();
443        } catch (IllegalArgumentException e) {
444            return null;
445        }
446
447    }
448
449    public String getActualNick() {
450        if (this.self.getName() != null) {
451            return this.self.getName();
452        } else {
453            return this.getProposedNick();
454        }
455    }
456
457    public boolean online() {
458        return this.isOnline;
459    }
460
461    public Error getError() {
462        return this.error;
463    }
464
465    public void setError(Error error) {
466        this.isOnline = isOnline && error == Error.NONE;
467        this.error = error;
468    }
469
470    public void setOnRenameListener(OnRenameListener listener) {
471        this.onRenameListener = listener;
472    }
473
474    public void setOffline() {
475        synchronized (users) {
476            this.users.clear();
477        }
478        this.error = Error.NO_RESPONSE;
479        this.isOnline = false;
480    }
481
482    public User getSelf() {
483        return self;
484    }
485
486    public boolean setSubject(String subject) {
487        return this.conversation.setAttribute("subject", subject);
488    }
489
490    public String getSubject() {
491        return this.conversation.getAttribute("subject");
492    }
493
494    public String getName() {
495        return this.conversation.getAttribute("muc_name");
496    }
497
498    private List<User> getFallbackUsersFromCryptoTargets() {
499        List<User> users = new ArrayList<>();
500        for (Jid jid : conversation.getAcceptedCryptoTargets()) {
501            User user = new User(this, null);
502            user.setRealJid(jid);
503            users.add(user);
504        }
505        return users;
506    }
507
508    public List<User> getUsersRelevantForNameAndAvatar() {
509        final List<User> users;
510        if (isOnline) {
511            users = getUsers(5);
512        } else {
513            users = getFallbackUsersFromCryptoTargets();
514        }
515        return users;
516    }
517
518    String createNameFromParticipants() {
519        List<User> users = getUsersRelevantForNameAndAvatar();
520        if (users.size() >= 2) {
521            StringBuilder builder = new StringBuilder();
522            for (User user : users) {
523                if (builder.length() != 0) {
524                    builder.append(", ");
525                }
526                String name = UIHelper.getDisplayName(user);
527                if (name != null) {
528                    builder.append(name.split("\\s+")[0]);
529                }
530            }
531            return builder.toString();
532        } else {
533            return null;
534        }
535    }
536
537    public long[] getPgpKeyIds() {
538        List<Long> ids = new ArrayList<>();
539        for (User user : this.users) {
540            if (user.getPgpKeyId() != 0) {
541                ids.add(user.getPgpKeyId());
542            }
543        }
544        ids.add(account.getPgpId());
545        long[] primitiveLongArray = new long[ids.size()];
546        for (int i = 0; i < ids.size(); ++i) {
547            primitiveLongArray[i] = ids.get(i);
548        }
549        return primitiveLongArray;
550    }
551
552    public boolean pgpKeysInUse() {
553        synchronized (users) {
554            for (User user : users) {
555                if (user.getPgpKeyId() != 0) {
556                    return true;
557                }
558            }
559        }
560        return false;
561    }
562
563    public boolean everybodyHasKeys() {
564        synchronized (users) {
565            for (User user : users) {
566                if (user.getPgpKeyId() == 0) {
567                    return false;
568                }
569            }
570        }
571        return true;
572    }
573
574    public Jid createJoinJid(String nick) {
575        try {
576            return conversation.getJid().withResource(nick);
577        } catch (final IllegalArgumentException e) {
578            return null;
579        }
580    }
581
582    public Jid getTrueCounterpart(Jid jid) {
583        if (jid.equals(getSelf().getFullJid())) {
584            return account.getJid().asBareJid();
585        }
586        User user = findUserByFullJid(jid);
587        return user == null ? null : user.realJid;
588    }
589
590    public String getPassword() {
591        this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
592        if (this.password == null && conversation.getBookmark() != null
593                && conversation.getBookmark().getPassword() != null) {
594            return conversation.getBookmark().getPassword();
595        } else {
596            return this.password;
597        }
598    }
599
600    public void setPassword(String password) {
601        if (conversation.getBookmark() != null) {
602            conversation.getBookmark().setPassword(password);
603        } else {
604            this.password = password;
605        }
606        conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
607    }
608
609    public Conversation getConversation() {
610        return this.conversation;
611    }
612
613    public List<Jid> getMembers(final boolean includeDomains) {
614        ArrayList<Jid> members = new ArrayList<>();
615        synchronized (users) {
616            for (User user : users) {
617                if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null && !user.realJid.asBareJid().equals(conversation.account.getJid().asBareJid()) && (!user.isDomain() || includeDomains)) {
618                    members.add(user.realJid);
619                }
620            }
621        }
622        return members;
623    }
624
625    public enum Affiliation {
626        OWNER(4, R.string.owner),
627        ADMIN(3, R.string.admin),
628        MEMBER(2, R.string.member),
629        OUTCAST(0, R.string.outcast),
630        NONE(1, R.string.no_affiliation);
631
632        private int resId;
633        private int rank;
634
635        Affiliation(int rank, int resId) {
636            this.resId = resId;
637            this.rank = rank;
638        }
639
640        public static Affiliation of(@Nullable String value) {
641            if (value == null) {
642                return NONE;
643            }
644            try {
645                return Affiliation.valueOf(value.toUpperCase(Locale.US));
646            } catch (IllegalArgumentException e) {
647                return NONE;
648            }
649        }
650
651        public int getResId() {
652            return resId;
653        }
654
655        @Override
656        public String toString() {
657            return name().toLowerCase(Locale.US);
658        }
659
660        public boolean outranks(Affiliation affiliation) {
661            return rank > affiliation.rank;
662        }
663
664        public boolean ranks(Affiliation affiliation) {
665            return rank >= affiliation.rank;
666        }
667    }
668
669    public enum Role {
670        MODERATOR(R.string.moderator, 3),
671        VISITOR(R.string.visitor, 1),
672        PARTICIPANT(R.string.participant, 2),
673        NONE(R.string.no_role, 0);
674
675        private int resId;
676        private int rank;
677
678        Role(int resId, int rank) {
679            this.resId = resId;
680            this.rank = rank;
681        }
682
683        public static Role of(@Nullable String value) {
684            if (value == null) {
685                return NONE;
686            }
687            try {
688                return Role.valueOf(value.toUpperCase(Locale.US));
689            } catch (IllegalArgumentException e) {
690                return NONE;
691            }
692        }
693
694        public int getResId() {
695            return resId;
696        }
697
698        @Override
699        public String toString() {
700            return name().toLowerCase(Locale.US);
701        }
702
703        public boolean ranks(Role role) {
704            return rank >= role.rank;
705        }
706    }
707
708    public enum Error {
709        NO_RESPONSE,
710        SERVER_NOT_FOUND,
711        REMOTE_SERVER_TIMEOUT,
712        NONE,
713        NICK_IN_USE,
714        PASSWORD_REQUIRED,
715        BANNED,
716        MEMBERS_ONLY,
717        RESOURCE_CONSTRAINT,
718        KICKED,
719        SHUTDOWN,
720        DESTROYED,
721        INVALID_NICK,
722        UNKNOWN,
723        NON_ANONYMOUS
724    }
725
726    private interface OnEventListener {
727        void onSuccess();
728
729        void onFailure();
730    }
731
732    public interface OnRenameListener extends OnEventListener {
733
734    }
735
736    public static class User implements Comparable<User>, AvatarService.Avatarable {
737        private Role role = Role.NONE;
738        private Affiliation affiliation = Affiliation.NONE;
739        private Jid realJid;
740        private Jid fullJid;
741        private long pgpKeyId = 0;
742        private Avatar avatar;
743        private MucOptions options;
744        private ChatState chatState = Config.DEFAULT_CHAT_STATE;
745
746        public User(MucOptions options, Jid fullJid) {
747            this.options = options;
748            this.fullJid = fullJid;
749        }
750
751        public String getName() {
752            return fullJid == null ? null : fullJid.getResource();
753        }
754
755        public Role getRole() {
756            return this.role;
757        }
758
759        public void setRole(String role) {
760            this.role = Role.of(role);
761        }
762
763        public Affiliation getAffiliation() {
764            return this.affiliation;
765        }
766
767        public void setAffiliation(String affiliation) {
768            this.affiliation = Affiliation.of(affiliation);
769        }
770
771        public long getPgpKeyId() {
772            if (this.pgpKeyId != 0) {
773                return this.pgpKeyId;
774            } else if (realJid != null) {
775                return getAccount().getRoster().getContact(realJid).getPgpKeyId();
776            } else {
777                return 0;
778            }
779        }
780
781        public void setPgpKeyId(long id) {
782            this.pgpKeyId = id;
783        }
784
785        public Contact getContact() {
786            if (fullJid != null) {
787                return getAccount().getRoster().getContactFromContactList(realJid);
788            } else if (realJid != null) {
789                return getAccount().getRoster().getContact(realJid);
790            } else {
791                return null;
792            }
793        }
794
795        public boolean setAvatar(Avatar avatar) {
796            if (this.avatar != null && this.avatar.equals(avatar)) {
797                return false;
798            } else {
799                this.avatar = avatar;
800                return true;
801            }
802        }
803
804        public String getAvatar() {
805            if (avatar != null) {
806                return avatar.getFilename();
807            }
808            Avatar avatar = realJid != null ? getAccount().getRoster().getContact(realJid).getAvatar() : null;
809            return avatar == null ? null : avatar.getFilename();
810        }
811
812        public Account getAccount() {
813            return options.getAccount();
814        }
815
816        public Conversation getConversation() {
817            return options.getConversation();
818        }
819
820        public Jid getFullJid() {
821            return fullJid;
822        }
823
824        @Override
825        public boolean equals(Object o) {
826            if (this == o) return true;
827            if (o == null || getClass() != o.getClass()) return false;
828
829            User user = (User) o;
830
831            if (role != user.role) return false;
832            if (affiliation != user.affiliation) return false;
833            if (realJid != null ? !realJid.equals(user.realJid) : user.realJid != null)
834                return false;
835            return fullJid != null ? fullJid.equals(user.fullJid) : user.fullJid == null;
836
837        }
838
839        public boolean isDomain() {
840            return realJid != null && realJid.getLocal() == null && role == Role.NONE;
841        }
842
843        @Override
844        public int hashCode() {
845            int result = role != null ? role.hashCode() : 0;
846            result = 31 * result + (affiliation != null ? affiliation.hashCode() : 0);
847            result = 31 * result + (realJid != null ? realJid.hashCode() : 0);
848            result = 31 * result + (fullJid != null ? fullJid.hashCode() : 0);
849            return result;
850        }
851
852        @Override
853        public String toString() {
854            return "[fulljid:" + String.valueOf(fullJid) + ",realjid:" + String.valueOf(realJid) + ",affiliation" + affiliation.toString() + "]";
855        }
856
857        public boolean realJidMatchesAccount() {
858            return realJid != null && realJid.equals(options.account.getJid().asBareJid());
859        }
860
861        @Override
862        public int compareTo(@NonNull User another) {
863            if (another.getAffiliation().outranks(getAffiliation())) {
864                return 1;
865            } else if (getAffiliation().outranks(another.getAffiliation())) {
866                return -1;
867            } else {
868                return getComparableName().compareToIgnoreCase(another.getComparableName());
869            }
870        }
871
872        public String getComparableName() {
873            Contact contact = getContact();
874            if (contact != null) {
875                return contact.getDisplayName();
876            } else {
877                String name = getName();
878                return name == null ? "" : name;
879            }
880        }
881
882        public Jid getRealJid() {
883            return realJid;
884        }
885
886        public void setRealJid(Jid jid) {
887            this.realJid = jid != null ? jid.asBareJid() : null;
888        }
889
890        public boolean setChatState(ChatState chatState) {
891            if (this.chatState == chatState) {
892                return false;
893            }
894            this.chatState = chatState;
895            return true;
896        }
897
898        @Override
899        public int getAvatarBackgroundColor() {
900            final String seed = realJid != null ? realJid.asBareJid().toString() : null;
901            return UIHelper.getColorForName(seed == null ? getName() : seed);
902        }
903
904        @Override
905        public String getAvatarName() {
906            return getConversation().getName().toString();
907        }
908    }
909}