MucOptions.java

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