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 private String joinnick;
91
92 public MucOptions(Account account) {
93 this.account = account;
94 }
95
96 public void deleteUser(String name) {
97 for(int i = 0; i < users.size(); ++i) {
98 if (users.get(i).getName().equals(name)) {
99 users.remove(i);
100 return;
101 }
102 }
103 }
104
105 public void addUser(User user) {
106 for(int i = 0; i < users.size(); ++i) {
107 if (users.get(i).getName().equals(user.getName())) {
108 users.set(i, user);
109 return;
110 }
111 }
112 users.add(user);
113 }
114
115 public void processPacket(PresencePacket packet, PgpEngine pgp) {
116 String[] fromParts = packet.getFrom().split("/");
117 if (fromParts.length>=2) {
118 String name = fromParts[1];
119 String type = packet.getAttribute("type");
120 if (type==null) {
121 User user = new User();
122 Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
123 user.setName(name);
124 user.setAffiliation(item.getAttribute("affiliation"));
125 user.setRole(item.getAttribute("role"));
126 user.setName(name);
127 if (name.equals(this.joinnick)) {
128 this.isOnline = true;
129 this.error = 0;
130 self = user;
131 if (aboutToRename) {
132 if (renameListener!=null) {
133 renameListener.onRename(true);
134 }
135 aboutToRename = false;
136 }
137 } else {
138 addUser(user);
139 }
140 if (pgp != null) {
141 Element x = packet.findChild("x",
142 "jabber:x:signed");
143 if (x != null) {
144 Element status = packet.findChild("status");
145 String msg;
146 if (status != null) {
147 msg = status.getContent();
148 } else {
149 msg = "";
150 }
151 user.setPgpKeyId(pgp.fetchKeyId(account,msg, x.getContent()));
152 }
153 }
154 } else if (type.equals("unavailable")) {
155 deleteUser(packet.getAttribute("from").split("/")[1]);
156 } else if (type.equals("error")) {
157 Element error = packet.findChild("error");
158 if (error.hasChild("conflict")) {
159 if (aboutToRename) {
160 if (renameListener!=null) {
161 renameListener.onRename(false);
162 }
163 aboutToRename = false;
164 this.setJoinNick(getActualNick());
165 } else {
166 this.error = ERROR_NICK_IN_USE;
167 }
168 }
169 }
170 }
171 }
172
173 public List<User> getUsers() {
174 return this.users;
175 }
176
177 public String getProposedNick() {
178 String[] mucParts = conversation.getContactJid().split("/");
179 if (conversation.getBookmark() != null && conversation.getBookmark().getNick() != null) {
180 return conversation.getBookmark().getNick();
181 } else {
182 if (mucParts.length == 2) {
183 return mucParts[1];
184 } else {
185 return account.getUsername();
186 }
187 }
188 }
189
190 public String getActualNick() {
191 if (this.self.getName()!=null) {
192 return this.self.getName();
193 } else {
194 return this.getProposedNick();
195 }
196 }
197
198 public void setJoinNick(String nick) {
199 this.joinnick = nick;
200 }
201
202 public void setConversation(Conversation conversation) {
203 this.conversation = conversation;
204 }
205
206 public boolean online() {
207 return this.isOnline;
208 }
209
210 public int getError() {
211 return this.error;
212 }
213
214 public void setOnRenameListener(OnRenameListener listener) {
215 this.renameListener = listener;
216 }
217
218 public OnRenameListener getOnRenameListener() {
219 return this.renameListener;
220 }
221
222 public void setOffline() {
223 this.users.clear();
224 this.error = 0;
225 this.isOnline = false;
226 }
227
228 public User getSelf() {
229 return self;
230 }
231
232 public void setSubject(String content) {
233 this.subject = content;
234 }
235
236 public String getSubject() {
237 return this.subject;
238 }
239
240 public void flagAboutToRename() {
241 this.aboutToRename = true;
242 }
243
244 public long[] getPgpKeyIds() {
245 List<Long> ids = new ArrayList<Long>();
246 for(User user : getUsers()) {
247 if(user.getPgpKeyId()!=0) {
248 ids.add(user.getPgpKeyId());
249 }
250 }
251 long[] primitivLongArray = new long[ids.size()];
252 for(int i = 0; i < ids.size(); ++i) {
253 primitivLongArray[i] = ids.get(i);
254 }
255 return primitivLongArray;
256 }
257
258 public boolean pgpKeysInUse() {
259 for(User user : getUsers()) {
260 if (user.getPgpKeyId()!=0) {
261 return true;
262 }
263 }
264 return false;
265 }
266
267 public boolean everybodyHasKeys() {
268 for(User user : getUsers()) {
269 if (user.getPgpKeyId()==0) {
270 return false;
271 }
272 }
273 return true;
274 }
275
276 public String getJoinJid() {
277 return this.conversation.getContactJid().split("/")[0]+"/"+this.joinnick;
278 }
279}