"Fossies" - the Fresh Open Source Software Archive

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