Mercurial > prosody-modules
comparison mod_muc_require_tos/mod_muc_require_tos.lua @ 4690:82dabfffaddf
mod_muc_require_tos: Add this new module
| author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
|---|---|
| date | Thu, 16 Sep 2021 20:41:14 +0200 |
| parents | |
| children | 1f1dbf652b37 |
comparison
equal
deleted
inserted
replaced
| 4689:ecfffbbcbf42 | 4690:82dabfffaddf |
|---|---|
| 1 local jid = require "util.jid"; | |
| 2 local st = require "util.stanza"; | |
| 3 | |
| 4 local quick_response_ns = "urn:xmpp:tmp:quick-response"; | |
| 5 local welcome_message = module:get_option_string("tos_welcome_message"); | |
| 6 local yes_message = module:get_option_string("tos_yes_message"); | |
| 7 local no_message = module:get_option_string("tos_no_message"); | |
| 8 | |
| 9 module:hook("muc-occupant-joined", function(event) | |
| 10 local origin = event.origin; | |
| 11 local room = event.room; | |
| 12 local occupant = event.occupant; | |
| 13 local nick = occupant.nick; | |
| 14 module:log("debug", "%s joined %s (%s)", nick, room, origin); | |
| 15 if occupant.role == "visitor" then | |
| 16 local message = st.message({ | |
| 17 type = "groupchat", | |
| 18 to = occupant.nick, | |
| 19 from = room.jid, | |
| 20 id = "foo", | |
| 21 ["xml:lang"] = "en", | |
| 22 }, welcome_message) | |
| 23 :tag("response", { xmlns = quick_response_ns, value = "yes", label = "I accept." }):up() | |
| 24 :tag("response", { xmlns = quick_response_ns, value = "no", label = "I decline." }):up(); | |
| 25 origin.send(message); | |
| 26 end | |
| 27 end); | |
| 28 | |
| 29 module:hook("muc-occupant-groupchat", function(event) | |
| 30 local occupant = event.occupant; | |
| 31 if occupant.role ~= "visitor" then | |
| 32 return; | |
| 33 end | |
| 34 local origin = event.origin; | |
| 35 local room = event.room; | |
| 36 local stanza = event.stanza; | |
| 37 -- Namespace must be nil instead of "jabber:client" here. | |
| 38 local body = stanza:get_child_text("body", nil); | |
| 39 module:log("debug", "%s replied %s", occupant.nick, body); | |
| 40 if body == "yes" then | |
| 41 room:set_affiliation(true, occupant.bare_jid, "member", "Agreed to the TOS."); | |
| 42 origin.send(st.reply(stanza):body(yes_message, { ["xml:lang"] = "en" })); | |
| 43 elseif body == "no" then | |
| 44 origin.send(st.reply(stanza):body(no_message, { ["xml:lang"] = "en" })); | |
| 45 room:set_role(true, occupant.nick, "none", "Declined the TOS."); | |
| 46 end | |
| 47 end, 51); -- Priority must be > 50, <forbidden/> is sent at this priority. |
