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