-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_roster_cloud.lua
96 lines (75 loc) · 2.27 KB
/
mod_roster_cloud.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
local https = require "ssl.https"
local ltn12 = require 'ltn12'
local timer = require 'util.timer'
local url = module:get_option_string('roster_cloud_url')
local secret = module:get_option_string('roster_cloud_secret')
module:require "sha1"
local JSON = module:require "json"
local function url_encode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w %-%_%.%~])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
local function sendRequest(username)
local request_body = 'username='..url_encode(username)..'&operation=sharedroster'
local response_body = {}
local signature = hmac_sha1(secret, request_body)
local r, status = https.request({
url = url,
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body),
method = 'POST',
headers = {
["content-type"] = "application/x-www-form-urlencoded",
["Content-Length"] = string.len(request_body),
["X-JSXC-SIGNATURE"] = 'sha1='..signature
}
})
return table.concat(response_body), status
end
local function inject_roster_contacts(username, host, roster)
module:log('debug', 'inject roster contacts for '..username)
local body, status = sendRequest(username)
if status ~= 200 then
module:log('error', body)
return
end
local response = JSON:decode(body)
if response.result == 'error' then
module:log('error', response.data.msg);
end
if response.result == 'success' then
for uid, entry in pairs(response.data.sharedRoster) do
local jid = uid..'@'..host
if not (jid == username..'@'..host or roster[jid]) then
module:log('debug', 'injecting '..jid..' as '..entry.name)
roster[jid] = {};
local r = roster[jid];
r.subscription = 'both';
r.persist = false;
r.name = entry.name;
r.groups = {}
for index, group in ipairs(entry.groups) do
r.groups[group] = true
end
end
end
end
if roster[false] then
roster[false].version = true;
end
end
function module.load()
if url == nil then
module:log('warn', 'Disabled, because we have no url')
return
end
module:log('debug', 'Loaded. Using the following endpoint: '..url)
module:hook('roster-load', inject_roster_contacts);
end
function module.unload()
end