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