Annotate

plugins/mod_mam/mod_mam.lua @ 7837:a17bddf62a28

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