MucOptions.java

  1package eu.siacs.conversations.entities;
  2
  3import android.annotation.SuppressLint;
  4
  5import java.util.ArrayList;
  6import java.util.Collections;
  7import java.util.HashSet;
  8import java.util.List;
  9import java.util.Set;
 10
 11import eu.siacs.conversations.R;
 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		NONE,
120		NICK_IN_USE,
121		PASSWORD_REQUIRED,
122		BANNED,
123		MEMBERS_ONLY,
124		KICKED,
125		SHUTDOWN,
126		UNKNOWN
127	}
128
129	public static final String STATUS_CODE_ROOM_CONFIG_CHANGED = "104";
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			Contact ourContact = getContact();
300			Contact anotherContact = another.getContact();
301			if (ourContact != null && anotherContact != null) {
302				return ourContact.compareTo(anotherContact);
303			} else if (ourContact == null && anotherContact != null) {
304				return getName().compareToIgnoreCase(anotherContact.getDisplayName());
305			} else if (ourContact != null) {
306				return ourContact.getDisplayName().compareToIgnoreCase(another.getName());
307			} else {
308				return getName().compareToIgnoreCase(another.getName());
309			}
310		}
311
312		public Jid getRealJid() {
313			return realJid;
314		}
315	}
316
317	private Account account;
318	private final Set<User> users = new HashSet<>();
319	private final List<String> features = new ArrayList<>();
320	private Data form = new Data();
321	private Conversation conversation;
322	private boolean isOnline = false;
323	private Error error = Error.NONE;
324	public OnRenameListener onRenameListener = null;
325	private User self;
326	private String subject = null;
327	private String password = null;
328	public boolean mNickChangingInProgress = false;
329
330	public MucOptions(Conversation conversation) {
331		this.account = conversation.getAccount();
332		this.conversation = conversation;
333		this.self = new User(this,createJoinJid(getProposedNick()));
334	}
335
336	public void updateFeatures(ArrayList<String> features) {
337		this.features.clear();
338		this.features.addAll(features);
339	}
340
341	public void updateFormData(Data form) {
342		this.form = form;
343	}
344
345	public boolean hasFeature(String feature) {
346		return this.features.contains(feature);
347	}
348
349	public boolean canInvite() {
350		Field field = this.form.getFieldByName("muc#roomconfig_allowinvites");
351		return !membersOnly() || self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
352	}
353
354	public boolean canChangeSubject() {
355		Field field = this.form.getFieldByName("muc#roomconfig_changesubject");
356		return self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
357	}
358
359	public boolean participating() {
360		return !online()
361				|| self.getRole().ranks(Role.PARTICIPANT)
362				|| hasFeature("muc_unmoderated");
363	}
364
365	public boolean membersOnly() {
366		return hasFeature("muc_membersonly");
367	}
368
369	public boolean mamSupport() {
370		// Update with "urn:xmpp:mam:1" once we support it
371		return hasFeature("urn:xmpp:mam:0");
372	}
373
374	public boolean nonanonymous() {
375		return hasFeature("muc_nonanonymous");
376	}
377
378	public boolean persistent() {
379		return hasFeature("muc_persistent");
380	}
381
382	public boolean moderated() {
383		return hasFeature("muc_moderated");
384	}
385
386	public User deleteUser(Jid jid) {
387		User user = findUserByFullJid(jid);
388		if (user != null) {
389			synchronized (users) {
390				users.remove(user);
391				if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null) {
392					user.role = Role.NONE;
393					user.avatar = null;
394					user.fullJid = null;
395					users.add(user);
396				}
397			}
398		}
399		return user;
400	}
401
402	public void addUser(User user) {
403		User old;
404		if (user.fullJid == null && user.realJid != null) {
405			old = findUserByRealJid(user.realJid);
406			if (old != null) {
407				if (old.fullJid != null) {
408					return; //don't add. user already exists
409				} else {
410					synchronized (users) {
411						users.remove(old);
412					}
413				}
414			}
415		} else if (user.realJid != null) {
416			old = findUserByRealJid(user.realJid);
417			synchronized (users) {
418				if (old != null && old.fullJid == null) {
419					users.remove(old);
420				}
421			}
422		}
423		old = findUserByFullJid(user.getFullJid());
424		synchronized (this.users) {
425			if (old != null) {
426				users.remove(old);
427			}
428			this.users.add(user);
429		}
430	}
431
432	public User findUserByFullJid(Jid jid) {
433		if (jid == null) {
434			return null;
435		}
436		synchronized (users) {
437			for (User user : users) {
438				if (jid.equals(user.getFullJid())) {
439					return user;
440				}
441			}
442		}
443		return null;
444	}
445
446	public User findUserByRealJid(Jid jid) {
447		if (jid == null) {
448			return null;
449		}
450		synchronized (users) {
451			for (User user : users) {
452				if (jid.equals(user.realJid)) {
453					return user;
454				}
455			}
456		}
457		return null;
458	}
459
460	public boolean isUserInRoom(Jid jid) {
461		return findUserByFullJid(jid) != null;
462	}
463
464	public void setError(Error error) {
465		this.isOnline = isOnline && error == Error.NONE;
466		this.error = error;
467	}
468
469	public void setOnline() {
470		this.isOnline = true;
471	}
472
473	public ArrayList<User> getUsers() {
474		return getUsers(true);
475	}
476
477	public ArrayList<User> getUsers(boolean includeOffline) {
478		synchronized (users) {
479			if (includeOffline) {
480				return new ArrayList<>(users);
481			} else {
482				ArrayList<User> onlineUsers = new ArrayList<>();
483				for (User user : users) {
484					if (user.getRole().ranks(Role.PARTICIPANT)) {
485						onlineUsers.add(user);
486					}
487				}
488				return onlineUsers;
489			}
490		}
491	}
492
493	public List<User> getUsers(int max) {
494		ArrayList<User> users = getUsers();
495		return users.subList(0, Math.min(max, users.size()));
496	}
497
498	public int getUserCount() {
499		synchronized (users) {
500			return users.size();
501		}
502	}
503
504	public String getProposedNick() {
505		if (conversation.getBookmark() != null
506				&& conversation.getBookmark().getNick() != null
507				&& !conversation.getBookmark().getNick().isEmpty()) {
508			return conversation.getBookmark().getNick();
509		} else if (!conversation.getJid().isBareJid()) {
510			return conversation.getJid().getResourcepart();
511		} else {
512			return account.getUsername();
513		}
514	}
515
516	public String getActualNick() {
517		if (this.self.getName() != null) {
518			return this.self.getName();
519		} else {
520			return this.getProposedNick();
521		}
522	}
523
524	public boolean online() {
525		return this.isOnline;
526	}
527
528	public Error getError() {
529		return this.error;
530	}
531
532	public void setOnRenameListener(OnRenameListener listener) {
533		this.onRenameListener = listener;
534	}
535
536	public void setOffline() {
537		synchronized (users) {
538			this.users.clear();
539		}
540		this.error = Error.NO_RESPONSE;
541		this.isOnline = false;
542	}
543
544	public User getSelf() {
545		return self;
546	}
547
548	public void setSubject(String content) {
549		this.subject = content;
550	}
551
552	public String getSubject() {
553		return this.subject;
554	}
555
556	public String createNameFromParticipants() {
557		if (getUserCount() >= 2) {
558			List<String> names = new ArrayList<>();
559			for (User user : getUsers(5)) {
560				Contact contact = user.getContact();
561				if (contact != null && !contact.getDisplayName().isEmpty()) {
562					names.add(contact.getDisplayName().split("\\s+")[0]);
563				} else if (user.getName() != null){
564					names.add(user.getName());
565				}
566			}
567			StringBuilder builder = new StringBuilder();
568			for (int i = 0; i < names.size(); ++i) {
569				builder.append(names.get(i));
570				if (i != names.size() - 1) {
571					builder.append(", ");
572				}
573			}
574			return builder.toString();
575		} else {
576			return null;
577		}
578	}
579
580	public long[] getPgpKeyIds() {
581		List<Long> ids = new ArrayList<>();
582		for (User user : this.users) {
583			if (user.getPgpKeyId() != 0) {
584				ids.add(user.getPgpKeyId());
585			}
586		}
587		ids.add(account.getPgpId());
588		long[] primitiveLongArray = new long[ids.size()];
589		for (int i = 0; i < ids.size(); ++i) {
590			primitiveLongArray[i] = ids.get(i);
591		}
592		return primitiveLongArray;
593	}
594
595	public boolean pgpKeysInUse() {
596		synchronized (users) {
597			for (User user : users) {
598				if (user.getPgpKeyId() != 0) {
599					return true;
600				}
601			}
602		}
603		return false;
604	}
605
606	public boolean everybodyHasKeys() {
607		synchronized (users) {
608			for (User user : users) {
609				if (user.getPgpKeyId() == 0) {
610					return false;
611				}
612			}
613		}
614		return true;
615	}
616
617	public Jid createJoinJid(String nick) {
618		try {
619			return Jid.fromString(this.conversation.getJid().toBareJid().toString() + "/" + nick);
620		} catch (final InvalidJidException e) {
621			return null;
622		}
623	}
624
625	public Jid getTrueCounterpart(Jid jid) {
626		if (jid.equals(getSelf().getFullJid())) {
627			return account.getJid().toBareJid();
628		}
629		User user = findUserByFullJid(jid);
630		return user == null ? null : user.realJid;
631	}
632
633	public String getPassword() {
634		this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
635		if (this.password == null && conversation.getBookmark() != null
636				&& conversation.getBookmark().getPassword() != null) {
637			return conversation.getBookmark().getPassword();
638		} else {
639			return this.password;
640		}
641	}
642
643	public void setPassword(String password) {
644		if (conversation.getBookmark() != null) {
645			conversation.getBookmark().setPassword(password);
646		} else {
647			this.password = password;
648		}
649		conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
650	}
651
652	public Conversation getConversation() {
653		return this.conversation;
654	}
655
656	public List<Jid> getMembers() {
657		ArrayList<Jid> members = new ArrayList<>();
658		synchronized (users) {
659			for (User user : users) {
660				if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null) {
661					members.add(user.realJid);
662				}
663			}
664		}
665		return members;
666	}
667}