瀏覽代碼

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
 local target_subdomain_pattern
18
 local target_subdomain_pattern
19
     = "^"..escaped_muc_domain_prefix..".([^%.]+)%."..escaped_muc_domain_base;
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
 --- Utility function to check and convert a room JID from
28
 --- Utility function to check and convert a room JID from
22
 -- virtual room1@muc.foo.example.com to real [foo]room1@muc.example.com
29
 -- virtual room1@muc.foo.example.com to real [foo]room1@muc.example.com
23
 -- @param room_jid the room jid to match and rewrite if needed
30
 -- @param room_jid the room jid to match and rewrite if needed
24
 -- @return returns room jid [foo]room1@muc.example.com when it has subdomain
31
 -- @return returns room jid [foo]room1@muc.example.com when it has subdomain
25
 -- otherwise room1@muc.example.com(the room_jid value untouched)
32
 -- otherwise room1@muc.example.com(the room_jid value untouched)
26
 local function room_jid_match_rewrite(room_jid)
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
     if not target_subdomain then
35
     if not target_subdomain then
30
         module:log("debug", "No need to rewrite out 'to' %s", room_jid);
36
         module:log("debug", "No need to rewrite out 'to' %s", room_jid);
31
         return room_jid;
37
         return room_jid;
38
     return room_jid
44
     return room_jid
39
 end
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
 --- Finds and returns room by its jid
65
 --- Finds and returns room by its jid
43
 -- @param room_jid the room jid to search in the muc component
66
 -- @param room_jid the room jid to search in the muc component
191
     get_room_from_jid = get_room_from_jid;
214
     get_room_from_jid = get_room_from_jid;
192
     async_handler_wrapper = async_handler_wrapper;
215
     async_handler_wrapper = async_handler_wrapper;
193
     room_jid_match_rewrite = room_jid_match_rewrite;
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
     update_presence_identity = update_presence_identity;
219
     update_presence_identity = update_presence_identity;
195
 };
220
 };

Loading…
取消
儲存