MucOptions.java

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