您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

util.lib.lua 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. local jid = require "util.jid";
  2. local runner, waiter = require "util.async".runner, require "util.async".waiter;
  3. local muc_domain_prefix
  4. = module:get_option_string("muc_mapper_domain_prefix", "conference");
  5. -- defaults to module.host, the module that uses the utility
  6. local muc_domain_base
  7. = module:get_option_string("muc_mapper_domain_base", module.host);
  8. -- The "real" MUC domain that we are proxying to
  9. local muc_domain = module:get_option_string(
  10. "muc_mapper_domain", muc_domain_prefix.."."..muc_domain_base);
  11. local escaped_muc_domain_base = muc_domain_base:gsub("%p", "%%%1");
  12. local escaped_muc_domain_prefix = muc_domain_prefix:gsub("%p", "%%%1");
  13. -- The pattern used to extract the target subdomain
  14. -- (e.g. extract 'foo' from 'foo.muc.example.com')
  15. local target_subdomain_pattern
  16. = "^"..escaped_muc_domain_prefix..".([^%.]+)%."..escaped_muc_domain_base;
  17. --- Utility function to check and convert a room JID from
  18. -- virtual room1@muc.foo.example.com to real [foo]room1@muc.example.com
  19. -- @param room_jid the room jid to match and rewrite if needed
  20. -- @return returns room jid [foo]room1@muc.example.com when it has subdomain
  21. -- otherwise room1@muc.example.com(the room_jid value untouched)
  22. local function room_jid_match_rewrite(room_jid)
  23. local node, host, resource = jid.split(room_jid);
  24. local target_subdomain = host and host:match(target_subdomain_pattern);
  25. if not target_subdomain then
  26. module:log("debug", "No need to rewrite out 'to' %s", room_jid);
  27. return room_jid;
  28. end
  29. -- Ok, rewrite room_jid address to new format
  30. local new_node, new_host, new_resource
  31. = "["..target_subdomain.."]"..node, muc_domain, resource;
  32. room_jid = jid.join(new_node, new_host, new_resource);
  33. module:log("debug", "Rewrote to %s", room_jid);
  34. return room_jid
  35. end
  36. --- Finds and returns room by its jid
  37. -- @param room_jid the room jid to search in the muc component
  38. -- @return returns room if found or nil
  39. function get_room_from_jid(room_jid)
  40. local _, host = jid.split(room_jid);
  41. local component = hosts[host];
  42. if component then
  43. local muc = component.modules.muc
  44. if muc and rawget(muc,"rooms") then
  45. -- We're running 0.9.x or 0.10 (old MUC API)
  46. return muc.rooms[room_jid];
  47. elseif muc and rawget(muc,"get_room_from_jid") then
  48. -- We're running >0.10 (new MUC API)
  49. return muc.get_room_from_jid(room_jid);
  50. else
  51. return
  52. end
  53. end
  54. end
  55. function wrap_async_run(event,handler)
  56. local result;
  57. local async_func = runner(function (event)
  58. local wait, done = waiter();
  59. result=handler(event);
  60. done();
  61. return result;
  62. end)
  63. async_func:run(event)
  64. return result;
  65. end
  66. return {
  67. get_room_from_jid = get_room_from_jid;
  68. wrap_async_run = wrap_async_run;
  69. room_jid_match_rewrite= room_jid_match_rewrite;
  70. };