2014-02-28 12:46:01 -05:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2014-07-27 12:07:04 -04:00
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
2014-03-02 23:01:02 -05:00
|
|
|
|
2014-05-22 09:36:41 -04:00
|
|
|
import eu.siacs.conversations.crypto.PgpEngine;
|
2014-03-02 23:01:02 -05:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2014-03-10 14:22:13 -04:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
|
2014-03-01 05:05:40 -05:00
|
|
|
import android.annotation.SuppressLint;
|
2014-02-28 12:46:01 -05:00
|
|
|
|
2014-03-01 05:05:40 -05:00
|
|
|
@SuppressLint("DefaultLocale")
|
|
|
|
public class MucOptions {
|
2014-07-18 06:44:33 -04:00
|
|
|
public static final int ERROR_NO_ERROR = 0;
|
2014-03-02 23:01:02 -05:00
|
|
|
public static final int ERROR_NICK_IN_USE = 1;
|
2014-07-18 06:44:33 -04:00
|
|
|
public static final int ERROR_ROOM_NOT_FOUND = 2;
|
2014-09-03 13:35:45 -04:00
|
|
|
public static final int ERROR_PASSWORD_REQUIRED = 3;
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
public interface OnRenameListener {
|
|
|
|
public void onRename(boolean success);
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-01 05:05:40 -05:00
|
|
|
public class User {
|
|
|
|
public static final int ROLE_MODERATOR = 3;
|
|
|
|
public static final int ROLE_NONE = 0;
|
|
|
|
public static final int ROLE_PARTICIPANT = 2;
|
|
|
|
public static final int ROLE_VISITOR = 1;
|
|
|
|
public static final int AFFILIATION_ADMIN = 4;
|
|
|
|
public static final int AFFILIATION_OWNER = 3;
|
|
|
|
public static final int AFFILIATION_MEMBER = 2;
|
|
|
|
public static final int AFFILIATION_OUTCAST = 1;
|
|
|
|
public static final int AFFILIATION_NONE = 0;
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-01 05:05:40 -05:00
|
|
|
private int role;
|
|
|
|
private int affiliation;
|
2014-03-02 23:01:02 -05:00
|
|
|
private String name;
|
2014-07-29 08:42:17 -04:00
|
|
|
private String jid;
|
2014-05-22 09:36:41 -04:00
|
|
|
private long pgpKeyId = 0;
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
public void setName(String user) {
|
|
|
|
this.name = user;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
|
|
|
public void setJid(String jid) {
|
|
|
|
this.jid = jid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getJid() {
|
|
|
|
return this.jid;
|
|
|
|
}
|
|
|
|
|
2014-03-01 05:05:40 -05:00
|
|
|
public int getRole() {
|
|
|
|
return this.role;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-01 05:05:40 -05:00
|
|
|
public void setRole(String role) {
|
|
|
|
role = role.toLowerCase();
|
|
|
|
if (role.equals("moderator")) {
|
|
|
|
this.role = ROLE_MODERATOR;
|
|
|
|
} else if (role.equals("participant")) {
|
|
|
|
this.role = ROLE_PARTICIPANT;
|
|
|
|
} else if (role.equals("visitor")) {
|
|
|
|
this.role = ROLE_VISITOR;
|
|
|
|
} else {
|
|
|
|
this.role = ROLE_NONE;
|
|
|
|
}
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-01 05:05:40 -05:00
|
|
|
public int getAffiliation() {
|
|
|
|
return this.affiliation;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
public void setAffiliation(String affiliation) {
|
|
|
|
if (affiliation.equalsIgnoreCase("admin")) {
|
|
|
|
this.affiliation = AFFILIATION_ADMIN;
|
|
|
|
} else if (affiliation.equalsIgnoreCase("owner")) {
|
|
|
|
this.affiliation = AFFILIATION_OWNER;
|
|
|
|
} else if (affiliation.equalsIgnoreCase("member")) {
|
|
|
|
this.affiliation = AFFILIATION_MEMBER;
|
|
|
|
} else if (affiliation.equalsIgnoreCase("outcast")) {
|
|
|
|
this.affiliation = AFFILIATION_OUTCAST;
|
|
|
|
} else {
|
|
|
|
this.affiliation = AFFILIATION_NONE;
|
|
|
|
}
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-05-22 09:36:41 -04:00
|
|
|
public void setPgpKeyId(long id) {
|
|
|
|
this.pgpKeyId = id;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-05-22 09:36:41 -04:00
|
|
|
public long getPgpKeyId() {
|
|
|
|
return this.pgpKeyId;
|
|
|
|
}
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-05-22 09:36:41 -04:00
|
|
|
private Account account;
|
2014-07-27 12:07:04 -04:00
|
|
|
private List<User> users = new CopyOnWriteArrayList<User>();
|
2014-03-02 23:01:02 -05:00
|
|
|
private Conversation conversation;
|
|
|
|
private boolean isOnline = false;
|
2014-07-18 06:44:33 -04:00
|
|
|
private int error = ERROR_ROOM_NOT_FOUND;
|
2014-03-02 23:01:02 -05:00
|
|
|
private OnRenameListener renameListener = null;
|
2014-03-19 10:05:01 -04:00
|
|
|
private boolean aboutToRename = false;
|
2014-03-03 20:51:01 -05:00
|
|
|
private User self = new User();
|
2014-03-14 17:40:56 -04:00
|
|
|
private String subject = null;
|
2014-07-16 06:34:09 -04:00
|
|
|
private String joinnick;
|
2014-09-03 13:35:45 -04:00
|
|
|
private String password = null;
|
2014-09-07 08:06:23 -04:00
|
|
|
private boolean passwordChanged = false;
|
2014-03-02 23:01:02 -05:00
|
|
|
|
2014-05-22 09:36:41 -04:00
|
|
|
public MucOptions(Account account) {
|
|
|
|
this.account = account;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
public void deleteUser(String name) {
|
2014-07-29 08:42:17 -04:00
|
|
|
for (int i = 0; i < users.size(); ++i) {
|
2014-03-02 23:01:02 -05:00
|
|
|
if (users.get(i).getName().equals(name)) {
|
|
|
|
users.remove(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
public void addUser(User user) {
|
2014-07-29 08:42:17 -04:00
|
|
|
for (int i = 0; i < users.size(); ++i) {
|
2014-03-02 23:01:02 -05:00
|
|
|
if (users.get(i).getName().equals(user.getName())) {
|
|
|
|
users.set(i, user);
|
|
|
|
return;
|
|
|
|
}
|
2014-03-01 05:05:40 -05:00
|
|
|
}
|
2014-03-02 23:01:02 -05:00
|
|
|
users.add(user);
|
2014-07-29 08:42:17 -04:00
|
|
|
}
|
|
|
|
|
2014-05-22 09:36:41 -04:00
|
|
|
public void processPacket(PresencePacket packet, PgpEngine pgp) {
|
2014-03-25 12:08:52 -04:00
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-07-29 08:42:17 -04:00
|
|
|
if (fromParts.length >= 2) {
|
2014-03-25 12:08:52 -04:00
|
|
|
String name = fromParts[1];
|
2014-03-02 23:01:02 -05:00
|
|
|
String type = packet.getAttribute("type");
|
2014-07-29 08:42:17 -04:00
|
|
|
if (type == null) {
|
2014-03-02 23:01:02 -05:00
|
|
|
User user = new User();
|
2014-07-29 08:42:17 -04:00
|
|
|
Element item = packet.findChild("x",
|
|
|
|
"http://jabber.org/protocol/muc#user")
|
|
|
|
.findChild("item");
|
2014-03-02 23:01:02 -05:00
|
|
|
user.setName(name);
|
|
|
|
user.setAffiliation(item.getAttribute("affiliation"));
|
|
|
|
user.setRole(item.getAttribute("role"));
|
2014-07-29 08:42:17 -04:00
|
|
|
user.setJid(item.getAttribute("jid"));
|
2014-03-02 23:01:02 -05:00
|
|
|
user.setName(name);
|
2014-07-16 06:34:09 -04:00
|
|
|
if (name.equals(this.joinnick)) {
|
2014-03-02 23:01:02 -05:00
|
|
|
this.isOnline = true;
|
2014-07-18 06:44:33 -04:00
|
|
|
this.error = ERROR_NO_ERROR;
|
2014-03-03 20:51:01 -05:00
|
|
|
self = user;
|
2014-07-16 06:34:09 -04:00
|
|
|
if (aboutToRename) {
|
2014-07-29 08:42:17 -04:00
|
|
|
if (renameListener != null) {
|
2014-07-16 06:34:09 -04:00
|
|
|
renameListener.onRename(true);
|
|
|
|
}
|
|
|
|
aboutToRename = false;
|
|
|
|
}
|
2014-09-07 08:06:23 -04:00
|
|
|
if (conversation.getBookmark() != null &&
|
|
|
|
conversation.getBookmark().isProvidePassword()) {
|
|
|
|
this.passwordChanged = false;
|
|
|
|
}
|
2014-03-03 20:51:01 -05:00
|
|
|
} else {
|
|
|
|
addUser(user);
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
2014-05-22 09:36:41 -04:00
|
|
|
if (pgp != null) {
|
2014-07-29 08:42:17 -04:00
|
|
|
Element x = packet.findChild("x", "jabber:x:signed");
|
2014-05-22 09:36:41 -04:00
|
|
|
if (x != null) {
|
|
|
|
Element status = packet.findChild("status");
|
|
|
|
String msg;
|
|
|
|
if (status != null) {
|
|
|
|
msg = status.getContent();
|
|
|
|
} else {
|
|
|
|
msg = "";
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
user.setPgpKeyId(pgp.fetchKeyId(account, msg,
|
|
|
|
x.getContent()));
|
2014-05-22 09:36:41 -04:00
|
|
|
}
|
|
|
|
}
|
2014-03-02 23:01:02 -05:00
|
|
|
} else if (type.equals("unavailable")) {
|
|
|
|
deleteUser(packet.getAttribute("from").split("/")[1]);
|
|
|
|
} else if (type.equals("error")) {
|
|
|
|
Element error = packet.findChild("error");
|
|
|
|
if (error.hasChild("conflict")) {
|
2014-03-19 10:05:01 -04:00
|
|
|
if (aboutToRename) {
|
2014-07-29 08:42:17 -04:00
|
|
|
if (renameListener != null) {
|
2014-03-19 10:05:01 -04:00
|
|
|
renameListener.onRename(false);
|
|
|
|
}
|
|
|
|
aboutToRename = false;
|
2014-07-15 15:54:11 -04:00
|
|
|
this.setJoinNick(getActualNick());
|
2014-03-19 10:05:01 -04:00
|
|
|
} else {
|
2014-07-29 08:42:17 -04:00
|
|
|
this.error = ERROR_NICK_IN_USE;
|
2014-03-19 10:05:01 -04:00
|
|
|
}
|
2014-09-03 13:35:45 -04:00
|
|
|
} else if (error.hasChild("not-authorized")) {
|
2014-09-07 08:06:23 -04:00
|
|
|
if (conversation.getBookmark() != null &&
|
|
|
|
conversation.getBookmark().isProvidePassword()) {
|
|
|
|
this.passwordChanged = true;
|
|
|
|
}
|
2014-09-03 13:35:45 -04:00
|
|
|
this.error = ERROR_PASSWORD_REQUIRED;
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
}
|
2014-03-25 12:08:52 -04:00
|
|
|
}
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
public List<User> getUsers() {
|
|
|
|
return this.users;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-07-15 11:11:43 -04:00
|
|
|
public String getProposedNick() {
|
|
|
|
String[] mucParts = conversation.getContactJid().split("/");
|
2014-07-29 08:42:17 -04:00
|
|
|
if (conversation.getBookmark() != null
|
|
|
|
&& conversation.getBookmark().getNick() != null) {
|
2014-07-15 11:11:43 -04:00
|
|
|
return conversation.getBookmark().getNick();
|
2014-03-02 23:01:02 -05:00
|
|
|
} else {
|
2014-07-15 11:11:43 -04:00
|
|
|
if (mucParts.length == 2) {
|
|
|
|
return mucParts[1];
|
2014-03-11 10:44:22 -04:00
|
|
|
} else {
|
2014-07-15 11:11:43 -04:00
|
|
|
return account.getUsername();
|
2014-03-11 10:44:22 -04:00
|
|
|
}
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-07-15 15:54:11 -04:00
|
|
|
public String getActualNick() {
|
2014-07-29 08:42:17 -04:00
|
|
|
if (this.self.getName() != null) {
|
2014-07-15 15:54:11 -04:00
|
|
|
return this.self.getName();
|
|
|
|
} else {
|
|
|
|
return this.getProposedNick();
|
|
|
|
}
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-07-15 11:11:43 -04:00
|
|
|
public void setJoinNick(String nick) {
|
2014-07-16 06:34:09 -04:00
|
|
|
this.joinnick = nick;
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
public void setConversation(Conversation conversation) {
|
|
|
|
this.conversation = conversation;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
public boolean online() {
|
|
|
|
return this.isOnline;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
public int getError() {
|
|
|
|
return this.error;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOnRenameListener(OnRenameListener listener) {
|
|
|
|
this.renameListener = listener;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
public OnRenameListener getOnRenameListener() {
|
|
|
|
return this.renameListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOffline() {
|
|
|
|
this.users.clear();
|
|
|
|
this.error = 0;
|
|
|
|
this.isOnline = false;
|
2014-03-01 05:05:40 -05:00
|
|
|
}
|
2014-03-03 20:51:01 -05:00
|
|
|
|
|
|
|
public User getSelf() {
|
|
|
|
return self;
|
|
|
|
}
|
2014-03-14 17:40:56 -04:00
|
|
|
|
|
|
|
public void setSubject(String content) {
|
|
|
|
this.subject = content;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-03-14 17:40:56 -04:00
|
|
|
public String getSubject() {
|
|
|
|
return this.subject;
|
|
|
|
}
|
2014-03-19 10:05:01 -04:00
|
|
|
|
|
|
|
public void flagAboutToRename() {
|
|
|
|
this.aboutToRename = true;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-05-22 14:54:54 -04:00
|
|
|
public long[] getPgpKeyIds() {
|
|
|
|
List<Long> ids = new ArrayList<Long>();
|
2014-07-29 08:42:17 -04:00
|
|
|
for (User user : getUsers()) {
|
|
|
|
if (user.getPgpKeyId() != 0) {
|
2014-05-22 14:54:54 -04:00
|
|
|
ids.add(user.getPgpKeyId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
long[] primitivLongArray = new long[ids.size()];
|
2014-07-29 08:42:17 -04:00
|
|
|
for (int i = 0; i < ids.size(); ++i) {
|
2014-05-22 14:54:54 -04:00
|
|
|
primitivLongArray[i] = ids.get(i);
|
|
|
|
}
|
|
|
|
return primitivLongArray;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-06-01 05:24:35 -04:00
|
|
|
public boolean pgpKeysInUse() {
|
2014-07-29 08:42:17 -04:00
|
|
|
for (User user : getUsers()) {
|
|
|
|
if (user.getPgpKeyId() != 0) {
|
2014-06-01 05:24:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-29 08:42:17 -04:00
|
|
|
|
2014-06-01 05:24:35 -04:00
|
|
|
public boolean everybodyHasKeys() {
|
2014-07-29 08:42:17 -04:00
|
|
|
for (User user : getUsers()) {
|
|
|
|
if (user.getPgpKeyId() == 0) {
|
2014-06-01 05:24:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-15 11:11:43 -04:00
|
|
|
|
|
|
|
public String getJoinJid() {
|
2014-07-29 08:42:17 -04:00
|
|
|
return this.conversation.getContactJid().split("/")[0] + "/"
|
|
|
|
+ this.joinnick;
|
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-07-29 08:42:17 -04:00
|
|
|
public String getTrueCounterpart(String counterpart) {
|
2014-08-31 10:28:21 -04:00
|
|
|
for (User user : this.getUsers()) {
|
2014-07-29 08:42:17 -04:00
|
|
|
if (user.getName().equals(counterpart)) {
|
|
|
|
return user.getJid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2014-07-15 11:11:43 -04:00
|
|
|
}
|
2014-09-03 13:35:45 -04:00
|
|
|
|
|
|
|
public String getPassword() {
|
2014-09-07 08:06:23 -04:00
|
|
|
if (conversation.getBookmark() != null &&
|
|
|
|
conversation.getBookmark().getPassword() != null) {
|
|
|
|
return conversation.getBookmark().getPassword();
|
|
|
|
} else {
|
|
|
|
return this.password;
|
|
|
|
}
|
2014-09-03 13:35:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setPassword(String password) {
|
2014-09-07 08:06:23 -04:00
|
|
|
if (conversation.getBookmark() != null &&
|
|
|
|
conversation.getBookmark().isProvidePassword()) {
|
|
|
|
conversation.getBookmark().setPassword(password);
|
|
|
|
} else {
|
|
|
|
this.password = password;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isPasswordChanged() {
|
|
|
|
return this.passwordChanged;
|
2014-09-03 13:35:45 -04:00
|
|
|
}
|
2014-09-07 08:06:23 -04:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|