Software /
code /
prosody-modules
Annotate
mod_mam_archive/mod_mam_archive.lua @ 1476:08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
author | syn@syn.im |
---|---|
date | Wed, 30 Jul 2014 21:27:56 +0200 |
parent | 1471:153df603f73d |
child | 1477:db870913e1cf |
rev | line source |
---|---|
1471 | 1 -- Prosody IM |
2 -- | |
3 -- This project is MIT/X11 licensed. Please see the | |
4 -- COPYING file in the source package for more information. | |
5 -- | |
6 local get_prefs = module:require"mod_mam/mamprefs".get; | |
7 local set_prefs = module:require"mod_mam/mamprefs".set; | |
8 local rsm = module:require "mod_mam/rsm"; | |
9 local jid_bare = require "util.jid".bare; | |
10 local jid_prep = require "util.jid".prep; | |
11 local date_parse = require "util.datetime".parse; | |
12 | |
13 local st = require "util.stanza"; | |
14 local archive_store = "archive2"; | |
15 local archive = module:open_store(archive_store, "archive"); | |
16 local global_default_policy = module:get_option("default_archive_policy", false); | |
17 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50); | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
18 local conversation_interval = tonumber(module:get_option_number("archive_conversation_interval", 86400)); |
1471 | 19 |
20 -- Feature discovery | |
21 local xmlns_archive = "urn:xmpp:archive" | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
22 local feature_archive = st.stanza("feature", {xmlns=xmlns_archive}):tag("optional"); |
1471 | 23 if(global_default_policy) then |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
24 feature_archive:tag("default"); |
1471 | 25 end |
26 module:add_extension(feature_archive); | |
27 module:add_feature("urn:xmpp:archive:auto"); | |
28 module:add_feature("urn:xmpp:archive:manage"); | |
29 module:add_feature("urn:xmpp:archive:pref"); | |
30 module:add_feature("http://jabber.org/protocol/rsm"); | |
31 -- -------------------------------------------------- | |
32 | |
33 local function date_format(s) | |
34 return os.date("%Y-%m-%dT%H:%M:%SZ", s); | |
35 end | |
36 | |
37 local function prefs_to_stanza(prefs) | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
38 local prefstanza = st.stanza("pref", { xmlns="urn:xmpp:archive" }); |
1471 | 39 local default = prefs[false] ~= nil and prefs[false] or global_default_policy; |
40 | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
41 prefstanza:tag("default", {otr="oppose", save=default and "true" or "false"}):up(); |
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
42 prefstanza:tag("method", {type="auto", use="concede"}):up(); |
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
43 prefstanza:tag("method", {type="local", use="concede"}):up(); |
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
44 prefstanza:tag("method", {type="manual", use="concede"}):up(); |
1471 | 45 |
46 for jid, choice in pairs(prefs) do | |
47 if jid then | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
48 prefstanza:tag("item", {jid=jid, otr="prefer", save=choice and "message" or "false" }):up() |
1471 | 49 end |
50 end | |
51 | |
52 return prefstanza; | |
53 end | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
54 local function prefs_from_stanza(stanza, username) |
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
55 local current_prefs = get_prefs(username); |
1471 | 56 |
57 -- "default" | "item" | "session" | "method" | |
58 for elem in stanza:children() do | |
59 if elem.name == "default" then | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
60 current_prefs[false] = elem.attr["save"] == "true"; |
1471 | 61 elseif elem.name == "item" then |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
62 current_prefs[elem.attr["jid"]] = not elem.attr["save"] == "false"; |
1471 | 63 elseif elem.name == "session" then |
64 module:log("info", "element is not supported: " .. tostring(elem)); | |
65 -- local found = false; | |
66 -- for child in data:children() do | |
67 -- if child.name == elem.name and child.attr["thread"] == elem.attr["thread"] then | |
68 -- for k, v in pairs(elem.attr) do | |
69 -- child.attr[k] = v; | |
70 -- end | |
71 -- found = true; | |
72 -- break; | |
73 -- end | |
74 -- end | |
75 -- if not found then | |
76 -- data:tag(elem.name, elem.attr):up(); | |
77 -- end | |
78 elseif elem.name == "method" then | |
79 module:log("info", "element is not supported: " .. tostring(elem)); | |
80 -- local newpref = stanza.tags[1]; -- iq:pref | |
81 -- for _, e in ipairs(newpref.tags) do | |
82 -- -- if e.name ~= "method" then continue end | |
83 -- local found = false; | |
84 -- for child in data:children() do | |
85 -- if child.name == "method" and child.attr["type"] == e.attr["type"] then | |
86 -- child.attr["use"] = e.attr["use"]; | |
87 -- found = true; | |
88 -- break; | |
89 -- end | |
90 -- end | |
91 -- if not found then | |
92 -- data:tag(e.name, e.attr):up(); | |
93 -- end | |
94 -- end | |
95 end | |
96 end | |
97 end | |
98 | |
99 ------------------------------------------------------------ | |
100 -- Preferences | |
101 ------------------------------------------------------------ | |
102 local function preferences_handler(event) | |
103 local origin, stanza = event.origin, event.stanza; | |
104 local user = origin.username; | |
105 local reply = st.reply(stanza); | |
106 | |
107 if stanza.attr.type == "get" then | |
108 reply:add_child(prefs_to_stanza(get_prefs(user))); | |
109 end | |
110 if stanza.attr.type == "set" then | |
111 local new_prefs = stanza:get_child("pref", xmlns_archive); | |
112 if not new_prefs then return false; end | |
113 | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
114 local prefs = prefs_from_stanza(stanza, origin.username); |
1471 | 115 local ok, err = set_prefs(user, prefs); |
116 | |
117 if not ok then | |
118 return origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error storing preferences: "..tostring(err))); | |
119 end | |
120 end | |
121 return origin.send(reply); | |
122 end | |
123 local function auto_handler(event) | |
124 local origin, stanza = event.origin, event.stanza; | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
125 if not stanza.attr["type"] == "set" then return false; end |
1471 | 126 |
127 local user = origin.username; | |
128 local prefs = get_prefs(user); | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
129 local auto = stanza:get_child("auto", xmlns_archive); |
1471 | 130 |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
131 prefs[false] = auto.attr["save"] ~= nil and auto.attr["save"] == "true" or false; |
1471 | 132 set_prefs(user, prefs); |
133 | |
134 return origin.send(st.reply(stanza)); | |
135 end | |
136 | |
137 -- excerpt from mod_storage_sql2 | |
138 local function get_db() | |
139 local mod_sql = module:require("sql"); | |
140 local params = module:get_option("sql"); | |
141 local engine; | |
142 | |
143 params = params or { driver = "SQLite3" }; | |
144 if params.driver == "SQLite3" then | |
145 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite"); | |
146 end | |
147 | |
148 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified"); | |
149 engine = mod_sql:create_engine(params); | |
150 engine:set_encoding(); | |
151 | |
152 return engine; | |
153 end | |
154 | |
155 ------------------------------------------------------------ | |
156 -- Collections. In our case there is one conversation with each contact for the whole day for simplicity | |
157 ------------------------------------------------------------ | |
158 local function list_stanza_to_query(origin, list_el) | |
159 local sql = "SELECT `with`, `when` / ".. conversation_interval .." as `day`, COUNT(0) FROM `prosodyarchive` WHERE `host`=? AND `user`=? AND `store`=? "; | |
160 local args = {origin.host, origin.username, archive_store}; | |
161 | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
162 local with = list_el.attr["with"]; |
1471 | 163 if with ~= nil then |
164 sql = sql .. "AND `with` = ? "; | |
165 table.insert(args, jid_bare(with)); | |
166 end | |
167 | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
168 local after = list_el.attr["start"]; |
1471 | 169 if after ~= nil then |
170 sql = sql .. "AND `when` >= ?"; | |
171 table.insert(args, date_parse(after)); | |
172 end | |
173 | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
174 local before = list_el.attr["end"]; |
1471 | 175 if before ~= nil then |
176 sql = sql .. "AND `when` <= ? "; | |
177 table.insert(args, date_parse(before)); | |
178 end | |
179 | |
180 sql = sql .. "GROUP BY `with`, `when` / ".. conversation_interval .." ORDER BY `when` / ".. conversation_interval .." ASC "; | |
181 | |
182 local qset = rsm.get(list_el); | |
183 local limit = math.min(qset and qset.max or default_max_items, max_max_items); | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
184 sql = sql.."LIMIT ?"; |
1471 | 185 table.insert(args, limit); |
186 | |
187 table.insert(args, 1, sql); | |
188 return args; | |
189 end | |
190 local function list_handler(event) | |
191 local db = get_db(); | |
192 local origin, stanza = event.origin, event.stanza; | |
193 local reply = st.reply(stanza); | |
194 | |
195 local query = list_stanza_to_query(origin, stanza.tags[1]); | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
196 local list = reply:tag("list", {xmlns=xmlns_archive}); |
1471 | 197 |
198 for row in db:select(unpack(query)) do | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
199 list:tag("chat", { |
1471 | 200 xmlns=xmlns_archive, |
201 with=row[1], | |
202 start=date_format(row[2] * conversation_interval), | |
203 version=row[3] | |
204 }):up(); | |
205 end | |
206 | |
207 origin.send(reply); | |
208 return true; | |
209 end | |
210 | |
211 ------------------------------------------------------------ | |
212 -- Message archive retrieval | |
213 ------------------------------------------------------------ | |
214 | |
215 local function retrieve_handler(event) | |
216 local origin, stanza = event.origin, event.stanza; | |
217 local reply = st.reply(stanza); | |
218 | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
219 local retrieve = stanza:get_child("retrieve", xmlns_archive); |
1471 | 220 |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
221 local qwith = retrieve.attr["with"]; |
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
222 local qstart = retrieve.attr["start"]; |
1471 | 223 |
224 module:log("debug", "Archive query, with %s from %s)", | |
225 qwith or "anyone", qstart or "the dawn of time"); | |
226 | |
227 if qstart then -- Validate timestamps | |
228 local vstart = (qstart and date_parse(qstart)); | |
229 if (qstart and not vstart) then | |
230 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp")) | |
231 return true | |
232 end | |
233 qstart = vstart; | |
234 end | |
235 | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
236 if qwith then -- Validate the "with" jid |
1471 | 237 local pwith = qwith and jid_prep(qwith); |
238 if pwith and not qwith then -- it failed prepping | |
239 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid JID")) | |
240 return true | |
241 end | |
242 qwith = jid_bare(pwith); | |
243 end | |
244 | |
245 -- RSM stuff | |
246 local qset = rsm.get(retrieve); | |
247 local qmax = math.min(qset and qset.max or default_max_items, max_max_items); | |
248 local reverse = qset and qset.before or false; | |
249 local before, after = qset and qset.before, qset and qset.after; | |
250 if type(before) ~= "string" then before = nil; end | |
251 | |
252 -- Load all the data! | |
253 local data, err = archive:find(origin.username, { | |
254 start = qstart; ["end"] = qstart + conversation_interval; | |
255 with = qwith; | |
256 limit = qmax; | |
257 before = before; after = after; | |
258 reverse = reverse; | |
259 total = true; | |
260 }); | |
261 | |
262 if not data then | |
263 return origin.send(st.error_reply(stanza, "cancel", "internal-server-error", err)); | |
264 end | |
265 local count = err; | |
266 | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
267 local chat = reply:tag("chat", {xmlns=xmlns_archive, with=qwith, start=date_format(qstart), version=count}); |
1471 | 268 |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
269 module:log("debug", "Count "..count); |
1471 | 270 for id, item, when in data do |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
271 local tag = jid_bare(item["attr"]["from"]) == jid_bare(origin.full_jid) and "from" or "to"; |
1471 | 272 tag = chat:tag(tag, {secs = when - qstart}); |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
273 tag:tag("body"):text(item[2][1]):up():up(); |
1471 | 274 end |
275 | |
276 origin.send(reply); | |
277 return true; | |
278 end | |
279 | |
280 local function not_implemented(event) | |
281 local origin, stanza = event.origin, event.stanza; | |
1476
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
282 local reply = st.reply(stanza):tag("error", {type="cancel"}); |
08ca6dd36e39
mod_mam_archive: Fixing issues noted in code review for 153df603f73d3b69c434f2790cff0270de14bb75
syn@syn.im
parents:
1471
diff
changeset
|
283 reply:tag("feature-not-implemented", {xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"}):up(); |
1471 | 284 origin.send(reply); |
285 end | |
286 | |
287 -- Preferences | |
288 module:hook("iq/self/urn:xmpp:archive:pref", preferences_handler); | |
289 module:hook("iq/self/urn:xmpp:archive:auto", auto_handler); | |
290 module:hook("iq/self/urn:xmpp:archive:itemremove", not_implemented); | |
291 module:hook("iq/self/urn:xmpp:archive:sessionremove", not_implemented); | |
292 | |
293 -- Message Archive Management | |
294 module:hook("iq/self/urn:xmpp:archive:list", list_handler); | |
295 module:hook("iq/self/urn:xmpp:archive:retrieve", retrieve_handler); | |
296 module:hook("iq/self/urn:xmpp:archive:remove", not_implemented); | |
297 | |
298 -- manual archiving | |
299 module:hook("iq/self/urn:xmpp:archive:save", not_implemented); | |
300 -- replication | |
301 module:hook("iq/self/urn:xmpp:archive:modified", not_implemented); |