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