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), null, 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) {
  80                user = new User(this, null, null, null, new HashSet<>());
  81                user.setRealJid(jid);
  82                user.setOnline(false);
  83                users.add(user);
  84            }
  85            user.affiliation = affiliation;
  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 (!fullJidIsSelf) {
 294                this.users.add(user);
 295                return !realJidFound && user.realJid != null;
 296            }
 297        }
 298        return false;
 299    }
 300
 301    public User findUserByName(final String name) {
 302        if (name == null) {
 303            return null;
 304        }
 305        synchronized (users) {
 306            for (User user : users) {
 307                if (name.equals(user.getName())) {
 308                    return user;
 309                }
 310            }
 311        }
 312        return null;
 313    }
 314
 315    public User findUserByFullJid(Jid jid) {
 316        if (jid == null) {
 317            return null;
 318        }
 319        synchronized (users) {
 320            for (User user : users) {
 321                if (jid.equals(user.getFullJid())) {
 322                    return user;
 323                }
 324            }
 325        }
 326        return null;
 327    }
 328
 329    public User findUserByRealJid(Jid jid) {
 330        if (jid == null) {
 331            return null;
 332        }
 333        synchronized (users) {
 334            for (User user : users) {
 335                if (jid.asBareJid().equals(user.realJid)) {
 336                    return user;
 337                }
 338            }
 339        }
 340        return null;
 341    }
 342
 343    public User findUserByOccupantId(final String id, final Jid counterpart) {
 344        if (id == null) {
 345            return null;
 346        }
 347        synchronized (users) {
 348            for (User user : users) {
 349                if (id.equals(user.getOccupantId())) {
 350                    return user;
 351                }
 352            }
 353        }
 354        final var user = new User(this, counterpart, id, null, new HashSet<>());
 355        user.setOnline(false);
 356        return user;
 357    }
 358
 359    public User findOrCreateUserByRealJid(Jid jid, Jid fullJid, final String occupantId) {
 360        User user = findUserByRealJid(jid);
 361        if (user == null) {
 362            user = new User(this, fullJid, occupantId, null, new HashSet<>());
 363            user.setRealJid(jid);
 364            user.setOnline(false);
 365        }
 366        return user;
 367    }
 368
 369    public User findUser(ReadByMarker readByMarker) {
 370        if (readByMarker.getRealJid() != null) {
 371            return findOrCreateUserByRealJid(readByMarker.getRealJid().asBareJid(), readByMarker.getFullJid(), null);
 372        } else if (readByMarker.getFullJid() != null) {
 373            return findUserByFullJid(readByMarker.getFullJid());
 374        } else {
 375            return null;
 376        }
 377    }
 378
 379    public boolean isContactInRoom(Contact contact) {
 380        return contact != null && isUserInRoom(findUserByRealJid(contact.getJid().asBareJid()));
 381    }
 382
 383    public boolean isUserInRoom(Jid jid) {
 384        return isUserInRoom(findUserByFullJid(jid));
 385    }
 386
 387    public boolean isUserInRoom(User user) {
 388        return user != null && user.isOnline();
 389    }
 390
 391    public boolean setOnline() {
 392        boolean before = this.isOnline;
 393        this.isOnline = true;
 394        return !before;
 395    }
 396
 397    public ArrayList<User> getUsers() {
 398        return getUsers(true);
 399    }
 400
 401    public ArrayList<User> getUsers(boolean includeOffline) {
 402        synchronized (users) {
 403            ArrayList<User> users = new ArrayList<>();
 404            for (User user : this.users) {
 405                if (!user.isDomain() && (includeOffline ? user.getAffiliation().ranks(Affiliation.NONE) : user.getRole().ranks(Role.PARTICIPANT))) {
 406                    users.add(user);
 407                }
 408            }
 409            return users;
 410        }
 411    }
 412
 413    public ArrayList<User> getUsersWithChatState(ChatState state, int max) {
 414        synchronized (users) {
 415            ArrayList<User> list = new ArrayList<>();
 416            for (User user : users) {
 417                if (user.chatState == state) {
 418                    list.add(user);
 419                    if (list.size() >= max) {
 420                        break;
 421                    }
 422                }
 423            }
 424            return list;
 425        }
 426    }
 427
 428    public List<User> getUsers(int max) {
 429        ArrayList<User> subset = new ArrayList<>();
 430        HashSet<Jid> jids = new HashSet<>();
 431        jids.add(account.getJid().asBareJid());
 432        synchronized (users) {
 433            for (User user : users) {
 434                if (user.getRealJid() == null || (user.getRealJid().getLocal() != null && jids.add(user.getRealJid()))) {
 435                    subset.add(user);
 436                }
 437                if (subset.size() >= max) {
 438                    break;
 439                }
 440            }
 441        }
 442        return subset;
 443    }
 444
 445    public static List<User> sub(List<User> users, int max) {
 446        ArrayList<User> subset = new ArrayList<>();
 447        HashSet<Jid> jids = new HashSet<>();
 448        for (User user : users) {
 449            jids.add(user.getAccount().getJid().asBareJid());
 450            if (user.getRealJid() == null || (user.getRealJid().getLocal() != null && jids.add(user.getRealJid()))) {
 451                subset.add(user);
 452            }
 453            if (subset.size() >= max) {
 454                break;
 455            }
 456        }
 457        return subset;
 458    }
 459
 460    public int getUserCount() {
 461        synchronized (users) {
 462            return users.size();
 463        }
 464    }
 465
 466    public String getProposedNick() {
 467        return getProposedNick(null);
 468    }
 469
 470    public String getProposedNick(final String mucNick) {
 471        final Bookmark bookmark = this.conversation.getBookmark();
 472        final String bookmarkedNick = normalize(account.getJid(), bookmark == null ? null : bookmark.getNick());
 473        if (bookmarkedNick != null) {
 474            this.tookProposedNickFromBookmark = true;
 475            return bookmarkedNick;
 476        } else if (mucNick != null) {
 477            return mucNick;
 478        } else if (!conversation.getJid().isBareJid()) {
 479            return conversation.getJid().getResource();
 480        } else {
 481            return defaultNick(account);
 482        }
 483    }
 484
 485    public static String defaultNick(final Account account) {
 486        final String displayName = normalize(account.getJid(), account.getDisplayName());
 487        if (displayName == null) {
 488            return JidHelper.localPartOrFallback(account.getJid());
 489        } else {
 490            return displayName;
 491        }
 492    }
 493
 494    private static String normalize(Jid account, String nick) {
 495        if (account == null || TextUtils.isEmpty(nick)) {
 496            return null;
 497        }
 498
 499        try {
 500            return account.withResource(nick).getResource();
 501        } catch (IllegalArgumentException e) {
 502            return nick;
 503        }
 504    }
 505
 506    public String getActualNick() {
 507        if (this.self.getNick() != null) {
 508            return this.self.getNick();
 509        } else {
 510            return this.getProposedNick();
 511        }
 512    }
 513
 514    public String getActualName() {
 515        if (this.self.getName() != null) {
 516            return this.self.getName();
 517        } else {
 518            return this.getProposedNick();
 519        }
 520    }
 521
 522    public boolean online() {
 523        return this.isOnline;
 524    }
 525
 526    public Error getError() {
 527        return this.error;
 528    }
 529
 530    public void setError(Error error) {
 531        this.isOnline = isOnline && error == Error.NONE;
 532        this.error = error;
 533    }
 534
 535    public void setOnRenameListener(OnRenameListener listener) {
 536        this.onRenameListener = listener;
 537    }
 538
 539    public void setOffline() {
 540        synchronized (users) {
 541            this.users.clear();
 542        }
 543        this.error = Error.NO_RESPONSE;
 544        this.isOnline = false;
 545    }
 546
 547    public User getSelf() {
 548        return self;
 549    }
 550
 551    public boolean setSubject(String subject) {
 552        return this.conversation.setAttribute("subject", subject);
 553    }
 554
 555    public String getSubject() {
 556        return this.conversation.getAttribute("subject");
 557    }
 558
 559    public String getName() {
 560        return this.conversation.getAttribute("muc_name");
 561    }
 562
 563    private List<User> getFallbackUsersFromCryptoTargets() {
 564        List<User> users = new ArrayList<>();
 565        for (Jid jid : conversation.getAcceptedCryptoTargets()) {
 566            User user = new User(this, null, null, null, new HashSet<>());
 567            user.setRealJid(jid);
 568            users.add(user);
 569        }
 570        return users;
 571    }
 572
 573    public List<User> getUsersRelevantForNameAndAvatar() {
 574        final List<User> users;
 575        if (isOnline) {
 576            users = getUsers(5);
 577        } else {
 578            users = getFallbackUsersFromCryptoTargets();
 579        }
 580        return users;
 581    }
 582
 583    String createNameFromParticipants() {
 584        List<User> users = getUsersRelevantForNameAndAvatar();
 585        if (users.size() >= 2) {
 586            StringBuilder builder = new StringBuilder();
 587            for (User user : users) {
 588                if (builder.length() != 0) {
 589                    builder.append(", ");
 590                }
 591                String name = UIHelper.getDisplayName(user);
 592                if (name != null) {
 593                    builder.append(name.split("\\s+")[0]);
 594                }
 595            }
 596            return builder.toString();
 597        } else {
 598            return null;
 599        }
 600    }
 601
 602    public long[] getPgpKeyIds() {
 603        List<Long> ids = new ArrayList<>();
 604        for (User user : this.users) {
 605            if (user.getPgpKeyId() != 0) {
 606                ids.add(user.getPgpKeyId());
 607            }
 608        }
 609        ids.add(account.getPgpId());
 610        long[] primitiveLongArray = new long[ids.size()];
 611        for (int i = 0; i < ids.size(); ++i) {
 612            primitiveLongArray[i] = ids.get(i);
 613        }
 614        return primitiveLongArray;
 615    }
 616
 617    public boolean pgpKeysInUse() {
 618        synchronized (users) {
 619            for (User user : users) {
 620                if (user.getPgpKeyId() != 0) {
 621                    return true;
 622                }
 623            }
 624        }
 625        return false;
 626    }
 627
 628    public boolean everybodyHasKeys() {
 629        synchronized (users) {
 630            for (User user : users) {
 631                if (user.getPgpKeyId() == 0) {
 632                    return false;
 633                }
 634            }
 635        }
 636        return true;
 637    }
 638
 639    public Jid createJoinJid(String nick) {
 640        return createJoinJid(nick, true);
 641    }
 642
 643    private Jid createJoinJid(String nick, boolean tryFix) {
 644        try {
 645            return conversation.getJid().withResource(nick);
 646        } catch (final IllegalArgumentException e) {
 647            try {
 648                return tryFix ? createJoinJid(gnu.inet.encoding.Punycode.encode(nick), false) : null;
 649            } catch (final Exception e2) {
 650                return null;
 651            }
 652        }
 653    }
 654
 655    public Jid getTrueCounterpart(Jid jid) {
 656        if (jid.equals(getSelf().getFullJid())) {
 657            return account.getJid().asBareJid();
 658        }
 659        User user = findUserByFullJid(jid);
 660        return user == null ? null : user.realJid;
 661    }
 662
 663    public String getPassword() {
 664        this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
 665        if (this.password == null && conversation.getBookmark() != null
 666                && conversation.getBookmark().getPassword() != null) {
 667            return conversation.getBookmark().getPassword();
 668        } else {
 669            return this.password;
 670        }
 671    }
 672
 673    public void setPassword(String password) {
 674        if (conversation.getBookmark() != null) {
 675            conversation.getBookmark().setPassword(password);
 676        } else {
 677            this.password = password;
 678        }
 679        conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
 680    }
 681
 682    public Conversation getConversation() {
 683        return this.conversation;
 684    }
 685
 686    public List<Jid> getMembers(final boolean includeDomains) {
 687        ArrayList<Jid> members = new ArrayList<>();
 688        synchronized (users) {
 689            for (User user : users) {
 690                if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null && !user.realJid.asBareJid().equals(conversation.account.getJid().asBareJid()) && (!user.isDomain() || includeDomains)) {
 691                    members.add(user.realJid);
 692                }
 693            }
 694        }
 695        return members;
 696    }
 697
 698    public enum Affiliation {
 699        OWNER(4, R.string.owner),
 700        ADMIN(3, R.string.admin),
 701        MEMBER(2, R.string.member),
 702        OUTCAST(0, R.string.outcast),
 703        NONE(1, R.string.no_affiliation);
 704
 705        private final int resId;
 706        private final int rank;
 707
 708        Affiliation(int rank, int resId) {
 709            this.resId = resId;
 710            this.rank = rank;
 711        }
 712
 713        public static Affiliation of(@Nullable String value) {
 714            if (value == null) {
 715                return NONE;
 716            }
 717            try {
 718                return Affiliation.valueOf(value.toUpperCase(Locale.US));
 719            } catch (IllegalArgumentException e) {
 720                return NONE;
 721            }
 722        }
 723
 724        public int getResId() {
 725            return resId;
 726        }
 727
 728        @Override
 729        public String toString() {
 730            return name().toLowerCase(Locale.US);
 731        }
 732
 733        public boolean outranks(Affiliation affiliation) {
 734            return rank > affiliation.rank;
 735        }
 736
 737        public boolean ranks(Affiliation affiliation) {
 738            return rank >= affiliation.rank;
 739        }
 740    }
 741
 742    public enum Role {
 743        MODERATOR(R.string.moderator, 3),
 744        VISITOR(R.string.visitor, 1),
 745        PARTICIPANT(R.string.participant, 2),
 746        NONE(R.string.no_role, 0);
 747
 748        private final int resId;
 749        private final int rank;
 750
 751        Role(int resId, int rank) {
 752            this.resId = resId;
 753            this.rank = rank;
 754        }
 755
 756        public static Role of(@Nullable String value) {
 757            if (value == null) {
 758                return NONE;
 759            }
 760            try {
 761                return Role.valueOf(value.toUpperCase(Locale.US));
 762            } catch (IllegalArgumentException e) {
 763                return NONE;
 764            }
 765        }
 766
 767        public int getResId() {
 768            return resId;
 769        }
 770
 771        @Override
 772        public String toString() {
 773            return name().toLowerCase(Locale.US);
 774        }
 775
 776        public boolean ranks(Role role) {
 777            return rank >= role.rank;
 778        }
 779    }
 780
 781    public enum Error {
 782        NO_RESPONSE,
 783        SERVER_NOT_FOUND,
 784        REMOTE_SERVER_TIMEOUT,
 785        NONE,
 786        NICK_IN_USE,
 787        PASSWORD_REQUIRED,
 788        BANNED,
 789        MEMBERS_ONLY,
 790        RESOURCE_CONSTRAINT,
 791        KICKED,
 792        SHUTDOWN,
 793        DESTROYED,
 794        INVALID_NICK,
 795        TECHNICAL_PROBLEMS,
 796        UNKNOWN,
 797        NON_ANONYMOUS
 798    }
 799
 800    private interface OnEventListener {
 801        void onSuccess();
 802
 803        void onFailure();
 804    }
 805
 806    public interface OnRenameListener extends OnEventListener {
 807
 808    }
 809
 810    public static class Hat implements Comparable<Hat> {
 811        private final Uri uri;
 812        private final String title;
 813
 814        public Hat(final Element el) {
 815            Uri parseUri = null; // null hat uri is invaild per spec
 816            try {
 817                parseUri = Uri.parse(el.getAttribute("uri"));
 818            } catch (final Exception e) { }
 819            uri = parseUri;
 820
 821            title = el.getAttribute("title");
 822        }
 823
 824        public Hat(final Uri uri, final String title) {
 825            this.uri = uri;
 826            this.title = title;
 827        }
 828
 829        public String toString() {
 830            return title == null ? "" : title;
 831        }
 832
 833        public int getColor() {
 834            return UIHelper.getColorForName(uri == null ? toString() : uri.toString());
 835        }
 836
 837        @Override
 838        public int compareTo(@NonNull Hat another) {
 839            return toString().compareTo(another.toString());
 840        }
 841    }
 842
 843    public static class User implements Comparable<User>, AvatarService.Avatarable {
 844        private Role role = Role.NONE;
 845        private Affiliation affiliation = Affiliation.NONE;
 846        private Jid realJid;
 847        private Jid fullJid;
 848        protected String nick;
 849        private long pgpKeyId = 0;
 850        protected Avatar avatar;
 851        private final MucOptions options;
 852        private ChatState chatState = Config.DEFAULT_CHAT_STATE;
 853        protected Set<Hat> hats;
 854        protected String occupantId;
 855        protected boolean online = true;
 856
 857        public User(MucOptions options, Jid fullJid, final String occupantId, final String nick, final Set<Hat> hats) {
 858            this.options = options;
 859            this.fullJid = fullJid;
 860            this.occupantId = occupantId;
 861            this.nick = nick;
 862            this.hats = hats;
 863
 864            if (occupantId != null && options != null) {
 865                final var sha1sum = options.getConversation().getAttribute("occupantAvatar/" + occupantId);
 866                if (sha1sum != null) {
 867                    avatar = new Avatar();
 868                    avatar.sha1sum = sha1sum;
 869                    avatar.owner = fullJid;
 870                }
 871
 872                if (nick == null) {
 873                    this.nick = options.getConversation().getAttribute("occupantNick/" + occupantId);
 874                } else if (!getNick().equals(getName())) {
 875                    options.getConversation().setAttribute("occupantNick/" + occupantId, nick);
 876                } else {
 877                    options.getConversation().setAttribute("occupantNick/" + occupantId, (String) null);
 878                }
 879            }
 880        }
 881
 882        public String getName() {
 883            return fullJid == null ? null : fullJid.getResource();
 884        }
 885
 886        public Jid getMuc() {
 887            return fullJid == null ? (options.getConversation().getJid().asBareJid()) : fullJid.asBareJid();
 888        }
 889
 890        public String getOccupantId() {
 891            return occupantId;
 892        }
 893
 894        public String getNick() {
 895            return nick == null ? getName() : nick;
 896        }
 897
 898        public void setOnline(final boolean o) {
 899            online = o;
 900        }
 901
 902        public boolean isOnline() {
 903            return fullJid != null && online;
 904        }
 905
 906        public Role getRole() {
 907            return this.role;
 908        }
 909
 910        public void setRole(String role) {
 911            this.role = Role.of(role);
 912        }
 913
 914        public Affiliation getAffiliation() {
 915            return this.affiliation;
 916        }
 917
 918        public void setAffiliation(String affiliation) {
 919            this.affiliation = Affiliation.of(affiliation);
 920        }
 921
 922        public Set<Hat> getHats() {
 923            return this.hats == null ? new HashSet<>() : hats;
 924        }
 925
 926        public long getPgpKeyId() {
 927            if (this.pgpKeyId != 0) {
 928                return this.pgpKeyId;
 929            } else if (realJid != null) {
 930                return getAccount().getRoster().getContact(realJid).getPgpKeyId();
 931            } else {
 932                return 0;
 933            }
 934        }
 935
 936        public void setPgpKeyId(long id) {
 937            this.pgpKeyId = id;
 938        }
 939
 940        public Contact getContact() {
 941            if (fullJid != null) {
 942                return getAccount().getRoster().getContactFromContactList(realJid);
 943            } else if (realJid != null) {
 944                return getAccount().getRoster().getContact(realJid);
 945            } else {
 946                return null;
 947            }
 948        }
 949
 950        public boolean setAvatar(Avatar avatar) {
 951            if (occupantId != null) {
 952                options.getConversation().setAttribute("occupantAvatar/" + occupantId, getContact() == null && avatar != null ? avatar.sha1sum : null);
 953            }
 954            if (this.avatar != null && this.avatar.equals(avatar)) {
 955                return false;
 956            } else {
 957                this.avatar = avatar;
 958                return true;
 959            }
 960        }
 961
 962        public String getAvatar() {
 963            if (avatar != null) {
 964                return avatar.getFilename();
 965            }
 966            Avatar avatar = realJid != null ? getAccount().getRoster().getContact(realJid).getAvatar() : null;
 967            return avatar == null ? null : avatar.getFilename();
 968        }
 969
 970        public Cid getAvatarCid() {
 971            if (avatar != null) {
 972                return avatar.cid();
 973            }
 974            Avatar avatar = realJid != null ? getAccount().getRoster().getContact(realJid).getAvatar() : null;
 975            return avatar == null ? null : avatar.cid();
 976        }
 977
 978        public Account getAccount() {
 979            return options.getAccount();
 980        }
 981
 982        public Conversation getConversation() {
 983            return options.getConversation();
 984        }
 985
 986        public Jid getFullJid() {
 987            return fullJid;
 988        }
 989
 990        @Override
 991        public boolean equals(Object o) {
 992            if (this == o) return true;
 993            if (o == null || getClass() != o.getClass()) return false;
 994
 995            User user = (User) o;
 996
 997            if (role != user.role) return false;
 998            if (affiliation != user.affiliation) return false;
 999            if (realJid != null ? !realJid.equals(user.realJid) : user.realJid != null)
1000                return false;
1001            return fullJid != null ? fullJid.equals(user.fullJid) : user.fullJid == null;
1002
1003        }
1004
1005        public boolean isDomain() {
1006            return realJid != null && realJid.getLocal() == null && role == Role.NONE;
1007        }
1008
1009        @Override
1010        public int hashCode() {
1011            int result = role != null ? role.hashCode() : 0;
1012            result = 31 * result + (affiliation != null ? affiliation.hashCode() : 0);
1013            result = 31 * result + (realJid != null ? realJid.hashCode() : 0);
1014            result = 31 * result + (fullJid != null ? fullJid.hashCode() : 0);
1015            return result;
1016        }
1017
1018        @Override
1019        public String toString() {
1020            return "[fulljid:" + fullJid + ",realjid:" + realJid + ",nick:" + nick + ",affiliation" + affiliation.toString() + "]";
1021        }
1022
1023        public boolean realJidMatchesAccount() {
1024            return realJid != null && realJid.equals(options.account.getJid().asBareJid());
1025        }
1026
1027        @Override
1028        public int compareTo(@NonNull User another) {
1029            if (another.getAffiliation().outranks(getAffiliation())) {
1030                return 1;
1031            } else if (getAffiliation().outranks(another.getAffiliation())) {
1032                return -1;
1033            } else {
1034                return getComparableName().compareToIgnoreCase(another.getComparableName());
1035            }
1036        }
1037
1038        public String getComparableName() {
1039            Contact contact = getContact();
1040            if (contact != null) {
1041                return contact.getDisplayName();
1042            } else {
1043                String name = getName();
1044                return name == null ? "" : name;
1045            }
1046        }
1047
1048        public Jid getRealJid() {
1049            return realJid;
1050        }
1051
1052        public void setRealJid(Jid jid) {
1053            this.realJid = jid != null ? jid.asBareJid() : null;
1054        }
1055
1056        public boolean setChatState(ChatState chatState) {
1057            if (this.chatState == chatState) {
1058                return false;
1059            }
1060            this.chatState = chatState;
1061            return true;
1062        }
1063
1064        @Override
1065        public int getAvatarBackgroundColor() {
1066            final String seed = realJid != null ? realJid.asBareJid().toString() : null;
1067            return UIHelper.getColorForName(seed == null ? getName() : seed);
1068        }
1069
1070        @Override
1071        public String getAvatarName() {
1072            return getConversation().getName().toString();
1073        }
1074    }
1075}