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