conversations-classic/src/main/java/eu/siacs/conversations/entities/MucOptions.java

521 lines
12 KiB
Java
Raw Normal View History

2014-10-22 16:38:44 +00:00
package eu.siacs.conversations.entities;
2015-07-20 12:26:29 +00:00
import android.annotation.SuppressLint;
2014-10-22 16:38:44 +00:00
import java.util.ArrayList;
import java.util.List;
import eu.siacs.conversations.R;
import eu.siacs.conversations.xmpp.forms.Data;
import eu.siacs.conversations.xmpp.forms.Field;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;
2015-12-03 17:18:34 +00:00
import eu.siacs.conversations.xmpp.pep.Avatar;
2014-10-22 16:38:44 +00:00
@SuppressLint("DefaultLocale")
public class MucOptions {
2015-12-03 17:18:34 +00:00
public Account getAccount() {
return this.conversation.getAccount();
}
public void setSelf(User user) {
this.self = user;
}
public enum Affiliation {
2015-01-07 14:03:29 +00:00
OWNER("owner", 4, R.string.owner),
ADMIN("admin", 3, R.string.admin),
MEMBER("member", 2, R.string.member),
OUTCAST("outcast", 0, R.string.outcast),
NONE("none", 1, R.string.no_affiliation);
Affiliation(String string, int rank, int resId) {
2015-01-07 14:03:29 +00:00
this.string = string;
this.resId = resId;
2015-01-07 14:03:29 +00:00
this.rank = rank;
}
2015-01-07 14:03:29 +00:00
private String string;
private int resId;
2015-01-07 14:03:29 +00:00
private int rank;
public int getResId() {
return resId;
}
2015-01-07 14:03:29 +00:00
@Override
public String toString() {
return this.string;
}
public boolean outranks(Affiliation affiliation) {
return rank > affiliation.rank;
}
public boolean ranks(Affiliation affiliation) {
return rank >= affiliation.rank;
}
}
public enum Role {
MODERATOR("moderator", R.string.moderator,3),
VISITOR("visitor", R.string.visitor,1),
PARTICIPANT("participant", R.string.participant,2),
NONE("none", R.string.no_role,0);
private Role(String string, int resId, int rank) {
2015-01-07 14:03:29 +00:00
this.string = string;
this.resId = resId;
this.rank = rank;
}
2015-01-07 14:03:29 +00:00
private String string;
private int resId;
private int rank;
public int getResId() {
return resId;
}
2015-01-07 14:03:29 +00:00
@Override
public String toString() {
return this.string;
}
public boolean ranks(Role role) {
return rank >= role.rank;
}
}
2014-10-22 16:38:44 +00:00
public static final int ERROR_NO_ERROR = 0;
public static final int ERROR_NICK_IN_USE = 1;
public static final int ERROR_UNKNOWN = 2;
2014-10-22 16:38:44 +00:00
public static final int ERROR_PASSWORD_REQUIRED = 3;
public static final int ERROR_BANNED = 4;
public static final int ERROR_MEMBERS_ONLY = 5;
public static final int KICKED_FROM_ROOM = 9;
public static final String STATUS_CODE_ROOM_CONFIG_CHANGED = "104";
public static final String STATUS_CODE_SELF_PRESENCE = "110";
2014-10-22 16:38:44 +00:00
public static final String STATUS_CODE_BANNED = "301";
public static final String STATUS_CODE_CHANGED_NICK = "303";
2014-10-22 16:38:44 +00:00
public static final String STATUS_CODE_KICKED = "307";
public static final String STATUS_CODE_LOST_MEMBERSHIP = "321";
2014-10-22 16:38:44 +00:00
private interface OnEventListener {
2015-12-03 17:18:34 +00:00
void onSuccess();
2015-12-03 17:18:34 +00:00
void onFailure();
}
public interface OnRenameListener extends OnEventListener {
}
2015-12-03 17:18:34 +00:00
public static class User {
private Role role = Role.NONE;
private Affiliation affiliation = Affiliation.NONE;
private Jid jid;
2015-12-03 17:18:34 +00:00
private Jid fullJid;
2014-10-22 16:38:44 +00:00
private long pgpKeyId = 0;
2015-12-03 17:18:34 +00:00
private Avatar avatar;
private MucOptions options;
2014-10-22 16:38:44 +00:00
2015-12-03 17:18:34 +00:00
public User(MucOptions options, Jid from) {
this.options = options;
this.fullJid = from;
2014-10-22 16:38:44 +00:00
}
2015-12-03 17:18:34 +00:00
public String getName() {
return this.fullJid.getResourcepart();
2014-10-22 16:38:44 +00:00
}
public void setJid(Jid jid) {
2014-10-22 16:38:44 +00:00
this.jid = jid;
}
public Jid getJid() {
2014-10-22 16:38:44 +00:00
return this.jid;
}
public Role getRole() {
2014-10-22 16:38:44 +00:00
return this.role;
}
public void setRole(String role) {
role = role.toLowerCase();
switch (role) {
case "moderator":
this.role = Role.MODERATOR;
break;
case "participant":
this.role = Role.PARTICIPANT;
break;
case "visitor":
this.role = Role.VISITOR;
break;
default:
this.role = Role.NONE;
break;
}
2014-10-22 16:38:44 +00:00
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (!(other instanceof User)) {
return false;
} else {
User o = (User) other;
2015-12-03 17:18:34 +00:00
return getName() != null && getName().equals(o.getName())
&& jid != null && jid.equals(o.jid)
&& affiliation == o.affiliation
&& role == o.role;
}
}
public Affiliation getAffiliation() {
2014-10-22 16:38:44 +00:00
return this.affiliation;
}
public void setAffiliation(String affiliation) {
affiliation = affiliation.toLowerCase();
switch (affiliation) {
case "admin":
this.affiliation = Affiliation.ADMIN;
break;
case "owner":
this.affiliation = Affiliation.OWNER;
break;
case "member":
this.affiliation = Affiliation.MEMBER;
break;
case "outcast":
this.affiliation = Affiliation.OUTCAST;
break;
default:
this.affiliation = Affiliation.NONE;
2014-10-22 16:38:44 +00:00
}
}
public void setPgpKeyId(long id) {
this.pgpKeyId = id;
}
public long getPgpKeyId() {
return this.pgpKeyId;
}
public Contact getContact() {
2015-12-03 17:18:34 +00:00
return getAccount().getRoster().getContactFromRoster(getJid());
}
public void setAvatar(Avatar avatar) {
this.avatar = avatar;
}
public String getAvatar() {
return avatar == null ? null : avatar.getFilename();
}
public Account getAccount() {
return options.getAccount();
}
public Jid getFullJid() {
return fullJid;
2014-10-22 16:38:44 +00:00
}
}
private Account account;
2015-12-03 17:18:34 +00:00
private final List<User> users = new ArrayList<>();
private List<String> features = new ArrayList<>();
private Data form = new Data();
2014-10-22 16:38:44 +00:00
private Conversation conversation;
private boolean isOnline = false;
private int error = ERROR_UNKNOWN;
2015-12-03 17:18:34 +00:00
public OnRenameListener onRenameListener = null;
private User self;
2014-10-22 16:38:44 +00:00
private String subject = null;
private String password = null;
2015-12-03 17:18:34 +00:00
public boolean mNickChangingInProgress = false;
2014-10-22 16:38:44 +00:00
public MucOptions(Conversation conversation) {
this.account = conversation.getAccount();
this.conversation = conversation;
2015-12-03 17:18:34 +00:00
this.self = new User(this,conversation.getJid());
2014-10-22 16:38:44 +00:00
}
public void updateFeatures(ArrayList<String> features) {
this.features.clear();
this.features.addAll(features);
}
public void updateFormData(Data form) {
this.form = form;
}
public boolean hasFeature(String feature) {
return this.features.contains(feature);
}
public boolean canInvite() {
Field field = this.form.getFieldByName("muc#roomconfig_allowinvites");
return !membersOnly() || self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
}
public boolean canChangeSubject() {
Field field = this.form.getFieldByName("muc#roomconfig_changesubject");
return self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
}
public boolean participating() {
return !online() || self.getRole().ranks(Role.PARTICIPANT);
}
public boolean membersOnly() {
return hasFeature("muc_membersonly");
}
public boolean mamSupport() {
// Update with "urn:xmpp:mam:1" once we support it
return hasFeature("urn:xmpp:mam:0");
}
2015-01-08 20:29:26 +00:00
public boolean nonanonymous() {
return hasFeature("muc_nonanonymous");
}
public boolean persistent() {
return hasFeature("muc_persistent");
}
public boolean moderated() {
return hasFeature("muc_moderated");
}
2014-10-22 16:38:44 +00:00
public void deleteUser(String name) {
2015-12-03 17:18:34 +00:00
synchronized (this.users) {
for (int i = 0; i < users.size(); ++i) {
if (users.get(i).getName().equals(name)) {
users.remove(i);
return;
}
2014-10-22 16:38:44 +00:00
}
}
}
public void addUser(User user) {
2015-12-03 17:18:34 +00:00
synchronized (this.users) {
for (int i = 0; i < users.size(); ++i) {
if (users.get(i).getName().equals(user.getName())) {
users.set(i, user);
return;
}
2014-10-22 16:38:44 +00:00
}
2015-12-03 17:18:34 +00:00
users.add(user);
2014-10-22 16:38:44 +00:00
}
}
2015-12-03 17:18:34 +00:00
public User findUser(String name) {
if (name == null) {
return null;
}
2015-12-03 17:18:34 +00:00
synchronized (this.users) {
for (User user : users) {
if (user.getName().equals(name)) {
return user;
}
}
}
2015-12-03 17:18:34 +00:00
return null;
}
2015-12-03 17:18:34 +00:00
public boolean isUserInRoom(String name) {
return findUser(name) != null;
}
public void setError(int error) {
this.isOnline = error == ERROR_NO_ERROR;
this.error = error;
}
2015-12-03 17:18:34 +00:00
public ArrayList<User> getUsers() {
synchronized (this.users) {
return new ArrayList(this.users);
2014-10-22 16:38:44 +00:00
}
}
2015-12-03 17:18:34 +00:00
public List<User> getUsers(int max) {
synchronized (this.users) {
return new ArrayList<>(users.subList(0,Math.min(users.size(),5)));
}
}
public int getUserCount() {
synchronized (this.users) {
return this.users.size();
}
2014-10-22 16:38:44 +00:00
}
public String getProposedNick() {
if (conversation.getBookmark() != null
2014-12-02 23:00:25 +00:00
&& conversation.getBookmark().getNick() != null
&& !conversation.getBookmark().getNick().isEmpty()) {
2014-10-22 16:38:44 +00:00
return conversation.getBookmark().getNick();
} else if (!conversation.getJid().isBareJid()) {
return conversation.getJid().getResourcepart();
2014-10-22 16:38:44 +00:00
} else {
2014-12-02 23:00:25 +00:00
return account.getUsername();
2014-10-22 16:38:44 +00:00
}
}
public String getActualNick() {
if (this.self.getName() != null) {
return this.self.getName();
} else {
return this.getProposedNick();
}
}
public boolean online() {
return this.isOnline;
}
public int getError() {
return this.error;
}
public void setOnRenameListener(OnRenameListener listener) {
this.onRenameListener = listener;
2014-10-22 16:38:44 +00:00
}
public void setOffline() {
this.users.clear();
this.error = 0;
this.isOnline = false;
}
public User getSelf() {
return self;
}
public void setSubject(String content) {
this.subject = content;
}
public String getSubject() {
return this.subject;
}
public String createNameFromParticipants() {
2015-12-03 17:18:34 +00:00
synchronized (this.users) {
if (users.size() >= 2) {
List<String> names = new ArrayList<String>();
for (User user : users) {
Contact contact = user.getContact();
if (contact != null && !contact.getDisplayName().isEmpty()) {
names.add(contact.getDisplayName().split("\\s+")[0]);
} else {
names.add(user.getName());
}
}
2015-12-03 17:18:34 +00:00
StringBuilder builder = new StringBuilder();
for (int i = 0; i < names.size(); ++i) {
builder.append(names.get(i));
if (i != names.size() - 1) {
builder.append(", ");
}
}
2015-12-03 17:18:34 +00:00
return builder.toString();
} else {
return null;
}
}
2014-10-22 16:38:44 +00:00
}
public long[] getPgpKeyIds() {
List<Long> ids = new ArrayList<>();
2015-12-03 17:18:34 +00:00
synchronized (this.users) {
for (User user : this.users) {
if (user.getPgpKeyId() != 0) {
ids.add(user.getPgpKeyId());
}
2014-10-22 16:38:44 +00:00
}
}
ids.add(account.getPgpId());
long[] primitiveLongArray = new long[ids.size()];
2014-10-22 16:38:44 +00:00
for (int i = 0; i < ids.size(); ++i) {
primitiveLongArray[i] = ids.get(i);
2014-10-22 16:38:44 +00:00
}
return primitiveLongArray;
2014-10-22 16:38:44 +00:00
}
public boolean pgpKeysInUse() {
2015-12-03 17:18:34 +00:00
synchronized (this.users) {
for (User user : this.users) {
if (user.getPgpKeyId() != 0) {
return true;
}
2014-10-22 16:38:44 +00:00
}
}
return false;
}
public boolean everybodyHasKeys() {
2015-12-03 17:18:34 +00:00
synchronized (this.users) {
for (User user : this.users) {
if (user.getPgpKeyId() == 0) {
return false;
}
2014-10-22 16:38:44 +00:00
}
}
return true;
}
public Jid createJoinJid(String nick) {
try {
return Jid.fromString(this.conversation.getJid().toBareJid().toString() + "/" + nick);
} catch (final InvalidJidException e) {
return null;
}
}
2014-10-22 16:38:44 +00:00
public Jid getTrueCounterpart(String counterpart) {
2015-12-03 17:18:34 +00:00
synchronized (this.users) {
for (User user : this.users) {
if (user.getName().equals(counterpart)) {
return user.getJid();
}
2014-10-22 16:38:44 +00:00
}
}
return null;
}
public String getPassword() {
this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
2014-10-22 16:38:44 +00:00
if (this.password == null && conversation.getBookmark() != null
&& conversation.getBookmark().getPassword() != null) {
return conversation.getBookmark().getPassword();
} else {
return this.password;
}
}
public void setPassword(String password) {
if (conversation.getBookmark() != null) {
conversation.getBookmark().setPassword(password);
} else {
this.password = password;
}
conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
2014-10-22 16:38:44 +00:00
}
public Conversation getConversation() {
return this.conversation;
}
}