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 return this.pgpKeyId;
246 }
247
248 public Contact getContact() {
249 if (fullJid != null) {
250 return getAccount().getRoster().getContactFromRoster(realJid);
251 } else if (realJid != null){
252 return getAccount().getRoster().getContact(realJid);
253 } else {
254 return null;
255 }
256 }
257
258 public boolean setAvatar(Avatar avatar) {
259 if (this.avatar != null && this.avatar.equals(avatar)) {
260 return false;
261 } else {
262 this.avatar = avatar;
263 return true;
264 }
265 }
266
267 public String getAvatar() {
268 return avatar == null ? null : avatar.getFilename();
269 }
270
271 public Account getAccount() {
272 return options.getAccount();
273 }
274
275 public Jid getFullJid() {
276 return fullJid;
277 }
278
279 @Override
280 public boolean equals(Object o) {
281 if (this == o) return true;
282 if (o == null || getClass() != o.getClass()) return false;
283
284 User user = (User) o;
285
286 if (role != user.role) return false;
287 if (affiliation != user.affiliation) return false;
288 if (realJid != null ? !realJid.equals(user.realJid) : user.realJid != null)
289 return false;
290 return fullJid != null ? fullJid.equals(user.fullJid) : user.fullJid == null;
291
292 }
293
294 @Override
295 public int hashCode() {
296 int result = role != null ? role.hashCode() : 0;
297 result = 31 * result + (affiliation != null ? affiliation.hashCode() : 0);
298 result = 31 * result + (realJid != null ? realJid.hashCode() : 0);
299 result = 31 * result + (fullJid != null ? fullJid.hashCode() : 0);
300 return result;
301 }
302
303 @Override
304 public String toString() {
305 return "[fulljid:"+String.valueOf(fullJid)+",realjid:"+String.valueOf(realJid)+",affiliation"+affiliation.toString()+"]";
306 }
307
308 public boolean realJidMatchesAccount() {
309 return realJid != null && realJid.equals(options.account.getJid().toBareJid());
310 }
311
312 @Override
313 public int compareTo(User another) {
314 if (another.getAffiliation().outranks(getAffiliation())) {
315 return 1;
316 } else if (getAffiliation().outranks(another.getAffiliation())) {
317 return -1;
318 } else {
319 return getComparableName().compareToIgnoreCase(another.getComparableName());
320 }
321 }
322
323
324 private String getComparableName() {
325 Contact contact = getContact();
326 if (contact != null) {
327 return contact.getDisplayName();
328 } else {
329 String name = getName();
330 return name == null ? "" : name;
331 }
332 }
333
334 public Jid getRealJid() {
335 return realJid;
336 }
337
338 public boolean setChatState(ChatState chatState) {
339 if (this.chatState == chatState) {
340 return false;
341 }
342 this.chatState = chatState;
343 return true;
344 }
345 }
346
347 private Account account;
348 private final Set<User> users = new HashSet<>();
349 private final List<String> features = new ArrayList<>();
350 private Data form = new Data();
351 private Conversation conversation;
352 private boolean isOnline = false;
353 private Error error = Error.NONE;
354 public OnRenameListener onRenameListener = null;
355 private User self;
356 private String subject = null;
357 private String password = null;
358
359 public MucOptions(Conversation conversation) {
360 this.account = conversation.getAccount();
361 this.conversation = conversation;
362 this.self = new User(this,createJoinJid(getProposedNick()));
363 }
364
365 public void updateFeatures(ArrayList<String> features) {
366 this.features.clear();
367 this.features.addAll(features);
368 }
369
370 public void updateFormData(Data form) {
371 this.form = form;
372 }
373
374 public boolean hasFeature(String feature) {
375 return this.features.contains(feature);
376 }
377
378 public boolean canInvite() {
379 Field field = this.form.getFieldByName("muc#roomconfig_allowinvites");
380 return !membersOnly() || self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
381 }
382
383 public boolean canChangeSubject() {
384 Field field = this.form.getFieldByName("muc#roomconfig_changesubject");
385 return self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
386 }
387
388 public boolean participating() {
389 return !online()
390 || self.getRole().ranks(Role.PARTICIPANT)
391 || hasFeature("muc_unmoderated");
392 }
393
394 public boolean membersOnly() {
395 return hasFeature("muc_membersonly");
396 }
397
398 public boolean mamSupport() {
399 return hasFeature(Namespace.MAM) || hasFeature(Namespace.MAM_LEGACY);
400 }
401
402 public boolean mamLegacy() {
403 return hasFeature(Namespace.MAM_LEGACY) && !hasFeature(Namespace.MAM);
404 }
405
406 public boolean nonanonymous() {
407 return hasFeature("muc_nonanonymous");
408 }
409
410 public boolean persistent() {
411 return hasFeature("muc_persistent");
412 }
413
414 public boolean moderated() {
415 return hasFeature("muc_moderated");
416 }
417
418 public User deleteUser(Jid jid) {
419 User user = findUserByFullJid(jid);
420 if (user != null) {
421 synchronized (users) {
422 users.remove(user);
423 boolean realJidInMuc = false;
424 for (User u : users) {
425 if (user.realJid != null && user.realJid.equals(u.realJid)) {
426 realJidInMuc = true;
427 break;
428 }
429 }
430 boolean self = user.realJid != null && user.realJid.equals(account.getJid().toBareJid());
431 if (membersOnly()
432 && nonanonymous()
433 && user.affiliation.ranks(Affiliation.MEMBER)
434 && user.realJid != null
435 && !realJidInMuc
436 && !self) {
437 user.role = Role.NONE;
438 user.avatar = null;
439 user.fullJid = null;
440 users.add(user);
441 }
442 }
443 }
444 return user;
445 }
446
447 public void updateUser(User user) {
448 User old;
449 if (user.fullJid == null && user.realJid != null) {
450 old = findUserByRealJid(user.realJid);
451 if (old != null) {
452 if (old.fullJid != null) {
453 return; //don't add. user already exists
454 } else {
455 synchronized (users) {
456 users.remove(old);
457 }
458 }
459 }
460 } else if (user.realJid != null) {
461 old = findUserByRealJid(user.realJid);
462 synchronized (users) {
463 if (old != null && old.fullJid == null) {
464 users.remove(old);
465 }
466 }
467 }
468 old = findUserByFullJid(user.getFullJid());
469 synchronized (this.users) {
470 if (old != null) {
471 users.remove(old);
472 }
473 boolean fullJidIsSelf = isOnline && user.getFullJid() != null && user.getFullJid().equals(self.getFullJid());
474 if ((!membersOnly() || user.getAffiliation().ranks(Affiliation.MEMBER))
475 && user.getAffiliation().outranks(Affiliation.OUTCAST)
476 && !fullJidIsSelf){
477 this.users.add(user);
478 }
479 }
480 }
481
482 public User findUserByFullJid(Jid jid) {
483 if (jid == null) {
484 return null;
485 }
486 synchronized (users) {
487 for (User user : users) {
488 if (jid.equals(user.getFullJid())) {
489 return user;
490 }
491 }
492 }
493 return null;
494 }
495
496 private User findUserByRealJid(Jid jid) {
497 if (jid == null) {
498 return null;
499 }
500 synchronized (users) {
501 for (User user : users) {
502 if (jid.equals(user.realJid)) {
503 return user;
504 }
505 }
506 }
507 return null;
508 }
509
510 public boolean isContactInRoom(Contact contact) {
511 return findUserByRealJid(contact.getJid().toBareJid()) != null;
512 }
513
514 public boolean isUserInRoom(Jid jid) {
515 return findUserByFullJid(jid) != null;
516 }
517
518 public void setError(Error error) {
519 this.isOnline = isOnline && error == Error.NONE;
520 this.error = error;
521 }
522
523 public void setOnline() {
524 this.isOnline = true;
525 }
526
527 public ArrayList<User> getUsers() {
528 return getUsers(true);
529 }
530
531 public ArrayList<User> getUsers(boolean includeOffline) {
532 synchronized (users) {
533 if (includeOffline) {
534 return new ArrayList<>(users);
535 } else {
536 ArrayList<User> onlineUsers = new ArrayList<>();
537 for (User user : users) {
538 if (user.getRole().ranks(Role.PARTICIPANT)) {
539 onlineUsers.add(user);
540 }
541 }
542 return onlineUsers;
543 }
544 }
545 }
546
547 public ArrayList<User> getUsersWithChatState(ChatState state, int max) {
548 synchronized (users) {
549 ArrayList<User> list = new ArrayList<>();
550 for(User user : users) {
551 if (user.chatState == state) {
552 list.add(user);
553 if (list.size() >= max) {
554 break;
555 }
556 }
557 }
558 return list;
559 }
560 }
561
562 public List<User> getUsers(int max) {
563 ArrayList<User> subset = new ArrayList<>();
564 HashSet<Jid> jids = new HashSet<>();
565 jids.add(account.getJid().toBareJid());
566 synchronized (users) {
567 for(User user : users) {
568 if (user.getRealJid() == null || jids.add(user.getRealJid())) {
569 subset.add(user);
570 }
571 if (subset.size() >= max) {
572 break;
573 }
574 }
575 }
576 return subset;
577 }
578
579 public int getUserCount() {
580 synchronized (users) {
581 return users.size();
582 }
583 }
584
585 public String getProposedNick() {
586 if (conversation.getBookmark() != null
587 && conversation.getBookmark().getNick() != null
588 && !conversation.getBookmark().getNick().trim().isEmpty()) {
589 return conversation.getBookmark().getNick().trim();
590 } else if (!conversation.getJid().isBareJid()) {
591 return conversation.getJid().getResourcepart();
592 } else {
593 return account.getUsername();
594 }
595 }
596
597 public String getActualNick() {
598 if (this.self.getName() != null) {
599 return this.self.getName();
600 } else {
601 return this.getProposedNick();
602 }
603 }
604
605 public boolean online() {
606 return this.isOnline;
607 }
608
609 public Error getError() {
610 return this.error;
611 }
612
613 public void setOnRenameListener(OnRenameListener listener) {
614 this.onRenameListener = listener;
615 }
616
617 public void setOffline() {
618 synchronized (users) {
619 this.users.clear();
620 }
621 this.error = Error.NO_RESPONSE;
622 this.isOnline = false;
623 }
624
625 public User getSelf() {
626 return self;
627 }
628
629 public void setSubject(String content) {
630 this.subject = content;
631 }
632
633 public String getSubject() {
634 return this.subject;
635 }
636
637 public String createNameFromParticipants() {
638 if (getUserCount() >= 2) {
639 StringBuilder builder = new StringBuilder();
640 for (User user : getUsers(5)) {
641 if (builder.length() != 0) {
642 builder.append(", ");
643 }
644 Contact contact = user.getContact();
645 if (contact != null && !contact.getDisplayName().isEmpty()) {
646 builder.append(contact.getDisplayName().split("\\s+")[0]);
647 } else if (user.getName() != null){
648 builder.append(user.getName());
649 }
650 }
651 return builder.toString();
652 } else {
653 return null;
654 }
655 }
656
657 public long[] getPgpKeyIds() {
658 List<Long> ids = new ArrayList<>();
659 for (User user : this.users) {
660 if (user.getPgpKeyId() != 0) {
661 ids.add(user.getPgpKeyId());
662 }
663 }
664 ids.add(account.getPgpId());
665 long[] primitiveLongArray = new long[ids.size()];
666 for (int i = 0; i < ids.size(); ++i) {
667 primitiveLongArray[i] = ids.get(i);
668 }
669 return primitiveLongArray;
670 }
671
672 public boolean pgpKeysInUse() {
673 synchronized (users) {
674 for (User user : users) {
675 if (user.getPgpKeyId() != 0) {
676 return true;
677 }
678 }
679 }
680 return false;
681 }
682
683 public boolean everybodyHasKeys() {
684 synchronized (users) {
685 for (User user : users) {
686 if (user.getPgpKeyId() == 0) {
687 return false;
688 }
689 }
690 }
691 return true;
692 }
693
694 public Jid createJoinJid(String nick) {
695 try {
696 return Jid.fromString(this.conversation.getJid().toBareJid().toString() + "/" + nick);
697 } catch (final InvalidJidException e) {
698 return null;
699 }
700 }
701
702 public Jid getTrueCounterpart(Jid jid) {
703 if (jid.equals(getSelf().getFullJid())) {
704 return account.getJid().toBareJid();
705 }
706 User user = findUserByFullJid(jid);
707 return user == null ? null : user.realJid;
708 }
709
710 public String getPassword() {
711 this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
712 if (this.password == null && conversation.getBookmark() != null
713 && conversation.getBookmark().getPassword() != null) {
714 return conversation.getBookmark().getPassword();
715 } else {
716 return this.password;
717 }
718 }
719
720 public void setPassword(String password) {
721 if (conversation.getBookmark() != null) {
722 conversation.getBookmark().setPassword(password);
723 } else {
724 this.password = password;
725 }
726 conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
727 }
728
729 public Conversation getConversation() {
730 return this.conversation;
731 }
732
733 public List<Jid> getMembers() {
734 ArrayList<Jid> members = new ArrayList<>();
735 synchronized (users) {
736 for (User user : users) {
737 if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null) {
738 members.add(user.realJid);
739 }
740 }
741 }
742 return members;
743 }
744}