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