MucOptions.java

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