Mercurial > prosody-modules
comparison mod_muc_webchat_url/mod_muc_webchat_url.lua @ 3672:b8bcea17ccd6
mod_muc_webchat_url: Advertise the URL to a webchat in disco#info
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Tue, 17 Sep 2019 17:12:59 +0200 |
| parents | mod_muc_lang/mod_muc_lang.lua@ef5d52ca31bb |
| children | 11ebf1da416b |
comparison
equal
deleted
inserted
replaced
| 3671:73c456d9ec67 | 3672:b8bcea17ccd6 |
|---|---|
| 1 local jid_split = require "util.jid".split; | |
| 2 module:depends"muc"; | |
| 3 | |
| 4 local webchat_baseurl = module:get_option_string("muc_webchat_baseurl", nil); | |
| 5 | |
| 6 local function get_webchat_url(room) | |
| 7 local webchat_url = room._data.webchat_url; | |
| 8 if webchat_url then -- explicitly configured | |
| 9 return webchat_url; | |
| 10 end | |
| 11 if not webchat_baseurl then | |
| 12 -- no template | |
| 13 return nil; | |
| 14 end | |
| 15 if room:get_hidden() or room:get_members_only() or room:get_password() then | |
| 16 -- not a public room | |
| 17 return nil; | |
| 18 end | |
| 19 return (webchat_baseurl:gsub("{(%w+)}", { | |
| 20 jid = room.jid, | |
| 21 node = select(1, jid_split(room.jid)), | |
| 22 host = select(2, jid_split(room.jid)), | |
| 23 })); | |
| 24 end | |
| 25 | |
| 26 module:hook("muc-config-form", function(event) | |
| 27 local room, form = event.room, event.form; | |
| 28 table.insert(form, { | |
| 29 name = "muc#roomconfig_webchat_url", | |
| 30 type = "text-single", | |
| 31 label = "URL where this room can be joined", | |
| 32 value = get_webchat_url(room), | |
| 33 }); | |
| 34 end); | |
| 35 | |
| 36 module:hook("muc-config-submitted", function(event) | |
| 37 local room, fields, changed = event.room, event.fields, event.changed; | |
| 38 local new = fields["muc#roomconfig_webchat_url"]; | |
| 39 if new ~= room._data.webchat_url then | |
| 40 room._data.webchat_url = new; | |
| 41 if type(changed) == "table" then | |
| 42 changed["muc#roomconfig_webchat_url"] = true; | |
| 43 else | |
| 44 event.changed = true; | |
| 45 end | |
| 46 end | |
| 47 end); | |
| 48 | |
| 49 module:hook("muc-disco#info", function (event) | |
| 50 local room, form, formdata = event.room, event.form, event.formdata; | |
| 51 | |
| 52 local webchat_url = get_webchat_url(room); | |
| 53 if not webchat_url or webchat_url == "" then | |
| 54 return; | |
| 55 end | |
| 56 table.insert(form, { | |
| 57 name = "muc#roominfo_webchat_url", | |
| 58 }); | |
| 59 formdata["muc#roominfo_webchat_url"] = webchat_url; | |
| 60 end); | |
| 61 |
