aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/entities/MucOptions.java
blob: 3bce5111d6c7079e49c545876a1801b899572a39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package eu.siacs.conversations.entities;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import eu.siacs.conversations.crypto.PgpEngine;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
import android.annotation.SuppressLint;

@SuppressLint("DefaultLocale")
public class MucOptions {
	public static final int ERROR_NO_ERROR = 0;
	public static final int ERROR_NICK_IN_USE = 1;
	public static final int ERROR_ROOM_NOT_FOUND = 2;
	
	public interface OnRenameListener {
		public void onRename(boolean success);
	}
	
	public class User {
		public static final int ROLE_MODERATOR = 3;
		public static final int ROLE_NONE = 0;
		public static final int ROLE_PARTICIPANT = 2;
		public static final int ROLE_VISITOR = 1;
		public static final int AFFILIATION_ADMIN = 4;
		public static final int AFFILIATION_OWNER = 3;
		public static final int AFFILIATION_MEMBER = 2;
		public static final int AFFILIATION_OUTCAST = 1;
		public static final int AFFILIATION_NONE = 0;
		
		private int role;
		private int affiliation;
		private String name;
		private long pgpKeyId = 0;
		
		public String getName() {
			return name;
		}
		public void setName(String user) {
			this.name = user;
		}
		
		public int getRole() {
			return this.role;
		}
		public void setRole(String role) {
			role = role.toLowerCase();
			if (role.equals("moderator")) {
				this.role = ROLE_MODERATOR;
			} else if (role.equals("participant")) {
				this.role = ROLE_PARTICIPANT;
			} else if (role.equals("visitor")) {
				this.role = ROLE_VISITOR;
			} else {
				this.role = ROLE_NONE;
			}
		}
		public int getAffiliation() {
			return this.affiliation;
		}
		public void setAffiliation(String affiliation) {
			if (affiliation.equalsIgnoreCase("admin")) {
				this.affiliation = AFFILIATION_ADMIN;
			} else if (affiliation.equalsIgnoreCase("owner")) {
				this.affiliation = AFFILIATION_OWNER;
			} else if (affiliation.equalsIgnoreCase("member")) {
				this.affiliation = AFFILIATION_MEMBER;
			} else if (affiliation.equalsIgnoreCase("outcast")) {
				this.affiliation = AFFILIATION_OUTCAST;
			} else {
				this.affiliation = AFFILIATION_NONE;
			}
		}
		public void setPgpKeyId(long id) {
			this.pgpKeyId = id;
		}
		
		public long getPgpKeyId() {
			return this.pgpKeyId;
		}
	}
	private Account account;
	private List<User> users = new CopyOnWriteArrayList<User>();
	private Conversation conversation;
	private boolean isOnline = false;
	private int error = ERROR_ROOM_NOT_FOUND;
	private OnRenameListener renameListener = null;
	private boolean aboutToRename = false;
	private User self = new User();
	private String subject = null;
	private String joinnick;

	public MucOptions(Account account) {
		this.account = account;
	}
	
	public void deleteUser(String name) {
		for(int i = 0; i < users.size(); ++i) {
			if (users.get(i).getName().equals(name)) {
				users.remove(i);
				return;
			}
		}
	}
	
	public void addUser(User user) {
		for(int i = 0; i < users.size(); ++i) {
			if (users.get(i).getName().equals(user.getName())) {
				users.set(i, user);
				return;
			}
		}
		users.add(user);
		}
	
	public void processPacket(PresencePacket packet, PgpEngine pgp) {
		String[] fromParts = packet.getFrom().split("/");
		if (fromParts.length>=2) {
			String name = fromParts[1];
			String type = packet.getAttribute("type");
			if (type==null) {
				User user = new User();
				Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
				user.setName(name);
				user.setAffiliation(item.getAttribute("affiliation"));
				user.setRole(item.getAttribute("role"));
				user.setName(name);
				if (name.equals(this.joinnick)) {
					this.isOnline = true;
					this.error = ERROR_NO_ERROR;
					self = user;
					if (aboutToRename) {
						if (renameListener!=null) {
							renameListener.onRename(true);
						}
						aboutToRename = false;
					}
				} else {
					addUser(user);
				}
				if (pgp != null) {
					Element x = packet.findChild("x",
							"jabber:x:signed");
					if (x != null) {
						Element status = packet.findChild("status");
						String msg;
						if (status != null) {
							msg = status.getContent();
						} else {
							msg = "";
						}
						user.setPgpKeyId(pgp.fetchKeyId(account,msg, x.getContent()));
					}
				}
			} else if (type.equals("unavailable")) {
				deleteUser(packet.getAttribute("from").split("/")[1]);
			} else if (type.equals("error")) {
				Element error = packet.findChild("error");
				if (error.hasChild("conflict")) {
					if (aboutToRename) {
						if (renameListener!=null) {
							renameListener.onRename(false);
						}
						aboutToRename = false;
						this.setJoinNick(getActualNick());
					} else {
						this.error  = ERROR_NICK_IN_USE;
					}
				}
			}
		}
	}
	
	public List<User> getUsers() {
		return this.users;
	}
	
	public String getProposedNick() {
		String[] mucParts = conversation.getContactJid().split("/");
		if (conversation.getBookmark() != null && conversation.getBookmark().getNick() != null) {
			return conversation.getBookmark().getNick();
		} else {
			if (mucParts.length == 2) {
				return mucParts[1];
			} else {
				return account.getUsername();
			}
		}
	}
	
	public String getActualNick() {
		if (this.self.getName()!=null) {
			return this.self.getName();
		} else {
			return this.getProposedNick();
		}
	}
	
	public void setJoinNick(String nick) {
		this.joinnick = nick;
	}
	
	public void setConversation(Conversation conversation) {
		this.conversation = conversation;
	}
	
	public boolean online() {
		return this.isOnline;
	}
	
	public int getError() {
		return this.error;
	}

	public void setOnRenameListener(OnRenameListener listener) {
		this.renameListener = listener;
	}
	
	public OnRenameListener getOnRenameListener() {
		return this.renameListener;
	}

	public void setOffline() {
		this.users.clear();
		this.error = 0;
		this.isOnline = false;
	}

	public User getSelf() {
		return self;
	}

	public void setSubject(String content) {
		this.subject = content;
	}
	
	public String getSubject() {
		return this.subject;
	}

	public void flagAboutToRename() {
		this.aboutToRename = true;
	}
	
	public long[] getPgpKeyIds() {
		List<Long> ids = new ArrayList<Long>();
		for(User user : getUsers()) {
			if(user.getPgpKeyId()!=0) {
				ids.add(user.getPgpKeyId());
			}
		}
		long[] primitivLongArray = new long[ids.size()];
		for(int i = 0; i < ids.size(); ++i) {
			primitivLongArray[i] = ids.get(i);
		}
		return primitivLongArray;
	}
	
	public boolean pgpKeysInUse() {
		for(User user : getUsers()) {
			if (user.getPgpKeyId()!=0) {
				return true;
			}
		}
		return false;
	}
	
	public boolean everybodyHasKeys() {
		for(User user : getUsers()) {
			if (user.getPgpKeyId()==0) {
				return false;
			}
		}
		return true;
	}

	public String getJoinJid() {
		return this.conversation.getContactJid().split("/")[0]+"/"+this.joinnick;
	}
}