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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. module:set_global();
  4. -- It is recommended to set muc_mapper_domain_base to the main domain being served (example.com)
  5. local jid = require "util.jid";
  6. local filters = require "util.filters";
  7. local muc_domain_prefix = module:get_option_string("muc_mapper_domain_prefix", "conference");
  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_domain_base' option set, disabling muc_mapper plugin inactive");
  11. return
  12. end
  13. -- The "real" MUC domain that we are proxying to
  14. local muc_domain = module:get_option_string("muc_mapper_domain", muc_domain_prefix.."."..muc_domain_base);
  15. local escaped_muc_domain_base = muc_domain_base:gsub("%p", "%%%1");
  16. local escaped_muc_domain_prefix = muc_domain_prefix:gsub("%p", "%%%1");
  17. -- The pattern used to extract the target subdomain (e.g. extract 'foo' from 'foo.muc.example.com')
  18. local target_subdomain_pattern = "^"..escaped_muc_domain_prefix..".([^%.]+)%."..escaped_muc_domain_base;
  19. -- table to store all incoming iqs without roomname in it, like discoinfo to the muc compoent
  20. local roomless_iqs = {};
  21. if not muc_domain then
  22. module:log("warn", "No 'muc_mapper_domain' option set, disabling muc_mapper plugin inactive");
  23. return
  24. end
  25. -- Utility function to check and convert a room JID from virtual room1@muc.foo.example.com to real [foo]room1@muc.example.com
  26. local function match_rewrite_to_jid(room_jid, stanza)
  27. local node, host, resource = jid.split(room_jid);
  28. local target_subdomain = host and host:match(target_subdomain_pattern);
  29. if not target_subdomain then
  30. module:log("debug", "No need to rewrite out 'to' %s", room_jid);
  31. return room_jid;
  32. end
  33. -- Ok, rewrite room_jid address to new format
  34. local new_node, new_host, new_resource;
  35. if node then
  36. new_node, new_host, new_resource = "["..target_subdomain.."]"..node, muc_domain, resource;
  37. else
  38. module:log("debug", "No room name provided so rewriting only host 'to' %s", room_jid);
  39. new_host, new_resource = muc_domain, resource;
  40. if (stanza.attr and stanza.attr.id) then
  41. roomless_iqs[stanza.attr.id] = stanza.attr.to;
  42. end
  43. end
  44. room_jid = jid.join(new_node, new_host, new_resource);
  45. module:log("debug", "Rewrote to %s", room_jid);
  46. return room_jid
  47. end
  48. -- Utility function to check and convert a room JID from real [foo]room1@muc.example.com to virtual room1@muc.foo.example.com
  49. local function match_rewrite_from_jid(room_jid, stanza)
  50. local node, host, resource = jid.split(room_jid);
  51. if host ~= muc_domain or not node then
  52. module:log("debug", "No need to rewrite %s (not from the MUC host) %s, %s", room_jid, stanza.attr.id, roomless_iqs[stanza.attr.id]);
  53. if (stanza.attr and stanza.attr.id and roomless_iqs[stanza.attr.id]) then
  54. local result = roomless_iqs[stanza.attr.id];
  55. roomless_iqs[stanza.attr.id] = nil;
  56. return result;
  57. end
  58. return room_jid;
  59. end
  60. local target_subdomain, target_node = node:match("^%[([^%]]+)%](.+)$");
  61. if not (target_node and target_subdomain) then
  62. module:log("debug", "Not rewriting... unexpected node format: %s", node);
  63. return room_jid;
  64. end
  65. -- Ok, rewrite room_jid address to pretty format
  66. local new_node, new_host, new_resource = target_node, muc_domain_prefix..".".. target_subdomain.."."..muc_domain_base, resource;
  67. room_jid = jid.join(new_node, new_host, new_resource);
  68. module:log("debug", "Rewrote to %s", room_jid);
  69. return room_jid
  70. end
  71. -- We must filter stanzas in order to hook in to all incoming and outgoing messaging which skips the stanza routers
  72. function filter_stanza(stanza)
  73. if stanza.name == "message" or stanza.name == "iq" or stanza.name == "presence" then
  74. module:log("debug", "Filtering stanza type %s to %s from %s",stanza.name,stanza.attr.to,stanza.attr.from);
  75. if stanza.name == "iq" then
  76. local conf = stanza:get_child('conference')
  77. if conf then
  78. module:log("debug", "Filtering stanza conference %s to %s from %s",conf.attr.room,stanza.attr.to,stanza.attr.from);
  79. conf.attr.room = match_rewrite_to_jid(conf.attr.room, stanza)
  80. end
  81. end
  82. if stanza.attr.to then
  83. stanza.attr.to = match_rewrite_to_jid(stanza.attr.to, stanza)
  84. end
  85. if stanza.attr.from then
  86. stanza.attr.from = match_rewrite_from_jid(stanza.attr.from, stanza)
  87. end
  88. end
  89. return stanza;
  90. end
  91. function filter_session(session)
  92. module:log("warn", "Session filters applied");
  93. -- filters.add_filter(session, "stanzas/in", filter_stanza_in);
  94. filters.add_filter(session, "stanzas/out", filter_stanza);
  95. end
  96. function module.load()
  97. if module.reloading then
  98. module:log("debug", "Reloading MUC mapper!");
  99. else
  100. module:log("debug", "First load of MUC mapper!");
  101. end
  102. filters.add_filter_hook(filter_session);
  103. end
  104. function module.unload()
  105. filters.remove_filter_hook(filter_session);
  106. end
  107. local function outgoing_stanza_rewriter(event)
  108. local stanza = event.stanza;
  109. if stanza.attr.to then
  110. stanza.attr.to = match_rewrite_to_jid(stanza.attr.to, stanza)
  111. end
  112. end
  113. local function incoming_stanza_rewriter(event)
  114. local stanza = event.stanza;
  115. if stanza.attr.from then
  116. stanza.attr.from = match_rewrite_from_jid(stanza.attr.from, stanza)
  117. end
  118. end
  119. -- The stanza rewriters helper functions are attached for all stanza router hooks
  120. local function hook_all_stanzas(handler, host_module, event_prefix)
  121. for _, stanza_type in ipairs({ "message", "presence", "iq" }) do
  122. for _, jid_type in ipairs({ "host", "bare", "full" }) do
  123. host_module:hook((event_prefix or "")..stanza_type.."/"..jid_type, handler);
  124. end
  125. end
  126. end
  127. function module.add_host(host_module)
  128. module:log("info",
  129. "Loading mod_muc_domain_mapper for host %s!", host_module.host);
  130. hook_all_stanzas(incoming_stanza_rewriter, host_module);
  131. hook_all_stanzas(outgoing_stanza_rewriter, host_module, "pre-");
  132. end