Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

mod_muc_domain_mapper.lua 3.3KB

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