浏览代码

prosody modules: add util function for rewritesplit JID

master
Aaron van Meerten 5 年前
父节点
当前提交
25ded0bdeb
共有 1 个文件被更改,包括 27 次插入2 次删除
  1. 27
    2
      resources/prosody-plugins/util.lib.lua

+ 27
- 2
resources/prosody-plugins/util.lib.lua 查看文件

@@ -18,14 +18,20 @@ local escaped_muc_domain_prefix = muc_domain_prefix:gsub("%p", "%%%1");
18 18
 local target_subdomain_pattern
19 19
     = "^"..escaped_muc_domain_prefix..".([^%.]+)%."..escaped_muc_domain_base;
20 20
 
21
+-- Utility function to split room JID to include room name and subdomain
22
+local function room_jid_split_subdomain(room_jid)
23
+    local node, host, resource = jid.split(room_jid);
24
+    local target_subdomain = host and host:match(target_subdomain_pattern);
25
+    return node, host, resource, target_subdomain
26
+end
27
+
21 28
 --- Utility function to check and convert a room JID from
22 29
 -- virtual room1@muc.foo.example.com to real [foo]room1@muc.example.com
23 30
 -- @param room_jid the room jid to match and rewrite if needed
24 31
 -- @return returns room jid [foo]room1@muc.example.com when it has subdomain
25 32
 -- otherwise room1@muc.example.com(the room_jid value untouched)
26 33
 local function room_jid_match_rewrite(room_jid)
27
-    local node, host, resource = jid.split(room_jid);
28
-    local target_subdomain = host and host:match(target_subdomain_pattern);
34
+    local node, host, resource, target_subdomain = room_jid_split_subdomain(room_jid);
29 35
     if not target_subdomain then
30 36
         module:log("debug", "No need to rewrite out 'to' %s", room_jid);
31 37
         return room_jid;
@@ -38,6 +44,23 @@ local function room_jid_match_rewrite(room_jid)
38 44
     return room_jid
39 45
 end
40 46
 
47
+local function internal_room_jid_match_rewrite(room_jid)
48
+    local node, host, resource = jid.split(room_jid);
49
+    if host ~= muc_domain or not node then
50
+        module:log("debug", "No need to rewrite %s (not from the MUC host)", room_jid);
51
+        return room_jid;
52
+    end
53
+    local target_subdomain, target_node = node:match("^%[([^%]]+)%](.+)$");
54
+    if not (target_node and target_subdomain) then
55
+        module:log("debug", "Not rewriting... unexpected node format: %s", node);
56
+        return room_jid;
57
+    end
58
+    -- Ok, rewrite room_jid address to pretty format
59
+    local new_node, new_host, new_resource = target_node, muc_domain_prefix..".".. target_subdomain.."."..muc_domain_base, resource;
60
+    room_jid = jid.join(new_node, new_host, new_resource);
61
+    module:log("debug", "Rewrote to %s", room_jid);
62
+    return room_jid
63
+end
41 64
 
42 65
 --- Finds and returns room by its jid
43 66
 -- @param room_jid the room jid to search in the muc component
@@ -191,5 +214,7 @@ return {
191 214
     get_room_from_jid = get_room_from_jid;
192 215
     async_handler_wrapper = async_handler_wrapper;
193 216
     room_jid_match_rewrite = room_jid_match_rewrite;
217
+    room_jid_split_subdomain = room_jid_split_subdomain;
218
+    internal_room_jid_match_rewrite = internal_room_jid_match_rewrite;
194 219
     update_presence_identity = update_presence_identity;
195 220
 };

正在加载...
取消
保存