Software /
code /
prosody-modules
Annotate
mod_http_roster_admin/mod_http_roster_admin.lua @ 2621:c4c51e43b788
mod_http_roster_admin: No need to manually purge.
mod_storage_memory now has the `auto_purge_enabled` option which does the job.
author | JC Brand <jc@opkode.com> |
---|---|
date | Wed, 15 Mar 2017 08:53:30 +0000 |
parent | 2617:7c3a1688e385 |
child | 2622:ac3ca6233ded |
rev | line source |
---|---|
2161 | 1 -- mod_http_roster_admin |
2 -- Description: Allow user rosters to be sourced from a remote HTTP API | |
3 -- | |
4 -- Version: 1.0 | |
5 -- Date: 2015-03-06 | |
6 -- Author: Matthew Wild <matthew@prosody.im> | |
7 -- License: MPLv2 | |
8 -- | |
9 -- Requirements: | |
10 -- Prosody config: | |
11 -- storage = { roster = "memory" } | |
12 -- modules_disabled = { "roster" } | |
13 -- Dependencies: | |
14 -- Prosody 0.9 | |
15 -- lua-cjson (Debian/Ubuntu/LuaRocks: lua-cjson) | |
16 | |
17 local http = require "net.http"; | |
18 local json = require "cjson"; | |
19 local it = require "util.iterators"; | |
20 local set = require "util.set"; | |
21 local rm = require "core.rostermanager"; | |
22 local st = require "util.stanza"; | |
23 local array = require "util.array"; | |
24 | |
2617
7c3a1688e385
Purge the roster from RAM when the user logs off.
JC Brand <jc@opkode.com>
parents:
2441
diff
changeset
|
25 local bare_sessions = prosody.bare_sessions; |
7c3a1688e385
Purge the roster from RAM when the user logs off.
JC Brand <jc@opkode.com>
parents:
2441
diff
changeset
|
26 local storagemanager = require "core.storagemanager"; |
7c3a1688e385
Purge the roster from RAM when the user logs off.
JC Brand <jc@opkode.com>
parents:
2441
diff
changeset
|
27 |
2161 | 28 local host = module.host; |
29 local sessions = hosts[host].sessions; | |
30 | |
31 local roster_url = module:get_option_string("http_roster_url", "http://localhost/%s"); | |
32 | |
33 -- Send a roster push to the named user, with the given roster, for the specified | |
34 -- contact's roster entry. Used to notify clients of changes/removals. | |
35 local function roster_push(username, roster, contact_jid) | |
36 local stanza = st.iq({type="set"}) | |
37 :tag("query", {xmlns = "jabber:iq:roster" }); | |
38 local item = roster[contact_jid]; | |
39 if item then | |
40 stanza:tag("item", {jid = contact_jid, subscription = item.subscription, name = item.name, ask = item.ask}); | |
41 for group in pairs(item.groups) do | |
42 stanza:tag("group"):text(group):up(); | |
43 end | |
44 else | |
45 stanza:tag("item", {jid = contact_jid, subscription = "remove"}); | |
46 end | |
47 stanza:up():up(); -- move out from item | |
48 for _, session in pairs(hosts[host].sessions[username].sessions) do | |
49 if session.interested then | |
50 session.send(stanza); | |
51 end | |
52 end | |
53 end | |
54 | |
55 -- Send latest presence from the named local user to a contact. | |
56 local function send_presence(username, contact_jid, available) | |
57 module:log("debug", "Sending %savailable presence from %s to contact %s", (available and "" or "un"), username, contact_jid); | |
58 for resource, session in pairs(sessions[username].sessions) do | |
59 local pres; | |
60 if available then | |
61 pres = st.clone(session.presence); | |
62 pres.attr.to = contact_jid; | |
63 else | |
64 pres = st.presence({ to = contact_jid, from = session.full_jid, type = "unavailable" }); | |
65 end | |
66 module:send(pres); | |
67 end | |
68 end | |
69 | |
70 -- Converts a 'friend' object from the API to a Prosody roster item object | |
71 local function friend_to_roster_item(friend) | |
72 return { | |
73 name = friend.name; | |
74 subscription = "both"; | |
75 groups = friend.groups or {}; | |
76 }; | |
77 end | |
78 | |
79 -- Returns a handler function to consume the data returned from | |
80 -- the API, compare it to the user's current roster, and perform | |
81 -- any actions necessary (roster pushes, presence probes) to | |
82 -- synchronize them. | |
83 local function updated_friends_handler(username, cb) | |
84 return (function (ok, code, friends) | |
85 if not ok then | |
86 cb(false, code); | |
87 end | |
88 local user = sessions[username]; | |
89 local roster = user.roster; | |
90 local old_contacts = set.new(array.collect(it.keys(roster))); | |
91 local new_contacts = set.new(array.collect(it.keys(friends))); | |
92 | |
93 -- These two entries are not real contacts, ignore them | |
94 old_contacts:remove(false); | |
95 old_contacts:remove("pending"); | |
96 | |
97 module:log("debug", "New friends list of %s: %s", username, json.encode(friends)); | |
98 | |
99 -- Calculate which contacts have been added/removed since | |
100 -- the last time we fetched the roster | |
101 local added_contacts = new_contacts - old_contacts; | |
102 local removed_contacts = old_contacts - new_contacts; | |
103 | |
104 local added, removed = 0, 0; | |
105 | |
106 -- Add new contacts and notify connected clients | |
107 for contact_jid in added_contacts do | |
108 module:log("debug", "Processing new friend of %s: %s", username, contact_jid); | |
109 roster[contact_jid] = friend_to_roster_item(friends[contact_jid]); | |
110 roster_push(username, roster, contact_jid); | |
111 send_presence(username, contact_jid, true); | |
112 added = added + 1; | |
113 end | |
114 | |
115 -- Remove contacts and notify connected clients | |
116 for contact_jid in removed_contacts do | |
117 module:log("debug", "Processing removed friend of %s: %s", username, contact_jid); | |
118 roster[contact_jid] = nil; | |
119 roster_push(username, roster, contact_jid); | |
120 send_presence(username, contact_jid, false); | |
121 removed = removed + 1; | |
122 end | |
123 module:log("debug", "User %s: added %d new contacts, removed %d contacts", username, added, removed); | |
124 cb(true); | |
125 end); | |
126 end | |
127 | |
128 -- Fetch the named user's roster from the API, call callback (cb) | |
129 -- with status and result (friends list) when received. | |
130 function fetch_roster(username, cb) | |
2210
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
131 local x = {headers = {}}; |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
132 x["headers"]["ACCEPT"] = "application/json, text/plain, */*"; |
2441
68ebc52222dc
Log URL called by http_roster_admin
JC Brand <jc@opkode.com>
parents:
2210
diff
changeset
|
133 module:log("debug", "Fetching roster at URL: %s", roster_url:format(username)); |
2161 | 134 local ok, err = http.request( |
2210
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
135 roster_url:format(username), |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
136 x, |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
137 function (roster_data, code) |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
138 if code ~= 200 then |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
139 module:log("error", "Error fetching roster from %s (code %d): %s", roster_url:format(username), code, tostring(roster_data):sub(1, 40):match("^[^\r\n]+")); |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
140 if code ~= 0 then |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
141 cb(nil, code, roster_data); |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
142 end |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
143 return; |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
144 end |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
145 module:log("debug", "Successfully fetched roster for %s", username); |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
146 module:log("debug", "The roster data is %s", roster_data); |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
147 cb(true, code, json.decode(roster_data)); |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
148 end |
126d79bf079b
mod_http_roster_admin: Also log if the status error is 0 (Connection refused)
JC Brand <jcbrand@minddistrict.com>
parents:
2161
diff
changeset
|
149 ); |
2161 | 150 if not ok then |
151 module:log("error", "Failed to connect to roster API at %s: %s", roster_url:format(username), err); | |
152 cb(false, 0, err); | |
153 end | |
154 end | |
155 | |
156 -- Fetch the named user's roster from the API, synchronize it with | |
157 -- the user's current roster. Notify callback (cb) with true/false | |
158 -- depending on success or failure. | |
159 function refresh_roster(username, cb) | |
160 local user = sessions[username]; | |
161 if not (user and user.roster) then | |
162 module:log("debug", "User's (%q) roster updated, but they are not online - ignoring", username); | |
163 cb(true); | |
164 return; | |
165 end | |
166 fetch_roster(username, updated_friends_handler(username, cb)); | |
167 end | |
168 | |
169 --- Roster protocol handling --- | |
170 | |
171 -- Build a reply to a "roster get" request | |
172 local function build_roster_reply(stanza, roster_data) | |
173 local roster = st.reply(stanza) | |
174 :tag("query", { xmlns = "jabber:iq:roster" }); | |
175 | |
176 for jid, item in pairs(roster_data) do | |
177 if jid and jid ~= "pending" then | |
178 roster:tag("item", { | |
179 jid = jid, | |
180 subscription = item.subscription, | |
181 ask = item.ask, | |
182 name = item.name, | |
183 }); | |
184 for group in pairs(item.groups) do | |
185 roster:tag("group"):text(group):up(); | |
186 end | |
187 roster:up(); -- move out from item | |
188 end | |
189 end | |
190 return roster; | |
191 end | |
192 | |
193 -- Handle clients requesting their roster (generally at login) | |
194 -- This will not work if mod_roster is loaded (in 0.9). | |
195 module:hook("iq-get/self/jabber:iq:roster:query", function(event) | |
196 local session, stanza = event.origin, event.stanza; | |
197 | |
198 session.interested = true; -- resource is interested in roster updates | |
199 | |
200 local roster = session.roster; | |
201 if roster[false].downloaded then | |
202 return session.send(build_roster_reply(stanza, roster)); | |
203 end | |
204 | |
205 -- It's possible that we can call this more than once for a new roster | |
206 -- Should happen rarely (multiple clients of the same user request the | |
207 -- roster in the time it takes the API to respond). Currently we just | |
208 -- issue multiple requests, as it's harmless apart from the wasted | |
209 -- requests. | |
210 fetch_roster(session.username, function (ok, code, friends) | |
211 if not ok then | |
212 session.send(st.error_reply(stanza, "cancel", "internal-server-error")); | |
213 session:close("internal-server-error"); | |
214 return; | |
215 end | |
216 | |
217 -- Are we the first callback to handle the downloaded roster? | |
218 local first = roster[false].downloaded == nil; | |
219 if first then | |
220 -- Fill out new roster | |
221 for jid, friend in pairs(friends) do | |
222 roster[jid] = friend_to_roster_item(friend); | |
223 end | |
224 end | |
2617
7c3a1688e385
Purge the roster from RAM when the user logs off.
JC Brand <jc@opkode.com>
parents:
2441
diff
changeset
|
225 |
7c3a1688e385
Purge the roster from RAM when the user logs off.
JC Brand <jc@opkode.com>
parents:
2441
diff
changeset
|
226 roster[false].downloaded = true; |
2161 | 227 |
228 -- Send full roster to client | |
229 session.send(build_roster_reply(stanza, roster)); | |
230 | |
231 if not first then | |
232 -- We already had a roster, make sure to handle any changes... | |
233 updated_friends_handler(session.username, nil)(ok, code, friends); | |
234 end | |
235 end); | |
236 | |
237 return true; | |
238 end); | |
239 | |
240 -- Prevent client from making changes to the roster. This will not | |
241 -- work if mod_roster is loaded (in 0.9). | |
242 module:hook("iq-set/self/jabber:iq:roster:query", function(event) | |
243 local session, stanza = event.origin, event.stanza; | |
244 return session.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
245 end); | |
246 | |
247 --- HTTP endpoint to trigger roster refresh --- | |
248 | |
249 -- Handles updating for a single user: GET /roster_admin/refresh/USERNAME | |
250 function handle_refresh_single(event, username) | |
251 refresh_roster(username, function (ok, code, err) | |
252 event.response.headers["Content-Type"] = "application/json"; | |
253 event.response:send(json.encode({ | |
254 status = ok and "ok" or "error"; | |
255 message = err or "roster update complete"; | |
256 })); | |
257 end); | |
258 return true; | |
259 end | |
260 | |
261 -- Handles updating for multiple users: POST /roster_admin/refresh | |
262 -- Payload should be a JSON array of usernames, e.g. ["user1", "user2", "user3"] | |
263 function handle_refresh_multi(event) | |
264 local users = json.decode(event.request.body); | |
265 if not users then | |
266 module:log("warn", "Multi-user refresh attempted with missing/invalid payload"); | |
267 event.response:send(400); | |
268 return true; | |
269 end | |
270 | |
271 local count, count_err = 0, 0; | |
272 | |
273 local function cb(ok) | |
274 count = count + 1; | |
275 if not ok then | |
276 count_err = count_err + 1; | |
277 end | |
278 | |
279 if count == #users then | |
280 event.response.headers["Content-Type"] = "application/json"; | |
281 event.response:send(json.encode({ | |
282 status = "ok"; | |
283 message = "roster update complete"; | |
284 updated = count - count_err; | |
285 errors = count_err; | |
286 })); | |
287 end | |
288 end | |
289 | |
290 for _, username in ipairs(users) do | |
291 refresh_roster(username, cb); | |
292 end | |
293 | |
294 return true; | |
295 end | |
296 | |
297 module:provides("http", { | |
298 route = { | |
299 ["POST /refresh"] = handle_refresh_multi; | |
300 ["GET /refresh/*"] = handle_refresh_single; | |
301 }; | |
302 }); |