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.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_CHAT_STATE;
 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 updateConfiguration(ServiceDiscoveryResult serviceDiscoveryResult) {
118        this.serviceDiscoveryResult = serviceDiscoveryResult;
119        String name;
120        Field roomConfigName = getRoomInfoForm().getFieldByName("muc#roomconfig_roomname");
121        if (roomConfigName != null) {
122            name = roomConfigName.getValue();
123        } else {
124            List<ServiceDiscoveryResult.Identity> identities = serviceDiscoveryResult.getIdentities();
125            String identityName = identities.size() > 0 ? identities.get(0).getName() : null;
126            final Jid jid = conversation.getJid();
127            if (identityName != null && !identityName.equals(jid == null ? null : jid.getEscapedLocal())) {
128                name = identityName;
129            } else {
130                name = null;
131            }
132        }
133        boolean changed = conversation.setAttribute("muc_name", name);
134        changed |= conversation.setAttribute(Conversation.ATTRIBUTE_MEMBERS_ONLY, this.hasFeature("muc_membersonly"));
135        changed |= conversation.setAttribute(Conversation.ATTRIBUTE_MODERATED, this.hasFeature("muc_moderated"));
136        changed |= conversation.setAttribute(Conversation.ATTRIBUTE_NON_ANONYMOUS, this.hasFeature("muc_nonanonymous"));
137        return changed;
138    }
139
140    private Data getRoomInfoForm() {
141        final List<Data> forms = serviceDiscoveryResult == null ? Collections.emptyList() : serviceDiscoveryResult.forms;
142        return forms.size() == 0 ? new Data() : forms.get(0);
143    }
144
145    public String getAvatar() {
146        return account.getRoster().getContact(conversation.getJid()).getAvatarFilename();
147    }
148
149    public boolean hasFeature(String feature) {
150        return this.serviceDiscoveryResult != null && this.serviceDiscoveryResult.features.contains(feature);
151    }
152
153    public boolean hasVCards() {
154        return hasFeature("vcard-temp");
155    }
156
157    public boolean canInvite() {
158        return !membersOnly() || self.getRole().ranks(Role.MODERATOR) || allowInvites();
159    }
160
161    public boolean allowInvites() {
162        final Field field = getRoomInfoForm().getFieldByName("muc#roomconfig_allowinvites");
163        return field != null && "1".equals(field.getValue());
164    }
165
166    public boolean canChangeSubject() {
167        return self.getRole().ranks(Role.MODERATOR) || participantsCanChangeSubject();
168    }
169
170    public boolean participantsCanChangeSubject() {
171        final Field field = getRoomInfoForm().getFieldByName("muc#roominfo_changesubject");
172        return field != null && "1".equals(field.getValue());
173    }
174
175    public boolean allowPm() {
176        final Field field = getRoomInfoForm().getFieldByName("muc#roomconfig_allowpm");
177        if (field == null) {
178            return true; //fall back if field does not exists
179        }
180        if ("anyone".equals(field.getValue())) {
181            return true;
182        } else if ("participants".equals(field.getValue())) {
183            return self.getRole().ranks(Role.PARTICIPANT);
184        } else if ("moderators".equals(field.getValue())) {
185            return self.getRole().ranks(Role.MODERATOR);
186        } else {
187            return false;
188        }
189    }
190
191    public boolean participating() {
192        return self.getRole().ranks(Role.PARTICIPANT) || !moderated();
193    }
194
195    public boolean membersOnly() {
196        return conversation.getBooleanAttribute(Conversation.ATTRIBUTE_MEMBERS_ONLY, false);
197    }
198
199    public List<String> getFeatures() {
200        return this.serviceDiscoveryResult != null ? this.serviceDiscoveryResult.features : Collections.emptyList();
201    }
202
203    public boolean nonanonymous() {
204        return conversation.getBooleanAttribute(Conversation.ATTRIBUTE_NON_ANONYMOUS, false);
205    }
206
207    public boolean isPrivateAndNonAnonymous() {
208        return membersOnly() && nonanonymous();
209    }
210
211    public boolean moderated() {
212        return conversation.getBooleanAttribute(Conversation.ATTRIBUTE_MODERATED, false);
213    }
214
215    public User deleteUser(Jid jid) {
216        User user = findUserByFullJid(jid);
217        if (user != null) {
218            synchronized (users) {
219                users.remove(user);
220                boolean realJidInMuc = false;
221                for (User u : users) {
222                    if (user.realJid != null && user.realJid.equals(u.realJid)) {
223                        realJidInMuc = true;
224                        break;
225                    }
226                }
227                boolean self = user.realJid != null && user.realJid.equals(account.getJid().asBareJid());
228                if (membersOnly()
229                        && nonanonymous()
230                        && user.affiliation.ranks(Affiliation.MEMBER)
231                        && user.realJid != null
232                        && !realJidInMuc
233                        && !self) {
234                    user.role = Role.NONE;
235                    user.avatar = null;
236                    user.fullJid = null;
237                    users.add(user);
238                }
239            }
240        }
241        return user;
242    }
243
244    //returns true if real jid was new;
245    public boolean updateUser(User user) {
246        User old;
247        boolean realJidFound = false;
248        if (user.fullJid == null && user.realJid != null) {
249            old = findUserByRealJid(user.realJid);
250            realJidFound = old != null;
251            if (old != null) {
252                if (old.fullJid != null) {
253                    return false; //don't add. user already exists
254                } else {
255                    synchronized (users) {
256                        users.remove(old);
257                    }
258                }
259            }
260        } else if (user.realJid != null) {
261            old = findUserByRealJid(user.realJid);
262            realJidFound = old != null;
263            synchronized (users) {
264                if (old != null && (old.fullJid == null || old.role == Role.NONE)) {
265                    users.remove(old);
266                }
267            }
268        }
269        old = findUserByFullJid(user.getFullJid());
270
271        synchronized (this.users) {
272            if (old != null) {
273                users.remove(old);
274            }
275            boolean fullJidIsSelf = isOnline && user.getFullJid() != null && user.getFullJid().equals(self.getFullJid());
276            if ((!membersOnly() || user.getAffiliation().ranks(Affiliation.MEMBER))
277                    && user.getAffiliation().outranks(Affiliation.OUTCAST)
278                    && !fullJidIsSelf) {
279                this.users.add(user);
280                return !realJidFound && user.realJid != null;
281            }
282        }
283        return false;
284    }
285
286    public User findUserByFullJid(Jid jid) {
287        if (jid == null) {
288            return null;
289        }
290        synchronized (users) {
291            for (User user : users) {
292                if (jid.equals(user.getFullJid())) {
293                    return user;
294                }
295            }
296        }
297        return null;
298    }
299
300    public User findUserByRealJid(Jid jid) {
301        if (jid == null) {
302            return null;
303        }
304        synchronized (users) {
305            for (User user : users) {
306                if (jid.equals(user.realJid)) {
307                    return user;
308                }
309            }
310        }
311        return null;
312    }
313
314    public User findOrCreateUserByRealJid(Jid jid, Jid fullJid) {
315        User user = findUserByRealJid(jid);
316        if (user == null) {
317            user = new User(this, fullJid);
318            user.setRealJid(jid);
319        }
320        return user;
321    }
322
323    public User findUser(ReadByMarker readByMarker) {
324        if (readByMarker.getRealJid() != null) {
325            return findOrCreateUserByRealJid(readByMarker.getRealJid().asBareJid(), readByMarker.getFullJid());
326        } else if (readByMarker.getFullJid() != null) {
327            return findUserByFullJid(readByMarker.getFullJid());
328        } else {
329            return null;
330        }
331    }
332
333    public boolean isContactInRoom(Contact contact) {
334        return contact != null && findUserByRealJid(contact.getJid().asBareJid()) != null;
335    }
336
337    public boolean isUserInRoom(Jid jid) {
338        return findUserByFullJid(jid) != null;
339    }
340
341    public boolean setOnline() {
342        boolean before = this.isOnline;
343        this.isOnline = true;
344        return !before;
345    }
346
347    public ArrayList<User> getUsers() {
348        return getUsers(true);
349    }
350
351    public ArrayList<User> getUsers(boolean includeOffline) {
352        synchronized (users) {
353            ArrayList<User> users = new ArrayList<>();
354            for (User user : this.users) {
355                if (!user.isDomain() && (includeOffline || user.getRole().ranks(Role.PARTICIPANT))) {
356                    users.add(user);
357                }
358            }
359            return users;
360        }
361    }
362
363    public ArrayList<User> getUsersWithChatState(ChatState state, int max) {
364        synchronized (users) {
365            ArrayList<User> list = new ArrayList<>();
366            for (User user : users) {
367                if (user.chatState == state) {
368                    list.add(user);
369                    if (list.size() >= max) {
370                        break;
371                    }
372                }
373            }
374            return list;
375        }
376    }
377
378    public List<User> getUsers(int max) {
379        ArrayList<User> subset = new ArrayList<>();
380        HashSet<Jid> jids = new HashSet<>();
381        jids.add(account.getJid().asBareJid());
382        synchronized (users) {
383            for (User user : users) {
384                if (user.getRealJid() == null || (user.getRealJid().getLocal() != null && jids.add(user.getRealJid()))) {
385                    subset.add(user);
386                }
387                if (subset.size() >= max) {
388                    break;
389                }
390            }
391        }
392        return subset;
393    }
394
395    public static List<User> sub(List<User> users, int max) {
396        ArrayList<User> subset = new ArrayList<>();
397        HashSet<Jid> jids = new HashSet<>();
398        for (User user : users) {
399            jids.add(user.getAccount().getJid().asBareJid());
400            if (user.getRealJid() == null || (user.getRealJid().getLocal() != null && jids.add(user.getRealJid()))) {
401                subset.add(user);
402            }
403            if (subset.size() >= max) {
404                break;
405            }
406        }
407        return subset;
408    }
409
410    public int getUserCount() {
411        synchronized (users) {
412            return users.size();
413        }
414    }
415
416    public String getProposedNick() {
417        final Bookmark bookmark = this.conversation.getBookmark();
418        final String bookmarkedNick = normalize(account.getJid(), bookmark == null ? null : bookmark.getNick());
419        if (bookmarkedNick != null) {
420            this.tookProposedNickFromBookmark = true;
421            return bookmarkedNick;
422        } else if (!conversation.getJid().isBareJid()) {
423            return conversation.getJid().getResource();
424        } else {
425            return defaultNick(account);
426        }
427    }
428
429    public static String defaultNick(final Account account) {
430        final String displayName = normalize(account.getJid(), account.getDisplayName());
431        if (displayName == null) {
432            return JidHelper.localPartOrFallback(account.getJid());
433        } else {
434            return displayName;
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_CHAT_STATE;
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}