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