1package eu.siacs.conversations.entities;
2
3import android.annotation.SuppressLint;
4
5import java.util.ArrayList;
6import java.util.HashSet;
7import java.util.List;
8import java.util.Set;
9
10import eu.siacs.conversations.Config;
11import eu.siacs.conversations.R;
12import eu.siacs.conversations.xml.Namespace;
13import eu.siacs.conversations.xmpp.chatstate.ChatState;
14import eu.siacs.conversations.xmpp.forms.Data;
15import eu.siacs.conversations.xmpp.forms.Field;
16import eu.siacs.conversations.xmpp.jid.InvalidJidException;
17import eu.siacs.conversations.xmpp.jid.Jid;
18import eu.siacs.conversations.xmpp.pep.Avatar;
19
20@SuppressLint("DefaultLocale")
21public class MucOptions {
22
23 private boolean mAutoPushConfiguration = true;
24
25 public Account getAccount() {
26 return this.conversation.getAccount();
27 }
28
29 public void setSelf(User user) {
30 this.self = user;
31 }
32
33 public void changeAffiliation(Jid jid, Affiliation affiliation) {
34 User user = findUserByRealJid(jid);
35 synchronized (users) {
36 if (user != null && user.getRole() == Role.NONE) {
37 users.remove(user);
38 if (affiliation.ranks(Affiliation.MEMBER)) {
39 user.affiliation = affiliation;
40 users.add(user);
41 }
42 }
43 }
44 }
45
46 public void flagNoAutoPushConfiguration() {
47 mAutoPushConfiguration = false;
48 }
49
50 public boolean autoPushConfiguration() {
51 return mAutoPushConfiguration;
52 }
53
54 public boolean isSelf(Jid counterpart) {
55 return counterpart.getResourcepart().equals(getActualNick());
56 }
57
58 public void resetChatState() {
59 synchronized (users) {
60 for(User user : users) {
61 user.chatState = Config.DEFAULT_CHATSTATE;
62 }
63 }
64 }
65
66 public enum Affiliation {
67 OWNER("owner", 4, R.string.owner),
68 ADMIN("admin", 3, R.string.admin),
69 MEMBER("member", 2, R.string.member),
70 OUTCAST("outcast", 0, R.string.outcast),
71 NONE("none", 1, R.string.no_affiliation);
72
73 Affiliation(String string, int rank, int resId) {
74 this.string = string;
75 this.resId = resId;
76 this.rank = rank;
77 }
78
79 private String string;
80 private int resId;
81 private int rank;
82
83 public int getResId() {
84 return resId;
85 }
86
87 @Override
88 public String toString() {
89 return this.string;
90 }
91
92 public boolean outranks(Affiliation affiliation) {
93 return rank > affiliation.rank;
94 }
95
96 public boolean ranks(Affiliation affiliation) {
97 return rank >= affiliation.rank;
98 }
99 }
100
101 public enum Role {
102 MODERATOR("moderator", R.string.moderator,3),
103 VISITOR("visitor", R.string.visitor,1),
104 PARTICIPANT("participant", R.string.participant,2),
105 NONE("none", R.string.no_role,0);
106
107 Role(String string, int resId, int rank) {
108 this.string = string;
109 this.resId = resId;
110 this.rank = rank;
111 }
112
113 private String string;
114 private int resId;
115 private int rank;
116
117 public int getResId() {
118 return resId;
119 }
120
121 @Override
122 public String toString() {
123 return this.string;
124 }
125
126 public boolean ranks(Role role) {
127 return rank >= role.rank;
128 }
129 }
130
131 public enum Error {
132 NO_RESPONSE,
133 SERVER_NOT_FOUND,
134 NONE,
135 NICK_IN_USE,
136 PASSWORD_REQUIRED,
137 BANNED,
138 MEMBERS_ONLY,
139 KICKED,
140 SHUTDOWN,
141 UNKNOWN
142 }
143
144 public static final String STATUS_CODE_SELF_PRESENCE = "110";
145 public static final String STATUS_CODE_ROOM_CREATED = "201";
146 public static final String STATUS_CODE_BANNED = "301";
147 public static final String STATUS_CODE_CHANGED_NICK = "303";
148 public static final String STATUS_CODE_KICKED = "307";
149 public static final String STATUS_CODE_AFFILIATION_CHANGE = "321";
150 public static final String STATUS_CODE_LOST_MEMBERSHIP = "322";
151 public static final String STATUS_CODE_SHUTDOWN = "332";
152
153 private interface OnEventListener {
154 void onSuccess();
155
156 void onFailure();
157 }
158
159 public interface OnRenameListener extends OnEventListener {
160
161 }
162
163 public static class User implements Comparable<User> {
164 private Role role = Role.NONE;
165 private Affiliation affiliation = Affiliation.NONE;
166 private Jid realJid;
167 private Jid fullJid;
168 private long pgpKeyId = 0;
169 private Avatar avatar;
170 private MucOptions options;
171 private ChatState chatState = Config.DEFAULT_CHATSTATE;
172
173 public User(MucOptions options, Jid from) {
174 this.options = options;
175 this.fullJid = from;
176 }
177
178 public String getName() {
179 return fullJid == null ? null : fullJid.getResourcepart();
180 }
181
182 public void setRealJid(Jid jid) {
183 this.realJid = jid != null ? jid.toBareJid() : null;
184 }
185
186 public Role getRole() {
187 return this.role;
188 }
189
190 public void setRole(String role) {
191 if (role == null) {
192 this.role = Role.NONE;
193 return;
194 }
195 role = role.toLowerCase();
196 switch (role) {
197 case "moderator":
198 this.role = Role.MODERATOR;
199 break;
200 case "participant":
201 this.role = Role.PARTICIPANT;
202 break;
203 case "visitor":
204 this.role = Role.VISITOR;
205 break;
206 default:
207 this.role = Role.NONE;
208 break;
209 }
210 }
211
212 public Affiliation getAffiliation() {
213 return this.affiliation;
214 }
215
216 public void setAffiliation(String affiliation) {
217 if (affiliation == null) {
218 this.affiliation = Affiliation.NONE;
219 return;
220 }
221 affiliation = affiliation.toLowerCase();
222 switch (affiliation) {
223 case "admin":
224 this.affiliation = Affiliation.ADMIN;
225 break;
226 case "owner":
227 this.affiliation = Affiliation.OWNER;
228 break;
229 case "member":
230 this.affiliation = Affiliation.MEMBER;
231 break;
232 case "outcast":
233 this.affiliation = Affiliation.OUTCAST;
234 break;
235 default:
236 this.affiliation = Affiliation.NONE;
237 }
238 }
239
240 public void setPgpKeyId(long id) {
241 this.pgpKeyId = id;
242 }
243
244 public long getPgpKeyId() {
245 if (this.pgpKeyId != 0) {
246 return this.pgpKeyId;
247 } else if (realJid != null) {
248 return getAccount().getRoster().getContact(realJid).getPgpKeyId();
249 } else {
250 return 0;
251 }
252 }
253
254 public Contact getContact() {
255 if (fullJid != null) {
256 return getAccount().getRoster().getContactFromRoster(realJid);
257 } else if (realJid != null){
258 return getAccount().getRoster().getContact(realJid);
259 } else {
260 return null;
261 }
262 }
263
264 public boolean setAvatar(Avatar avatar) {
265 if (this.avatar != null && this.avatar.equals(avatar)) {
266 return false;
267 } else {
268 this.avatar = avatar;
269 return true;
270 }
271 }
272
273 public String getAvatar() {
274 return avatar == null ? null : avatar.getFilename();
275 }
276
277 public Account getAccount() {
278 return options.getAccount();
279 }
280
281 public Jid getFullJid() {
282 return fullJid;
283 }
284
285 @Override
286 public boolean equals(Object o) {
287 if (this == o) return true;
288 if (o == null || getClass() != o.getClass()) return false;
289
290 User user = (User) o;
291
292 if (role != user.role) return false;
293 if (affiliation != user.affiliation) return false;
294 if (realJid != null ? !realJid.equals(user.realJid) : user.realJid != null)
295 return false;
296 return fullJid != null ? fullJid.equals(user.fullJid) : user.fullJid == null;
297
298 }
299
300 @Override
301 public int hashCode() {
302 int result = role != null ? role.hashCode() : 0;
303 result = 31 * result + (affiliation != null ? affiliation.hashCode() : 0);
304 result = 31 * result + (realJid != null ? realJid.hashCode() : 0);
305 result = 31 * result + (fullJid != null ? fullJid.hashCode() : 0);
306 return result;
307 }
308
309 @Override
310 public String toString() {
311 return "[fulljid:"+String.valueOf(fullJid)+",realjid:"+String.valueOf(realJid)+",affiliation"+affiliation.toString()+"]";
312 }
313
314 public boolean realJidMatchesAccount() {
315 return realJid != null && realJid.equals(options.account.getJid().toBareJid());
316 }
317
318 @Override
319 public int compareTo(User another) {
320 if (another.getAffiliation().outranks(getAffiliation())) {
321 return 1;
322 } else if (getAffiliation().outranks(another.getAffiliation())) {
323 return -1;
324 } else {
325 return getComparableName().compareToIgnoreCase(another.getComparableName());
326 }
327 }
328
329
330 private String getComparableName() {
331 Contact contact = getContact();
332 if (contact != null) {
333 return contact.getDisplayName();
334 } else {
335 String name = getName();
336 return name == null ? "" : name;
337 }
338 }
339
340 public Jid getRealJid() {
341 return realJid;
342 }
343
344 public boolean setChatState(ChatState chatState) {
345 if (this.chatState == chatState) {
346 return false;
347 }
348 this.chatState = chatState;
349 return true;
350 }
351 }
352
353 private Account account;
354 private final Set<User> users = new HashSet<>();
355 private final List<String> features = new ArrayList<>();
356 private Data form = new Data();
357 private Conversation conversation;
358 private boolean isOnline = false;
359 private Error error = Error.NONE;
360 public OnRenameListener onRenameListener = null;
361 private User self;
362 private String subject = null;
363 private String password = null;
364
365 public MucOptions(Conversation conversation) {
366 this.account = conversation.getAccount();
367 this.conversation = conversation;
368 this.self = new User(this,createJoinJid(getProposedNick()));
369 }
370
371 public void updateFeatures(ArrayList<String> features) {
372 this.features.clear();
373 this.features.addAll(features);
374 }
375
376 public void updateFormData(Data form) {
377 this.form = form;
378 }
379
380 public boolean hasFeature(String feature) {
381 return this.features.contains(feature);
382 }
383
384 public boolean canInvite() {
385 Field field = this.form.getFieldByName("muc#roomconfig_allowinvites");
386 return !membersOnly() || self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
387 }
388
389 public boolean canChangeSubject() {
390 Field field = this.form.getFieldByName("muc#roomconfig_changesubject");
391 return self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
392 }
393
394 public boolean participating() {
395 return !online()
396 || self.getRole().ranks(Role.PARTICIPANT)
397 || hasFeature("muc_unmoderated");
398 }
399
400 public boolean membersOnly() {
401 return hasFeature("muc_membersonly");
402 }
403
404 public boolean mamSupport() {
405 return hasFeature(Namespace.MAM) || hasFeature(Namespace.MAM_LEGACY);
406 }
407
408 public boolean mamLegacy() {
409 return hasFeature(Namespace.MAM_LEGACY) && !hasFeature(Namespace.MAM);
410 }
411
412 public boolean nonanonymous() {
413 return hasFeature("muc_nonanonymous");
414 }
415
416 public boolean persistent() {
417 return hasFeature("muc_persistent");
418 }
419
420 public boolean moderated() {
421 return hasFeature("muc_moderated");
422 }
423
424 public User deleteUser(Jid jid) {
425 User user = findUserByFullJid(jid);
426 if (user != null) {
427 synchronized (users) {
428 users.remove(user);
429 boolean realJidInMuc = false;
430 for (User u : users) {
431 if (user.realJid != null && user.realJid.equals(u.realJid)) {
432 realJidInMuc = true;
433 break;
434 }
435 }
436 boolean self = user.realJid != null && user.realJid.equals(account.getJid().toBareJid());
437 if (membersOnly()
438 && nonanonymous()
439 && user.affiliation.ranks(Affiliation.MEMBER)
440 && user.realJid != null
441 && !realJidInMuc
442 && !self) {
443 user.role = Role.NONE;
444 user.avatar = null;
445 user.fullJid = null;
446 users.add(user);
447 }
448 }
449 }
450 return user;
451 }
452
453 public void updateUser(User user) {
454 User old;
455 if (user.fullJid == null && user.realJid != null) {
456 old = findUserByRealJid(user.realJid);
457 if (old != null) {
458 if (old.fullJid != null) {
459 return; //don't add. user already exists
460 } else {
461 synchronized (users) {
462 users.remove(old);
463 }
464 }
465 }
466 } else if (user.realJid != null) {
467 old = findUserByRealJid(user.realJid);
468 synchronized (users) {
469 if (old != null && old.fullJid == null) {
470 users.remove(old);
471 }
472 }
473 }
474 old = findUserByFullJid(user.getFullJid());
475 synchronized (this.users) {
476 if (old != null) {
477 users.remove(old);
478 }
479 boolean fullJidIsSelf = isOnline && user.getFullJid() != null && user.getFullJid().equals(self.getFullJid());
480 if ((!membersOnly() || user.getAffiliation().ranks(Affiliation.MEMBER))
481 && user.getAffiliation().outranks(Affiliation.OUTCAST)
482 && !fullJidIsSelf){
483 this.users.add(user);
484 }
485 }
486 }
487
488 public User findUserByFullJid(Jid jid) {
489 if (jid == null) {
490 return null;
491 }
492 synchronized (users) {
493 for (User user : users) {
494 if (jid.equals(user.getFullJid())) {
495 return user;
496 }
497 }
498 }
499 return null;
500 }
501
502 private User findUserByRealJid(Jid jid) {
503 if (jid == null) {
504 return null;
505 }
506 synchronized (users) {
507 for (User user : users) {
508 if (jid.equals(user.realJid)) {
509 return user;
510 }
511 }
512 }
513 return null;
514 }
515
516 public boolean isContactInRoom(Contact contact) {
517 return findUserByRealJid(contact.getJid().toBareJid()) != null;
518 }
519
520 public boolean isUserInRoom(Jid jid) {
521 return findUserByFullJid(jid) != null;
522 }
523
524 public void setError(Error error) {
525 this.isOnline = isOnline && error == Error.NONE;
526 this.error = error;
527 }
528
529 public void setOnline() {
530 this.isOnline = true;
531 }
532
533 public ArrayList<User> getUsers() {
534 return getUsers(true);
535 }
536
537 public ArrayList<User> getUsers(boolean includeOffline) {
538 synchronized (users) {
539 if (includeOffline) {
540 return new ArrayList<>(users);
541 } else {
542 ArrayList<User> onlineUsers = new ArrayList<>();
543 for (User user : users) {
544 if (user.getRole().ranks(Role.PARTICIPANT)) {
545 onlineUsers.add(user);
546 }
547 }
548 return onlineUsers;
549 }
550 }
551 }
552
553 public ArrayList<User> getUsersWithChatState(ChatState state, int max) {
554 synchronized (users) {
555 ArrayList<User> list = new ArrayList<>();
556 for(User user : users) {
557 if (user.chatState == state) {
558 list.add(user);
559 if (list.size() >= max) {
560 break;
561 }
562 }
563 }
564 return list;
565 }
566 }
567
568 public List<User> getUsers(int max) {
569 ArrayList<User> subset = new ArrayList<>();
570 HashSet<Jid> jids = new HashSet<>();
571 jids.add(account.getJid().toBareJid());
572 synchronized (users) {
573 for(User user : users) {
574 if (user.getRealJid() == null || jids.add(user.getRealJid())) {
575 subset.add(user);
576 }
577 if (subset.size() >= max) {
578 break;
579 }
580 }
581 }
582 return subset;
583 }
584
585 public int getUserCount() {
586 synchronized (users) {
587 return users.size();
588 }
589 }
590
591 public String getProposedNick() {
592 if (conversation.getBookmark() != null
593 && conversation.getBookmark().getNick() != null
594 && !conversation.getBookmark().getNick().trim().isEmpty()) {
595 return conversation.getBookmark().getNick().trim();
596 } else if (!conversation.getJid().isBareJid()) {
597 return conversation.getJid().getResourcepart();
598 } else {
599 return account.getUsername();
600 }
601 }
602
603 public String getActualNick() {
604 if (this.self.getName() != null) {
605 return this.self.getName();
606 } else {
607 return this.getProposedNick();
608 }
609 }
610
611 public boolean online() {
612 return this.isOnline;
613 }
614
615 public Error getError() {
616 return this.error;
617 }
618
619 public void setOnRenameListener(OnRenameListener listener) {
620 this.onRenameListener = listener;
621 }
622
623 public void setOffline() {
624 synchronized (users) {
625 this.users.clear();
626 }
627 this.error = Error.NO_RESPONSE;
628 this.isOnline = false;
629 }
630
631 public User getSelf() {
632 return self;
633 }
634
635 public void setSubject(String content) {
636 this.subject = content;
637 }
638
639 public String getSubject() {
640 return this.subject;
641 }
642
643 public String createNameFromParticipants() {
644 if (getUserCount() >= 2) {
645 StringBuilder builder = new StringBuilder();
646 for (User user : getUsers(5)) {
647 if (builder.length() != 0) {
648 builder.append(", ");
649 }
650 Contact contact = user.getContact();
651 if (contact != null && !contact.getDisplayName().isEmpty()) {
652 builder.append(contact.getDisplayName().split("\\s+")[0]);
653 } else if (user.getName() != null){
654 builder.append(user.getName());
655 }
656 }
657 return builder.toString();
658 } else {
659 return null;
660 }
661 }
662
663 public long[] getPgpKeyIds() {
664 List<Long> ids = new ArrayList<>();
665 for (User user : this.users) {
666 if (user.getPgpKeyId() != 0) {
667 ids.add(user.getPgpKeyId());
668 }
669 }
670 ids.add(account.getPgpId());
671 long[] primitiveLongArray = new long[ids.size()];
672 for (int i = 0; i < ids.size(); ++i) {
673 primitiveLongArray[i] = ids.get(i);
674 }
675 return primitiveLongArray;
676 }
677
678 public boolean pgpKeysInUse() {
679 synchronized (users) {
680 for (User user : users) {
681 if (user.getPgpKeyId() != 0) {
682 return true;
683 }
684 }
685 }
686 return false;
687 }
688
689 public boolean everybodyHasKeys() {
690 synchronized (users) {
691 for (User user : users) {
692 if (user.getPgpKeyId() == 0) {
693 return false;
694 }
695 }
696 }
697 return true;
698 }
699
700 public Jid createJoinJid(String nick) {
701 try {
702 return Jid.fromString(this.conversation.getJid().toBareJid().toString() + "/" + nick);
703 } catch (final InvalidJidException e) {
704 return null;
705 }
706 }
707
708 public Jid getTrueCounterpart(Jid jid) {
709 if (jid.equals(getSelf().getFullJid())) {
710 return account.getJid().toBareJid();
711 }
712 User user = findUserByFullJid(jid);
713 return user == null ? null : user.realJid;
714 }
715
716 public String getPassword() {
717 this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
718 if (this.password == null && conversation.getBookmark() != null
719 && conversation.getBookmark().getPassword() != null) {
720 return conversation.getBookmark().getPassword();
721 } else {
722 return this.password;
723 }
724 }
725
726 public void setPassword(String password) {
727 if (conversation.getBookmark() != null) {
728 conversation.getBookmark().setPassword(password);
729 } else {
730 this.password = password;
731 }
732 conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
733 }
734
735 public Conversation getConversation() {
736 return this.conversation;
737 }
738
739 public List<Jid> getMembers() {
740 ArrayList<Jid> members = new ArrayList<>();
741 synchronized (users) {
742 for (User user : users) {
743 if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null) {
744 members.add(user.realJid);
745 }
746 }
747 }
748 return members;
749 }
750}