You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mod_muc_domain_mapper.lua 3.3KB

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