"Fossies" - the Fresh Open Source Software Archive

Member "jitsi-meet-5079/resources/prosody-plugins/mod_muc_domain_mapper.lua" (17 Jun 2021, 3367 Bytes) of package /linux/misc/jitsi-meet-5079.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lua source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 -- Maps MUC JIDs like room1@muc.foo.example.com to JIDs like [foo]room1@muc.example.com
    2 -- Must be loaded on the client host in Prosody
    3 
    4 -- It is recommended to set muc_mapper_domain_base to the main domain being served (example.com)
    5 
    6 local jid = require "util.jid";
    7 
    8 local filters = require "util.filters";
    9 
   10 local muc_domain_base = module:get_option_string("muc_mapper_domain_base");
   11 if not muc_domain_base then
   12     module:log("warn", "No 'muc_mapper_domain_base' option set, disabling muc_mapper plugin inactive");
   13     return
   14 end
   15 
   16 local util = module:require "util";
   17 local room_jid_match_rewrite = util.room_jid_match_rewrite;
   18 local internal_room_jid_match_rewrite = util.internal_room_jid_match_rewrite;
   19 
   20 -- We must filter stanzas in order to hook in to all incoming and outgoing messaging which skips the stanza routers
   21 function filter_stanza(stanza)
   22     if stanza.name == "message" or stanza.name == "iq" or stanza.name == "presence" then
   23         -- module:log("debug", "Filtering stanza type %s  to %s from %s",stanza.name,stanza.attr.to,stanza.attr.from);
   24         if stanza.name == "iq" then
   25             local conf = stanza:get_child('conference')
   26             if conf then
   27                 -- module:log("debug", "Filtering stanza conference %s to %s from %s",conf.attr.room,stanza.attr.to,stanza.attr.from);
   28                 conf.attr.room = room_jid_match_rewrite(conf.attr.room, stanza)
   29             end
   30         end
   31         if stanza.attr.to then
   32             stanza.attr.to = room_jid_match_rewrite(stanza.attr.to, stanza)
   33         end
   34         if stanza.attr.from then
   35             stanza.attr.from = internal_room_jid_match_rewrite(stanza.attr.from, stanza)
   36         end
   37     end
   38     return stanza;
   39 end
   40 
   41 function filter_session(session)
   42     -- module:log("warn", "Session filters applied");
   43     filters.add_filter(session, "stanzas/out", filter_stanza);
   44 end
   45 
   46 function module.load()
   47     if module.reloading then
   48         module:log("debug", "Reloading MUC mapper!");
   49     else
   50         module:log("debug", "First load of MUC mapper!");
   51     end
   52     filters.add_filter_hook(filter_session);
   53 end
   54 
   55 function module.unload()
   56     filters.remove_filter_hook(filter_session);
   57 end
   58 
   59 
   60 local function outgoing_stanza_rewriter(event)
   61     local stanza = event.stanza;
   62     if stanza.attr.to then
   63         stanza.attr.to = room_jid_match_rewrite(stanza.attr.to, stanza)
   64     end
   65 end
   66 
   67 local function incoming_stanza_rewriter(event)
   68     local stanza = event.stanza;
   69     if stanza.attr.from then
   70         stanza.attr.from = internal_room_jid_match_rewrite(stanza.attr.from, stanza)
   71     end
   72 end
   73 
   74 -- The stanza rewriters helper functions are attached for all stanza router hooks
   75 local function hook_all_stanzas(handler, host_module, event_prefix)
   76     for _, stanza_type in ipairs({ "message", "presence", "iq" }) do
   77         for _, jid_type in ipairs({ "host", "bare", "full" }) do
   78             host_module:hook((event_prefix or "")..stanza_type.."/"..jid_type, handler);
   79         end
   80     end
   81 end
   82 
   83 function add_host(host)
   84     module:log("info", "Loading mod_muc_domain_mapper for host %s!", host);
   85     local host_module = module:context(host);
   86     hook_all_stanzas(incoming_stanza_rewriter, host_module);
   87     hook_all_stanzas(outgoing_stanza_rewriter, host_module, "pre-");
   88 end
   89 
   90 prosody.events.add_handler("host-activated", add_host);
   91 for host in pairs(prosody.hosts) do
   92     add_host(host);
   93 end