1package eu.siacs.conversations.entities;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import eu.siacs.conversations.crypto.PgpEngine;
7import eu.siacs.conversations.xml.Element;
8import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
9import android.annotation.SuppressLint;
10
11@SuppressLint("DefaultLocale")
12public class MucOptions {
13 public static final int ERROR_NICK_IN_USE = 1;
14
15 public interface OnRenameListener {
16 public void onRename(boolean success);
17 }
18
19 public class User {
20 public static final int ROLE_MODERATOR = 3;
21 public static final int ROLE_NONE = 0;
22 public static final int ROLE_PARTICIPANT = 2;
23 public static final int ROLE_VISITOR = 1;
24 public static final int AFFILIATION_ADMIN = 4;
25 public static final int AFFILIATION_OWNER = 3;
26 public static final int AFFILIATION_MEMBER = 2;
27 public static final int AFFILIATION_OUTCAST = 1;
28 public static final int AFFILIATION_NONE = 0;
29
30 private int role;
31 private int affiliation;
32 private String name;
33 private long pgpKeyId = 0;
34
35 public String getName() {
36 return name;
37 }
38 public void setName(String user) {
39 this.name = user;
40 }
41
42 public int getRole() {
43 return this.role;
44 }
45 public void setRole(String role) {
46 role = role.toLowerCase();
47 if (role.equals("moderator")) {
48 this.role = ROLE_MODERATOR;
49 } else if (role.equals("participant")) {
50 this.role = ROLE_PARTICIPANT;
51 } else if (role.equals("visitor")) {
52 this.role = ROLE_VISITOR;
53 } else {
54 this.role = ROLE_NONE;
55 }
56 }
57 public int getAffiliation() {
58 return this.affiliation;
59 }
60 public void setAffiliation(String affiliation) {
61 if (affiliation.equalsIgnoreCase("admin")) {
62 this.affiliation = AFFILIATION_ADMIN;
63 } else if (affiliation.equalsIgnoreCase("owner")) {
64 this.affiliation = AFFILIATION_OWNER;
65 } else if (affiliation.equalsIgnoreCase("member")) {
66 this.affiliation = AFFILIATION_MEMBER;
67 } else if (affiliation.equalsIgnoreCase("outcast")) {
68 this.affiliation = AFFILIATION_OUTCAST;
69 } else {
70 this.affiliation = AFFILIATION_NONE;
71 }
72 }
73 public void setPgpKeyId(long id) {
74 this.pgpKeyId = id;
75 }
76
77 public long getPgpKeyId() {
78 return this.pgpKeyId;
79 }
80 }
81 private Account account;
82 private ArrayList<User> users = new ArrayList<User>();
83 private Conversation conversation;
84 private boolean isOnline = false;
85 private int error = 0;
86 private OnRenameListener renameListener = null;
87 private boolean aboutToRename = false;
88 private User self = new User();
89 private String subject = null;
90
91 public MucOptions(Account account) {
92 this.account = account;
93 }
94
95 public void deleteUser(String name) {
96 for(int i = 0; i < users.size(); ++i) {
97 if (users.get(i).getName().equals(name)) {
98 users.remove(i);
99 return;
100 }
101 }
102 }
103
104 public void addUser(User user) {
105 for(int i = 0; i < users.size(); ++i) {
106 if (users.get(i).getName().equals(user.getName())) {
107 users.set(i, user);
108 return;
109 }
110 }
111 users.add(user);
112 }
113
114 public void processPacket(PresencePacket packet, PgpEngine pgp) {
115 String[] fromParts = packet.getFrom().split("/");
116 if (fromParts.length>=2) {
117 String name = fromParts[1];
118 String type = packet.getAttribute("type");
119 if (type==null) {
120 User user = new User();
121 Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
122 user.setName(name);
123 user.setAffiliation(item.getAttribute("affiliation"));
124 user.setRole(item.getAttribute("role"));
125 user.setName(name);
126 if (name.equals(getNick())) {
127 this.isOnline = true;
128 this.error = 0;
129 self = user;
130 } else {
131 addUser(user);
132 }
133 if (pgp != null) {
134 Element x = packet.findChild("x",
135 "jabber:x:signed");
136 if (x != null) {
137 Element status = packet.findChild("status");
138 String msg;
139 if (status != null) {
140 msg = status.getContent();
141 } else {
142 msg = "";
143 }
144 user.setPgpKeyId(pgp.fetchKeyId(account,msg, x.getContent()));
145 }
146 }
147 } else if (type.equals("unavailable")) {
148 if (name.equals(getNick())) {
149 Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
150 String nick = item.getAttribute("nick");
151 if (nick!=null) {
152 aboutToRename = false;
153 if (renameListener!=null) {
154 renameListener.onRename(true);
155 }
156 this.setNick(nick);
157 }
158 }
159 deleteUser(packet.getAttribute("from").split("/")[1]);
160 } else if (type.equals("error")) {
161 Element error = packet.findChild("error");
162 if (error.hasChild("conflict")) {
163 if (aboutToRename) {
164 if (renameListener!=null) {
165 renameListener.onRename(false);
166 }
167 aboutToRename = false;
168 } else {
169 this.error = ERROR_NICK_IN_USE;
170 }
171 }
172 }
173 }
174 }
175
176 public List<User> getUsers() {
177 return this.users;
178 }
179
180 public String getNick() {
181 String[] split = conversation.getContactJid().split("/");
182 if (split.length == 2) {
183 return split[1];
184 } else {
185 if (conversation.getAccount()!=null) {
186 return conversation.getAccount().getUsername();
187 } else {
188 return null;
189 }
190 }
191 }
192
193 public void setNick(String nick) {
194 String jid = conversation.getContactJid().split("/")[0]+"/"+nick;
195 conversation.setContactJid(jid);
196 }
197
198 public void setConversation(Conversation conversation) {
199 this.conversation = conversation;
200 }
201
202 public boolean online() {
203 return this.isOnline;
204 }
205
206 public int getError() {
207 return this.error;
208 }
209
210 public void setOnRenameListener(OnRenameListener listener) {
211 this.renameListener = listener;
212 }
213
214 public OnRenameListener getOnRenameListener() {
215 return this.renameListener;
216 }
217
218 public void setOffline() {
219 this.users.clear();
220 this.error = 0;
221 this.isOnline = false;
222 }
223
224 public User getSelf() {
225 return self;
226 }
227
228 public void setSubject(String content) {
229 this.subject = content;
230 }
231
232 public String getSubject() {
233 return this.subject;
234 }
235
236 public void flagAboutToRename() {
237 this.aboutToRename = true;
238 }
239
240 public long[] getPgpKeyIds() {
241 List<Long> ids = new ArrayList<Long>();
242 for(User user : getUsers()) {
243 if(user.getPgpKeyId()!=0) {
244 ids.add(user.getPgpKeyId());
245 }
246 }
247 long[] primitivLongArray = new long[ids.size()];
248 for(int i = 0; i < ids.size(); ++i) {
249 primitivLongArray[i] = ids.get(i);
250 }
251 return primitivLongArray;
252 }
253
254 public boolean pgpKeysInUse() {
255 for(User user : getUsers()) {
256 if (user.getPgpKeyId()!=0) {
257 return true;
258 }
259 }
260 return false;
261 }
262
263 public boolean everybodyHasKeys() {
264 for(User user : getUsers()) {
265 if (user.getPgpKeyId()==0) {
266 return false;
267 }
268 }
269 return true;
270 }
271}