Annotate

plugins/mod_mam/mod_mam.lua @ 8000:42dd766cfe97

mod_mam: Remove dead code (default will be filled in by mamprefs.lib)
author Kim Alvefur <zash@zash.se>
date Thu, 23 Mar 2017 14:45:18 +0100
parent 7979:a1e88642411d
child 8047:eb9784561387
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7851
80ee0d9cd56f mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents: 7850
diff changeset
1 -- Prosody IM
80ee0d9cd56f mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents: 7850
diff changeset
2 -- Copyright (C) 2008-2017 Matthew Wild
80ee0d9cd56f mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents: 7850
diff changeset
3 -- Copyright (C) 2008-2017 Waqas Hussain
80ee0d9cd56f mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents: 7850
diff changeset
4 -- Copyright (C) 2011-2017 Kim Alvefur
80ee0d9cd56f mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents: 7850
diff changeset
5 --
80ee0d9cd56f mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents: 7850
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
80ee0d9cd56f mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents: 7850
diff changeset
7 -- COPYING file in the source package for more information.
80ee0d9cd56f mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents: 7850
diff changeset
8 --
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 -- XEP-0313: Message Archive Management for Prosody
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 --
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11
7903
036c771b842a mod_mam: Update namespace to XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 7902
diff changeset
12 local xmlns_mam = "urn:xmpp:mam:2";
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local xmlns_delay = "urn:xmpp:delay";
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local xmlns_forward = "urn:xmpp:forward:0";
7890
9e4de27e8e08 mod_mam: Move stanza ID namespace to a common variable
Kim Alvefur <zash@zash.se>
parents: 7882
diff changeset
15 local xmlns_st_id = "urn:xmpp:sid:0";
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local um = require "core.usermanager";
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local st = require "util.stanza";
7882
1017a4f8929d mod_mam: Correct import of util.rsm
Kim Alvefur <zash@zash.se>
parents: 7856
diff changeset
19 local rsm = require "util.rsm";
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local get_prefs = module:require"mamprefs".get;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local set_prefs = module:require"mamprefs".set;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local prefs_to_stanza = module:require"mamprefsxml".tostanza;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local prefs_from_stanza = module:require"mamprefsxml".fromstanza;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local jid_bare = require "util.jid".bare;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local jid_split = require "util.jid".split;
7845
eeb22f912577 mod_mam: Filter out spoofed XEP-0359 tags
Kim Alvefur <zash@zash.se>
parents: 7844
diff changeset
26 local jid_prepped_split = require "util.jid".prepped_split;
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local dataform = require "util.dataforms".new;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local host = module.host;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local rm_load_roster = require "core.rostermanager".load_roster;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
7838
e5d5e5946af5 mod_mam: Use is_stanza from util.stanza
Kim Alvefur <zash@zash.se>
parents: 7837
diff changeset
32 local is_stanza = st.is_stanza;
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 local tostring = tostring;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local time_now = os.time;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 local m_min = math.min;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 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
37 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50);
7849
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
38 local strip_tags = module:get_option_set("dont_archive_namespaces", { "http://jabber.org/protocol/chatstates" });
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
7850
10d91860172f mod_mam: Change store name to "archive" but make it configurable for people who have data in "archive2"
Kim Alvefur <zash@zash.se>
parents: 7849
diff changeset
40 local archive_store = module:get_option_string("archive_store", "archive");
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local archive = assert(module:open_store(archive_store, "archive"));
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 if archive.name == "null" or not archive.find then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 if not archive.find then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 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
46 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
47 else
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 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
49 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 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
51 module:log("info", "Using in-memory fallback archive driver");
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 archive = module:require "fallback_archive";
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 local cleanup;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 -- Handle prefs.
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 module:hook("iq/self/"..xmlns_mam..":prefs", function(event)
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 local origin, stanza = event.origin, event.stanza;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 local user = origin.username;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 if stanza.attr.type == "get" then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local prefs = prefs_to_stanza(get_prefs(user));
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 local reply = st.reply(stanza):add_child(prefs);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 origin.send(reply);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 else -- type == "set"
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 local new_prefs = stanza:get_child("prefs", xmlns_mam);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 local prefs = prefs_from_stanza(new_prefs);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 local ok, err = set_prefs(user, prefs);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 if not ok then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 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
71 else
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 origin.send(st.reply(stanza));
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 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 return true;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 end);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 local query_form = dataform {
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 { name = "FORM_TYPE"; type = "hidden"; value = xmlns_mam; };
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 { name = "with"; type = "jid-single"; };
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 { name = "start"; type = "text-single" };
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 { name = "end"; type = "text-single"; };
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
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 -- Serve form
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 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
87 local origin, stanza = event.origin, event.stanza;
7904
c011cecad576 mod_mam: Add missing wrapping <query> element when returning the query form
Kim Alvefur <zash@zash.se>
parents: 7903
diff changeset
88 origin.send(st.reply(stanza):query(xmlns_mam):add_child(query_form:form()));
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 return true;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 end);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 -- Handle archive queries
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 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
94 local origin, stanza = event.origin, event.stanza;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 local query = stanza.tags[1];
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 local qid = query.attr.queryid;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 if cleanup then cleanup[origin.username] = true; end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 -- Search query parameters
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 local qwith, qstart, qend;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 local form = query:get_child("x", "jabber:x:data");
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 if form then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 local err;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 form, err = query_form:data(form);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 if err then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 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
108 return true;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 qwith, qstart, qend = form["with"], form["start"], form["end"];
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 qwith = qwith and jid_bare(qwith); -- dataforms does jidprep
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 if qstart or qend then -- Validate timestamps
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 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
116 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
117 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
118 return true;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 qstart, qend = vstart, vend;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 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
124 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
125
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 -- RSM stuff
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 local qset = rsm.get(query);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 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
129 local reverse = qset and qset.before or false;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 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
131 if type(before) ~= "string" then before = nil; end
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 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
150
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 local results = {};
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 -- Wrap it in stuff and deliver
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 local first, last;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 local count = 0;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 local complete = "true";
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 for id, item, when in data do
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 count = count + 1;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 if count > qmax then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 complete = nil;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 break;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 local fwd_st = st.message(msg_reply_attr)
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id })
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 :tag("forwarded", { xmlns = xmlns_forward })
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up();
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 if not is_stanza(item) then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 item = st.deserialize(item);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 item.attr.xmlns = "jabber:client";
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 fwd_st:add_child(item);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 if not first then first = id; end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 last = id;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 if reverse then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 results[count] = fwd_st;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 else
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 origin.send(fwd_st);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 end
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
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 if reverse then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 for i = #results, 1, -1 do
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 origin.send(results[i]);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 first, last = last, first;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 -- That's all folks!
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 module:log("debug", "Archive query %s completed", tostring(qid));
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193
7839
f3e1925f29c2 mod_mam: Update to XEP-0313 v0.5.1
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
194 origin.send(st.reply(stanza)
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 :tag("fin", { xmlns = xmlns_mam, queryid = qid, complete = complete })
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 :add_child(rsm.generate {
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 first = first, last = last, count = total }));
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 return true;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 end);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 local function has_in_roster(user, who)
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 local roster = rm_load_roster(user, host);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 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
204 return roster[who];
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 local function shall_store(user, who)
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 -- TODO Cache this?
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 if not um.user_exists(user, host) then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 return false;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 local prefs = get_prefs(user);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 local rule = prefs[who];
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 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
215 if rule ~= nil then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 return rule;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 -- Below could be done by a metatable
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 local default = prefs[false];
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 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
221 if default == "roster" then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 return has_in_roster(user, who);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 return 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
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 -- Handle messages
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 local function message_handler(event, c2s)
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 local origin, stanza = event.origin, event.stanza;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 local log = c2s and origin.log or module._log;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 local orig_type = stanza.attr.type or "normal";
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 local orig_from = stanza.attr.from;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 local orig_to = stanza.attr.to or orig_from;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 -- 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
235
7844
316f5166eedb mod_mam: Find out which party is the user and which is the 'with' earlier
Kim Alvefur <zash@zash.se>
parents: 7843
diff changeset
236 -- Whos storage do we put it in?
316f5166eedb mod_mam: Find out which party is the user and which is the 'with' earlier
Kim Alvefur <zash@zash.se>
parents: 7843
diff changeset
237 local store_user = c2s and origin.username or jid_split(orig_to);
316f5166eedb mod_mam: Find out which party is the user and which is the 'with' earlier
Kim Alvefur <zash@zash.se>
parents: 7843
diff changeset
238 -- And who are they chatting with?
316f5166eedb mod_mam: Find out which party is the user and which is the 'with' earlier
Kim Alvefur <zash@zash.se>
parents: 7843
diff changeset
239 local with = jid_bare(c2s and orig_to or orig_from);
316f5166eedb mod_mam: Find out which party is the user and which is the 'with' earlier
Kim Alvefur <zash@zash.se>
parents: 7843
diff changeset
240
7845
eeb22f912577 mod_mam: Filter out spoofed XEP-0359 tags
Kim Alvefur <zash@zash.se>
parents: 7844
diff changeset
241 -- Filter out <stanza-id> that claim to be from us
eeb22f912577 mod_mam: Filter out spoofed XEP-0359 tags
Kim Alvefur <zash@zash.se>
parents: 7844
diff changeset
242 stanza:maptags(function (tag)
7890
9e4de27e8e08 mod_mam: Move stanza ID namespace to a common variable
Kim Alvefur <zash@zash.se>
parents: 7882
diff changeset
243 if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id then
7856
bd5008a7dcbd mod_mam: Use correct local name for util.jid.prepped_split
Kim Alvefur <zash@zash.se>
parents: 7851
diff changeset
244 local by_user, by_host, res = jid_prepped_split(tag.attr.by);
7845
eeb22f912577 mod_mam: Filter out spoofed XEP-0359 tags
Kim Alvefur <zash@zash.se>
parents: 7844
diff changeset
245 if not res and by_host == module.host and by_user == store_user then
eeb22f912577 mod_mam: Filter out spoofed XEP-0359 tags
Kim Alvefur <zash@zash.se>
parents: 7844
diff changeset
246 return nil;
eeb22f912577 mod_mam: Filter out spoofed XEP-0359 tags
Kim Alvefur <zash@zash.se>
parents: 7844
diff changeset
247 end
eeb22f912577 mod_mam: Filter out spoofed XEP-0359 tags
Kim Alvefur <zash@zash.se>
parents: 7844
diff changeset
248 end
eeb22f912577 mod_mam: Filter out spoofed XEP-0359 tags
Kim Alvefur <zash@zash.se>
parents: 7844
diff changeset
249 return tag;
eeb22f912577 mod_mam: Filter out spoofed XEP-0359 tags
Kim Alvefur <zash@zash.se>
parents: 7844
diff changeset
250 end);
eeb22f912577 mod_mam: Filter out spoofed XEP-0359 tags
Kim Alvefur <zash@zash.se>
parents: 7844
diff changeset
251
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 -- 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
253 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
254 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
255 return;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 end
7842
9332b43931f5 mod_mam: Add some comments explaining archive expiry
Kim Alvefur <zash@zash.se>
parents: 7841
diff changeset
257
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258 -- or if hints suggest we shouldn't
7843
04b09fd144eb mod_mam: Alter hints processing
Kim Alvefur <zash@zash.se>
parents: 7842
diff changeset
259 if not stanza:get_child("store", "urn:xmpp:hints") then -- No hint telling us we should store
04b09fd144eb mod_mam: Alter hints processing
Kim Alvefur <zash@zash.se>
parents: 7842
diff changeset
260 if stanza:get_child("no-permanent-store", "urn:xmpp:hints")
04b09fd144eb mod_mam: Alter hints processing
Kim Alvefur <zash@zash.se>
parents: 7842
diff changeset
261 or stanza:get_child("no-store", "urn:xmpp:hints") then -- Hint telling us we should NOT store
04b09fd144eb mod_mam: Alter hints processing
Kim Alvefur <zash@zash.se>
parents: 7842
diff changeset
262 log("debug", "Not archiving stanza: %s (hint)", stanza:top_tag());
04b09fd144eb mod_mam: Alter hints processing
Kim Alvefur <zash@zash.se>
parents: 7842
diff changeset
263 return;
04b09fd144eb mod_mam: Alter hints processing
Kim Alvefur <zash@zash.se>
parents: 7842
diff changeset
264 end
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266
7849
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
267 if not strip_tags:empty() then
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
268 stanza = st.clone(stanza);
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
269 stanza:maptags(function (tag)
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
270 if strip_tags:contains(tag.attr.xmlns) then
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
271 return nil;
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
272 else
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
273 return tag;
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
274 end
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
275 end);
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
276 if #stanza.tags == 0 then
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
277 return;
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
278 end
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
279 end
93a068ef4b2c mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763)
Kim Alvefur <zash@zash.se>
parents: 7848
diff changeset
280
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 -- Check with the users preferences
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 if shall_store(store_user, with) then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 log("debug", "Archiving stanza: %s", stanza:top_tag());
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 -- And stash it
7908
dbdaa8487ecd mod_mam: Fix to treat first return value from archive:append as assigned ID
Kim Alvefur <zash@zash.se>
parents: 7906
diff changeset
286 local ok = archive:append(store_user, nil, stanza, time_now(), with);
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287 if ok then
7908
dbdaa8487ecd mod_mam: Fix to treat first return value from archive:append as assigned ID
Kim Alvefur <zash@zash.se>
parents: 7906
diff changeset
288 local id = ok;
7890
9e4de27e8e08 mod_mam: Move stanza ID namespace to a common variable
Kim Alvefur <zash@zash.se>
parents: 7882
diff changeset
289 stanza:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up();
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 if cleanup then cleanup[store_user] = true; end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 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
292 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 else
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 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
295 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 local function c2s_message_handler(event)
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299 return message_handler(event, true);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301
7902
92b70a921acb mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents: 7901
diff changeset
302 local function strip_stanza_id(event)
92b70a921acb mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents: 7901
diff changeset
303 local strip_by = jid_bare(event.origin.full_jid);
92b70a921acb mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents: 7901
diff changeset
304 event.stanza:maptags(function(tag)
92b70a921acb mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents: 7901
diff changeset
305 if not ( tag.attr.xmlns == xmlns_st_id and tag.attr.by == strip_by ) then
92b70a921acb mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents: 7901
diff changeset
306 return tag;
92b70a921acb mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents: 7901
diff changeset
307 end
92b70a921acb mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents: 7901
diff changeset
308 end);
92b70a921acb mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents: 7901
diff changeset
309 end
92b70a921acb mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents: 7901
diff changeset
310
92b70a921acb mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents: 7901
diff changeset
311 module:hook("pre-message/bare", strip_stanza_id, -1);
92b70a921acb mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents: 7901
diff changeset
312 module:hook("pre-message/full", strip_stanza_id, -1);
92b70a921acb mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents: 7901
diff changeset
313
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
314 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
315 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
316 if cleanup_after ~= "never" then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317 local day = 86400;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318 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
319 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
320 if not n then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 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
322 return false;
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
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 cleanup_after = tonumber(n) * ( multipliers[m] or 1 );
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 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
328
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329 if not archive.delete then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 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
331 return false;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333
7842
9332b43931f5 mod_mam: Add some comments explaining archive expiry
Kim Alvefur <zash@zash.se>
parents: 7841
diff changeset
334 -- Set of known users to do message expiry for
9332b43931f5 mod_mam: Add some comments explaining archive expiry
Kim Alvefur <zash@zash.se>
parents: 7841
diff changeset
335 -- Populated either below or when new messages are added
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336 cleanup = {};
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337
7842
9332b43931f5 mod_mam: Add some comments explaining archive expiry
Kim Alvefur <zash@zash.se>
parents: 7841
diff changeset
338 -- Iterating over users is not supported by all authentication modules
9332b43931f5 mod_mam: Add some comments explaining archive expiry
Kim Alvefur <zash@zash.se>
parents: 7841
diff changeset
339 -- Catch and ignore error if not supported
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340 pcall(function ()
7901
fc859aed9279 mod_mam: Fix wording of comment
Kim Alvefur <zash@zash.se>
parents: 7891
diff changeset
341 -- If this works, then we schedule cleanup for all known users on startup
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 for user in um.users(module.host) do
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 cleanup[user] = true;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
345 end);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
346
7842
9332b43931f5 mod_mam: Add some comments explaining archive expiry
Kim Alvefur <zash@zash.se>
parents: 7841
diff changeset
347 -- At odd intervals, delete old messages for one user
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 module:add_timer(math.random(10, 60), function()
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 local user = next(cleanup);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350 if user then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
351 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
352 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
353 if not ok then
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 module:log("warn", "Could not expire archives for user %s: %s", user, err);
7848
efe5232793aa mod_mam: If archive:delete() gives a number, this should be the number of deleted items
Kim Alvefur <zash@zash.se>
parents: 7846
diff changeset
355 elseif type(ok) == "number" then
efe5232793aa mod_mam: If archive:delete() gives a number, this should be the number of deleted items
Kim Alvefur <zash@zash.se>
parents: 7846
diff changeset
356 module:log("debug", "Removed %d messages", ok);
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
358 cleanup[user] = nil;
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360 return math.random(cleanup_interval, cleanup_interval * 2);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 end);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
362 end
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364 -- Stanzas sent by local clients
7840
92b6aa3ea2ce mod_mam: Decrease priority to zero
Kim Alvefur <zash@zash.se>
parents: 7839
diff changeset
365 module:hook("pre-message/bare", c2s_message_handler, 0);
92b6aa3ea2ce mod_mam: Decrease priority to zero
Kim Alvefur <zash@zash.se>
parents: 7839
diff changeset
366 module:hook("pre-message/full", c2s_message_handler, 0);
7905
daf83bd94906 mod_mam: Fix typo in comment
Kim Alvefur <zash@zash.se>
parents: 7904
diff changeset
367 -- Stanzas to local clients
7840
92b6aa3ea2ce mod_mam: Decrease priority to zero
Kim Alvefur <zash@zash.se>
parents: 7839
diff changeset
368 module:hook("message/bare", message_handler, 0);
92b6aa3ea2ce mod_mam: Decrease priority to zero
Kim Alvefur <zash@zash.se>
parents: 7839
diff changeset
369 module:hook("message/full", message_handler, 0);
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
370
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
371 module:hook("account-disco-info", function(event)
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 (event.reply or event.stanza):tag("feature", {var=xmlns_mam}):up();
7891
01d2a2af3146 mod_mam: Advertise Stanza ID support (XEP-0359) (thanks iNPUTmice)
Kim Alvefur <zash@zash.se>
parents: 7890
diff changeset
373 (event.reply or event.stanza):tag("feature", {var=xmlns_st_id}):up();
7836
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
374 end);
30fac9154fd4 mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
375