Software /
code /
prosody
Comparison
plugins/mod_mam/mod_mam.lua @ 7857:db48b1697234
Merge 0.10->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 23 Jan 2017 19:34:14 +0100 |
parent | 7856:bd5008a7dcbd |
child | 7882:1017a4f8929d |
comparison
equal
deleted
inserted
replaced
7822:4caaa0c185f1 | 7857:db48b1697234 |
---|---|
1 -- Prosody IM | |
2 -- Copyright (C) 2008-2017 Matthew Wild | |
3 -- Copyright (C) 2008-2017 Waqas Hussain | |
4 -- Copyright (C) 2011-2017 Kim Alvefur | |
5 -- | |
6 -- This project is MIT/X11 licensed. Please see the | |
7 -- COPYING file in the source package for more information. | |
8 -- | |
9 -- XEP-0313: Message Archive Management for Prosody | |
10 -- | |
11 | |
12 local xmlns_mam = "urn:xmpp:mam:1"; | |
13 local xmlns_delay = "urn:xmpp:delay"; | |
14 local xmlns_forward = "urn:xmpp:forward:0"; | |
15 | |
16 local um = require "core.usermanager"; | |
17 local st = require "util.stanza"; | |
18 local rsm = require "rsm"; | |
19 local get_prefs = module:require"mamprefs".get; | |
20 local set_prefs = module:require"mamprefs".set; | |
21 local prefs_to_stanza = module:require"mamprefsxml".tostanza; | |
22 local prefs_from_stanza = module:require"mamprefsxml".fromstanza; | |
23 local jid_bare = require "util.jid".bare; | |
24 local jid_split = require "util.jid".split; | |
25 local jid_prepped_split = require "util.jid".prepped_split; | |
26 local dataform = require "util.dataforms".new; | |
27 local host = module.host; | |
28 | |
29 local rm_load_roster = require "core.rostermanager".load_roster; | |
30 | |
31 local is_stanza = st.is_stanza; | |
32 local tostring = tostring; | |
33 local time_now = os.time; | |
34 local m_min = math.min; | |
35 local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse; | |
36 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50); | |
37 local global_default_policy = module:get_option("default_archive_policy", true); | |
38 if global_default_policy ~= "roster" then | |
39 global_default_policy = module:get_option_boolean("default_archive_policy", global_default_policy); | |
40 end | |
41 local strip_tags = module:get_option_set("dont_archive_namespaces", { "http://jabber.org/protocol/chatstates" }); | |
42 | |
43 local archive_store = module:get_option_string("archive_store", "archive"); | |
44 local archive = assert(module:open_store(archive_store, "archive")); | |
45 | |
46 if archive.name == "null" or not archive.find then | |
47 if not archive.find then | |
48 module:log("debug", "Attempt to open archive storage returned a valid driver but it does not seem to implement the storage API"); | |
49 module:log("debug", "mod_%s does not support archiving", archive._provided_by or archive.name and "storage_"..archive.name.."(?)" or "<unknown>"); | |
50 else | |
51 module:log("debug", "Attempt to open archive storage returned null driver"); | |
52 end | |
53 module:log("debug", "See https://prosody.im/doc/storage and https://prosody.im/doc/archiving for more information"); | |
54 module:log("info", "Using in-memory fallback archive driver"); | |
55 archive = module:require "fallback_archive"; | |
56 end | |
57 | |
58 local cleanup; | |
59 | |
60 -- Handle prefs. | |
61 module:hook("iq/self/"..xmlns_mam..":prefs", function(event) | |
62 local origin, stanza = event.origin, event.stanza; | |
63 local user = origin.username; | |
64 if stanza.attr.type == "get" then | |
65 local prefs = prefs_to_stanza(get_prefs(user)); | |
66 local reply = st.reply(stanza):add_child(prefs); | |
67 origin.send(reply); | |
68 else -- type == "set" | |
69 local new_prefs = stanza:get_child("prefs", xmlns_mam); | |
70 local prefs = prefs_from_stanza(new_prefs); | |
71 local ok, err = set_prefs(user, prefs); | |
72 if not ok then | |
73 origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error storing preferences: "..tostring(err))); | |
74 else | |
75 origin.send(st.reply(stanza)); | |
76 end | |
77 end | |
78 return true; | |
79 end); | |
80 | |
81 local query_form = dataform { | |
82 { name = "FORM_TYPE"; type = "hidden"; value = xmlns_mam; }; | |
83 { name = "with"; type = "jid-single"; }; | |
84 { name = "start"; type = "text-single" }; | |
85 { name = "end"; type = "text-single"; }; | |
86 }; | |
87 | |
88 -- Serve form | |
89 module:hook("iq-get/self/"..xmlns_mam..":query", function(event) | |
90 local origin, stanza = event.origin, event.stanza; | |
91 origin.send(st.reply(stanza):add_child(query_form:form())); | |
92 return true; | |
93 end); | |
94 | |
95 -- Handle archive queries | |
96 module:hook("iq-set/self/"..xmlns_mam..":query", function(event) | |
97 local origin, stanza = event.origin, event.stanza; | |
98 local query = stanza.tags[1]; | |
99 local qid = query.attr.queryid; | |
100 | |
101 if cleanup then cleanup[origin.username] = true; end | |
102 | |
103 -- Search query parameters | |
104 local qwith, qstart, qend; | |
105 local form = query:get_child("x", "jabber:x:data"); | |
106 if form then | |
107 local err; | |
108 form, err = query_form:data(form); | |
109 if err then | |
110 origin.send(st.error_reply(stanza, "modify", "bad-request", select(2, next(err)))); | |
111 return true; | |
112 end | |
113 qwith, qstart, qend = form["with"], form["start"], form["end"]; | |
114 qwith = qwith and jid_bare(qwith); -- dataforms does jidprep | |
115 end | |
116 | |
117 if qstart or qend then -- Validate timestamps | |
118 local vstart, vend = (qstart and timestamp_parse(qstart)), (qend and timestamp_parse(qend)); | |
119 if (qstart and not vstart) or (qend and not vend) then | |
120 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp")) | |
121 return true; | |
122 end | |
123 qstart, qend = vstart, vend; | |
124 end | |
125 | |
126 module:log("debug", "Archive query, id %s with %s from %s until %s)", | |
127 tostring(qid), qwith or "anyone", qstart or "the dawn of time", qend or "now"); | |
128 | |
129 -- RSM stuff | |
130 local qset = rsm.get(query); | |
131 local qmax = m_min(qset and qset.max or default_max_items, max_max_items); | |
132 local reverse = qset and qset.before or false; | |
133 local before, after = qset and qset.before, qset and qset.after; | |
134 if type(before) ~= "string" then before = nil; end | |
135 | |
136 | |
137 -- Load all the data! | |
138 local data, err = archive:find(origin.username, { | |
139 start = qstart; ["end"] = qend; -- Time range | |
140 with = qwith; | |
141 limit = qmax + 1; | |
142 before = before; after = after; | |
143 reverse = reverse; | |
144 total = true; | |
145 }); | |
146 | |
147 if not data then | |
148 origin.send(st.error_reply(stanza, "cancel", "internal-server-error", err)); | |
149 return true; | |
150 end | |
151 local total = tonumber(err); | |
152 | |
153 local msg_reply_attr = { to = stanza.attr.from, from = stanza.attr.to }; | |
154 | |
155 local results = {}; | |
156 | |
157 -- Wrap it in stuff and deliver | |
158 local first, last; | |
159 local count = 0; | |
160 local complete = "true"; | |
161 for id, item, when in data do | |
162 count = count + 1; | |
163 if count > qmax then | |
164 complete = nil; | |
165 break; | |
166 end | |
167 local fwd_st = st.message(msg_reply_attr) | |
168 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id }) | |
169 :tag("forwarded", { xmlns = xmlns_forward }) | |
170 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up(); | |
171 | |
172 if not is_stanza(item) then | |
173 item = st.deserialize(item); | |
174 end | |
175 item.attr.xmlns = "jabber:client"; | |
176 fwd_st:add_child(item); | |
177 | |
178 if not first then first = id; end | |
179 last = id; | |
180 | |
181 if reverse then | |
182 results[count] = fwd_st; | |
183 else | |
184 origin.send(fwd_st); | |
185 end | |
186 end | |
187 | |
188 if reverse then | |
189 for i = #results, 1, -1 do | |
190 origin.send(results[i]); | |
191 end | |
192 first, last = last, first; | |
193 end | |
194 | |
195 -- That's all folks! | |
196 module:log("debug", "Archive query %s completed", tostring(qid)); | |
197 | |
198 origin.send(st.reply(stanza) | |
199 :tag("fin", { xmlns = xmlns_mam, queryid = qid, complete = complete }) | |
200 :add_child(rsm.generate { | |
201 first = first, last = last, count = total })); | |
202 return true; | |
203 end); | |
204 | |
205 local function has_in_roster(user, who) | |
206 local roster = rm_load_roster(user, host); | |
207 module:log("debug", "%s has %s in roster? %s", user, who, roster[who] and "yes" or "no"); | |
208 return roster[who]; | |
209 end | |
210 | |
211 local function shall_store(user, who) | |
212 -- TODO Cache this? | |
213 if not um.user_exists(user, host) then | |
214 return false; | |
215 end | |
216 local prefs = get_prefs(user); | |
217 local rule = prefs[who]; | |
218 module:log("debug", "%s's rule for %s is %s", user, who, tostring(rule)); | |
219 if rule ~= nil then | |
220 return rule; | |
221 end | |
222 -- Below could be done by a metatable | |
223 local default = prefs[false]; | |
224 module:log("debug", "%s's default rule is %s", user, tostring(default)); | |
225 if default == nil then | |
226 default = global_default_policy; | |
227 module:log("debug", "Using global default rule, %s", tostring(default)); | |
228 end | |
229 if default == "roster" then | |
230 return has_in_roster(user, who); | |
231 end | |
232 return default; | |
233 end | |
234 | |
235 -- Handle messages | |
236 local function message_handler(event, c2s) | |
237 local origin, stanza = event.origin, event.stanza; | |
238 local log = c2s and origin.log or module._log; | |
239 local orig_type = stanza.attr.type or "normal"; | |
240 local orig_from = stanza.attr.from; | |
241 local orig_to = stanza.attr.to or orig_from; | |
242 -- Stanza without 'to' are treated as if it was to their own bare jid | |
243 | |
244 -- Whos storage do we put it in? | |
245 local store_user = c2s and origin.username or jid_split(orig_to); | |
246 -- And who are they chatting with? | |
247 local with = jid_bare(c2s and orig_to or orig_from); | |
248 | |
249 -- Filter out <stanza-id> that claim to be from us | |
250 stanza:maptags(function (tag) | |
251 if tag.name == "stanza-id" and tag.attr.xmlns == "urn:xmpp:sid:0" then | |
252 local by_user, by_host, res = jid_prepped_split(tag.attr.by); | |
253 if not res and by_host == module.host and by_user == store_user then | |
254 return nil; | |
255 end | |
256 end | |
257 return tag; | |
258 end); | |
259 | |
260 -- We store chat messages or normal messages that have a body | |
261 if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then | |
262 log("debug", "Not archiving stanza: %s (type)", stanza:top_tag()); | |
263 return; | |
264 end | |
265 | |
266 -- or if hints suggest we shouldn't | |
267 if not stanza:get_child("store", "urn:xmpp:hints") then -- No hint telling us we should store | |
268 if stanza:get_child("no-permanent-store", "urn:xmpp:hints") | |
269 or stanza:get_child("no-store", "urn:xmpp:hints") then -- Hint telling us we should NOT store | |
270 log("debug", "Not archiving stanza: %s (hint)", stanza:top_tag()); | |
271 return; | |
272 end | |
273 end | |
274 | |
275 if not strip_tags:empty() then | |
276 stanza = st.clone(stanza); | |
277 stanza:maptags(function (tag) | |
278 if strip_tags:contains(tag.attr.xmlns) then | |
279 return nil; | |
280 else | |
281 return tag; | |
282 end | |
283 end); | |
284 if #stanza.tags == 0 then | |
285 return; | |
286 end | |
287 end | |
288 | |
289 -- Check with the users preferences | |
290 if shall_store(store_user, with) then | |
291 log("debug", "Archiving stanza: %s", stanza:top_tag()); | |
292 | |
293 -- And stash it | |
294 local ok, id = archive:append(store_user, nil, stanza, time_now(), with); | |
295 if ok then | |
296 stanza:tag("stanza-id", { xmlns = "urn:xmpp:sid:0", by = store_user.."@"..host, id = id }):up(); | |
297 if cleanup then cleanup[store_user] = true; end | |
298 module:fire_event("archive-message-added", { origin = origin, stanza = stanza, for_user = store_user, id = id }); | |
299 end | |
300 else | |
301 log("debug", "Not archiving stanza: %s (prefs)", stanza:top_tag()); | |
302 end | |
303 end | |
304 | |
305 local function c2s_message_handler(event) | |
306 return message_handler(event, true); | |
307 end | |
308 | |
309 local cleanup_after = module:get_option_string("archive_expires_after", "1w"); | |
310 local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60); | |
311 if cleanup_after ~= "never" then | |
312 local day = 86400; | |
313 local multipliers = { d = day, w = day * 7, m = 31 * day, y = 365.2425 * day }; | |
314 local n, m = cleanup_after:lower():match("(%d+)%s*([dwmy]?)"); | |
315 if not n then | |
316 module:log("error", "Could not parse archive_expires_after string %q", cleanup_after); | |
317 return false; | |
318 end | |
319 | |
320 cleanup_after = tonumber(n) * ( multipliers[m] or 1 ); | |
321 | |
322 module:log("debug", "archive_expires_after = %d -- in seconds", cleanup_after); | |
323 | |
324 if not archive.delete then | |
325 module:log("error", "archive_expires_after set but mod_%s does not support deleting", archive._provided_by); | |
326 return false; | |
327 end | |
328 | |
329 -- Set of known users to do message expiry for | |
330 -- Populated either below or when new messages are added | |
331 cleanup = {}; | |
332 | |
333 -- Iterating over users is not supported by all authentication modules | |
334 -- Catch and ignore error if not supported | |
335 pcall(function () | |
336 -- If this works, then we schedule cleanup for all known known | |
337 for user in um.users(module.host) do | |
338 cleanup[user] = true; | |
339 end | |
340 end); | |
341 | |
342 -- At odd intervals, delete old messages for one user | |
343 module:add_timer(math.random(10, 60), function() | |
344 local user = next(cleanup); | |
345 if user then | |
346 module:log("debug", "Removing old messages for user %q", user); | |
347 local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; }) | |
348 if not ok then | |
349 module:log("warn", "Could not expire archives for user %s: %s", user, err); | |
350 elseif type(ok) == "number" then | |
351 module:log("debug", "Removed %d messages", ok); | |
352 end | |
353 cleanup[user] = nil; | |
354 end | |
355 return math.random(cleanup_interval, cleanup_interval * 2); | |
356 end); | |
357 end | |
358 | |
359 -- Stanzas sent by local clients | |
360 module:hook("pre-message/bare", c2s_message_handler, 0); | |
361 module:hook("pre-message/full", c2s_message_handler, 0); | |
362 -- Stanszas to local clients | |
363 module:hook("message/bare", message_handler, 0); | |
364 module:hook("message/full", message_handler, 0); | |
365 | |
366 module:hook("account-disco-info", function(event) | |
367 (event.reply or event.stanza):tag("feature", {var=xmlns_mam}):up(); | |
368 end); | |
369 |