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;
|
2016-02-01 11:11:40 +00:00
|
|
|
import java.util.Collections;
|
2016-02-29 12:18:07 +00:00
|
|
|
import java.util.HashSet;
|
2014-10-22 16:38:44 +00:00
|
|
|
import java.util.List;
|
2016-02-29 12:18:07 +00:00
|
|
|
import java.util.Set;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2015-01-05 14:06:39 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2015-11-26 16:44:29 +00:00
|
|
|
import eu.siacs.conversations.xmpp.forms.Data;
|
|
|
|
import eu.siacs.conversations.xmpp.forms.Field;
|
2014-11-05 20:55:47 +00:00
|
|
|
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;
|
2015-01-05 14:06:39 +00:00
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
@SuppressLint("DefaultLocale")
|
|
|
|
public class MucOptions {
|
2015-01-05 14:06:39 +00:00
|
|
|
|
2016-05-27 08:35:00 +00:00
|
|
|
private boolean mAutoPushConfiguration = true;
|
|
|
|
|
2015-12-03 17:18:34 +00:00
|
|
|
public Account getAccount() {
|
|
|
|
return this.conversation.getAccount();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSelf(User user) {
|
|
|
|
this.self = user;
|
|
|
|
}
|
|
|
|
|
2016-05-16 12:10:40 +00:00
|
|
|
public void changeAffiliation(Jid jid, Affiliation affiliation) {
|
|
|
|
User user = findUserByRealJid(jid);
|
2016-05-19 08:47:03 +00:00
|
|
|
synchronized (users) {
|
|
|
|
if (user != null && user.getRole() == Role.NONE) {
|
|
|
|
users.remove(user);
|
|
|
|
if (affiliation.ranks(Affiliation.MEMBER)) {
|
|
|
|
user.affiliation = affiliation;
|
|
|
|
users.add(user);
|
|
|
|
}
|
2016-05-16 13:50:57 +00:00
|
|
|
}
|
2016-05-16 12:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 08:35:00 +00:00
|
|
|
public void flagNoAutoPushConfiguration() {
|
|
|
|
mAutoPushConfiguration = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean autoPushConfiguration() {
|
|
|
|
return mAutoPushConfiguration;
|
|
|
|
}
|
|
|
|
|
2015-01-05 14:06:39 +00:00
|
|
|
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);
|
2015-01-05 14:06:39 +00:00
|
|
|
|
2015-09-13 15:13:46 +00:00
|
|
|
Affiliation(String string, int rank, int resId) {
|
2015-01-07 14:03:29 +00:00
|
|
|
this.string = string;
|
2015-01-05 14:06:39 +00:00
|
|
|
this.resId = resId;
|
2015-01-07 14:03:29 +00:00
|
|
|
this.rank = rank;
|
2015-01-05 14:06:39 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 14:03:29 +00:00
|
|
|
private String string;
|
2015-01-05 14:06:39 +00:00
|
|
|
private int resId;
|
2015-01-07 14:03:29 +00:00
|
|
|
private int rank;
|
2015-01-05 14:06:39 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-01-05 14:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public enum Role {
|
2015-09-13 15:13:46 +00:00
|
|
|
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);
|
2015-01-05 14:06:39 +00:00
|
|
|
|
2016-02-01 11:11:40 +00:00
|
|
|
Role(String string, int resId, int rank) {
|
2015-01-07 14:03:29 +00:00
|
|
|
this.string = string;
|
2015-01-05 14:06:39 +00:00
|
|
|
this.resId = resId;
|
2015-09-13 15:13:46 +00:00
|
|
|
this.rank = rank;
|
2015-01-05 14:06:39 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 14:03:29 +00:00
|
|
|
private String string;
|
2015-01-05 14:06:39 +00:00
|
|
|
private int resId;
|
2015-09-13 15:13:46 +00:00
|
|
|
private int rank;
|
2015-01-05 14:06:39 +00:00
|
|
|
|
|
|
|
public int getResId() {
|
|
|
|
return resId;
|
|
|
|
}
|
2015-01-07 14:03:29 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return this.string;
|
|
|
|
}
|
2015-09-13 15:13:46 +00:00
|
|
|
|
|
|
|
public boolean ranks(Role role) {
|
|
|
|
return rank >= role.rank;
|
|
|
|
}
|
2015-01-05 14:06:39 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 19:19:58 +00:00
|
|
|
public enum Error {
|
|
|
|
NO_RESPONSE,
|
2016-09-06 10:15:08 +00:00
|
|
|
SEVRER_NOT_FOUND,
|
2016-02-22 19:19:58 +00:00
|
|
|
NONE,
|
|
|
|
NICK_IN_USE,
|
|
|
|
PASSWORD_REQUIRED,
|
|
|
|
BANNED,
|
|
|
|
MEMBERS_ONLY,
|
|
|
|
KICKED,
|
|
|
|
SHUTDOWN,
|
|
|
|
UNKNOWN
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2015-01-07 17:34:24 +00:00
|
|
|
public static final String STATUS_CODE_ROOM_CONFIG_CHANGED = "104";
|
2014-11-20 20:31:29 +00:00
|
|
|
public static final String STATUS_CODE_SELF_PRESENCE = "110";
|
2016-05-26 10:39:31 +00:00
|
|
|
public static final String STATUS_CODE_ROOM_CREATED = "201";
|
2014-10-22 16:38:44 +00:00
|
|
|
public static final String STATUS_CODE_BANNED = "301";
|
2014-11-20 20:31:29 +00:00
|
|
|
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";
|
2016-02-22 19:19:58 +00:00
|
|
|
public static final String STATUS_CODE_AFFILIATION_CHANGE = "321";
|
|
|
|
public static final String STATUS_CODE_LOST_MEMBERSHIP = "322";
|
|
|
|
public static final String STATUS_CODE_SHUTDOWN = "332";
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2014-11-20 17:20:42 +00:00
|
|
|
private interface OnEventListener {
|
2015-12-03 17:18:34 +00:00
|
|
|
void onSuccess();
|
2015-01-05 14:06:39 +00:00
|
|
|
|
2015-12-03 17:18:34 +00:00
|
|
|
void onFailure();
|
2014-11-20 17:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnRenameListener extends OnEventListener {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-17 13:01:56 +00:00
|
|
|
public static class User implements Comparable<User> {
|
2015-01-07 17:34:24 +00:00
|
|
|
private Role role = Role.NONE;
|
|
|
|
private Affiliation affiliation = Affiliation.NONE;
|
2016-05-16 12:10:40 +00:00
|
|
|
private Jid realJid;
|
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() {
|
2016-05-16 12:10:40 +00:00
|
|
|
return fullJid == null ? null : fullJid.getResourcepart();
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 12:10:40 +00:00
|
|
|
public void setRealJid(Jid jid) {
|
|
|
|
this.realJid = jid != null ? jid.toBareJid() : null;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 14:06:39 +00:00
|
|
|
public Role getRole() {
|
2014-10-22 16:38:44 +00:00
|
|
|
return this.role;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRole(String role) {
|
2016-05-16 12:10:40 +00:00
|
|
|
if (role == null) {
|
|
|
|
this.role = Role.NONE;
|
|
|
|
return;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
role = role.toLowerCase();
|
2014-12-21 20:43:58 +00:00
|
|
|
switch (role) {
|
|
|
|
case "moderator":
|
2015-01-05 14:06:39 +00:00
|
|
|
this.role = Role.MODERATOR;
|
2014-12-21 20:43:58 +00:00
|
|
|
break;
|
|
|
|
case "participant":
|
2015-01-05 14:06:39 +00:00
|
|
|
this.role = Role.PARTICIPANT;
|
2014-12-21 20:43:58 +00:00
|
|
|
break;
|
|
|
|
case "visitor":
|
2015-01-05 14:06:39 +00:00
|
|
|
this.role = Role.VISITOR;
|
2014-12-21 20:43:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-01-05 14:06:39 +00:00
|
|
|
this.role = Role.NONE;
|
2014-12-21 20:43:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 14:06:39 +00:00
|
|
|
public Affiliation getAffiliation() {
|
2014-10-22 16:38:44 +00:00
|
|
|
return this.affiliation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAffiliation(String affiliation) {
|
2016-05-16 12:10:40 +00:00
|
|
|
if (affiliation == null) {
|
|
|
|
this.affiliation = Affiliation.NONE;
|
|
|
|
return;
|
|
|
|
}
|
2015-01-05 14:06:39 +00:00
|
|
|
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() {
|
2016-05-17 13:01:56 +00:00
|
|
|
if (fullJid != null) {
|
|
|
|
return getAccount().getRoster().getContactFromRoster(realJid);
|
|
|
|
} else if (realJid != null){
|
|
|
|
return getAccount().getRoster().getContact(realJid);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2015-12-03 17:18:34 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 20:36:48 +00:00
|
|
|
public boolean setAvatar(Avatar avatar) {
|
|
|
|
if (this.avatar != null && this.avatar.equals(avatar)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
this.avatar = avatar;
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-03 17:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-05-16 12:10:40 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
|
|
|
User user = (User) o;
|
|
|
|
|
|
|
|
if (role != user.role) return false;
|
|
|
|
if (affiliation != user.affiliation) return false;
|
|
|
|
if (realJid != null ? !realJid.equals(user.realJid) : user.realJid != null)
|
|
|
|
return false;
|
|
|
|
return fullJid != null ? fullJid.equals(user.fullJid) : user.fullJid == null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
int result = role != null ? role.hashCode() : 0;
|
|
|
|
result = 31 * result + (affiliation != null ? affiliation.hashCode() : 0);
|
|
|
|
result = 31 * result + (realJid != null ? realJid.hashCode() : 0);
|
|
|
|
result = 31 * result + (fullJid != null ? fullJid.hashCode() : 0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "[fulljid:"+String.valueOf(fullJid)+",realjid:"+String.valueOf(realJid)+",affiliation"+affiliation.toString()+"]";
|
|
|
|
}
|
2016-05-17 12:25:58 +00:00
|
|
|
|
|
|
|
public boolean realJidMatchesAccount() {
|
|
|
|
return realJid != null && realJid.equals(options.account.getJid().toBareJid());
|
|
|
|
}
|
2016-05-17 13:01:56 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compareTo(User another) {
|
2016-06-14 12:41:32 +00:00
|
|
|
if (another.getAffiliation().outranks(getAffiliation())) {
|
|
|
|
return 1;
|
|
|
|
} else if (getAffiliation().outranks(another.getAffiliation())) {
|
|
|
|
return -1;
|
2016-05-17 13:01:56 +00:00
|
|
|
} else {
|
2016-07-04 17:30:19 +00:00
|
|
|
return getComparableName().compareToIgnoreCase(another.getComparableName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String getComparableName() {
|
|
|
|
Contact contact = getContact();
|
|
|
|
if (contact != null) {
|
|
|
|
return contact.getDisplayName();
|
|
|
|
} else {
|
|
|
|
String name = getName();
|
|
|
|
return name == null ? "" : name;
|
2016-05-17 13:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-19 08:47:03 +00:00
|
|
|
|
|
|
|
public Jid getRealJid() {
|
|
|
|
return realJid;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Account account;
|
2016-05-19 08:47:03 +00:00
|
|
|
private final Set<User> users = new HashSet<>();
|
2016-02-29 15:32:24 +00:00
|
|
|
private final List<String> features = new ArrayList<>();
|
2015-11-26 16:44:29 +00:00
|
|
|
private Data form = new Data();
|
2014-10-22 16:38:44 +00:00
|
|
|
private Conversation conversation;
|
|
|
|
private boolean isOnline = false;
|
2016-02-22 19:19:58 +00:00
|
|
|
private Error error = Error.NONE;
|
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-04 13:07:16 +00:00
|
|
|
this.self = new User(this,createJoinJid(getProposedNick()));
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:34:24 +00:00
|
|
|
public void updateFeatures(ArrayList<String> features) {
|
|
|
|
this.features.clear();
|
|
|
|
this.features.addAll(features);
|
|
|
|
}
|
|
|
|
|
2015-11-26 16:44:29 +00:00
|
|
|
public void updateFormData(Data form) {
|
|
|
|
this.form = form;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:34:24 +00:00
|
|
|
public boolean hasFeature(String feature) {
|
|
|
|
return this.features.contains(feature);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean canInvite() {
|
2015-11-26 16:44:29 +00:00
|
|
|
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()));
|
2015-01-07 17:34:24 +00:00
|
|
|
}
|
|
|
|
|
2015-09-13 15:13:46 +00:00
|
|
|
public boolean participating() {
|
2016-02-21 10:42:41 +00:00
|
|
|
return !online()
|
|
|
|
|| self.getRole().ranks(Role.PARTICIPANT)
|
|
|
|
|| hasFeature("muc_unmoderated");
|
2015-09-13 15:13:46 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:34:24 +00:00
|
|
|
public boolean membersOnly() {
|
|
|
|
return hasFeature("muc_membersonly");
|
|
|
|
}
|
|
|
|
|
2015-10-02 20:00:11 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:10:32 +00:00
|
|
|
public boolean persistent() {
|
|
|
|
return hasFeature("muc_persistent");
|
|
|
|
}
|
|
|
|
|
2015-09-13 15:13:46 +00:00
|
|
|
public boolean moderated() {
|
|
|
|
return hasFeature("muc_moderated");
|
|
|
|
}
|
|
|
|
|
2016-05-16 12:10:40 +00:00
|
|
|
public User deleteUser(Jid jid) {
|
|
|
|
User user = findUserByFullJid(jid);
|
|
|
|
if (user != null) {
|
2016-05-19 08:47:03 +00:00
|
|
|
synchronized (users) {
|
|
|
|
users.remove(user);
|
2016-06-28 08:32:06 +00:00
|
|
|
if (membersOnly() &&
|
|
|
|
nonanonymous() &&
|
|
|
|
user.affiliation.ranks(Affiliation.MEMBER) &&
|
|
|
|
user.realJid != null) {
|
2016-05-19 08:47:03 +00:00
|
|
|
user.role = Role.NONE;
|
|
|
|
user.avatar = null;
|
|
|
|
user.fullJid = null;
|
|
|
|
users.add(user);
|
|
|
|
}
|
2016-05-16 12:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return user;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addUser(User user) {
|
2016-05-16 12:10:40 +00:00
|
|
|
User old;
|
|
|
|
if (user.fullJid == null && user.realJid != null) {
|
|
|
|
old = findUserByRealJid(user.realJid);
|
|
|
|
if (old != null) {
|
2016-05-19 08:47:03 +00:00
|
|
|
if (old.fullJid != null) {
|
|
|
|
return; //don't add. user already exists
|
|
|
|
} else {
|
|
|
|
synchronized (users) {
|
|
|
|
users.remove(old);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 12:10:40 +00:00
|
|
|
}
|
|
|
|
} else if (user.realJid != null) {
|
|
|
|
old = findUserByRealJid(user.realJid);
|
2016-05-19 08:47:03 +00:00
|
|
|
synchronized (users) {
|
|
|
|
if (old != null && old.fullJid == null) {
|
|
|
|
users.remove(old);
|
|
|
|
}
|
2016-05-16 12:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
old = findUserByFullJid(user.getFullJid());
|
2016-05-19 08:47:03 +00:00
|
|
|
synchronized (this.users) {
|
|
|
|
if (old != null) {
|
|
|
|
users.remove(old);
|
|
|
|
}
|
|
|
|
this.users.add(user);
|
2016-05-16 12:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public User findUserByFullJid(Jid jid) {
|
|
|
|
if (jid == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-05-19 08:47:03 +00:00
|
|
|
synchronized (users) {
|
|
|
|
for (User user : users) {
|
|
|
|
if (jid.equals(user.getFullJid())) {
|
|
|
|
return user;
|
|
|
|
}
|
2016-05-16 12:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 15:11:31 +00:00
|
|
|
private User findUserByRealJid(Jid jid) {
|
2016-05-16 12:10:40 +00:00
|
|
|
if (jid == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-05-19 08:47:03 +00:00
|
|
|
synchronized (users) {
|
|
|
|
for (User user : users) {
|
|
|
|
if (jid.equals(user.realJid)) {
|
|
|
|
return user;
|
|
|
|
}
|
2016-05-16 12:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2014-11-20 17:20:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 15:11:31 +00:00
|
|
|
public boolean isContactInRoom(Contact contact) {
|
|
|
|
return findUserByRealJid(contact.getJid().toBareJid()) != null;
|
|
|
|
}
|
|
|
|
|
2016-05-16 12:10:40 +00:00
|
|
|
public boolean isUserInRoom(Jid jid) {
|
|
|
|
return findUserByFullJid(jid) != null;
|
2015-12-03 17:18:34 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 19:19:58 +00:00
|
|
|
public void setError(Error error) {
|
|
|
|
this.isOnline = isOnline && error == Error.NONE;
|
2014-11-20 17:20:42 +00:00
|
|
|
this.error = error;
|
|
|
|
}
|
|
|
|
|
2016-02-01 11:11:40 +00:00
|
|
|
public void setOnline() {
|
|
|
|
this.isOnline = true;
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:18:34 +00:00
|
|
|
public ArrayList<User> getUsers() {
|
2016-05-17 08:43:48 +00:00
|
|
|
return getUsers(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<User> getUsers(boolean includeOffline) {
|
2016-05-19 08:47:03 +00:00
|
|
|
synchronized (users) {
|
|
|
|
if (includeOffline) {
|
|
|
|
return new ArrayList<>(users);
|
|
|
|
} else {
|
|
|
|
ArrayList<User> onlineUsers = new ArrayList<>();
|
|
|
|
for (User user : users) {
|
|
|
|
if (user.getRole().ranks(Role.PARTICIPANT)) {
|
|
|
|
onlineUsers.add(user);
|
|
|
|
}
|
2016-05-17 08:43:48 +00:00
|
|
|
}
|
2016-05-19 08:47:03 +00:00
|
|
|
return onlineUsers;
|
2016-05-17 08:43:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 17:18:34 +00:00
|
|
|
public List<User> getUsers(int max) {
|
2016-03-13 16:42:17 +00:00
|
|
|
ArrayList<User> users = getUsers();
|
|
|
|
return users.subList(0, Math.min(max, users.size()));
|
2015-12-03 17:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getUserCount() {
|
2016-05-19 08:47:03 +00:00
|
|
|
synchronized (users) {
|
|
|
|
return 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
|
2016-06-03 17:24:11 +00:00
|
|
|
&& !conversation.getBookmark().getNick().trim().isEmpty()) {
|
|
|
|
return conversation.getBookmark().getNick().trim();
|
2014-12-21 20:43:58 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2016-02-22 19:19:58 +00:00
|
|
|
public Error getError() {
|
2014-10-22 16:38:44 +00:00
|
|
|
return this.error;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOnRenameListener(OnRenameListener listener) {
|
2014-11-20 17:20:42 +00:00
|
|
|
this.onRenameListener = listener;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setOffline() {
|
2016-05-19 08:47:03 +00:00
|
|
|
synchronized (users) {
|
|
|
|
this.users.clear();
|
|
|
|
}
|
2016-02-22 19:19:58 +00:00
|
|
|
this.error = Error.NO_RESPONSE;
|
2014-10-22 16:38:44 +00:00
|
|
|
this.isOnline = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public User getSelf() {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSubject(String content) {
|
|
|
|
this.subject = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getSubject() {
|
|
|
|
return this.subject;
|
|
|
|
}
|
|
|
|
|
2014-11-20 17:20:42 +00:00
|
|
|
public String createNameFromParticipants() {
|
2016-05-19 08:47:03 +00:00
|
|
|
if (getUserCount() >= 2) {
|
2016-02-01 11:11:40 +00:00
|
|
|
List<String> names = new ArrayList<>();
|
|
|
|
for (User user : getUsers(5)) {
|
|
|
|
Contact contact = user.getContact();
|
|
|
|
if (contact != null && !contact.getDisplayName().isEmpty()) {
|
|
|
|
names.add(contact.getDisplayName().split("\\s+")[0]);
|
2016-05-16 12:10:40 +00:00
|
|
|
} else if (user.getName() != null){
|
2016-02-01 11:11:40 +00:00
|
|
|
names.add(user.getName());
|
2014-11-20 17:20:42 +00:00
|
|
|
}
|
2016-02-01 11:11:40 +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(", ");
|
2014-11-20 17:20:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-01 11:11:40 +00:00
|
|
|
return builder.toString();
|
|
|
|
} else {
|
|
|
|
return null;
|
2014-11-20 17:20:42 +00:00
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public long[] getPgpKeyIds() {
|
2014-11-05 20:55:47 +00:00
|
|
|
List<Long> ids = new ArrayList<>();
|
2016-05-16 12:10:40 +00:00
|
|
|
for (User user : this.users) {
|
2016-02-01 11:11:40 +00:00
|
|
|
if (user.getPgpKeyId() != 0) {
|
|
|
|
ids.add(user.getPgpKeyId());
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-26 16:44:29 +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) {
|
2015-11-26 16:44:29 +00:00
|
|
|
primitiveLongArray[i] = ids.get(i);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2015-11-26 16:44:29 +00:00
|
|
|
return primitiveLongArray;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean pgpKeysInUse() {
|
2016-05-19 08:47:03 +00:00
|
|
|
synchronized (users) {
|
|
|
|
for (User user : users) {
|
|
|
|
if (user.getPgpKeyId() != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean everybodyHasKeys() {
|
2016-05-19 08:47:03 +00:00
|
|
|
synchronized (users) {
|
|
|
|
for (User user : users) {
|
|
|
|
if (user.getPgpKeyId() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-20 17:20:42 +00:00
|
|
|
public Jid createJoinJid(String nick) {
|
2014-12-21 20:43:58 +00:00
|
|
|
try {
|
2015-01-05 14:06:39 +00:00
|
|
|
return Jid.fromString(this.conversation.getJid().toBareJid().toString() + "/" + nick);
|
2014-12-21 20:43:58 +00:00
|
|
|
} catch (final InvalidJidException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2016-05-16 12:10:40 +00:00
|
|
|
public Jid getTrueCounterpart(Jid jid) {
|
|
|
|
if (jid.equals(getSelf().getFullJid())) {
|
2016-04-13 09:13:47 +00:00
|
|
|
return account.getJid().toBareJid();
|
|
|
|
}
|
2016-05-16 12:10:40 +00:00
|
|
|
User user = findUserByFullJid(jid);
|
2016-05-19 08:47:03 +00:00
|
|
|
return user == null ? null : user.realJid;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getPassword() {
|
2014-11-20 17:20:42 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-11-20 17:20:42 +00:00
|
|
|
conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Conversation getConversation() {
|
|
|
|
return this.conversation;
|
|
|
|
}
|
2016-02-29 12:18:07 +00:00
|
|
|
|
|
|
|
public List<Jid> getMembers() {
|
2016-05-16 12:10:40 +00:00
|
|
|
ArrayList<Jid> members = new ArrayList<>();
|
2016-05-19 08:47:03 +00:00
|
|
|
synchronized (users) {
|
|
|
|
for (User user : users) {
|
|
|
|
if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null) {
|
|
|
|
members.add(user.realJid);
|
|
|
|
}
|
2016-05-16 12:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return members;
|
2016-02-29 12:18:07 +00:00
|
|
|
}
|
2014-11-09 18:13:19 +00:00
|
|
|
}
|