MucOptions.java

  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.utils.JidHelper;
 13import eu.siacs.conversations.utils.UIHelper;
 14import eu.siacs.conversations.xml.Namespace;
 15import eu.siacs.conversations.xmpp.chatstate.ChatState;
 16import eu.siacs.conversations.xmpp.forms.Data;
 17import eu.siacs.conversations.xmpp.forms.Field;
 18import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 19import eu.siacs.conversations.xmpp.jid.Jid;
 20import eu.siacs.conversations.xmpp.pep.Avatar;
 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.getResourcepart();
183		}
184
185		public void setRealJid(Jid jid) {
186			this.realJid = jid != null ? jid.toBareJid() : 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().toBareJid());
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 void updateFeatures(ArrayList<String> features) {
378		this.features.clear();
379		this.features.addAll(features);
380	}
381
382	public void updateFormData(Data form) {
383		this.form = form;
384	}
385
386	public boolean hasFeature(String feature) {
387		return this.features.contains(feature);
388	}
389
390	public boolean canInvite() {
391		Field field = this.form.getFieldByName("muc#roomconfig_allowinvites");
392		return !membersOnly() || self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
393	}
394
395	public boolean canChangeSubject() {
396		Field field = this.form.getFieldByName("muc#roomconfig_changesubject");
397		return self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
398	}
399
400	public boolean allowPm() {
401		Field field = this.form.getFieldByName("muc#roomconfig_allowpm");
402		return field == null || "1".equals(field.getValue());
403	}
404
405	public boolean participating() {
406		return !online()
407				|| self.getRole().ranks(Role.PARTICIPANT)
408				|| hasFeature("muc_unmoderated");
409	}
410
411	public boolean membersOnly() {
412		return hasFeature("muc_membersonly");
413	}
414
415	public boolean mamSupport() {
416		return hasFeature(Namespace.MAM) || hasFeature(Namespace.MAM_LEGACY);
417	}
418
419	public boolean mamLegacy() {
420		return hasFeature(Namespace.MAM_LEGACY) && !hasFeature(Namespace.MAM);
421	}
422
423	public boolean nonanonymous() {
424		return hasFeature("muc_nonanonymous");
425	}
426
427	public boolean isPrivateAndNonAnonymous() {
428		return membersOnly() && nonanonymous();
429	}
430
431	public boolean persistent() {
432		return hasFeature("muc_persistent");
433	}
434
435	public boolean moderated() {
436		return hasFeature("muc_moderated");
437	}
438
439	public User deleteUser(Jid jid) {
440		User user = findUserByFullJid(jid);
441		if (user != null) {
442			synchronized (users) {
443				users.remove(user);
444				boolean realJidInMuc = false;
445				for (User u : users) {
446					if (user.realJid != null && user.realJid.equals(u.realJid)) {
447						realJidInMuc = true;
448						break;
449					}
450				}
451				boolean self = user.realJid != null && user.realJid.equals(account.getJid().toBareJid());
452				if (membersOnly()
453						&& nonanonymous()
454						&& user.affiliation.ranks(Affiliation.MEMBER)
455						&& user.realJid != null
456						&& !realJidInMuc
457						&& !self) {
458					user.role = Role.NONE;
459					user.avatar = null;
460					user.fullJid = null;
461					users.add(user);
462				}
463			}
464		}
465		return user;
466	}
467
468	//returns true if real jid was new;
469	public boolean updateUser(User user) {
470		User old;
471		boolean realJidFound = false;
472		if (user.fullJid == null && user.realJid != null) {
473			old = findUserByRealJid(user.realJid);
474			realJidFound = old != null;
475			if (old != null) {
476				if (old.fullJid != null) {
477					return false; //don't add. user already exists
478				} else {
479					synchronized (users) {
480						users.remove(old);
481					}
482				}
483			}
484		} else if (user.realJid != null) {
485			old = findUserByRealJid(user.realJid);
486			realJidFound = old != null;
487			synchronized (users) {
488				if (old != null && old.fullJid == null) {
489					users.remove(old);
490				}
491			}
492		}
493		old = findUserByFullJid(user.getFullJid());
494		synchronized (this.users) {
495			if (old != null) {
496				users.remove(old);
497			}
498			boolean fullJidIsSelf = isOnline && user.getFullJid() != null && user.getFullJid().equals(self.getFullJid());
499			if ((!membersOnly() || user.getAffiliation().ranks(Affiliation.MEMBER))
500					&& user.getAffiliation().outranks(Affiliation.OUTCAST)
501					&& !fullJidIsSelf){
502				this.users.add(user);
503				return !realJidFound && user.realJid != null;
504			}
505		}
506		return false;
507	}
508
509	public User findUserByFullJid(Jid jid) {
510		if (jid == null) {
511			return null;
512		}
513		synchronized (users) {
514			for (User user : users) {
515				if (jid.equals(user.getFullJid())) {
516					return user;
517				}
518			}
519		}
520		return null;
521	}
522
523	public User findUserByRealJid(Jid jid) {
524		if (jid == null) {
525			return null;
526		}
527		synchronized (users) {
528			for (User user : users) {
529				if (jid.equals(user.realJid)) {
530					return user;
531				}
532			}
533		}
534		return null;
535	}
536
537	public User findUser(ReadByMarker readByMarker) {
538		if (readByMarker.getRealJid() != null) {
539			User user = findUserByRealJid(readByMarker.getRealJid().toBareJid());
540			if (user == null) {
541				user = new User(this,readByMarker.getFullJid());
542				user.setRealJid(readByMarker.getRealJid());
543			}
544			return user;
545		} else if (readByMarker.getFullJid() != null) {
546			return findUserByFullJid(readByMarker.getFullJid());
547		} else {
548			return null;
549		}
550	}
551
552	public boolean isContactInRoom(Contact contact) {
553		return findUserByRealJid(contact.getJid().toBareJid()) != null;
554	}
555
556	public boolean isUserInRoom(Jid jid) {
557		return findUserByFullJid(jid) != null;
558	}
559
560	public void setError(Error error) {
561		this.isOnline = isOnline && error == Error.NONE;
562		this.error = error;
563	}
564
565	public boolean setOnline() {
566		boolean before = this.isOnline;
567		this.isOnline = true;
568		return !before;
569	}
570
571	public ArrayList<User> getUsers() {
572		return getUsers(true);
573	}
574
575	public ArrayList<User> getUsers(boolean includeOffline) {
576		synchronized (users) {
577			if (includeOffline) {
578				return new ArrayList<>(users);
579			} else {
580				ArrayList<User> onlineUsers = new ArrayList<>();
581				for (User user : users) {
582					if (user.getRole().ranks(Role.PARTICIPANT)) {
583						onlineUsers.add(user);
584					}
585				}
586				return onlineUsers;
587			}
588		}
589	}
590
591	public ArrayList<User> getUsersWithChatState(ChatState state, int max) {
592		synchronized (users) {
593			ArrayList<User> list = new ArrayList<>();
594			for(User user : users) {
595				if (user.chatState == state) {
596					list.add(user);
597					if (list.size() >= max) {
598						break;
599					}
600				}
601			}
602			return list;
603		}
604	}
605
606	public List<User> getUsers(int max) {
607		ArrayList<User> subset = new ArrayList<>();
608		HashSet<Jid> jids = new HashSet<>();
609		jids.add(account.getJid().toBareJid());
610		synchronized (users) {
611			for(User user : users) {
612				if (user.getRealJid() == null || jids.add(user.getRealJid())) {
613					subset.add(user);
614				}
615				if (subset.size() >= max) {
616					break;
617				}
618			}
619		}
620		return subset;
621	}
622
623	public int getUserCount() {
624		synchronized (users) {
625			return users.size();
626		}
627	}
628
629	private String getProposedNick() {
630		if (conversation.getBookmark() != null
631				&& conversation.getBookmark().getNick() != null
632				&& !conversation.getBookmark().getNick().trim().isEmpty()) {
633			return conversation.getBookmark().getNick().trim();
634		} else if (!conversation.getJid().isBareJid()) {
635			return conversation.getJid().getResourcepart();
636		} else {
637			return JidHelper.localPartOrFallback(account.getJid());
638		}
639	}
640
641	public String getActualNick() {
642		if (this.self.getName() != null) {
643			return this.self.getName();
644		} else {
645			return this.getProposedNick();
646		}
647	}
648
649	public boolean online() {
650		return this.isOnline;
651	}
652
653	public Error getError() {
654		return this.error;
655	}
656
657	public void setOnRenameListener(OnRenameListener listener) {
658		this.onRenameListener = listener;
659	}
660
661	public void setOffline() {
662		synchronized (users) {
663			this.users.clear();
664		}
665		this.error = Error.NO_RESPONSE;
666		this.isOnline = false;
667	}
668
669	public User getSelf() {
670		return self;
671	}
672
673	public boolean setSubject(String subject) {
674		return this.conversation.setAttribute("subject",subject);
675	}
676
677	public String getSubject() {
678		return this.conversation.getAttribute("subject");
679	}
680
681	public List<User> getFallbackUsersFromCryptoTargets() {
682		List<User> users = new ArrayList<>();
683		for(Jid jid : conversation.getAcceptedCryptoTargets()) {
684			User user = new User(this,null);
685			user.setRealJid(jid);
686			users.add(user);
687		}
688		return users;
689	}
690
691	public List<User> getUsersRelevantForNameAndAvatar() {
692		final List<User> users;
693		if (isOnline) {
694			users = getUsers(5);
695		} else {
696			users = getFallbackUsersFromCryptoTargets();
697		}
698		return users;
699	}
700
701	public String createNameFromParticipants() {
702		List<User> users = getUsersRelevantForNameAndAvatar();
703		if (users.size() >= 2) {
704			StringBuilder builder = new StringBuilder();
705			for (User user : users) {
706				if (builder.length() != 0) {
707					builder.append(", ");
708				}
709				String name = UIHelper.getDisplayName(user);
710				if (name != null) {
711					builder.append(name.split("\\s+")[0]);
712				}
713			}
714			return builder.toString();
715		} else {
716			return null;
717		}
718	}
719
720	public long[] getPgpKeyIds() {
721		List<Long> ids = new ArrayList<>();
722		for (User user : this.users) {
723			if (user.getPgpKeyId() != 0) {
724				ids.add(user.getPgpKeyId());
725			}
726		}
727		ids.add(account.getPgpId());
728		long[] primitiveLongArray = new long[ids.size()];
729		for (int i = 0; i < ids.size(); ++i) {
730			primitiveLongArray[i] = ids.get(i);
731		}
732		return primitiveLongArray;
733	}
734
735	public boolean pgpKeysInUse() {
736		synchronized (users) {
737			for (User user : users) {
738				if (user.getPgpKeyId() != 0) {
739					return true;
740				}
741			}
742		}
743		return false;
744	}
745
746	public boolean everybodyHasKeys() {
747		synchronized (users) {
748			for (User user : users) {
749				if (user.getPgpKeyId() == 0) {
750					return false;
751				}
752			}
753		}
754		return true;
755	}
756
757	public Jid createJoinJid(String nick) {
758		try {
759			return Jid.fromString(this.conversation.getJid().toBareJid().toString() + "/" + nick);
760		} catch (final InvalidJidException e) {
761			return null;
762		}
763	}
764
765	public Jid getTrueCounterpart(Jid jid) {
766		if (jid.equals(getSelf().getFullJid())) {
767			return account.getJid().toBareJid();
768		}
769		User user = findUserByFullJid(jid);
770		return user == null ? null : user.realJid;
771	}
772
773	public String getPassword() {
774		this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
775		if (this.password == null && conversation.getBookmark() != null
776				&& conversation.getBookmark().getPassword() != null) {
777			return conversation.getBookmark().getPassword();
778		} else {
779			return this.password;
780		}
781	}
782
783	public void setPassword(String password) {
784		if (conversation.getBookmark() != null) {
785			conversation.getBookmark().setPassword(password);
786		} else {
787			this.password = password;
788		}
789		conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
790	}
791
792	public Conversation getConversation() {
793		return this.conversation;
794	}
795
796	public List<Jid> getMembers() {
797		ArrayList<Jid> members = new ArrayList<>();
798		synchronized (users) {
799			for (User user : users) {
800				if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null) {
801					members.add(user.realJid);
802				}
803			}
804		}
805		return members;
806	}
807}