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