1package eu.siacs.conversations.entities;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.concurrent.CopyOnWriteArrayList;
6
7import eu.siacs.conversations.crypto.PgpEngine;
8import eu.siacs.conversations.xml.Element;
9import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
10import android.annotation.SuppressLint;
11
12@SuppressLint("DefaultLocale")
13public class MucOptions {
14 public static final int ERROR_NO_ERROR = 0;
15 public static final int ERROR_NICK_IN_USE = 1;
16 public static final int ERROR_ROOM_NOT_FOUND = 2;
17 public static final int ERROR_PASSWORD_REQUIRED = 3;
18 public static final int ERROR_BANNED = 4;
19 public static final int ERROR_MEMBERS_ONLY = 5;
20
21 public static final int KICKED_FROM_ROOM = 9;
22
23 public static final String STATUS_CODE_BANNED = "301";
24 public static final String STATUS_CODE_KICKED = "307";
25
26 public interface OnRenameListener {
27 public void onRename(boolean success);
28 }
29
30 public class User {
31 public static final int ROLE_MODERATOR = 3;
32 public static final int ROLE_NONE = 0;
33 public static final int ROLE_PARTICIPANT = 2;
34 public static final int ROLE_VISITOR = 1;
35 public static final int AFFILIATION_ADMIN = 4;
36 public static final int AFFILIATION_OWNER = 3;
37 public static final int AFFILIATION_MEMBER = 2;
38 public static final int AFFILIATION_OUTCAST = 1;
39 public static final int AFFILIATION_NONE = 0;
40
41 private int role;
42 private int affiliation;
43 private String name;
44 private String jid;
45 private long pgpKeyId = 0;
46
47 public String getName() {
48 return name;
49 }
50
51 public void setName(String user) {
52 this.name = user;
53 }
54
55 public void setJid(String jid) {
56 this.jid = jid;
57 }
58
59 public String getJid() {
60 return this.jid;
61 }
62
63 public int getRole() {
64 return this.role;
65 }
66
67 public void setRole(String role) {
68 role = role.toLowerCase();
69 if (role.equals("moderator")) {
70 this.role = ROLE_MODERATOR;
71 } else if (role.equals("participant")) {
72 this.role = ROLE_PARTICIPANT;
73 } else if (role.equals("visitor")) {
74 this.role = ROLE_VISITOR;
75 } else {
76 this.role = ROLE_NONE;
77 }
78 }
79
80 public int getAffiliation() {
81 return this.affiliation;
82 }
83
84 public void setAffiliation(String affiliation) {
85 if (affiliation.equalsIgnoreCase("admin")) {
86 this.affiliation = AFFILIATION_ADMIN;
87 } else if (affiliation.equalsIgnoreCase("owner")) {
88 this.affiliation = AFFILIATION_OWNER;
89 } else if (affiliation.equalsIgnoreCase("member")) {
90 this.affiliation = AFFILIATION_MEMBER;
91 } else if (affiliation.equalsIgnoreCase("outcast")) {
92 this.affiliation = AFFILIATION_OUTCAST;
93 } else {
94 this.affiliation = AFFILIATION_NONE;
95 }
96 }
97
98 public void setPgpKeyId(long id) {
99 this.pgpKeyId = id;
100 }
101
102 public long getPgpKeyId() {
103 return this.pgpKeyId;
104 }
105
106 public Contact getContact() {
107 return account.getRoster().getContactFromRoster(getJid());
108 }
109 }
110
111 private Account account;
112 private List<User> users = new CopyOnWriteArrayList<User>();
113 private Conversation conversation;
114 private boolean isOnline = false;
115 private int error = ERROR_ROOM_NOT_FOUND;
116 private OnRenameListener renameListener = null;
117 private boolean aboutToRename = false;
118 private User self = new User();
119 private String subject = null;
120 private String joinnick;
121 private String password = null;
122
123 public MucOptions(Conversation conversation) {
124 this.account = conversation.getAccount();
125 this.conversation = conversation;
126 }
127
128 public void deleteUser(String name) {
129 for (int i = 0; i < users.size(); ++i) {
130 if (users.get(i).getName().equals(name)) {
131 users.remove(i);
132 return;
133 }
134 }
135 }
136
137 public void addUser(User user) {
138 for (int i = 0; i < users.size(); ++i) {
139 if (users.get(i).getName().equals(user.getName())) {
140 users.set(i, user);
141 return;
142 }
143 }
144 users.add(user);
145 }
146
147 public void processPacket(PresencePacket packet, PgpEngine pgp) {
148 String[] fromParts = packet.getFrom().split("/", 2);
149 if (fromParts.length >= 2) {
150 String name = fromParts[1];
151 String type = packet.getAttribute("type");
152 if (type == null) {
153 User user = new User();
154 Element item = packet.findChild("x",
155 "http://jabber.org/protocol/muc#user")
156 .findChild("item");
157 user.setName(name);
158 user.setAffiliation(item.getAttribute("affiliation"));
159 user.setRole(item.getAttribute("role"));
160 user.setJid(item.getAttribute("jid"));
161 user.setName(name);
162 if (name.equals(this.joinnick)) {
163 this.isOnline = true;
164 this.error = ERROR_NO_ERROR;
165 self = user;
166 if (aboutToRename) {
167 if (renameListener != null) {
168 renameListener.onRename(true);
169 }
170 aboutToRename = false;
171 }
172 } else {
173 addUser(user);
174 }
175 if (pgp != null) {
176 Element x = packet.findChild("x", "jabber:x:signed");
177 if (x != null) {
178 Element status = packet.findChild("status");
179 String msg;
180 if (status != null) {
181 msg = status.getContent();
182 } else {
183 msg = "";
184 }
185 user.setPgpKeyId(pgp.fetchKeyId(account, msg,
186 x.getContent()));
187 }
188 }
189 } else if (type.equals("unavailable") && name.equals(this.joinnick)) {
190 Element x = packet.findChild("x",
191 "http://jabber.org/protocol/muc#user");
192 if (x != null) {
193 Element status = x.findChild("status");
194 if (status != null) {
195 String code = status.getAttribute("code");
196 if (STATUS_CODE_KICKED.equals(code)) {
197 this.isOnline = false;
198 this.error = KICKED_FROM_ROOM;
199 } else if (STATUS_CODE_BANNED.equals(code)) {
200 this.isOnline = false;
201 this.error = ERROR_BANNED;
202 }
203 }
204 }
205 } else if (type.equals("unavailable")) {
206 deleteUser(packet.getAttribute("from").split("/", 2)[1]);
207 } else if (type.equals("error")) {
208 Element error = packet.findChild("error");
209 if (error != null && error.hasChild("conflict")) {
210 if (aboutToRename) {
211 if (renameListener != null) {
212 renameListener.onRename(false);
213 }
214 aboutToRename = false;
215 this.setJoinNick(getActualNick());
216 } else {
217 this.error = ERROR_NICK_IN_USE;
218 }
219 } else if (error != null && error.hasChild("not-authorized")) {
220 this.error = ERROR_PASSWORD_REQUIRED;
221 } else if (error != null && error.hasChild("forbidden")) {
222 this.error = ERROR_BANNED;
223 } else if (error != null
224 && error.hasChild("registration-required")) {
225 this.error = ERROR_MEMBERS_ONLY;
226 }
227 }
228 }
229 }
230
231 public List<User> getUsers() {
232 return this.users;
233 }
234
235 public String getProposedNick() {
236 String[] mucParts = conversation.getContactJid().split("/", 2);
237 if (conversation.getBookmark() != null
238 && conversation.getBookmark().getNick() != null) {
239 return conversation.getBookmark().getNick();
240 } else {
241 if (mucParts.length == 2) {
242 return mucParts[1];
243 } else {
244 return account.getUsername();
245 }
246 }
247 }
248
249 public String getActualNick() {
250 if (this.self.getName() != null) {
251 return this.self.getName();
252 } else {
253 return this.getProposedNick();
254 }
255 }
256
257 public void setJoinNick(String nick) {
258 this.joinnick = nick;
259 }
260
261 public boolean online() {
262 return this.isOnline;
263 }
264
265 public int getError() {
266 return this.error;
267 }
268
269 public void setOnRenameListener(OnRenameListener listener) {
270 this.renameListener = listener;
271 }
272
273 public OnRenameListener getOnRenameListener() {
274 return this.renameListener;
275 }
276
277 public void setOffline() {
278 this.users.clear();
279 this.error = 0;
280 this.isOnline = false;
281 }
282
283 public User getSelf() {
284 return self;
285 }
286
287 public void setSubject(String content) {
288 this.subject = content;
289 }
290
291 public String getSubject() {
292 return this.subject;
293 }
294
295 public void flagAboutToRename() {
296 this.aboutToRename = true;
297 }
298
299 public long[] getPgpKeyIds() {
300 List<Long> ids = new ArrayList<Long>();
301 for (User user : getUsers()) {
302 if (user.getPgpKeyId() != 0) {
303 ids.add(user.getPgpKeyId());
304 }
305 }
306 long[] primitivLongArray = new long[ids.size()];
307 for (int i = 0; i < ids.size(); ++i) {
308 primitivLongArray[i] = ids.get(i);
309 }
310 return primitivLongArray;
311 }
312
313 public boolean pgpKeysInUse() {
314 for (User user : getUsers()) {
315 if (user.getPgpKeyId() != 0) {
316 return true;
317 }
318 }
319 return false;
320 }
321
322 public boolean everybodyHasKeys() {
323 for (User user : getUsers()) {
324 if (user.getPgpKeyId() == 0) {
325 return false;
326 }
327 }
328 return true;
329 }
330
331 public String getJoinJid() {
332 return this.conversation.getContactJid().split("/", 2)[0] + "/"
333 + this.joinnick;
334 }
335
336 public String getTrueCounterpart(String counterpart) {
337 for (User user : this.getUsers()) {
338 if (user.getName().equals(counterpart)) {
339 return user.getJid();
340 }
341 }
342 return null;
343 }
344
345 public String getPassword() {
346 this.password = conversation
347 .getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
348 if (this.password == null && conversation.getBookmark() != null
349 && conversation.getBookmark().getPassword() != null) {
350 return conversation.getBookmark().getPassword();
351 } else {
352 return this.password;
353 }
354 }
355
356 public void setPassword(String password) {
357 if (conversation.getBookmark() != null) {
358 conversation.getBookmark().setPassword(password);
359 } else {
360 this.password = password;
361 }
362 conversation
363 .setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
364 }
365
366 public Conversation getConversation() {
367 return this.conversation;
368 }
369}