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