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