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.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) {
548		synchronized (users) {
549			ArrayList<User> list = new ArrayList<>();
550			for(User user : users) {
551				if (user.chatState == state) {
552					list.add(user);
553				}
554			}
555			return list;
556		}
557	}
558
559	public List<User> getUsers(int max) {
560		ArrayList<User> subset = new ArrayList<>();
561		HashSet<Jid> jids = new HashSet<>();
562		jids.add(account.getJid().toBareJid());
563		synchronized (users) {
564			for(User user : users) {
565				if (user.getRealJid() == null || jids.add(user.getRealJid())) {
566					subset.add(user);
567				}
568				if (subset.size() >= max) {
569					break;
570				}
571			}
572		}
573		return subset;
574	}
575
576	public int getUserCount() {
577		synchronized (users) {
578			return users.size();
579		}
580	}
581
582	public String getProposedNick() {
583		if (conversation.getBookmark() != null
584				&& conversation.getBookmark().getNick() != null
585				&& !conversation.getBookmark().getNick().trim().isEmpty()) {
586			return conversation.getBookmark().getNick().trim();
587		} else if (!conversation.getJid().isBareJid()) {
588			return conversation.getJid().getResourcepart();
589		} else {
590			return account.getUsername();
591		}
592	}
593
594	public String getActualNick() {
595		if (this.self.getName() != null) {
596			return this.self.getName();
597		} else {
598			return this.getProposedNick();
599		}
600	}
601
602	public boolean online() {
603		return this.isOnline;
604	}
605
606	public Error getError() {
607		return this.error;
608	}
609
610	public void setOnRenameListener(OnRenameListener listener) {
611		this.onRenameListener = listener;
612	}
613
614	public void setOffline() {
615		synchronized (users) {
616			this.users.clear();
617		}
618		this.error = Error.NO_RESPONSE;
619		this.isOnline = false;
620	}
621
622	public User getSelf() {
623		return self;
624	}
625
626	public void setSubject(String content) {
627		this.subject = content;
628	}
629
630	public String getSubject() {
631		return this.subject;
632	}
633
634	public String createNameFromParticipants() {
635		if (getUserCount() >= 2) {
636			StringBuilder builder = new StringBuilder();
637			for (User user : getUsers(5)) {
638				if (builder.length() != 0) {
639					builder.append(", ");
640				}
641				Contact contact = user.getContact();
642				if (contact != null && !contact.getDisplayName().isEmpty()) {
643					builder.append(contact.getDisplayName().split("\\s+")[0]);
644				} else if (user.getName() != null){
645					builder.append(user.getName());
646				}
647			}
648			return builder.toString();
649		} else {
650			return null;
651		}
652	}
653
654	public long[] getPgpKeyIds() {
655		List<Long> ids = new ArrayList<>();
656		for (User user : this.users) {
657			if (user.getPgpKeyId() != 0) {
658				ids.add(user.getPgpKeyId());
659			}
660		}
661		ids.add(account.getPgpId());
662		long[] primitiveLongArray = new long[ids.size()];
663		for (int i = 0; i < ids.size(); ++i) {
664			primitiveLongArray[i] = ids.get(i);
665		}
666		return primitiveLongArray;
667	}
668
669	public boolean pgpKeysInUse() {
670		synchronized (users) {
671			for (User user : users) {
672				if (user.getPgpKeyId() != 0) {
673					return true;
674				}
675			}
676		}
677		return false;
678	}
679
680	public boolean everybodyHasKeys() {
681		synchronized (users) {
682			for (User user : users) {
683				if (user.getPgpKeyId() == 0) {
684					return false;
685				}
686			}
687		}
688		return true;
689	}
690
691	public Jid createJoinJid(String nick) {
692		try {
693			return Jid.fromString(this.conversation.getJid().toBareJid().toString() + "/" + nick);
694		} catch (final InvalidJidException e) {
695			return null;
696		}
697	}
698
699	public Jid getTrueCounterpart(Jid jid) {
700		if (jid.equals(getSelf().getFullJid())) {
701			return account.getJid().toBareJid();
702		}
703		User user = findUserByFullJid(jid);
704		return user == null ? null : user.realJid;
705	}
706
707	public String getPassword() {
708		this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
709		if (this.password == null && conversation.getBookmark() != null
710				&& conversation.getBookmark().getPassword() != null) {
711			return conversation.getBookmark().getPassword();
712		} else {
713			return this.password;
714		}
715	}
716
717	public void setPassword(String password) {
718		if (conversation.getBookmark() != null) {
719			conversation.getBookmark().setPassword(password);
720		} else {
721			this.password = password;
722		}
723		conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
724	}
725
726	public Conversation getConversation() {
727		return this.conversation;
728	}
729
730	public List<Jid> getMembers() {
731		ArrayList<Jid> members = new ArrayList<>();
732		synchronized (users) {
733			for (User user : users) {
734				if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null) {
735					members.add(user.realJid);
736				}
737			}
738		}
739		return members;
740	}
741}