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