Annotate

plugins/mod_muc_mam.lua @ 8722:7ee93b3fa160

mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
author Kim Alvefur <zash@zash.se>
date Mon, 02 Apr 2018 19:44:44 +0200
child 8723:f3bdb20214ab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 module:log("error", "mod_%s should be loaded only on a MUC component, not normal hosts", module.name);
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;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local it = require"util.iterators";
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 -- Support both old and new MUC code
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local mod_muc = module:depends"muc";
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local rooms = rawget(mod_muc, "rooms");
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 local each_room = rawget(mod_muc, "each_room") or function() return it.values(rooms); end;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local new_muc = not rooms;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 if new_muc then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 rooms = module:shared"muc/rooms";
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 else
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 -- COMPAT: We don't (currently?) support injecting stanza-id
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 -- on Prosody 0.10 and prior, which is required by mam:2
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 xmlns_mam = "urn:xmpp:mam:1";
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 function (jid)
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 return rooms[jid];
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
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 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
44 local tostring = tostring;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 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
46 local m_min = math.min;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 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
49
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 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
51 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
52
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 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
54 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
55 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 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
58 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
59
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 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
61 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
62
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 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
64 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
65 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
66 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
67 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
68 else
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 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
70 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 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
72 return false;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 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
76 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
77 return true;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 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
80 if enabled == nil then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 return log_by_default;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 return enabled;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 end
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 local send_history, save_to_history;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 -- Override history methods for all rooms.
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 if not new_muc then -- 0.10 or older
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 module:hook("muc-room-created", function (event)
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 local room = event.room;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 if archiving_enabled(room) then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 room.send_history = send_history;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 room.save_to_history = save_to_history;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 end);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 function module.load()
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 for room in each_room() do
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 if archiving_enabled(room) then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 room.send_history = send_history;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 room.save_to_history = save_to_history;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 function module.unload()
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 for room in each_room() do
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 if room.send_history == send_history then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 room.send_history = nil;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 room.save_to_history = nil;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 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
117 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
118 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
119 table.insert(form,
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 {
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 name = muc_form_enable,
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 type = "boolean",
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 label = "Enable archiving?",
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 value = archiving_enabled(room),
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 }
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 );
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 end);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 module:hook("muc-config-submitted", function(event)
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 local room, fields, changed = event.room, event.fields, event.changed;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 local new = fields[muc_form_enable];
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 if new ~= room._data.archiving then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 room._data.archiving = new;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 if type(changed) == "table" then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 changed[muc_form_enable] = true;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 else
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 event.changed = true;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 if new then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 room.send_history = send_history;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 room.save_to_history = save_to_history;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 else
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 room.send_history = nil;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 room.save_to_history = nil;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 end);
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
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 -- 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
151 local query_form = dataform {
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 { 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
153 { 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
154 { 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
155 { 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
156 };
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 -- Serve form
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 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
160 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
161 origin.send(st.reply(stanza):add_child(query_form:form()));
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 return true;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 end);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 -- Handle archive queries
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 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
167 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
168 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
169 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
170 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
171 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
172
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 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
174 if not room then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 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
176 return true;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 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
179
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 -- 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
181 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
182 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
183 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
184 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
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
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 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
189
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 -- Search query parameters
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 local qstart, qend;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 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
193 if form then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 local err;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 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
196 if err then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 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
198 return true;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 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
201 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 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
204 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
205 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
206 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
207 return true;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 qstart, qend = vstart, vend;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 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
213 tostring(qid),
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 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
215 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
216
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 -- RSM stuff
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 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
219 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
220 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
221
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 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
223 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
224
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 -- Load all the data!
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 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
227 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
228 limit = qmax + 1;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 before = before; after = after;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 reverse = reverse;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 with = "message<groupchat";
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 });
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 not data then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 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
236 return true;
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 local total = tonumber(err);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 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
241
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242 local results = {};
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 -- 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
245 local first, last;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 local count = 0;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 local complete = "true";
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 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
249 count = count + 1;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250 if count > qmax then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 complete = nil;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 break;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 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
255 :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
256 :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
257 :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
258
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 -- 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
260 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
261 item:maptags(function (tag)
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 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
263 return nil;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 return tag;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 end);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 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
269 item = st.deserialize(item);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 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
272 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
273
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 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
275 last = id;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 if reverse then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 results[count] = fwd_st;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 else
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 origin.send(fwd_st);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 if reverse then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 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
286 origin.send(results[i]);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 first, last = last, first;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 -- That's all folks!
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292 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
293
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 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
295 :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
296 :add_child(rsm.generate {
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 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
298 return true;
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
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 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
302 local room = event.room;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 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
304 local room_jid = room.jid;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305 local maxstanzas = event.maxstanzas or math.huge;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306 local maxchars = event.maxchars;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 local since = event.since;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
308 local to = event.to;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
309
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
310 -- Load all the data!
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311 local query = {
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 limit = math.min(maxstanzas, get_historylength(room));
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313 start = since;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
314 reverse = true;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
315 with = "message<groupchat";
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
316 }
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317 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
318
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319 if not data then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 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
321 return
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 local history, i = {}, 1;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 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
327 item.attr.to = to;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 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
329 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
330 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
331 item:maptags(function (tag)
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332 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
333 return nil;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 return tag;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336 end);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338 if maxchars then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 local chars = #tostring(item);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340 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
341 break
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 maxchars = maxchars - chars;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
345 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
346 -- 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
347 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 function event.next_stanza()
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 i = i - 1;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350 return history[i];
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
351 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352 return true;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
353 end, 1);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355 function send_history(self, to, stanza)
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
356 local maxchars, maxstanzas, seconds, since;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357 local history_tag = stanza:find("{http://jabber.org/protocol/muc}x/history")
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
358 if history_tag then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 module:log("debug", tostring(history_tag));
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360 local history_attr = history_tag.attr;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 maxchars = tonumber(history_attr.maxchars);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
362 maxstanzas = tonumber(history_attr.maxstanzas);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363 seconds = tonumber(history_attr.seconds);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364 since = history_attr.since;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
365 if since then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
366 since = timestamp_parse(since);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
367 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
368 if seconds then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
369 since = math.max(os.time() - seconds, since or 0);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
370 end
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
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
373 local event = {
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
374 room = self;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
375 to = to; -- `to` is required to calculate the character count for `maxchars`
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
376 maxchars = maxchars, maxstanzas = maxstanzas, since = since;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
377 next_stanza = function() end; -- events should define this iterator
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
378 };
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
379
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
380 module:fire_event("muc-get-history", event);
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 for msg in event.next_stanza, event do
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
383 self:_route_stanza(msg);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
384 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
385 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
386
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 -- Handle messages
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388 function save_to_history(self, stanza)
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
389 local room_node, room_host = jid_split(self.jid);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
391 -- 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
392 stanza:maptags(function (tag)
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
393 if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394 and jid_prep(tag.attr.by) == self.jid then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
395 return nil;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
396 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397 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
398 return nil;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
399 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400 return tag;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
401 end);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
402
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
403 local stored_stanza = stanza;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
404
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
405 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
406 stored_stanza = st.clone(stanza);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
407 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
408 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
409 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
410 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
411 :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
412 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
413
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
414 -- Policy check
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
415 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
416
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
417 -- And stash it
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
418 local with = stanza.name
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
419 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
420 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
421 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
422
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
423 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
424
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
425 if id then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
426 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
427 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
428 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
429
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
430 module:hook("muc-broadcast-message", function (event)
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
431 local room, stanza = event.room, event.stanza;
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
432 if stanza:get_child("body") then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
433 save_to_history(room, stanza);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
434 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
435 end);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
436
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
437 if module:get_option_boolean("muc_log_presences", true) then
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
438 module:hook("muc-occupant-joined", function (event)
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
439 save_to_history(event.room, st.stanza("presence", { from = event.nick }));
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
440 end);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
441 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
442 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
443 end);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
444 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
445
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
446 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
447 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
448 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
449 else
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
450 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
451 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
452 archive:delete(room_node);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
453 end);
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
454 end
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
455
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
456 -- And role/affiliation changes?
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
457
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
458 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
459
7ee93b3fa160 mod_muc_mam: Import mod_mam_muc from prosody-modules ba6466fa6823
Kim Alvefur <zash@zash.se>
parents:
diff changeset
460 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
461 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
462 end);