2014-02-28 17:46:01 +00:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
2014-03-03 04:01:02 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-05-22 13:36:41 +00:00
|
|
|
import eu.siacs.conversations.crypto.PgpEngine;
|
2014-03-03 04:01:02 +00:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2014-03-10 18:22:13 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
|
2014-03-01 10:05:40 +00:00
|
|
|
import android.annotation.SuppressLint;
|
2014-02-28 17:46:01 +00:00
|
|
|
|
2014-03-01 10:05:40 +00:00
|
|
|
@SuppressLint("DefaultLocale")
|
|
|
|
public class MucOptions {
|
2014-03-03 04:01:02 +00:00
|
|
|
public static final int ERROR_NICK_IN_USE = 1;
|
|
|
|
|
|
|
|
public interface OnRenameListener {
|
|
|
|
public void onRename(boolean success);
|
|
|
|
}
|
|
|
|
|
2014-03-01 10:05:40 +00: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;
|
|
|
|
|
|
|
|
private int role;
|
|
|
|
private int affiliation;
|
2014-03-03 04:01:02 +00:00
|
|
|
private String name;
|
2014-05-22 13:36:41 +00:00
|
|
|
private long pgpKeyId = 0;
|
2014-03-03 04:01:02 +00:00
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
public void setName(String user) {
|
|
|
|
this.name = user;
|
|
|
|
}
|
|
|
|
|
2014-03-01 10:05:40 +00:00
|
|
|
public int getRole() {
|
|
|
|
return this.role;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public int getAffiliation() {
|
|
|
|
return this.affiliation;
|
|
|
|
}
|
2014-03-03 04:01:02 +00: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-05-22 13:36:41 +00:00
|
|
|
public void setPgpKeyId(long id) {
|
|
|
|
this.pgpKeyId = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getPgpKeyId() {
|
|
|
|
return this.pgpKeyId;
|
|
|
|
}
|
2014-03-03 04:01:02 +00:00
|
|
|
}
|
2014-05-22 13:36:41 +00:00
|
|
|
private Account account;
|
2014-03-03 04:01:02 +00:00
|
|
|
private ArrayList<User> users = new ArrayList<User>();
|
|
|
|
private Conversation conversation;
|
|
|
|
private boolean isOnline = false;
|
|
|
|
private int error = 0;
|
|
|
|
private OnRenameListener renameListener = null;
|
2014-03-19 14:05:01 +00:00
|
|
|
private boolean aboutToRename = false;
|
2014-03-04 01:51:01 +00:00
|
|
|
private User self = new User();
|
2014-03-14 21:40:56 +00:00
|
|
|
private String subject = null;
|
2014-03-03 04:01:02 +00:00
|
|
|
|
2014-05-22 13:36:41 +00:00
|
|
|
public MucOptions(Account account) {
|
|
|
|
this.account = account;
|
|
|
|
}
|
2014-03-03 04:01:02 +00:00
|
|
|
|
|
|
|
public void deleteUser(String name) {
|
|
|
|
for(int i = 0; i < users.size(); ++i) {
|
|
|
|
if (users.get(i).getName().equals(name)) {
|
|
|
|
users.remove(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addUser(User user) {
|
|
|
|
for(int i = 0; i < users.size(); ++i) {
|
|
|
|
if (users.get(i).getName().equals(user.getName())) {
|
|
|
|
users.set(i, user);
|
|
|
|
return;
|
|
|
|
}
|
2014-03-01 10:05:40 +00:00
|
|
|
}
|
2014-03-03 04:01:02 +00:00
|
|
|
users.add(user);
|
|
|
|
}
|
|
|
|
|
2014-05-22 13:36:41 +00:00
|
|
|
public void processPacket(PresencePacket packet, PgpEngine pgp) {
|
2014-03-25 16:08:52 +00:00
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
|
|
|
if (fromParts.length>=2) {
|
|
|
|
String name = fromParts[1];
|
2014-03-03 04:01:02 +00:00
|
|
|
String type = packet.getAttribute("type");
|
|
|
|
if (type==null) {
|
|
|
|
User user = new User();
|
2014-03-27 01:02:59 +00:00
|
|
|
Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
|
2014-03-03 04:01:02 +00:00
|
|
|
user.setName(name);
|
|
|
|
user.setAffiliation(item.getAttribute("affiliation"));
|
|
|
|
user.setRole(item.getAttribute("role"));
|
|
|
|
user.setName(name);
|
|
|
|
if (name.equals(getNick())) {
|
|
|
|
this.isOnline = true;
|
|
|
|
this.error = 0;
|
2014-03-04 01:51:01 +00:00
|
|
|
self = user;
|
|
|
|
} else {
|
|
|
|
addUser(user);
|
2014-03-03 04:01:02 +00:00
|
|
|
}
|
2014-05-22 13:36:41 +00:00
|
|
|
if (pgp != null) {
|
|
|
|
Element x = packet.findChild("x",
|
|
|
|
"jabber:x:signed");
|
|
|
|
if (x != null) {
|
|
|
|
Element status = packet.findChild("status");
|
|
|
|
String msg;
|
|
|
|
if (status != null) {
|
|
|
|
msg = status.getContent();
|
|
|
|
} else {
|
|
|
|
msg = "";
|
|
|
|
}
|
|
|
|
user.setPgpKeyId(pgp.fetchKeyId(account,msg, x.getContent()));
|
|
|
|
}
|
|
|
|
}
|
2014-03-03 04:01:02 +00:00
|
|
|
} else if (type.equals("unavailable")) {
|
|
|
|
if (name.equals(getNick())) {
|
2014-03-27 01:02:59 +00:00
|
|
|
Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
|
2014-03-03 04:01:02 +00:00
|
|
|
String nick = item.getAttribute("nick");
|
|
|
|
if (nick!=null) {
|
2014-03-19 14:05:01 +00:00
|
|
|
aboutToRename = false;
|
2014-03-03 04:01:02 +00:00
|
|
|
if (renameListener!=null) {
|
|
|
|
renameListener.onRename(true);
|
|
|
|
}
|
|
|
|
this.setNick(nick);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deleteUser(packet.getAttribute("from").split("/")[1]);
|
|
|
|
} else if (type.equals("error")) {
|
|
|
|
Element error = packet.findChild("error");
|
|
|
|
if (error.hasChild("conflict")) {
|
2014-03-19 14:05:01 +00:00
|
|
|
if (aboutToRename) {
|
|
|
|
if (renameListener!=null) {
|
|
|
|
renameListener.onRename(false);
|
|
|
|
}
|
|
|
|
aboutToRename = false;
|
|
|
|
} else {
|
|
|
|
this.error = ERROR_NICK_IN_USE;
|
|
|
|
}
|
2014-03-03 04:01:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-25 16:08:52 +00:00
|
|
|
}
|
2014-03-03 04:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<User> getUsers() {
|
|
|
|
return this.users;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getNick() {
|
|
|
|
String[] split = conversation.getContactJid().split("/");
|
|
|
|
if (split.length == 2) {
|
|
|
|
return split[1];
|
|
|
|
} else {
|
2014-03-11 14:44:22 +00:00
|
|
|
if (conversation.getAccount()!=null) {
|
|
|
|
return conversation.getAccount().getUsername();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-03 04:01:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setNick(String nick) {
|
|
|
|
String jid = conversation.getContactJid().split("/")[0]+"/"+nick;
|
|
|
|
conversation.setContactJid(jid);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setConversation(Conversation conversation) {
|
|
|
|
this.conversation = conversation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean online() {
|
|
|
|
return this.isOnline;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getError() {
|
|
|
|
return this.error;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOnRenameListener(OnRenameListener listener) {
|
|
|
|
this.renameListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
public OnRenameListener getOnRenameListener() {
|
|
|
|
return this.renameListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOffline() {
|
|
|
|
this.users.clear();
|
|
|
|
this.error = 0;
|
|
|
|
this.isOnline = false;
|
2014-03-01 10:05:40 +00:00
|
|
|
}
|
2014-03-04 01:51:01 +00:00
|
|
|
|
|
|
|
public User getSelf() {
|
|
|
|
return self;
|
|
|
|
}
|
2014-03-14 21:40:56 +00:00
|
|
|
|
|
|
|
public void setSubject(String content) {
|
|
|
|
this.subject = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getSubject() {
|
|
|
|
return this.subject;
|
|
|
|
}
|
2014-03-19 14:05:01 +00:00
|
|
|
|
|
|
|
public void flagAboutToRename() {
|
|
|
|
this.aboutToRename = true;
|
|
|
|
}
|
2014-05-22 18:54:54 +00:00
|
|
|
|
|
|
|
public long[] getPgpKeyIds() {
|
|
|
|
List<Long> ids = new ArrayList<Long>();
|
|
|
|
for(User user : getUsers()) {
|
|
|
|
if(user.getPgpKeyId()!=0) {
|
|
|
|
ids.add(user.getPgpKeyId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
long[] primitivLongArray = new long[ids.size()];
|
|
|
|
for(int i = 0; i < ids.size(); ++i) {
|
|
|
|
primitivLongArray[i] = ids.get(i);
|
|
|
|
}
|
|
|
|
return primitivLongArray;
|
|
|
|
}
|
2014-03-03 04:01:02 +00:00
|
|
|
}
|