Annotate

mod_muc_require_tos/mod_muc_require_tos.lua @ 5283:cc89c97befe7

mod_http_admin_api: Tweak token session to please module:may() module:may() checks for type == "c2s", but mod_tokenauth does not currently include that or most common session properties. Fixes a traceback resulting from a different code path where module:may() tries to index event.stanza, which does not exist for http events.
author Kim Alvefur <zash@zash.se>
date Tue, 28 Mar 2023 20:45:11 +0200
parent 4693:59f53cf66573
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4690
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 local jid = require "util.jid";
4693
59f53cf66573 mod_muc_require_tos: Generate a message id instead of hardcoding "foo"
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4692
diff changeset
2 local id = require "util.id";
4690
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 local st = require "util.stanza";
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 local quick_response_ns = "urn:xmpp:tmp:quick-response";
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 local welcome_message = module:get_option_string("tos_welcome_message");
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 local yes_message = module:get_option_string("tos_yes_message");
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 local no_message = module:get_option_string("tos_no_message");
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9
4691
1f1dbf652b37 mod_muc_require_tos: Send the message after the subject, once the client is fully joined
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4690
diff changeset
10 module:hook("muc-occupant-session-new", function(event)
4690
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 local origin = event.origin;
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 local room = event.room;
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 local occupant = event.occupant;
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 local nick = occupant.nick;
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 module:log("debug", "%s joined %s (%s)", nick, room, origin);
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 if occupant.role == "visitor" then
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 local message = st.message({
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 type = "groupchat",
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 to = occupant.nick,
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 from = room.jid,
4693
59f53cf66573 mod_muc_require_tos: Generate a message id instead of hardcoding "foo"
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4692
diff changeset
21 id = id.medium(),
4690
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 ["xml:lang"] = "en",
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 }, welcome_message)
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 :tag("response", { xmlns = quick_response_ns, value = "yes", label = "I accept." }):up()
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 :tag("response", { xmlns = quick_response_ns, value = "no", label = "I decline." }):up();
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 origin.send(message);
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 end
4691
1f1dbf652b37 mod_muc_require_tos: Send the message after the subject, once the client is fully joined
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4690
diff changeset
28 end, 19);
4690
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 module:hook("muc-occupant-groupchat", function(event)
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 local occupant = event.occupant;
4692
37232302dfb9 mod_muc_require_tos: Prevent a traceback when receiving a message with occupant being nil
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4691
diff changeset
32 if occupant == nil or occupant.role ~= "visitor" then
4690
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 return;
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 end
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 local origin = event.origin;
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 local room = event.room;
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 local stanza = event.stanza;
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 -- Namespace must be nil instead of "jabber:client" here.
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 local body = stanza:get_child_text("body", nil);
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 module:log("debug", "%s replied %s", occupant.nick, body);
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 if body == "yes" then
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 room:set_affiliation(true, occupant.bare_jid, "member", "Agreed to the TOS.");
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 origin.send(st.reply(stanza):body(yes_message, { ["xml:lang"] = "en" }));
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 elseif body == "no" then
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 origin.send(st.reply(stanza):body(no_message, { ["xml:lang"] = "en" }));
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 room:set_role(true, occupant.nick, "none", "Declined the TOS.");
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 end
82dabfffaddf mod_muc_require_tos: Add this new module
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 end, 51); -- Priority must be > 50, <forbidden/> is sent at this priority.