Debian Codegraph @

xmpp-vala/src/module/xep/0045_muc/module.vala

raw
1 using Gee;
2
3 namespace Xmpp.Xep.Muc {
4
5 private const string NS_URI = "http://jabber.org/protocol/muc";
6 private const string NS_URI_ADMIN = NS_URI + "#admin";
7 private const string NS_URI_OWNER = NS_URI + "#owner";
8 private const string NS_URI_USER = NS_URI + "#user";
9 private const string NS_URI_REQUEST = NS_URI + "#request";
10
11 public enum MucEnterError {
12 PASSWORD_REQUIRED,
13 BANNED,
14 ROOM_DOESNT_EXIST,
15 CREATION_RESTRICTED,
16 USE_RESERVED_ROOMNICK,
17 NOT_IN_MEMBER_LIST,
18 NICK_CONFLICT,
19 OCCUPANT_LIMIT_REACHED,
20 }
21
22 public enum Affiliation {
23 NONE,
24 ADMIN,
25 MEMBER,
26 OUTCAST,
27 OWNER
28 }
29
30 public enum Role {
31 NONE,
32 MODERATOR,
33 PARTICIPANT,
34 VISITOR
35 }
36
37 public enum Feature {
38 REGISTER,
39 ROOMCONFIG,
40 ROOMINFO,
41 HIDDEN,
42 MEMBERS_ONLY,
43 MODERATED,
44 NON_ANONYMOUS,
45 OPEN,
46 PASSWORD_PROTECTED,
47 PERSISTENT,
48 PUBLIC,
49 ROOMS,
50 SEMI_ANONYMOUS,
51 STABLE_ID,
52 TEMPORARY,
53 UNMODERATED,
54 UNSECURED
55 }
56
57 public static void add_muc_pm_message_stanza_x_node(MessageStanza message_stanza) {
58 StanzaNode x_node = new StanzaNode.build("x", "http://jabber.org/protocol/muc#user").add_self_xmlns();
59 message_stanza.stanza.put_node(x_node);
60 }
61
62 public class JoinResult {
63 public MucEnterError? muc_error;
64 public string? stanza_error;
65 public string? nick;
66 public bool newly_created = false;
67 }
68
69 public class Module : XmppStreamModule {
70 public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0045_muc_module");
71
72 public signal void received_occupant_affiliation(XmppStream stream, Jid jid, Affiliation? affiliation);
73 public signal void received_occupant_jid(XmppStream stream, Jid jid, Jid? real_jid);
74 public signal void received_occupant_role(XmppStream stream, Jid jid, Role? role);
75 public signal void subject_set(XmppStream stream, string? subject, Jid jid);
76 public signal void invite_received(XmppStream stream, Jid room_jid, Jid from_jid, string? password, string? reason);
77 public signal void voice_request_received(XmppStream stream, Jid room_jid, Jid from_jid, string nick);
78 public signal void room_info_updated(XmppStream stream, Jid muc_jid);
79
80 public signal void self_removed_from_room(XmppStream stream, Jid jid, StatusCode code);
81 public signal void removed_from_room(XmppStream stream, Jid jid, StatusCode? code);
82
83 private ReceivedPipelineListener received_pipeline_listener;
84
85 public Module() {
86 received_pipeline_listener = new ReceivedPipelineListener(this);
87 }
88
89 public async JoinResult? enter(XmppStream stream, Jid bare_jid, string nick, string? password, DateTime? history_since, bool receive_history, StanzaNode? additional_node) {
90 try {
91 Presence.Stanza presence = new Presence.Stanza();
92 presence.to = bare_jid.with_resource(nick);
93
94 StanzaNode x_node = new StanzaNode.build("x", NS_URI).add_self_xmlns();
95 if (password != null) {
96 x_node.put_node(new StanzaNode.build("password", NS_URI).put_node(new StanzaNode.text(password)));
97 }
98 if (history_since != null || !receive_history) {
99 StanzaNode history_node = new StanzaNode.build("history", NS_URI);
100 x_node.put_node(history_node);
101
102 if (history_since != null) {
103 history_node.set_attribute("since", DateTimeProfiles.to_datetime(history_since));
104 } else if (!receive_history) {
105 history_node.set_attribute("maxchars", "0");
106 }
107 }
108 presence.stanza.put_node(x_node);
109
110 if (additional_node != null) {
111 presence.stanza.put_node(additional_node);
112 }
113
114 stream.get_flag(Flag.IDENTITY).start_muc_enter(bare_jid, presence.id);
115
116 query_room_info.begin(stream, bare_jid);
117 stream.get_module(Presence.Module.IDENTITY).send_presence(stream, presence);
118
119 var promise = new Promise<JoinResult?>();
120 stream.get_flag(Flag.IDENTITY).enter_futures[bare_jid] = promise;
121 try {
122 JoinResult? enter_result = yield promise.future.wait_async();
123 stream.get_flag(Flag.IDENTITY).enter_futures.unset(bare_jid);
124 return enter_result;
125 } catch (Gee.FutureError e) {
126 return null;
127 }
128 } catch (InvalidJidError e) {
129 return new JoinResult() { muc_error = MucEnterError.NICK_CONFLICT };
130 }
131 }
132
133 public void exit(XmppStream stream, Jid jid) {
134 try {
135 string? nick = stream.get_flag(Flag.IDENTITY).get_muc_nick(jid);
136 if (nick == null) return;
137 Presence.Stanza presence = new Presence.Stanza();
138 presence.to = jid.with_resource(nick);
139 presence.type_ = Presence.Stanza.TYPE_UNAVAILABLE;
140 stream.get_module(Presence.Module.IDENTITY).send_presence(stream, presence);
141 } catch (InvalidJidError e) {
142 warning("Tried to leave room with invalid nick: %s", e.message);
143 }
144 }
145
146 public void change_subject(XmppStream stream, Jid jid, string subject) {
147 MessageStanza message = new MessageStanza();
148 message.to = jid;
149 message.type_ = MessageStanza.TYPE_GROUPCHAT;
150 message.stanza.put_node((new StanzaNode.build("subject")).put_node(new StanzaNode.text(subject)));
151 stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, message);
152 }
153
154 public void change_nick(XmppStream stream, Jid jid, string new_nick) {
155 // TODO: Return if successful
156 try {
157 Presence.Stanza presence = new Presence.Stanza();
158 presence.to = jid.with_resource(new_nick);
159 stream.get_module(Presence.Module.IDENTITY).send_presence(stream, presence);
160 } catch (InvalidJidError e) {
161 warning("Tried to change nick to invalid nick: %s", e.message);
162 }
163 }
164
165 public void invite(XmppStream stream, Jid to_muc, Jid jid) {
166 MessageStanza message = new MessageStanza();
167 message.to = to_muc;
168 StanzaNode invite_node = new StanzaNode.build("x", NS_URI_USER).add_self_xmlns()
169 .put_node(new StanzaNode.build("invite", NS_URI_USER).put_attribute("to", jid.to_string()));
170 message.stanza.put_node(invite_node);
171 stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, message);
172 }
173
174 public void request_voice(XmppStream stream, Jid to_muc) {
175 MessageStanza message = new MessageStanza() { to=to_muc };
176
177 DataForms.DataForm submit_node = new DataForms.DataForm();
178 submit_node.get_submit_node();
179
180 DataForms.DataForm.Field field_node = new DataForms.DataForm.Field() { var="FORM_TYPE" };
181 field_node.set_value_string(NS_URI_REQUEST);
182
183 DataForms.DataForm.ListSingleField single_field = new DataForms.DataForm.ListSingleField(new StanzaNode.build("field", DataForms.NS_URI)) { var="muc#role", label="Requested role", value="participant" };
184
185 submit_node.add_field(field_node);
186 submit_node.add_field(single_field);
187
188 message.stanza.put_node(submit_node.stanza_node);
189
190 stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, message);
191 }
192
193 public void kick(XmppStream stream, Jid jid, string nick) {
194 change_role(stream, jid, nick, "none");
195 }
196
197 /* XEP 0046: "A user cannot be kicked by a moderator with a lower affiliation." (XEP 0045 8.2) */
198 public bool kick_possible(XmppStream stream, Jid occupant) {
199 try {
200 Jid muc_jid = occupant.bare_jid;
201 Flag flag = stream.get_flag(Flag.IDENTITY);
202 string own_nick = flag.get_muc_nick(muc_jid);
203 Affiliation my_affiliation = flag.get_affiliation(muc_jid, muc_jid.with_resource(own_nick));
204 Affiliation other_affiliation = flag.get_affiliation(muc_jid, occupant);
205 switch (my_affiliation) {
206 case Affiliation.MEMBER:
207 if (other_affiliation == Affiliation.ADMIN || other_affiliation == Affiliation.OWNER) return false;
208 break;
209 case Affiliation.ADMIN:
210 if (other_affiliation == Affiliation.OWNER) return false;
211 break;
212 case Affiliation.OWNER:
213 return true;
214 default:
215 return false;
216 }
217 return true;
218 } catch (InvalidJidError e) {
219 warning("Tried to kick with invalid nick: %s", e.message);
220 return false;
221 }
222 }
223
224 public void change_role(XmppStream stream, Jid jid, string nick, string new_role) {
225 StanzaNode query = new StanzaNode.build("query", NS_URI_ADMIN).add_self_xmlns();
226 query.put_node(new StanzaNode.build("item", NS_URI_ADMIN).put_attribute("nick", nick, NS_URI_ADMIN).put_attribute("role", new_role, NS_URI_ADMIN));
227 Iq.Stanza iq = new Iq.Stanza.set(query) { to=jid };
228 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
229 }
230
231 public async void change_affiliation(XmppStream stream, Jid muc_jid, Jid? user_jid, string? nick, string new_affiliation) {
232 StanzaNode item_node = new StanzaNode.build("item", NS_URI_ADMIN)
233 .put_attribute("affiliation", new_affiliation, NS_URI_ADMIN);
234 if (user_jid != null) {
235 // Some servers don't allow full JIDs and reply error:modify - jid-malformed - "Bare JID expected, got full JID". Make them bare JIDs.
236 item_node.put_attribute("jid", user_jid.bare_jid.to_string(), NS_URI_ADMIN);
237 }
238 if (nick != null) item_node.put_attribute("nick", nick, NS_URI_ADMIN);
239
240 StanzaNode query = new StanzaNode.build("query", NS_URI_ADMIN).add_self_xmlns().put_node(item_node);
241 Iq.Stanza iq = new Iq.Stanza.set(query) { to=muc_jid };
242 yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
243 }
244
245 public async DataForms.DataForm? get_config_form(XmppStream stream, Jid jid) {
246 Iq.Stanza get_iq = new Iq.Stanza.get(new StanzaNode.build("query", NS_URI_OWNER).add_self_xmlns()) { to=jid };
247 Iq.Stanza result_iq = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, get_iq);
248
249 StanzaNode? x_node = result_iq.stanza.get_deep_subnode(NS_URI_OWNER + ":query", DataForms.NS_URI + ":x");
250 if (x_node != null) {
251 DataForms.DataForm data_form = DataForms.DataForm.create_from_node(x_node);
252 return data_form;
253 }
254 return null;
255 }
256
257 public async void set_config_form(XmppStream stream, Jid jid, DataForms.DataForm data_form) {
258 StanzaNode stanza_node = new StanzaNode.build("query", NS_URI_OWNER);
259 stanza_node.add_self_xmlns().put_node(data_form.get_submit_node());
260 Iq.Stanza set_iq = new Iq.Stanza.set(stanza_node) { to=jid };
261 yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, set_iq);
262 }
263
264 public override void attach(XmppStream stream) {
265 stream.add_flag(new Flag());
266 stream.get_module(MessageModule.IDENTITY).received_message.connect(on_received_message);
267 stream.get_module(MessageModule.IDENTITY).received_pipeline.connect(received_pipeline_listener);
268 stream.get_module(Presence.Module.IDENTITY).received_available.connect(on_received_available);
269 stream.get_module(Presence.Module.IDENTITY).received_presence.connect(check_for_enter_error);
270 stream.get_module(Presence.Module.IDENTITY).received_unavailable.connect(on_received_unavailable);
271 stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI);
272 }
273
274 public override void detach(XmppStream stream) {
275 stream.get_module(MessageModule.IDENTITY).received_message.disconnect(on_received_message);
276 stream.get_module(MessageModule.IDENTITY).received_pipeline.disconnect(received_pipeline_listener);
277 stream.get_module(Presence.Module.IDENTITY).received_available.disconnect(on_received_available);
278 stream.get_module(Presence.Module.IDENTITY).received_presence.disconnect(check_for_enter_error);
279 stream.get_module(Presence.Module.IDENTITY).received_unavailable.disconnect(on_received_unavailable);
280 stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI);
281 }
282
283 public override string get_ns() { return NS_URI; }
284 public override string get_id() { return IDENTITY.id; }
285
286 private void on_received_message(XmppStream stream, MessageStanza message) {
287 if (message.type_ == MessageStanza.TYPE_GROUPCHAT) {
288 StanzaNode? subject_node = message.stanza.get_subnode("subject");
289 if (subject_node != null && message.body == null){
290 string subject = subject_node.get_string_content();
291 stream.get_flag(Flag.IDENTITY).set_muc_subject(message.from, subject);
292 subject_set(stream, subject, message.from);
293 }
294
295 StanzaNode? x_node = message.stanza.get_subnode("x", NS_URI_USER);
296 if (x_node != null) {
297 Gee.List<int> status_codes = get_status_codes(x_node);
298 if (!status_codes.is_empty) {
299 if (status_codes.contains(StatusCode.CONFIG_CHANGE_NON_PRIVACY) ||
300 status_codes.contains(StatusCode.NON_ANONYMOUS) ||
301 status_codes.contains(StatusCode.SEMI_ANONYMOUS)) {
302 query_room_info.begin(stream, message.from.bare_jid);
303 }
304 }
305 }
306 }
307 }
308
309 private void check_for_enter_error(XmppStream stream, Presence.Stanza presence) {
310 Flag flag = stream.get_flag(Flag.IDENTITY);
311 if (presence.is_error() && flag.is_muc_enter_outstanding() && flag.is_occupant(presence.from)) {
312 Jid bare_jid = presence.from.bare_jid;
313 ErrorStanza? error_stanza = presence.get_error();
314 if (flag.get_enter_id(bare_jid) == presence.id) {
315 MucEnterError? error = null;
316 switch (error_stanza.condition) {
317 case ErrorStanza.CONDITION_NOT_AUTHORIZED:
318 if (ErrorStanza.TYPE_AUTH == error_stanza.type_) error = MucEnterError.PASSWORD_REQUIRED;
319 break;
320 case ErrorStanza.CONDITION_REGISTRATION_REQUIRED:
321 if (ErrorStanza.TYPE_AUTH == error_stanza.type_) error = MucEnterError.NOT_IN_MEMBER_LIST;
322 break;
323 case ErrorStanza.CONDITION_FORBIDDEN:
324 if (ErrorStanza.TYPE_AUTH == error_stanza.type_) error = MucEnterError.BANNED;
325 break;
326 case ErrorStanza.CONDITION_SERVICE_UNAVAILABLE:
327 if (ErrorStanza.TYPE_WAIT == error_stanza.type_) error = MucEnterError.OCCUPANT_LIMIT_REACHED;
328 break;
329 case ErrorStanza.CONDITION_ITEM_NOT_FOUND:
330 if (ErrorStanza.TYPE_CANCEL == error_stanza.type_) error = MucEnterError.ROOM_DOESNT_EXIST;
331 break;
332 case ErrorStanza.CONDITION_CONFLICT:
333 if (ErrorStanza.TYPE_CANCEL == error_stanza.type_) error = MucEnterError.NICK_CONFLICT;
334 break;
335 case ErrorStanza.CONDITION_NOT_ALLOWED:
336 if (ErrorStanza.TYPE_CANCEL == error_stanza.type_) error = MucEnterError.CREATION_RESTRICTED;
337 break;
338 case ErrorStanza.CONDITION_NOT_ACCEPTABLE:
339 if (ErrorStanza.TYPE_CANCEL == error_stanza.type_) error = MucEnterError.USE_RESERVED_ROOMNICK;
340 break;
341 }
342 if (error != null) {
343 flag.enter_futures[bare_jid].set_value(new JoinResult() {muc_error=error});
344 } else {
345 flag.enter_futures[bare_jid].set_value(new JoinResult() {stanza_error=error_stanza.condition});
346 }
347 flag.finish_muc_enter(bare_jid);
348 }
349 }
350 }
351
352 private void on_received_available(XmppStream stream, Presence.Stanza presence) {
353 Flag flag = stream.get_flag(Flag.IDENTITY);
354 if (flag.is_occupant(presence.from)) {
355 StanzaNode? x_node = presence.stanza.get_subnode("x", NS_URI_USER);
356 if (x_node != null) {
357 ArrayList<int> status_codes = get_status_codes(x_node);
358 if (status_codes.contains(StatusCode.SELF_PRESENCE)) {
359 Jid bare_jid = presence.from.bare_jid;
360 if (flag.get_enter_id(bare_jid) != null) {
361
362 // TODO only query that if we actually have the rights to
363 query_affiliation.begin(stream, bare_jid, "member");
364 query_affiliation.begin(stream, bare_jid, "admin");
365 query_affiliation.begin(stream, bare_jid, "owner");
366
367 flag.finish_muc_enter(bare_jid);
368 var join_result = new JoinResult() { nick=presence.from.resourcepart, newly_created=status_codes.contains(StatusCode.NEW_ROOM_CREATED) };
369 flag.enter_futures[bare_jid].set_value(join_result);
370 }
371
372 flag.set_muc_nick(presence.from);
373 }
374 string? affiliation_str = x_node.get_deep_attribute("item", "affiliation");
375 Affiliation? affiliation = null;
376 if (affiliation_str != null) {
377 affiliation = parse_affiliation(affiliation_str);
378 flag.set_affiliation(presence.from.bare_jid, presence.from, affiliation);
379 received_occupant_affiliation(stream, presence.from, affiliation);
380 }
381 string? jid_ = x_node.get_deep_attribute("item", "jid");
382 if (jid_ != null) {
383 try {
384 Jid jid = new Jid(jid_);
385 flag.set_real_jid(presence.from, jid);
386 if (affiliation != null) {
387 stream.get_flag(Flag.IDENTITY).set_offline_member(presence.from, jid, affiliation);
388 }
389 received_occupant_jid(stream, presence.from, jid);
390 } catch (InvalidJidError e) {
391 warning("Received invalid occupant jid: %s", e.message);
392 }
393 }
394 string? role_str = x_node.get_deep_attribute("item", "role");
395 if (role_str != null) {
396 Role role = parse_role(role_str);
397 flag.set_occupant_role(presence.from, role);
398 received_occupant_role(stream, presence.from, role);
399 }
400 }
401 }
402 }
403
404 private void on_received_unavailable(XmppStream stream, Presence.Stanza presence) {
405 Flag flag = stream.get_flag(Flag.IDENTITY);
406 if (!flag.is_occupant(presence.from)) return;
407
408 StanzaNode? x_node = presence.stanza.get_subnode("x", NS_URI_USER);
409 if (x_node == null) return;
410
411 ArrayList<int> status_codes = get_status_codes(x_node);
412
413 if (StatusCode.SELF_PRESENCE in status_codes) {
414 flag.remove_occupant_info(presence.from);
415 }
416
417 foreach (StatusCode code in USER_REMOVED_CODES) {
418 if (code in status_codes) {
419 if (StatusCode.SELF_PRESENCE in status_codes) {
420 flag.left_muc(stream, presence.from.bare_jid);
421 self_removed_from_room(stream, presence.from, code);
422 Presence.Flag presence_flag = stream.get_flag(Presence.Flag.IDENTITY);
423 presence_flag.remove_presence(presence.from.bare_jid);
424 } else {
425 removed_from_room(stream, presence.from, code);
426 }
427 }
428 }
429 }
430
431 private async void query_room_info(XmppStream stream, Jid jid) {
432 ServiceDiscovery.InfoResult? info_result = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).request_info(stream, jid);
433 if (info_result == null) return;
434
435 Gee.List<Feature> features = new ArrayList<Feature>();
436
437 foreach (ServiceDiscovery.Identity identity in info_result.identities) {
438 if (identity.category == "conference" && identity.name != null) {
439 stream.get_flag(Flag.IDENTITY).set_room_name(jid, identity.name);
440 }
441 }
442
443 foreach (string feature in info_result.features) {
444 Feature? parsed = null;
445 switch (feature) {
446 case "http://jabber.org/protocol/muc#register": parsed = Feature.REGISTER; break;
447 case "http://jabber.org/protocol/muc#roomconfig": parsed = Feature.ROOMCONFIG; break;
448 case "http://jabber.org/protocol/muc#roominfo": parsed = Feature.ROOMINFO; break;
449 case "http://jabber.org/protocol/muc#stable_id": parsed = Feature.STABLE_ID; break;
450 case "muc_hidden": parsed = Feature.HIDDEN; break;
451 case "muc_membersonly": parsed = Feature.MEMBERS_ONLY; break;
452 case "muc_moderated": parsed = Feature.MODERATED; break;
453 case "muc_nonanonymous": parsed = Feature.NON_ANONYMOUS; break;
454 case "muc_open": parsed = Feature.OPEN; break;
455 case "muc_passwordprotected": parsed = Feature.PASSWORD_PROTECTED; break;
456 case "muc_persistent": parsed = Feature.PERSISTENT; break;
457 case "muc_public": parsed = Feature.PUBLIC; break;
458 case "muc_rooms": parsed = Feature.ROOMS; break;
459 case "muc_semianonymous": parsed = Feature.SEMI_ANONYMOUS; break;
460 case "muc_temporary": parsed = Feature.TEMPORARY; break;
461 case "muc_unmoderated": parsed = Feature.UNMODERATED; break;
462 case "muc_unsecured": parsed = Feature.UNSECURED; break;
463 }
464 if (parsed != null) features.add(parsed);
465 }
466 stream.get_flag(Flag.IDENTITY).set_room_features(jid, features);
467 room_info_updated(stream, jid);
468 }
469
470 private async Gee.List<Jid>? query_affiliation(XmppStream stream, Jid jid, string affiliation) {
471 Iq.Stanza iq = new Iq.Stanza.get(
472 new StanzaNode.build("query", NS_URI_ADMIN)
473 .add_self_xmlns()
474 .put_node(new StanzaNode.build("item", NS_URI_ADMIN)
475 .put_attribute("affiliation", affiliation))
476 ) { to=jid };
477
478 Iq.Stanza iq_result = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
479 if (iq_result.is_error()) return null;
480
481 StanzaNode? query_node = iq_result.stanza.get_subnode("query", NS_URI_ADMIN);
482 if (query_node == null) return null;
483
484 Gee.List<StanzaNode> item_nodes = query_node.get_subnodes("item", NS_URI_ADMIN);
485 Gee.List<Jid> ret_jids = new ArrayList<Jid>(Jid.equals_func);
486 foreach (StanzaNode item in item_nodes) {
487 string jid__ = item.get_attribute("jid");
488 string? affiliation_ = item.get_attribute("affiliation");
489 if (jid__ != null && affiliation_ != null) {
490 try {
491 Jid jid_ = new Jid(jid__);
492 stream.get_flag(Flag.IDENTITY).set_offline_member(iq_result.from, jid_, parse_affiliation(affiliation_));
493 ret_jids.add(jid_);
494 received_occupant_jid(stream, iq_result.from, jid_);
495 } catch (InvalidJidError e) {
496 warning("Received invalid occupant jid: %s", e.message);
497 }
498 }
499 }
500 return ret_jids;
501 }
502
503 private static ArrayList<int> get_status_codes(StanzaNode x_node) {
504 ArrayList<int> ret = new ArrayList<int>();
505 foreach (StanzaNode status_node in x_node.get_subnodes("status", NS_URI_USER)) {
506 ret.add(int.parse(status_node.get_attribute("code")));
507 }
508 return ret;
509 }
510
511 private static Affiliation parse_affiliation(string affiliation_str) {
512 Affiliation affiliation;
513 switch (affiliation_str) {
514 case "admin":
515 affiliation = Affiliation.ADMIN; break;
516 case "member":
517 affiliation = Affiliation.MEMBER; break;
518 case "outcast":
519 affiliation = Affiliation.OUTCAST; break;
520 case "owner":
521 affiliation = Affiliation.OWNER; break;
522 default:
523 affiliation = Affiliation.NONE; break;
524 }
525 return affiliation;
526 }
527
528 private static Role parse_role(string role_str) {
529 Role role;
530 switch (role_str) {
531 case "moderator":
532 role = Role.MODERATOR; break;
533 case "participant":
534 role = Role.PARTICIPANT; break;
535 case "visitor":
536 role = Role.VISITOR; break;
537 default:
538 role = Role.NONE; break;
539 }
540 return role;
541 }
542 }
543
544 public class ReceivedPipelineListener : StanzaListener<MessageStanza> {
545
546 private string[] after_actions_const = {"EXTRACT_MESSAGE_2"};
547
548 public override string action_group { get { return ""; } }
549 public override string[] after_actions { get { return after_actions_const; } }
550
551 Module outer;
552
553 public ReceivedPipelineListener(Module outer) {
554 this.outer = outer;
555 }
556
557 public override async bool run(XmppStream stream, MessageStanza message) {
558 if (message.type_ == MessageStanza.TYPE_NORMAL) {
559 StanzaNode? x_node = message.stanza.get_subnode("x", NS_URI_USER);
560 if (x_node != null) {
561 StanzaNode? invite_node = x_node.get_subnode("invite", NS_URI_USER);
562 string? password = null;
563 StanzaNode? password_node = x_node.get_subnode("password", NS_URI_USER);
564 if (password_node != null) password = password_node.get_string_content();
565 if (invite_node != null) {
566 Jid? from_jid = null;
567 try {
568 string from = invite_node.get_attribute("from");
569 if (from != null) from_jid = new Jid(from);
570 } catch (InvalidJidError e) {
571 warning("Received invite from invalid jid: %s", e.message);
572 }
573 if (from_jid != null) {
574 StanzaNode? reason_node = invite_node.get_subnode("reason", NS_URI_USER);
575 string? reason = null;
576 if (reason_node != null) reason = reason_node.get_string_content();
577 bool is_mam_message = Xmpp.MessageArchiveManagement.MessageFlag.get_flag(message) != null; // TODO
578 if (!is_mam_message) outer.invite_received(stream, message.from, from_jid, password, reason);
579 return true;
580 }
581 }
582 }
583
584 StanzaNode? x_field_node = message.stanza.get_subnode("x", DataForms.NS_URI);
585 if (x_field_node != null){
586 Gee.List<StanzaNode>? fields = x_field_node.get_subnodes("field", DataForms.NS_URI);
587 Jid? from_jid = null;
588 string? nick = null;
589
590 if (fields.size!=0){
591 foreach (var field_node in fields){
592 string? var_ = field_node.get_attribute("var");
593 if (var_ == "muc#jid"){
594 StanzaNode? value_node = field_node.get_subnode("value", DataForms.NS_URI);
595 try {
596 if (value_node != null) from_jid = new Jid(value_node.get_string_content());
597 } catch (InvalidJidError e) {
598 return false;
599 }
600 }
601 else if (var_ == "muc#roomnick"){
602 StanzaNode? value_node = field_node.get_subnode("value", DataForms.NS_URI);
603 if (value_node != null) nick = value_node.get_string_content();
604 }
605 else if (var_ == "muc#role"){
606 StanzaNode? value_node = field_node.get_subnode("value", DataForms.NS_URI);
607 if (value_node != null) {
608 if (value_node.get_string_content() != "participant") {
609 warning("Voice request with role other than participant");
610 }
611 }
612 }
613 }
614 if (from_jid == null || nick == null) {
615 warning("Voice request without from_jid or nick");
616 return false;
617 }
618
619 outer.voice_request_received(stream, message.from, from_jid, nick);
620 return true;
621 }
622 }
623 }
624 return false;
625 }
626 }
627
628 }