Software / code / prosody
Annotate
plugins/mod_mam/mod_mam.lua @ 10224:94e341dee51c
core.certmanager: Move EECDH ciphers before EDH in default cipherstring
The original intent of having kEDH before kEECDH was that if a `dhparam`
file was specified, this would be interpreted as a preference by the
admin for old and well-tested Diffie-Hellman key agreement over newer
elliptic curve ones. Otherwise the faster elliptic curve ciphersuites
would be preferred. This didn't really work as intended since this
affects the ClientHello on outgoing s2s connections, leading to some
servers using poorly configured kEDH.
With Debian shipping OpenSSL settings that enforce a higher security
level, this caused interoperability problems with servers that use DH
params smaller than 2048 bits. E.g. jabber.org at the time of this
writing has 1024 bit DH params.
MattJ says
> Curves have won, and OpenSSL is less weird about them now
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 25 Aug 2019 20:22:35 +0200 |
| parent | 10111:0f335815244f |
| child | 10299:fd94721186b8 |
| 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; |
|
9879
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
36 local timestamp, timestamp_parse, datestamp = import( "util.datetime", "datetime", "parse", "date"); |
|
7836
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"); |
|
8047
eb9784561387
mod_mam: Remove extraneous assert when opening the store
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8000
diff
changeset
|
41 local archive = module:open_store(archive_store, "archive"); |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
|
9886
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
43 local cleanup_after = module:get_option_string("archive_expires_after", "1w"); |
|
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
44 local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60); |
|
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
45 local archive_item_limit = module:get_option_number("storage_archive_item_limit", archive.caps and archive.caps.quota or 1000); |
|
9553
9a5485550bfd
mod_mam: Ignore case of null storage driver
Kim Alvefur <zash@zash.se>
parents:
8905
diff
changeset
|
46 if not archive.find then |
|
9554
cba6e6168d26
mod_mam: Upgrade case of invalid archive store driver to hard error
Kim Alvefur <zash@zash.se>
parents:
9553
diff
changeset
|
47 error("mod_"..(archive._provided_by or archive.name and "storage_"..archive.name).." does not support archiving\n" |
|
cba6e6168d26
mod_mam: Upgrade case of invalid archive store driver to hard error
Kim Alvefur <zash@zash.se>
parents:
9553
diff
changeset
|
48 .."See https://prosody.im/doc/storage and https://prosody.im/doc/archiving for more information"); |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 end |
|
8577
75d5eee6fcdf
mod_mam: Add an option for whether to include 'total' counts by default in queries
Kim Alvefur <zash@zash.se>
parents:
8576
diff
changeset
|
50 local use_total = module:get_option_boolean("mam_include_total", true); |
|
8132
6ddddfe05a74
mod_mam: Don't ask the storage backend to count all items when expiry is disabled
Kim Alvefur <zash@zash.se>
parents:
8047
diff
changeset
|
51 |
|
9879
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
52 function schedule_cleanup() |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
53 -- replaced by non-noop later if cleanup is enabled |
|
8230
154852646095
mod_mam: Use a FIFO queue for scheduling archive expiry
Kim Alvefur <zash@zash.se>
parents:
8207
diff
changeset
|
54 end |
|
154852646095
mod_mam: Use a FIFO queue for scheduling archive expiry
Kim Alvefur <zash@zash.se>
parents:
8207
diff
changeset
|
55 |
|
7836
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; |
|
8252
63e505578d4f
mod_mam: Also return the preferences on set
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8250
diff
changeset
|
60 if stanza.attr.type == "set" then |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 local new_prefs = stanza:get_child("prefs", xmlns_mam); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 local prefs = prefs_from_stanza(new_prefs); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 local ok, err = set_prefs(user, prefs); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 if not ok then |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error storing preferences: "..tostring(err))); |
|
8252
63e505578d4f
mod_mam: Also return the preferences on set
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8250
diff
changeset
|
66 return true; |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 end |
|
8538
3eb4cafb3b64
mod_mam: Implement option to enable MAM implicitly when client support is detected (#867)
Kim Alvefur <zash@zash.se>
parents:
8252
diff
changeset
|
69 local prefs = prefs_to_stanza(get_prefs(user, true)); |
|
8252
63e505578d4f
mod_mam: Also return the preferences on set
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8250
diff
changeset
|
70 local reply = st.reply(stanza):add_child(prefs); |
|
63e505578d4f
mod_mam: Also return the preferences on set
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8250
diff
changeset
|
71 origin.send(reply); |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 return true; |
|
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 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 local query_form = dataform { |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 { name = "FORM_TYPE"; type = "hidden"; value = xmlns_mam; }; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 { name = "with"; type = "jid-single"; }; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 { name = "start"; type = "text-single" }; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 { name = "end"; type = "text-single"; }; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 }; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 -- Serve form |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 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
|
84 local origin, stanza = event.origin, event.stanza; |
|
8538
3eb4cafb3b64
mod_mam: Implement option to enable MAM implicitly when client support is detected (#867)
Kim Alvefur <zash@zash.se>
parents:
8252
diff
changeset
|
85 get_prefs(origin.username, true); |
|
7904
c011cecad576
mod_mam: Add missing wrapping <query> element when returning the query form
Kim Alvefur <zash@zash.se>
parents:
7903
diff
changeset
|
86 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
|
87 return true; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 end); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 -- Handle archive queries |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 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
|
92 local origin, stanza = event.origin, event.stanza; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 local query = stanza.tags[1]; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 local qid = query.attr.queryid; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 |
|
8538
3eb4cafb3b64
mod_mam: Implement option to enable MAM implicitly when client support is detected (#867)
Kim Alvefur <zash@zash.se>
parents:
8252
diff
changeset
|
96 get_prefs(origin.username, true); |
|
7836
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 -- Search query parameters |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 local qwith, qstart, qend; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 local form = query:get_child("x", "jabber:x:data"); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 if form then |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 local err; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 form, err = query_form:data(form); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 if err then |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 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
|
106 return true; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 qwith, qstart, qend = form["with"], form["start"], form["end"]; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 qwith = qwith and jid_bare(qwith); -- dataforms does jidprep |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 if qstart or qend then -- Validate timestamps |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 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
|
114 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
|
115 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
|
116 return true; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 qstart, qend = vstart, vend; |
|
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 |
|
10072
128142990b2b
mod_mam: Include username that performed query
Kim Alvefur <zash@zash.se>
parents:
10071
diff
changeset
|
121 module:log("debug", "Archive query by %s id=%s with=%s when=%s...%s", |
|
128142990b2b
mod_mam: Include username that performed query
Kim Alvefur <zash@zash.se>
parents:
10071
diff
changeset
|
122 origin.username, |
|
10073
9137c9ce8b52
mod_mam: Use stanza id in log message as fallback if no query id
Kim Alvefur <zash@zash.se>
parents:
10072
diff
changeset
|
123 qid or stanza.attr.id, |
|
10071
b36e2631203d
mod_mam: Make log message more compact
Kim Alvefur <zash@zash.se>
parents:
10030
diff
changeset
|
124 qwith or "*", |
|
b36e2631203d
mod_mam: Make log message more compact
Kim Alvefur <zash@zash.se>
parents:
10030
diff
changeset
|
125 qstart and timestamp(qstart) or "", |
|
b36e2631203d
mod_mam: Make log message more compact
Kim Alvefur <zash@zash.se>
parents:
10030
diff
changeset
|
126 qend and timestamp(qend) or ""); |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 -- RSM stuff |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 local qset = rsm.get(query); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 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
|
131 local reverse = qset and qset.before or false; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 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
|
133 if type(before) ~= "string" then before = nil; end |
| 10077 | 134 if qset then |
| 135 module:log("debug", "Archive query id=%s rsm=%q", qid or stanza.attr.id, qset); | |
| 136 end | |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 -- Load all the data! |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 local data, err = archive:find(origin.username, { |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 start = qstart; ["end"] = qend; -- Time range |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 with = qwith; |
|
8904
faca839ddbbb
mod_mam: Handle edge-case of max=0 so that complete attr is set (fixes #1128)
Kim Alvefur <zash@zash.se>
parents:
8580
diff
changeset
|
142 limit = qmax == 0 and 0 or qmax + 1; |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 before = before; after = after; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 reverse = reverse; |
|
8172
66e32c34250b
mod_mam: Request a total count if no items are requested
Kim Alvefur <zash@zash.se>
parents:
8149
diff
changeset
|
145 total = use_total or qmax == 0; |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 }); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 if not data then |
|
10076
ee85cea08127
mod_mam: Log query failure reason
Kim Alvefur <zash@zash.se>
parents:
10075
diff
changeset
|
149 module:log("debug", "Archive query id=%s failed: %s", qid or stanza.attr.id, err); |
|
10021
4715c5d1eb69
mod_mam: Propagate item-not-found to client (fixes #1325)
Kim Alvefur <zash@zash.se>
parents:
9894
diff
changeset
|
150 if err == "item-not-found" then |
|
4715c5d1eb69
mod_mam: Propagate item-not-found to client (fixes #1325)
Kim Alvefur <zash@zash.se>
parents:
9894
diff
changeset
|
151 origin.send(st.error_reply(stanza, "modify", "item-not-found")); |
|
4715c5d1eb69
mod_mam: Propagate item-not-found to client (fixes #1325)
Kim Alvefur <zash@zash.se>
parents:
9894
diff
changeset
|
152 else |
|
4715c5d1eb69
mod_mam: Propagate item-not-found to client (fixes #1325)
Kim Alvefur <zash@zash.se>
parents:
9894
diff
changeset
|
153 origin.send(st.error_reply(stanza, "cancel", "internal-server-error")); |
|
4715c5d1eb69
mod_mam: Propagate item-not-found to client (fixes #1325)
Kim Alvefur <zash@zash.se>
parents:
9894
diff
changeset
|
154 end |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 return true; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 local total = tonumber(err); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 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
|
160 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 local results = {}; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 -- Wrap it in stuff and deliver |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 local first, last; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 local count = 0; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 local complete = "true"; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 for id, item, when in data do |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 count = count + 1; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 if count > qmax then |
|
8905
65d1a80d3565
mod_mam: Add coment on how 'complete' works
Kim Alvefur <zash@zash.se>
parents:
8904
diff
changeset
|
170 -- We requested qmax+1 items. If that many items are retrieved then |
|
65d1a80d3565
mod_mam: Add coment on how 'complete' works
Kim Alvefur <zash@zash.se>
parents:
8904
diff
changeset
|
171 -- there are more results to page through, so: |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 complete = nil; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 break; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 local fwd_st = st.message(msg_reply_attr) |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id }) |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 :tag("forwarded", { xmlns = xmlns_forward }) |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up(); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 if not is_stanza(item) then |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 item = st.deserialize(item); |
|
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 item.attr.xmlns = "jabber:client"; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 fwd_st:add_child(item); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 if not first then first = id; end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 last = id; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 if reverse then |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 results[count] = fwd_st; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 else |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 origin.send(fwd_st); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 if reverse then |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 for i = #results, 1, -1 do |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 origin.send(results[i]); |
|
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 first, last = last, first; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 |
|
7839
f3e1925f29c2
mod_mam: Update to XEP-0313 v0.5.1
Kim Alvefur <zash@zash.se>
parents:
7838
diff
changeset
|
203 origin.send(st.reply(stanza) |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 :tag("fin", { xmlns = xmlns_mam, queryid = qid, complete = complete }) |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 :add_child(rsm.generate { |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 first = first, last = last, count = total })); |
|
10078
44371850c6b0
mod_mam: Move final log message to end of query procedure
Kim Alvefur <zash@zash.se>
parents:
10077
diff
changeset
|
207 |
|
44371850c6b0
mod_mam: Move final log message to end of query procedure
Kim Alvefur <zash@zash.se>
parents:
10077
diff
changeset
|
208 -- That's all folks! |
|
10079
a36c731ed540
mod_mam: Report correct count of results for forward queries
Kim Alvefur <zash@zash.se>
parents:
10078
diff
changeset
|
209 module:log("debug", "Archive query id=%s completed, %d items returned", qid or stanza.attr.id, complete and count or count - 1); |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 return true; |
|
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 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 local function has_in_roster(user, who) |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
214 local roster = rm_load_roster(user, host); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
215 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
|
216 return roster[who]; |
|
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 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 local function shall_store(user, who) |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
220 -- TODO Cache this? |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 if not um.user_exists(user, host) then |
|
9879
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
222 module:log("debug", "%s@%s does not exist", user, host) |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 return false; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
225 local prefs = get_prefs(user); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 local rule = prefs[who]; |
|
10111
0f335815244f
plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents:
10079
diff
changeset
|
227 module:log("debug", "%s's rule for %s is %s", user, who, rule); |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 if rule ~= nil then |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 return rule; |
|
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 -- Below could be done by a metatable |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 local default = prefs[false]; |
|
10111
0f335815244f
plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents:
10079
diff
changeset
|
233 module:log("debug", "%s's default rule is %s", user, default); |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 if default == "roster" then |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 return has_in_roster(user, who); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 return default; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
239 |
|
8207
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
240 local function strip_stanza_id(stanza, user) |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
241 if stanza:get_child("stanza-id", xmlns_st_id) then |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
242 stanza = st.clone(stanza); |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
243 stanza:maptags(function (tag) |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
244 if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id then |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
245 local by_user, by_host, res = jid_prepped_split(tag.attr.by); |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
246 if not res and by_host == host and by_user == user then |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
247 return nil; |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
248 end |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
249 end |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
250 return tag; |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
251 end); |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
252 end |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
253 return stanza; |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
254 end |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
255 |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 -- Handle messages |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 local function message_handler(event, c2s) |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 local origin, stanza = event.origin, event.stanza; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 local log = c2s and origin.log or module._log; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
260 local orig_type = stanza.attr.type or "normal"; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 local orig_from = stanza.attr.from; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
262 local orig_to = stanza.attr.to or orig_from; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 -- 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
|
264 |
|
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
|
265 -- 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
|
266 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
|
267 -- 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
|
268 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
|
269 |
|
7845
eeb22f912577
mod_mam: Filter out spoofed XEP-0359 tags
Kim Alvefur <zash@zash.se>
parents:
7844
diff
changeset
|
270 -- Filter out <stanza-id> that claim to be from us |
|
8207
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
271 event.stanza = strip_stanza_id(stanza, store_user); |
|
7845
eeb22f912577
mod_mam: Filter out spoofed XEP-0359 tags
Kim Alvefur <zash@zash.se>
parents:
7844
diff
changeset
|
272 |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 -- 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
|
274 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
|
275 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
|
276 return; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 end |
|
7842
9332b43931f5
mod_mam: Add some comments explaining archive expiry
Kim Alvefur <zash@zash.se>
parents:
7841
diff
changeset
|
278 |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 -- or if hints suggest we shouldn't |
|
7843
04b09fd144eb
mod_mam: Alter hints processing
Kim Alvefur <zash@zash.se>
parents:
7842
diff
changeset
|
280 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
|
281 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
|
282 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
|
283 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
|
284 return; |
|
04b09fd144eb
mod_mam: Alter hints processing
Kim Alvefur <zash@zash.se>
parents:
7842
diff
changeset
|
285 end |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
286 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
287 |
|
8193
bb0118e46c45
mod_mam: Clone stanzas before mutating (thanks waqas) (fixes #961)
Kim Alvefur <zash@zash.se>
parents:
8172
diff
changeset
|
288 local clone_for_storage; |
|
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
|
289 if not strip_tags:empty() then |
|
8193
bb0118e46c45
mod_mam: Clone stanzas before mutating (thanks waqas) (fixes #961)
Kim Alvefur <zash@zash.se>
parents:
8172
diff
changeset
|
290 clone_for_storage = st.clone(stanza); |
|
bb0118e46c45
mod_mam: Clone stanzas before mutating (thanks waqas) (fixes #961)
Kim Alvefur <zash@zash.se>
parents:
8172
diff
changeset
|
291 clone_for_storage:maptags(function (tag) |
|
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
|
292 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
|
293 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
|
294 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
|
295 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
|
296 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
|
297 end); |
|
8193
bb0118e46c45
mod_mam: Clone stanzas before mutating (thanks waqas) (fixes #961)
Kim Alvefur <zash@zash.se>
parents:
8172
diff
changeset
|
298 if #clone_for_storage.tags == 0 then |
|
8250
9ea5ea53744b
mod_mam: Log a message when not archiving because it only had ignored tags
Kim Alvefur <zash@zash.se>
parents:
8231
diff
changeset
|
299 log("debug", "Not archiving stanza: %s (empty when stripped)", stanza:top_tag()); |
|
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
|
300 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
|
301 end |
|
8193
bb0118e46c45
mod_mam: Clone stanzas before mutating (thanks waqas) (fixes #961)
Kim Alvefur <zash@zash.se>
parents:
8172
diff
changeset
|
302 else |
|
bb0118e46c45
mod_mam: Clone stanzas before mutating (thanks waqas) (fixes #961)
Kim Alvefur <zash@zash.se>
parents:
8172
diff
changeset
|
303 clone_for_storage = stanza; |
|
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
|
304 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
|
305 |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
306 -- Check with the users preferences |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
307 if shall_store(store_user, with) then |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
308 log("debug", "Archiving stanza: %s", stanza:top_tag()); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
309 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
310 -- And stash it |
|
9886
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
311 local time = time_now(); |
|
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
312 local ok, err = archive:append(store_user, nil, clone_for_storage, time, with); |
|
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
313 if not ok and err == "quota-limit" then |
|
9894
8747ccf0008c
mod_mam: On quota hit, separately delete by time and by item count
Kim Alvefur <zash@zash.se>
parents:
9886
diff
changeset
|
314 if type(cleanup_after) == "number" then |
|
8747ccf0008c
mod_mam: On quota hit, separately delete by time and by item count
Kim Alvefur <zash@zash.se>
parents:
9886
diff
changeset
|
315 module:log("debug", "User '%s' over quota, cleaning archive", store_user); |
|
8747ccf0008c
mod_mam: On quota hit, separately delete by time and by item count
Kim Alvefur <zash@zash.se>
parents:
9886
diff
changeset
|
316 local cleaned = archive:delete(store_user, { |
|
8747ccf0008c
mod_mam: On quota hit, separately delete by time and by item count
Kim Alvefur <zash@zash.se>
parents:
9886
diff
changeset
|
317 ["end"] = (os.time() - cleanup_after); |
|
8747ccf0008c
mod_mam: On quota hit, separately delete by time and by item count
Kim Alvefur <zash@zash.se>
parents:
9886
diff
changeset
|
318 }); |
|
8747ccf0008c
mod_mam: On quota hit, separately delete by time and by item count
Kim Alvefur <zash@zash.se>
parents:
9886
diff
changeset
|
319 if cleaned then |
|
8747ccf0008c
mod_mam: On quota hit, separately delete by time and by item count
Kim Alvefur <zash@zash.se>
parents:
9886
diff
changeset
|
320 ok, err = archive:append(store_user, nil, clone_for_storage, time, with); |
|
8747ccf0008c
mod_mam: On quota hit, separately delete by time and by item count
Kim Alvefur <zash@zash.se>
parents:
9886
diff
changeset
|
321 end |
|
8747ccf0008c
mod_mam: On quota hit, separately delete by time and by item count
Kim Alvefur <zash@zash.se>
parents:
9886
diff
changeset
|
322 end |
|
8747ccf0008c
mod_mam: On quota hit, separately delete by time and by item count
Kim Alvefur <zash@zash.se>
parents:
9886
diff
changeset
|
323 if not ok and (archive.caps and archive.caps.truncate) then |
|
8747ccf0008c
mod_mam: On quota hit, separately delete by time and by item count
Kim Alvefur <zash@zash.se>
parents:
9886
diff
changeset
|
324 module:log("debug", "User '%s' over quota, truncating archive", store_user); |
|
9886
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
325 local truncated = archive:delete(store_user, { |
|
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
326 truncate = archive_item_limit - 1; |
|
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
327 }); |
|
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
328 if truncated then |
|
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
329 ok, err = archive:append(store_user, nil, clone_for_storage, time, with); |
|
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
330 end |
|
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
331 end |
|
710a116341cd
mod_mam: Trim archive when quota has been exceeded
Kim Alvefur <zash@zash.se>
parents:
9882
diff
changeset
|
332 end |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
333 if ok then |
|
8193
bb0118e46c45
mod_mam: Clone stanzas before mutating (thanks waqas) (fixes #961)
Kim Alvefur <zash@zash.se>
parents:
8172
diff
changeset
|
334 local clone_for_other_handlers = st.clone(stanza); |
|
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
|
335 local id = ok; |
|
8193
bb0118e46c45
mod_mam: Clone stanzas before mutating (thanks waqas) (fixes #961)
Kim Alvefur <zash@zash.se>
parents:
8172
diff
changeset
|
336 clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up(); |
|
bb0118e46c45
mod_mam: Clone stanzas before mutating (thanks waqas) (fixes #961)
Kim Alvefur <zash@zash.se>
parents:
8172
diff
changeset
|
337 event.stanza = clone_for_other_handlers; |
|
8230
154852646095
mod_mam: Use a FIFO queue for scheduling archive expiry
Kim Alvefur <zash@zash.se>
parents:
8207
diff
changeset
|
338 schedule_cleanup(store_user); |
|
8193
bb0118e46c45
mod_mam: Clone stanzas before mutating (thanks waqas) (fixes #961)
Kim Alvefur <zash@zash.se>
parents:
8172
diff
changeset
|
339 module:fire_event("archive-message-added", { origin = origin, stanza = clone_for_storage, for_user = store_user, id = id }); |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
340 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
341 else |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
342 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
|
343 end |
|
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 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
346 local function c2s_message_handler(event) |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
347 return message_handler(event, true); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
348 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
349 |
|
8207
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
350 -- Filter out <stanza-id> before the message leaves the server to prevent privacy leak. |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
351 local function strip_stanza_id_after_other_events(event) |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
352 event.stanza = strip_stanza_id(event.stanza, event.origin.username); |
|
7902
92b70a921acb
mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents:
7901
diff
changeset
|
353 end |
|
92b70a921acb
mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents:
7901
diff
changeset
|
354 |
|
8207
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
355 module:hook("pre-message/bare", strip_stanza_id_after_other_events, -1); |
|
8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
Kim Alvefur <zash@zash.se>
parents:
8206
diff
changeset
|
356 module:hook("pre-message/full", strip_stanza_id_after_other_events, -1); |
|
7902
92b70a921acb
mod_mam: Strip stanza-id tags after carbons
Kim Alvefur <zash@zash.se>
parents:
7901
diff
changeset
|
357 |
|
8575
5040c8ce32dd
Backed out changeset 97a094fdf101, interferes with 6ddddfe05a74
Kim Alvefur <zash@zash.se>
parents:
8538
diff
changeset
|
358 if cleanup_after ~= "never" then |
|
9879
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
359 local cleanup_storage = module:open_store("archive_cleanup"); |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
360 local cleanup_map = module:open_store("archive_cleanup", "map"); |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
361 |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
362 local day = 86400; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
363 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
|
364 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
|
365 if not n then |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
366 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
|
367 return false; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
368 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
369 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
370 cleanup_after = tonumber(n) * ( multipliers[m] or 1 ); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
371 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
372 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
|
373 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
374 if not archive.delete then |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
375 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
|
376 return false; |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
377 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
378 |
|
9879
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
379 -- For each day, store a set of users that have new messages. To expire |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
380 -- messages, we collect the union of sets of users from dates that fall |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
381 -- outside the cleanup range. |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
382 |
|
10028
79ba2d709e72
mod_mam: Cache last date that archive owner has messages to reduce writes (fixes #1368)
Kim Alvefur <zash@zash.se>
parents:
9879
diff
changeset
|
383 local last_date = require "util.cache".new(module:get_option_number("archive_cleanup_date_cache_size", 1000)); |
|
9879
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
384 function schedule_cleanup(username, date) |
|
10028
79ba2d709e72
mod_mam: Cache last date that archive owner has messages to reduce writes (fixes #1368)
Kim Alvefur <zash@zash.se>
parents:
9879
diff
changeset
|
385 date = date or datestamp(); |
|
79ba2d709e72
mod_mam: Cache last date that archive owner has messages to reduce writes (fixes #1368)
Kim Alvefur <zash@zash.se>
parents:
9879
diff
changeset
|
386 if last_date:get(username) == date then return end |
|
79ba2d709e72
mod_mam: Cache last date that archive owner has messages to reduce writes (fixes #1368)
Kim Alvefur <zash@zash.se>
parents:
9879
diff
changeset
|
387 local ok = cleanup_map:set(date, username, true); |
|
79ba2d709e72
mod_mam: Cache last date that archive owner has messages to reduce writes (fixes #1368)
Kim Alvefur <zash@zash.se>
parents:
9879
diff
changeset
|
388 if ok then |
|
79ba2d709e72
mod_mam: Cache last date that archive owner has messages to reduce writes (fixes #1368)
Kim Alvefur <zash@zash.se>
parents:
9879
diff
changeset
|
389 last_date:set(username, date); |
|
79ba2d709e72
mod_mam: Cache last date that archive owner has messages to reduce writes (fixes #1368)
Kim Alvefur <zash@zash.se>
parents:
9879
diff
changeset
|
390 end |
|
9879
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
391 end |
|
9757
03ed7f10d8da
mod_mam: Measure how long it takes to run the message expiry job job
Kim Alvefur <zash@zash.se>
parents:
9752
diff
changeset
|
392 local cleanup_time = module:measure("cleanup", "times"); |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
393 |
|
9879
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
394 cleanup_runner = require "util.async".runner(function () |
|
9757
03ed7f10d8da
mod_mam: Measure how long it takes to run the message expiry job job
Kim Alvefur <zash@zash.se>
parents:
9752
diff
changeset
|
395 local cleanup_done = cleanup_time(); |
|
9879
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
396 local users = {}; |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
397 local cut_off = datestamp(os.time() - cleanup_after); |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
398 for date in cleanup_storage:users() do |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
399 if date <= cut_off then |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
400 module:log("debug", "Messages from %q should be expired", date); |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
401 local messages_this_day = cleanup_storage:get(date); |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
402 if messages_this_day then |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
403 for user in pairs(messages_this_day) do |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
404 users[user] = true; |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
405 end |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
406 if date < cut_off then |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
407 -- Messages from the same day as the cut-off might not have expired yet, |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
408 -- but all earlier will have, so clear storage for those days. |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
409 cleanup_storage:set(date, nil); |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
410 end |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
411 end |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
412 end |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
413 end |
|
9879
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
414 local sum, num_users = 0, 0; |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
415 for user in pairs(users) do |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
416 local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; }) |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
417 if ok then |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
418 num_users = num_users + 1; |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
419 sum = sum + (tonumber(ok) or 0); |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
420 end |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
421 end |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
422 module:log("info", "Deleted %d expired messages for %d users", sum, num_users); |
|
9757
03ed7f10d8da
mod_mam: Measure how long it takes to run the message expiry job job
Kim Alvefur <zash@zash.se>
parents:
9752
diff
changeset
|
423 cleanup_done(); |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
424 end); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
425 |
|
9879
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
426 cleanup_task = module:add_timer(1, function () |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
427 cleanup_runner:run(true); |
|
ddc07fb8dcd4
mod_mam: Perform message expiry based on building an index by date (backport of 39ee70fbb009 from trunk)
Kim Alvefur <zash@zash.se>
parents:
9555
diff
changeset
|
428 return cleanup_interval; |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
429 end); |
|
8132
6ddddfe05a74
mod_mam: Don't ask the storage backend to count all items when expiry is disabled
Kim Alvefur <zash@zash.se>
parents:
8047
diff
changeset
|
430 else |
|
8576
07ff7b8b702b
mod_mam: Log a debug message if archive expiry has been disabled
Kim Alvefur <zash@zash.se>
parents:
8575
diff
changeset
|
431 module:log("debug", "Archive expiry disabled"); |
|
8132
6ddddfe05a74
mod_mam: Don't ask the storage backend to count all items when expiry is disabled
Kim Alvefur <zash@zash.se>
parents:
8047
diff
changeset
|
432 -- Don't ask the backend to count the potentially unbounded number of items, |
|
6ddddfe05a74
mod_mam: Don't ask the storage backend to count all items when expiry is disabled
Kim Alvefur <zash@zash.se>
parents:
8047
diff
changeset
|
433 -- it'll get slow. |
|
8577
75d5eee6fcdf
mod_mam: Add an option for whether to include 'total' counts by default in queries
Kim Alvefur <zash@zash.se>
parents:
8576
diff
changeset
|
434 use_total = module:get_option_boolean("mam_include_total", false); |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
435 end |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
436 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
437 -- Stanzas sent by local clients |
|
7840
92b6aa3ea2ce
mod_mam: Decrease priority to zero
Kim Alvefur <zash@zash.se>
parents:
7839
diff
changeset
|
438 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
|
439 module:hook("pre-message/full", c2s_message_handler, 0); |
| 7905 | 440 -- Stanzas to local clients |
|
7840
92b6aa3ea2ce
mod_mam: Decrease priority to zero
Kim Alvefur <zash@zash.se>
parents:
7839
diff
changeset
|
441 module:hook("message/bare", message_handler, 0); |
|
92b6aa3ea2ce
mod_mam: Decrease priority to zero
Kim Alvefur <zash@zash.se>
parents:
7839
diff
changeset
|
442 module:hook("message/full", message_handler, 0); |
|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
443 |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
444 module:hook("account-disco-info", function(event) |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
445 (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
|
446 (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
|
447 end); |
|
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
448 |