MucOptions.java

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