MucOptions.java

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