Annotate

mod_mam_archive/mod_mam_archive.lua @ 1471:153df603f73d

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