Software /
code /
prosody
Annotate
plugins/mod_muc_mam.lua @ 9954:36280801ddf1
util.poll: Minimize scope of methods
File scope is enough
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 13 Apr 2019 23:55:34 +0200 |
parent | 9882:18f025b3987d |
child | 10000:189b00a782bf |
rev | line source |
---|---|
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- XEP-0313: Message Archive Management for Prosody MUC |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 -- Copyright (C) 2011-2017 Kim Alvefur |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- This file is MIT/X11 licensed. |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 if module:get_host_type() ~= "component" then |
9872
e1d68f32ce29
mod_muc_mam: Set error status if loaded on incorrect host type
Matthew Wild <mwild1@gmail.com>
parents:
9845
diff
changeset
|
7 module:log_status("error", "mod_%s should be loaded only on a MUC component, not normal hosts", module.name); |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 return; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local xmlns_mam = "urn:xmpp:mam:2"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local xmlns_delay = "urn:xmpp:delay"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local xmlns_forward = "urn:xmpp:forward:0"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 local xmlns_st_id = "urn:xmpp:sid:0"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local xmlns_muc_user = "http://jabber.org/protocol/muc#user"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local muc_form_enable = "muc#roomconfig_enablearchiving" |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 local st = require "util.stanza"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local rsm = require "util.rsm"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 local jid_bare = require "util.jid".bare; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 local jid_split = require "util.jid".split; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 local jid_prep = require "util.jid".prep; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 local dataform = require "util.dataforms".new; |
9844
3de80fc511ab
mod_muc_mam: Validate that the FORM_TYPE of a query is as expected
Kim Alvefur <zash@zash.se>
parents:
9552
diff
changeset
|
24 local get_form_type = require "util.dataforms".get_type; |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 local mod_muc = module:depends"muc"; |
8723
f3bdb20214ab
mod_muc_mam: Remove 0.10 compat code
Kim Alvefur <zash@zash.se>
parents:
8722
diff
changeset
|
27 local get_room_from_jid = mod_muc.get_room_from_jid; |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 local is_stanza = st.is_stanza; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 local tostring = tostring; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 local time_now = os.time; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 local m_min = math.min; |
9880
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
33 local timestamp, timestamp_parse, datestamp = import( "util.datetime", "datetime", "parse", "date"); |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 local default_history_length = 20; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 local max_history_length = module:get_option_number("max_history_messages", math.huge); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 local function get_historylength(room) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 return math.min(room._data.history_length or default_history_length, max_history_length); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
9880
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
43 function schedule_cleanup() |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
44 -- replaced by non-noop later if cleanup is enabled |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
45 end |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
46 |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 local log_all_rooms = module:get_option_boolean("muc_log_all_rooms", false); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 local log_by_default = module:get_option_boolean("muc_log_by_default", true); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 local archive_store = "muc_log"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 local archive = module:open_store(archive_store, "archive"); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 if archive.name == "null" or not archive.find then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 if not archive.find then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 module:log("error", "Attempt to open archive storage returned a driver without archive API support"); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 module:log("error", "mod_%s does not support archiving", |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 archive._provided_by or archive.name and "storage_"..archive.name.."(?)" or "<unknown>"); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 else |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 module:log("error", "Attempt to open archive storage returned null driver"); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 module:log("info", "See https://prosody.im/doc/storage and https://prosody.im/doc/archiving for more information"); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 return false; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 local function archiving_enabled(room) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 if log_all_rooms then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 return true; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 local enabled = room._data.archiving; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 if enabled == nil then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 return log_by_default; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 return enabled; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 if not log_all_rooms then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 module:hook("muc-config-form", function(event) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 local room, form = event.room, event.form; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 table.insert(form, |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 { |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 name = muc_form_enable, |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 type = "boolean", |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 label = "Enable archiving?", |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 value = archiving_enabled(room), |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 } |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 ); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 end); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 |
8723
f3bdb20214ab
mod_muc_mam: Remove 0.10 compat code
Kim Alvefur <zash@zash.se>
parents:
8722
diff
changeset
|
89 module:hook("muc-config-submitted/"..muc_form_enable, function(event) |
f3bdb20214ab
mod_muc_mam: Remove 0.10 compat code
Kim Alvefur <zash@zash.se>
parents:
8722
diff
changeset
|
90 event.room._data.archiving = event.value; |
8832
a9c3b15b9d37
mod_muc_mam: Set status code 170/171 on archiving configuration change
Kim Alvefur <zash@zash.se>
parents:
8786
diff
changeset
|
91 event.status_codes[event.value and "170" or "171"] = true; |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 end); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 -- Note: We ignore the 'with' field as this is internally used for stanza types |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 local query_form = dataform { |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 { name = "FORM_TYPE"; type = "hidden"; value = xmlns_mam; }; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 { name = "with"; type = "jid-single"; }; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 { name = "start"; type = "text-single" }; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 { name = "end"; type = "text-single"; }; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 }; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 -- Serve form |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 module:hook("iq-get/bare/"..xmlns_mam..":query", function(event) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 local origin, stanza = event.origin, event.stanza; |
9552
87e25d352de3
mod_muc_mam: Fix missing form container element
Kim Alvefur <zash@zash.se>
parents:
9001
diff
changeset
|
106 origin.send(st.reply(stanza):tag("query", { xmlns = xmlns_mam }):add_child(query_form:form())); |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 return true; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 end); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 -- Handle archive queries |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 module:hook("iq-set/bare/"..xmlns_mam..":query", function(event) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 local origin, stanza = event.origin, event.stanza; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 local room_jid = stanza.attr.to; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 local room_node = jid_split(room_jid); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 local orig_from = stanza.attr.from; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 local query = stanza.tags[1]; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 local room = get_room_from_jid(room_jid); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 if not room then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 origin.send(st.error_reply(stanza, "cancel", "item-not-found")) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 return true; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 local from = jid_bare(orig_from); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 -- Banned or not a member of a members-only room? |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 local from_affiliation = room:get_affiliation(from); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 if from_affiliation == "outcast" -- banned |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 or room:get_members_only() and not from_affiliation then -- members-only, not a member |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 origin.send(st.error_reply(stanza, "auth", "forbidden")) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 return true; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 local qid = query.attr.queryid; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 -- Search query parameters |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 local qstart, qend; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 local form = query:get_child("x", "jabber:x:data"); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 if form then |
9844
3de80fc511ab
mod_muc_mam: Validate that the FORM_TYPE of a query is as expected
Kim Alvefur <zash@zash.se>
parents:
9552
diff
changeset
|
139 local form_type, err = get_form_type(form); |
3de80fc511ab
mod_muc_mam: Validate that the FORM_TYPE of a query is as expected
Kim Alvefur <zash@zash.se>
parents:
9552
diff
changeset
|
140 if form_type ~= xmlns_mam then |
3de80fc511ab
mod_muc_mam: Validate that the FORM_TYPE of a query is as expected
Kim Alvefur <zash@zash.se>
parents:
9552
diff
changeset
|
141 origin.send(st.error_reply(stanza, "modify", "bad-request", "Unexpected FORM_TYPE, expected '"..xmlns_mam.."'")); |
3de80fc511ab
mod_muc_mam: Validate that the FORM_TYPE of a query is as expected
Kim Alvefur <zash@zash.se>
parents:
9552
diff
changeset
|
142 return true; |
3de80fc511ab
mod_muc_mam: Validate that the FORM_TYPE of a query is as expected
Kim Alvefur <zash@zash.se>
parents:
9552
diff
changeset
|
143 end |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 form, err = query_form:data(form); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 if err then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 origin.send(st.error_reply(stanza, "modify", "bad-request", select(2, next(err)))); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 return true; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 qstart, qend = form["start"], form["end"]; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 if qstart or qend then -- Validate timestamps |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 local vstart, vend = (qstart and timestamp_parse(qstart)), (qend and timestamp_parse(qend)) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 if (qstart and not vstart) or (qend and not vend) then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp")) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 return true; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 qstart, qend = vstart, vend; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 module:log("debug", "Archive query id %s from %s until %s)", |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 tostring(qid), |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 qstart and timestamp(qstart) or "the dawn of time", |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 qend and timestamp(qend) or "now"); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 -- RSM stuff |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 local qset = rsm.get(query); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 local qmax = m_min(qset and qset.max or default_max_items, max_max_items); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 local reverse = qset and qset.before or false; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 local before, after = qset and qset.before, qset and qset.after; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 if type(before) ~= "string" then before = nil; end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 -- Load all the data! |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 local data, err = archive:find(room_node, { |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 start = qstart; ["end"] = qend; -- Time range |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 limit = qmax + 1; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 before = before; after = after; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 reverse = reverse; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 with = "message<groupchat"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 }); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 if not data then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 origin.send(st.error_reply(stanza, "cancel", "internal-server-error")); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 return true; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 local total = tonumber(err); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 local msg_reply_attr = { to = stanza.attr.from, from = stanza.attr.to }; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 local results = {}; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 -- Wrap it in stuff and deliver |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 local first, last; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 local count = 0; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 local complete = "true"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 for id, item, when in data do |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 count = count + 1; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 if count > qmax then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 complete = nil; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 break; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 local fwd_st = st.message(msg_reply_attr) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id }) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 :tag("forwarded", { xmlns = xmlns_forward }) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up(); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 -- Strip <x> tag, containing the original senders JID, unless the room makes this public |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 if room:get_whois() ~= "anyone" then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 item:maptags(function (tag) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 if tag.name == "x" and tag.attr.xmlns == xmlns_muc_user then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
212 return nil; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
214 return tag; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
215 end); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
216 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 if not is_stanza(item) then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 item = st.deserialize(item); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 end |
9841
a44f562e01a5
mod_muc_mam: Strip the stanza 'to' attribute (fixes #1259)
Kim Alvefur <zash@zash.se>
parents:
9552
diff
changeset
|
220 item.attr.to = nil; |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 item.attr.xmlns = "jabber:client"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
222 fwd_st:add_child(item); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 if not first then first = id; end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
225 last = id; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
227 if reverse then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 results[count] = fwd_st; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 else |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
230 origin.send(fwd_st); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
231 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 if reverse then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 for i = #results, 1, -1 do |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 origin.send(results[i]); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 first, last = last, first; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
239 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
241 -- That's all folks! |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 module:log("debug", "Archive query %s completed", tostring(qid)); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
244 origin.send(st.reply(stanza) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 :tag("fin", { xmlns = xmlns_mam, queryid = qid, complete = complete }) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
246 :add_child(rsm.generate { |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 first = first, last = last, count = total })); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 return true; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
249 end); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
250 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 module:hook("muc-get-history", function (event) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 local room = event.room; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 if not archiving_enabled(room) then return end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 local room_jid = room.jid; |
8783
79133eca0656
mod_muc_mam: Tweak calculation of max history to retrieve
Kim Alvefur <zash@zash.se>
parents:
8725
diff
changeset
|
255 local maxstanzas = event.maxstanzas; |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 local maxchars = event.maxchars; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 local since = event.since; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 local to = event.to; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 |
8784
64b78e3deb96
mod_muc_mam: Skip fetching history if no history was requested
Kim Alvefur <zash@zash.se>
parents:
8783
diff
changeset
|
260 if maxstanzas == 0 or maxchars == 0 then |
64b78e3deb96
mod_muc_mam: Skip fetching history if no history was requested
Kim Alvefur <zash@zash.se>
parents:
8783
diff
changeset
|
261 return -- No history requested |
64b78e3deb96
mod_muc_mam: Skip fetching history if no history was requested
Kim Alvefur <zash@zash.se>
parents:
8783
diff
changeset
|
262 end |
64b78e3deb96
mod_muc_mam: Skip fetching history if no history was requested
Kim Alvefur <zash@zash.se>
parents:
8783
diff
changeset
|
263 |
8783
79133eca0656
mod_muc_mam: Tweak calculation of max history to retrieve
Kim Alvefur <zash@zash.se>
parents:
8725
diff
changeset
|
264 if not maxstanzas or maxstanzas > get_historylength(room) then |
79133eca0656
mod_muc_mam: Tweak calculation of max history to retrieve
Kim Alvefur <zash@zash.se>
parents:
8725
diff
changeset
|
265 maxstanzas = get_historylength(room); |
79133eca0656
mod_muc_mam: Tweak calculation of max history to retrieve
Kim Alvefur <zash@zash.se>
parents:
8725
diff
changeset
|
266 end |
79133eca0656
mod_muc_mam: Tweak calculation of max history to retrieve
Kim Alvefur <zash@zash.se>
parents:
8725
diff
changeset
|
267 |
8785
82b889608503
mod_muc_mam: Skip fetching history if built-in recent history is enough
Kim Alvefur <zash@zash.se>
parents:
8784
diff
changeset
|
268 if room._history and #room._history >= maxstanzas then |
82b889608503
mod_muc_mam: Skip fetching history if built-in recent history is enough
Kim Alvefur <zash@zash.se>
parents:
8784
diff
changeset
|
269 return -- It can deal with this itself |
82b889608503
mod_muc_mam: Skip fetching history if built-in recent history is enough
Kim Alvefur <zash@zash.se>
parents:
8784
diff
changeset
|
270 end |
82b889608503
mod_muc_mam: Skip fetching history if built-in recent history is enough
Kim Alvefur <zash@zash.se>
parents:
8784
diff
changeset
|
271 |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
272 -- Load all the data! |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 local query = { |
8783
79133eca0656
mod_muc_mam: Tweak calculation of max history to retrieve
Kim Alvefur <zash@zash.se>
parents:
8725
diff
changeset
|
274 limit = maxstanzas; |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
275 start = since; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 reverse = true; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 with = "message<groupchat"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 } |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 local data, err = archive:find(jid_split(room_jid), query); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 if not data then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
282 module:log("error", "Could not fetch history: %s", tostring(err)); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
283 return |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
284 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
285 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
286 local history, i = {}, 1; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
287 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
288 for id, item, when in data do |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
289 item.attr.to = to; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
290 item:tag("delay", { xmlns = "urn:xmpp:delay", from = room_jid, stamp = timestamp(when) }):up(); -- XEP-0203 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
291 item:tag("stanza-id", { xmlns = xmlns_st_id, by = room_jid, id = id }):up(); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
292 if room:get_whois() ~= "anyone" then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
293 item:maptags(function (tag) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
294 if tag.name == "x" and tag.attr.xmlns == xmlns_muc_user then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
295 return nil; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
296 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
297 return tag; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
298 end); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
299 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
300 if maxchars then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
301 local chars = #tostring(item); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
302 if maxchars - chars < 0 then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
303 break |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
304 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
305 maxchars = maxchars - chars; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
306 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
307 history[i], i = item, i+1; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
308 -- module:log("debug", tostring(item)); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
309 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
310 function event.next_stanza() |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
311 i = i - 1; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
312 return history[i]; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
313 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
314 return true; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
315 end, 1); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
316 |
8786
adffadd88ff0
mod_muc_mam: Have stanza-id filtering over all groupchat messages, not just ones considered for history
Kim Alvefur <zash@zash.se>
parents:
8785
diff
changeset
|
317 module:hook("muc-broadcast-messages", function (event) |
adffadd88ff0
mod_muc_mam: Have stanza-id filtering over all groupchat messages, not just ones considered for history
Kim Alvefur <zash@zash.se>
parents:
8785
diff
changeset
|
318 local room, stanza = event.room, event.stanza; |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
319 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
320 -- Filter out <stanza-id> that claim to be from us |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
321 stanza:maptags(function (tag) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
322 if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id |
8786
adffadd88ff0
mod_muc_mam: Have stanza-id filtering over all groupchat messages, not just ones considered for history
Kim Alvefur <zash@zash.se>
parents:
8785
diff
changeset
|
323 and jid_prep(tag.attr.by) == room.jid then |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
324 return nil; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
325 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
326 if tag.name == "x" and tag.attr.xmlns == xmlns_muc_user then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
327 return nil; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
328 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
329 return tag; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
330 end); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
331 |
8786
adffadd88ff0
mod_muc_mam: Have stanza-id filtering over all groupchat messages, not just ones considered for history
Kim Alvefur <zash@zash.se>
parents:
8785
diff
changeset
|
332 end, 0); |
adffadd88ff0
mod_muc_mam: Have stanza-id filtering over all groupchat messages, not just ones considered for history
Kim Alvefur <zash@zash.se>
parents:
8785
diff
changeset
|
333 |
adffadd88ff0
mod_muc_mam: Have stanza-id filtering over all groupchat messages, not just ones considered for history
Kim Alvefur <zash@zash.se>
parents:
8785
diff
changeset
|
334 -- Handle messages |
adffadd88ff0
mod_muc_mam: Have stanza-id filtering over all groupchat messages, not just ones considered for history
Kim Alvefur <zash@zash.se>
parents:
8785
diff
changeset
|
335 local function save_to_history(self, stanza) |
adffadd88ff0
mod_muc_mam: Have stanza-id filtering over all groupchat messages, not just ones considered for history
Kim Alvefur <zash@zash.se>
parents:
8785
diff
changeset
|
336 local room_node, room_host = jid_split(self.jid); |
adffadd88ff0
mod_muc_mam: Have stanza-id filtering over all groupchat messages, not just ones considered for history
Kim Alvefur <zash@zash.se>
parents:
8785
diff
changeset
|
337 |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
338 local stored_stanza = stanza; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
339 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
340 if stanza.name == "message" and self:get_whois() == "anyone" then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
341 stored_stanza = st.clone(stanza); |
9841
a44f562e01a5
mod_muc_mam: Strip the stanza 'to' attribute (fixes #1259)
Kim Alvefur <zash@zash.se>
parents:
9552
diff
changeset
|
342 stored_stanza.attr.to = nil; |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
343 local actor = jid_bare(self._occupants[stanza.attr.from].jid); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
344 local affiliation = self:get_affiliation(actor) or "none"; |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
345 local role = self:get_role(actor) or self:get_default_role(affiliation); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
346 stored_stanza:add_direct_child(st.stanza("x", { xmlns = xmlns_muc_user }) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
347 :tag("item", { affiliation = affiliation; role = role; jid = actor })); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
348 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
349 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
350 -- Policy check |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
351 if not archiving_enabled(self) then return end -- Don't log |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
352 |
9843
17060708d0eb
mod_muc_mam: Add comment about the tricks done with the 'with' field
Kim Alvefur <zash@zash.se>
parents:
9842
diff
changeset
|
353 -- Save the type in the 'with' field, allows storing presence without conflicts |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
354 local with = stanza.name |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
355 if stanza.attr.type then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
356 with = with .. "<" .. stanza.attr.type |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
357 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
358 |
9842
6f39be2e0be5
mod_muc_mam: Move a comment to the line it describes
Kim Alvefur <zash@zash.se>
parents:
9841
diff
changeset
|
359 -- And stash it |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
360 local id = archive:append(room_node, nil, stored_stanza, time_now(), with); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
361 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
362 if id then |
9880
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
363 schedule_cleanup(room_node); |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
364 stanza:add_direct_child(st.stanza("stanza-id", { xmlns = xmlns_st_id, by = self.jid, id = id })); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
365 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
366 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
367 |
8724
cfcc78c50905
mod_muc_mam: Let muc/history.lib decide which messages to store
Kim Alvefur <zash@zash.se>
parents:
8723
diff
changeset
|
368 module:hook("muc-add-history", function (event) |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
369 local room, stanza = event.room, event.stanza; |
8724
cfcc78c50905
mod_muc_mam: Let muc/history.lib decide which messages to store
Kim Alvefur <zash@zash.se>
parents:
8723
diff
changeset
|
370 save_to_history(room, stanza); |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
371 end); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
372 |
9001
23070882f3d4
mod_muc_mam: Disable presence logging by default
Kim Alvefur <zash@zash.se>
parents:
8832
diff
changeset
|
373 if module:get_option_boolean("muc_log_presences", false) then |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
374 module:hook("muc-occupant-joined", function (event) |
8725
ced09d7d7c96
mod_muc_mam: Save the MUC <x> on recorded join stanzas
Kim Alvefur <zash@zash.se>
parents:
8724
diff
changeset
|
375 save_to_history(event.room, st.stanza("presence", { from = event.nick }):tag("x", { xmlns = "http://jabber.org/protocol/muc" })); |
8722
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
376 end); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
377 module:hook("muc-occupant-left", function (event) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
378 save_to_history(event.room, st.stanza("presence", { type = "unavailable", from = event.nick })); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
379 end); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
380 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
381 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
382 if not archive.delete then |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
383 module:log("warn", "Storage driver %s does not support deletion", archive._provided_by); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
384 module:log("warn", "Archived message will persist after a room has been destroyed"); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
385 else |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
386 module:hook("muc-room-destroyed", function(event) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
387 local room_node = jid_split(event.room.jid); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
388 archive:delete(room_node); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
389 end); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
390 end |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
391 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
392 -- And role/affiliation changes? |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
393 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
394 module:add_feature(xmlns_mam); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
395 |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
396 module:hook("muc-disco#info", function(event) |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
397 event.reply:tag("feature", {var=xmlns_mam}):up(); |
7ee93b3fa160
mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
398 end); |
9880
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
399 |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
400 -- Cleanup |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
401 |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
402 local cleanup_after = module:get_option_string("muc_log_expires_after", "1w"); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
403 local cleanup_interval = module:get_option_number("muc_log_cleanup_interval", 4 * 60 * 60); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
404 |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
405 if cleanup_after ~= "never" then |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
406 local cleanup_storage = module:open_store("muc_log_cleanup"); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
407 local cleanup_map = module:open_store("muc_log_cleanup", "map"); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
408 |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
409 local day = 86400; |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
410 local multipliers = { d = day, w = day * 7, m = 31 * day, y = 365.2425 * day }; |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
411 local n, m = cleanup_after:lower():match("(%d+)%s*([dwmy]?)"); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
412 if not n then |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
413 module:log("error", "Could not parse muc_log_expires_after string %q", cleanup_after); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
414 return false; |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
415 end |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
416 |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
417 cleanup_after = tonumber(n) * ( multipliers[m] or 1 ); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
418 |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
419 module:log("debug", "muc_log_expires_after = %d -- in seconds", cleanup_after); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
420 |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
421 if not archive.delete then |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
422 module:log("error", "muc_log_expires_after set but mod_%s does not support deleting", archive._provided_by); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
423 return false; |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
424 end |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
425 |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
426 -- For each day, store a set of rooms that have new messages. To expire |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
427 -- messages, we collect the union of sets of rooms from dates that fall |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
428 -- outside the cleanup range. |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
429 |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
430 function schedule_cleanup(roomname, date) |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
431 cleanup_map:set(date or datestamp(), roomname, true); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
432 end |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
433 |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
434 cleanup_runner = require "util.async".runner(function () |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
435 local rooms = {}; |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
436 local cut_off = datestamp(os.time() - cleanup_after); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
437 for date in cleanup_storage:users() do |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
438 if date <= cut_off then |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
439 module:log("debug", "Messages from %q should be expired", date); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
440 local messages_this_day = cleanup_storage:get(date); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
441 if messages_this_day then |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
442 for room in pairs(messages_this_day) do |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
443 rooms[room] = true; |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
444 end |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
445 if date < cut_off then |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
446 -- Messages from the same day as the cut-off might not have expired yet, |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
447 -- but all earlier will have, so clear storage for those days. |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
448 cleanup_storage:set(date, nil); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
449 end |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
450 end |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
451 end |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
452 end |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
453 local sum, num_rooms = 0, 0; |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
454 for room in pairs(rooms) do |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
455 local ok, err = archive:delete(room, { ["end"] = os.time() - cleanup_after; }) |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
456 if ok then |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
457 num_rooms = num_rooms + 1; |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
458 sum = sum + (tonumber(ok) or 0); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
459 end |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
460 end |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
461 module:log("info", "Deleted %d expired messages for %d rooms", sum, num_rooms); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
462 end); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
463 |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
464 cleanup_task = module:add_timer(1, function () |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
465 cleanup_runner:run(true); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
466 return cleanup_interval; |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
467 end); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
468 else |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
469 module:log("debug", "Archive expiry disabled"); |
78885b1bbb91
mod_muc_mam: Copy cleanup mechanism from mod_mam (fixes #672)
Kim Alvefur <zash@zash.se>
parents:
9843
diff
changeset
|
470 end |