annotate mod_mam_muc/mod_mam_muc.lua @ 2949:39994c6bb314

mod_mam_muc: Advertise mam:1 instead of mam:2 if not using new MUC code [fixes #1118]
author Matthew Wild <mwild1@gmail.com>
date Mon, 26 Mar 2018 08:05:22 +0100
parents b69ef062806e
children f7ebf8fcd602
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1313
440d7276ca62 mod_mam_muc: Rename variable
Kim Alvefur <zash@zash.se>
parents: 1312
diff changeset
1 -- XEP-0313: Message Archive Management for Prosody MUC
2626
17883c405df3 mod_mam_muc: Shorten some lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2507
diff changeset
2 -- Copyright (C) 2011-2017 Kim Alvefur
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 --
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- This file is MIT/X11 licensed.
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
1973
9329a11c03a6 mod_mam_muc: Add guard to prevent loading on normal hosts
Kim Alvefur <zash@zash.se>
parents: 1865
diff changeset
6 if module:get_host_type() ~= "component" then
9329a11c03a6 mod_mam_muc: Add guard to prevent loading on normal hosts
Kim Alvefur <zash@zash.se>
parents: 1865
diff changeset
7 module:log("error", "mod_%s should be loaded only on a MUC component, not normal hosts", module.name);
9329a11c03a6 mod_mam_muc: Add guard to prevent loading on normal hosts
Kim Alvefur <zash@zash.se>
parents: 1865
diff changeset
8 return;
9329a11c03a6 mod_mam_muc: Add guard to prevent loading on normal hosts
Kim Alvefur <zash@zash.se>
parents: 1865
diff changeset
9 end
9329a11c03a6 mod_mam_muc: Add guard to prevent loading on normal hosts
Kim Alvefur <zash@zash.se>
parents: 1865
diff changeset
10
2627
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
11 local xmlns_mam = "urn:xmpp:mam:2";
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local xmlns_delay = "urn:xmpp:delay";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local xmlns_forward = "urn:xmpp:forward:0";
2627
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
14 local xmlns_st_id = "urn:xmpp:sid:0";
2721
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
15 local xmlns_muc_user = "http://jabber.org/protocol/muc#user";
2716
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
16 local muc_form_enable = "muc#roomconfig_enablearchiving"
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local st = require "util.stanza";
2497
c6761ebe613b mod_mam_muc: Use util.rsm (fixes #829, depends on a recent 0.10+)
Kim Alvefur <zash@zash.se>
parents: 2429
diff changeset
19 local rsm = require "util.rsm";
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local jid_bare = require "util.jid".bare;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local jid_split = require "util.jid".split;
2627
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
22 local jid_prep = require "util.jid".prep;
1536
4fb280768efc mod_mam_muc: Add missing import and change the namespace to MAM v0.3
Kim Alvefur <zash@zash.se>
parents: 1535
diff changeset
23 local dataform = require "util.dataforms".new;
1551
5127f4db9d39 mod_mam_muc: Try to use new MUC API for getting room objects (fixes queries to not yet initialized rooms)
Kim Alvefur <zash@zash.se>
parents: 1548
diff changeset
24 local it = require"util.iterators";
1534
4dd6eebc8fbd mod_mam_muc: Minor moving about of variables
Kim Alvefur <zash@zash.se>
parents: 1533
diff changeset
25
1551
5127f4db9d39 mod_mam_muc: Try to use new MUC API for getting room objects (fixes queries to not yet initialized rooms)
Kim Alvefur <zash@zash.se>
parents: 1548
diff changeset
26 -- Support both old and new MUC code
1534
4dd6eebc8fbd mod_mam_muc: Minor moving about of variables
Kim Alvefur <zash@zash.se>
parents: 1533
diff changeset
27 local mod_muc = module:depends"muc";
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
28 local rooms = rawget(mod_muc, "rooms");
1551
5127f4db9d39 mod_mam_muc: Try to use new MUC API for getting room objects (fixes queries to not yet initialized rooms)
Kim Alvefur <zash@zash.se>
parents: 1548
diff changeset
29 local each_room = rawget(mod_muc, "each_room") or function() return it.values(rooms); end;
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
30 local new_muc = not rooms;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
31 if new_muc then
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
32 rooms = module:shared"muc/rooms";
2949
39994c6bb314 mod_mam_muc: Advertise mam:1 instead of mam:2 if not using new MUC code [fixes #1118]
Matthew Wild <mwild1@gmail.com>
parents: 2835
diff changeset
33 else
39994c6bb314 mod_mam_muc: Advertise mam:1 instead of mam:2 if not using new MUC code [fixes #1118]
Matthew Wild <mwild1@gmail.com>
parents: 2835
diff changeset
34 -- COMPAT: We don't (currently?) support injecting stanza-id
39994c6bb314 mod_mam_muc: Advertise mam:1 instead of mam:2 if not using new MUC code [fixes #1118]
Matthew Wild <mwild1@gmail.com>
parents: 2835
diff changeset
35 -- on Prosody 0.10 and prior, which is required by mam:2
39994c6bb314 mod_mam_muc: Advertise mam:1 instead of mam:2 if not using new MUC code [fixes #1118]
Matthew Wild <mwild1@gmail.com>
parents: 2835
diff changeset
36 xmlns_mam = "urn:xmpp:mam:1";
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
37 end
1551
5127f4db9d39 mod_mam_muc: Try to use new MUC API for getting room objects (fixes queries to not yet initialized rooms)
Kim Alvefur <zash@zash.se>
parents: 1548
diff changeset
38 local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
5127f4db9d39 mod_mam_muc: Try to use new MUC API for getting room objects (fixes queries to not yet initialized rooms)
Kim Alvefur <zash@zash.se>
parents: 1548
diff changeset
39 function (jid)
5127f4db9d39 mod_mam_muc: Try to use new MUC API for getting room objects (fixes queries to not yet initialized rooms)
Kim Alvefur <zash@zash.se>
parents: 1548
diff changeset
40 return rooms[jid];
5127f4db9d39 mod_mam_muc: Try to use new MUC API for getting room objects (fixes queries to not yet initialized rooms)
Kim Alvefur <zash@zash.se>
parents: 1548
diff changeset
41 end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42
2498
e065e94f0ac6 mod_mam_muc: Remove fallback for util.stanza.is_stanza, it should be available
Kim Alvefur <zash@zash.se>
parents: 2497
diff changeset
43 local is_stanza = st.is_stanza;
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 local tostring = tostring;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 local time_now = os.time;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 local m_min = math.min;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse;
2831
d65868fed77a mod_mam_muc: Copy max_archive_query_results option from mod_mam
Kim Alvefur <zash@zash.se>
parents: 2723
diff changeset
48 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50);
2718
02d688ba7739 mod_mam_muc: Make calculation of max history length more like in mod_muc
Kim Alvefur <zash@zash.se>
parents: 2716
diff changeset
49
02d688ba7739 mod_mam_muc: Make calculation of max history length more like in mod_muc
Kim Alvefur <zash@zash.se>
parents: 2716
diff changeset
50 local default_history_length = 20;
02d688ba7739 mod_mam_muc: Make calculation of max history length more like in mod_muc
Kim Alvefur <zash@zash.se>
parents: 2716
diff changeset
51 local max_history_length = module:get_option_number("max_history_messages", math.huge);
02d688ba7739 mod_mam_muc: Make calculation of max history length more like in mod_muc
Kim Alvefur <zash@zash.se>
parents: 2716
diff changeset
52
02d688ba7739 mod_mam_muc: Make calculation of max history length more like in mod_muc
Kim Alvefur <zash@zash.se>
parents: 2716
diff changeset
53 local function get_historylength(room)
02d688ba7739 mod_mam_muc: Make calculation of max history length more like in mod_muc
Kim Alvefur <zash@zash.se>
parents: 2716
diff changeset
54 return math.min(room._data.history_length or default_history_length, max_history_length);
02d688ba7739 mod_mam_muc: Make calculation of max history length more like in mod_muc
Kim Alvefur <zash@zash.se>
parents: 2716
diff changeset
55 end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
1143
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
57 local log_all_rooms = module:get_option_boolean("muc_log_all_rooms", false);
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
58 local log_by_default = module:get_option_boolean("muc_log_by_default", true);
1142
fabdaa0d99e3 mod_mam_muc: Stap archived messages
Kim Alvefur <zash@zash.se>
parents: 1141
diff changeset
59
1571
eed7db9f3157 mod_mam_muc, mod_http_muc_log: Change store name from 'archive2' to 'muc_log' to distinguish it from personal MAM archives. Old data will require migration.
Kim Alvefur <zash@zash.se>
parents: 1551
diff changeset
60 local archive_store = "muc_log";
1311
27b2a357c73c mod_mam_muc: porting archive checks from mod_mam (to avoid tracebacks about calling null functions)
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 1279
diff changeset
61 local archive = module:open_store(archive_store, "archive");
2031
f21147d56bc4 mod_mam_muc: Yell loudly when archive store can't be opened
Kim Alvefur <zash@zash.se>
parents: 1973
diff changeset
62
f21147d56bc4 mod_mam_muc: Yell loudly when archive store can't be opened
Kim Alvefur <zash@zash.se>
parents: 1973
diff changeset
63 if archive.name == "null" or not archive.find then
f21147d56bc4 mod_mam_muc: Yell loudly when archive store can't be opened
Kim Alvefur <zash@zash.se>
parents: 1973
diff changeset
64 if not archive.find then
2626
17883c405df3 mod_mam_muc: Shorten some lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2507
diff changeset
65 module:log("error", "Attempt to open archive storage returned a driver without archive API support");
17883c405df3 mod_mam_muc: Shorten some lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2507
diff changeset
66 module:log("error", "mod_%s does not support archiving",
17883c405df3 mod_mam_muc: Shorten some lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2507
diff changeset
67 archive._provided_by or archive.name and "storage_"..archive.name.."(?)" or "<unknown>");
2031
f21147d56bc4 mod_mam_muc: Yell loudly when archive store can't be opened
Kim Alvefur <zash@zash.se>
parents: 1973
diff changeset
68 else
f21147d56bc4 mod_mam_muc: Yell loudly when archive store can't be opened
Kim Alvefur <zash@zash.se>
parents: 1973
diff changeset
69 module:log("error", "Attempt to open archive storage returned null driver");
f21147d56bc4 mod_mam_muc: Yell loudly when archive store can't be opened
Kim Alvefur <zash@zash.se>
parents: 1973
diff changeset
70 end
f21147d56bc4 mod_mam_muc: Yell loudly when archive store can't be opened
Kim Alvefur <zash@zash.se>
parents: 1973
diff changeset
71 module:log("info", "See https://prosody.im/doc/storage and https://prosody.im/doc/archiving for more information");
f21147d56bc4 mod_mam_muc: Yell loudly when archive store can't be opened
Kim Alvefur <zash@zash.se>
parents: 1973
diff changeset
72 return false;
1311
27b2a357c73c mod_mam_muc: porting archive checks from mod_mam (to avoid tracebacks about calling null functions)
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 1279
diff changeset
73 end
27b2a357c73c mod_mam_muc: porting archive checks from mod_mam (to avoid tracebacks about calling null functions)
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 1279
diff changeset
74
2716
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
75 local function archiving_enabled(room)
1542
ccb9dc624ebd mod_mam_muc: Split logic for determining if logging is enabled into a function
Kim Alvefur <zash@zash.se>
parents: 1541
diff changeset
76 if log_all_rooms then
ccb9dc624ebd mod_mam_muc: Split logic for determining if logging is enabled into a function
Kim Alvefur <zash@zash.se>
parents: 1541
diff changeset
77 return true;
ccb9dc624ebd mod_mam_muc: Split logic for determining if logging is enabled into a function
Kim Alvefur <zash@zash.se>
parents: 1541
diff changeset
78 end
2716
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
79 local enabled = room._data.archiving;
1542
ccb9dc624ebd mod_mam_muc: Split logic for determining if logging is enabled into a function
Kim Alvefur <zash@zash.se>
parents: 1541
diff changeset
80 if enabled == nil then
ccb9dc624ebd mod_mam_muc: Split logic for determining if logging is enabled into a function
Kim Alvefur <zash@zash.se>
parents: 1541
diff changeset
81 return log_by_default;
ccb9dc624ebd mod_mam_muc: Split logic for determining if logging is enabled into a function
Kim Alvefur <zash@zash.se>
parents: 1541
diff changeset
82 end
ccb9dc624ebd mod_mam_muc: Split logic for determining if logging is enabled into a function
Kim Alvefur <zash@zash.se>
parents: 1541
diff changeset
83 return enabled;
ccb9dc624ebd mod_mam_muc: Split logic for determining if logging is enabled into a function
Kim Alvefur <zash@zash.se>
parents: 1541
diff changeset
84 end
ccb9dc624ebd mod_mam_muc: Split logic for determining if logging is enabled into a function
Kim Alvefur <zash@zash.se>
parents: 1541
diff changeset
85
1314
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
86 local send_history, save_to_history;
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
87
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
88 -- Override history methods for all rooms.
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
89 if not new_muc then -- 0.10 or older
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
90 module:hook("muc-room-created", function (event)
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
91 local room = event.room;
2716
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
92 if archiving_enabled(room) then
1533
915bdcb35e79 mod_mam_muc: Restructure initialization
Kim Alvefur <zash@zash.se>
parents: 1532
diff changeset
93 room.send_history = send_history;
915bdcb35e79 mod_mam_muc: Restructure initialization
Kim Alvefur <zash@zash.se>
parents: 1532
diff changeset
94 room.save_to_history = save_to_history;
1314
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
95 end
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
96 end);
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
97
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
98 function module.load()
1551
5127f4db9d39 mod_mam_muc: Try to use new MUC API for getting room objects (fixes queries to not yet initialized rooms)
Kim Alvefur <zash@zash.se>
parents: 1548
diff changeset
99 for room in each_room() do
2716
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
100 if archiving_enabled(room) then
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
101 room.send_history = send_history;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
102 room.save_to_history = save_to_history;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
103 end
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
104 end
1314
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
105 end
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
106 function module.unload()
1551
5127f4db9d39 mod_mam_muc: Try to use new MUC API for getting room objects (fixes queries to not yet initialized rooms)
Kim Alvefur <zash@zash.se>
parents: 1548
diff changeset
107 for room in each_room() do
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
108 if room.send_history == send_history then
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
109 room.send_history = nil;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
110 room.save_to_history = nil;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
111 end
1314
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
112 end
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
113 end
1533
915bdcb35e79 mod_mam_muc: Restructure initialization
Kim Alvefur <zash@zash.se>
parents: 1532
diff changeset
114 end
915bdcb35e79 mod_mam_muc: Restructure initialization
Kim Alvefur <zash@zash.se>
parents: 1532
diff changeset
115
915bdcb35e79 mod_mam_muc: Restructure initialization
Kim Alvefur <zash@zash.se>
parents: 1532
diff changeset
116 if not log_all_rooms then
1276
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
117 module:hook("muc-config-form", function(event)
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
118 local room, form = event.room, event.form;
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
119 table.insert(form,
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
120 {
2716
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
121 name = muc_form_enable,
1276
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
122 type = "boolean",
2716
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
123 label = "Enable archiving?",
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
124 value = archiving_enabled(room),
1276
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
125 }
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
126 );
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
127 end);
1143
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
128
1276
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
129 module:hook("muc-config-submitted", function(event)
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
130 local room, fields, changed = event.room, event.fields, event.changed;
2716
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
131 local new = fields[muc_form_enable];
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
132 if new ~= room._data.archiving then
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
133 room._data.archiving = new;
1276
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
134 if type(changed) == "table" then
2716
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
135 changed[muc_form_enable] = true;
1276
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
136 else
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
137 event.changed = true;
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
138 end
1314
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
139 if new then
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
140 room.send_history = send_history;
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
141 room.save_to_history = save_to_history;
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
142 else
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
143 room.send_history = nil;
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
144 room.save_to_history = nil;
cc9831033f5d mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
Kim Alvefur <zash@zash.se>
parents: 1313
diff changeset
145 end
1143
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
146 end
1276
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
147 end);
01dfaf2f2782 mod_mam_muc: Hide logging option from room configuration if set to log all rooms
Kim Alvefur <zash@zash.se>
parents: 1275
diff changeset
148 end
1143
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
149
1546
fff2858c554f mod_mam_muc: Remove parsing of 'with' field in MAM form, we currently ignore it anyways
Kim Alvefur <zash@zash.se>
parents: 1543
diff changeset
150 -- Note: We ignore the 'with' field as this is internally used for stanza types
1535
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
151 local query_form = dataform {
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
152 { name = "FORM_TYPE"; type = "hidden"; value = xmlns_mam; };
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
153 { name = "with"; type = "jid-single"; };
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
154 { name = "start"; type = "text-single" };
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
155 { name = "end"; type = "text-single"; };
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
156 };
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
157
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
158 -- Serve form
1627
458c80904525 mod_mam_muc: fix <iq type='get' to='room-bare-jid'> query
Stuart Carnie <stuart.carnie@gmail.com>
parents: 1616
diff changeset
159 module:hook("iq-get/bare/"..xmlns_mam..":query", function(event)
1535
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
160 local origin, stanza = event.origin, event.stanza;
1691
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
161 origin.send(st.reply(stanza):add_child(query_form:form()));
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
162 return true;
1535
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
163 end);
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
164
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 -- Handle archive queries
1535
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
166 module:hook("iq-set/bare/"..xmlns_mam..":query", function(event)
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 local origin, stanza = event.origin, event.stanza;
2426
deedb77c4f96 mod_mam_muc: Rename room and room JID variables for consistency
Kim Alvefur <zash@zash.se>
parents: 2414
diff changeset
168 local room_jid = stanza.attr.to;
deedb77c4f96 mod_mam_muc: Rename room and room JID variables for consistency
Kim Alvefur <zash@zash.se>
parents: 2414
diff changeset
169 local room_node = jid_split(room_jid);
1382
ba17268490b7 mod_mam_muc: Fix to, from on result messages (thanks daurnimator)
Kim Alvefur <zash@zash.se>
parents: 1369
diff changeset
170 local orig_from = stanza.attr.from;
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 local query = stanza.tags[1];
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172
2426
deedb77c4f96 mod_mam_muc: Rename room and room JID variables for consistency
Kim Alvefur <zash@zash.se>
parents: 2414
diff changeset
173 local room = get_room_from_jid(room_jid);
deedb77c4f96 mod_mam_muc: Rename room and room JID variables for consistency
Kim Alvefur <zash@zash.se>
parents: 2414
diff changeset
174 if not room then
1865
db8b256f51ff mod_admin_web,mod_carbons,mod_csi_compat,mod_mam_muc,mod_tcpproxy: Explicitly return true
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
175 origin.send(st.error_reply(stanza, "cancel", "item-not-found"))
db8b256f51ff mod_admin_web,mod_carbons,mod_csi_compat,mod_mam_muc,mod_tcpproxy: Explicitly return true
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
176 return true;
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 end
1382
ba17268490b7 mod_mam_muc: Fix to, from on result messages (thanks daurnimator)
Kim Alvefur <zash@zash.se>
parents: 1369
diff changeset
178 local from = jid_bare(orig_from);
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179
1140
402cb9b604eb mod_mam_muc: Send proper error reply when one is not allowed to query archive
Kim Alvefur <zash@zash.se>
parents: 1139
diff changeset
180 -- Banned or not a member of a members-only room?
2426
deedb77c4f96 mod_mam_muc: Rename room and room JID variables for consistency
Kim Alvefur <zash@zash.se>
parents: 2414
diff changeset
181 local from_affiliation = room:get_affiliation(from);
1312
a48cf3ccdf9c mod_mam_muc: Use public API of rooms for authorization check
Kim Alvefur <zash@zash.se>
parents: 1311
diff changeset
182 if from_affiliation == "outcast" -- banned
2426
deedb77c4f96 mod_mam_muc: Rename room and room JID variables for consistency
Kim Alvefur <zash@zash.se>
parents: 2414
diff changeset
183 or room:get_members_only() and not from_affiliation then -- members-only, not a member
1865
db8b256f51ff mod_admin_web,mod_carbons,mod_csi_compat,mod_mam_muc,mod_tcpproxy: Explicitly return true
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
184 origin.send(st.error_reply(stanza, "auth", "forbidden"))
db8b256f51ff mod_admin_web,mod_carbons,mod_csi_compat,mod_mam_muc,mod_tcpproxy: Explicitly return true
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
185 return true;
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
188 local qid = query.attr.queryid;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
189
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
190 -- Search query parameters
1546
fff2858c554f mod_mam_muc: Remove parsing of 'with' field in MAM form, we currently ignore it anyways
Kim Alvefur <zash@zash.se>
parents: 1543
diff changeset
191 local qstart, qend;
1535
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
192 local form = query:get_child("x", "jabber:x:data");
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
193 if form then
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
194 local err;
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
195 form, err = query_form:data(form);
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
196 if err then
1691
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
197 origin.send(st.error_reply(stanza, "modify", "bad-request", select(2, next(err))));
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
198 return true;
1535
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
199 end
1546
fff2858c554f mod_mam_muc: Remove parsing of 'with' field in MAM form, we currently ignore it anyways
Kim Alvefur <zash@zash.se>
parents: 1543
diff changeset
200 qstart, qend = form["start"], form["end"];
1535
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
201 end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
203 if qstart or qend then -- Validate timestamps
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
204 local vstart, vend = (qstart and timestamp_parse(qstart)), (qend and timestamp_parse(qend))
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
205 if (qstart and not vstart) or (qend and not vend) then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
206 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp"))
2500
ec046f915506 mod_mam_muc: Minor syntax change
Kim Alvefur <zash@zash.se>
parents: 2498
diff changeset
207 return true;
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
208 end
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
209 qstart, qend = vstart, vend;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
210 end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211
2719
7568157bf998 mod_mam_muc: Add debug logging like in mod_mam
Kim Alvefur <zash@zash.se>
parents: 2718
diff changeset
212 module:log("debug", "Archive query id %s from %s until %s)",
7568157bf998 mod_mam_muc: Add debug logging like in mod_mam
Kim Alvefur <zash@zash.se>
parents: 2718
diff changeset
213 tostring(qid),
7568157bf998 mod_mam_muc: Add debug logging like in mod_mam
Kim Alvefur <zash@zash.se>
parents: 2718
diff changeset
214 qstart and timestamp(qstart) or "the dawn of time",
7568157bf998 mod_mam_muc: Add debug logging like in mod_mam
Kim Alvefur <zash@zash.se>
parents: 2718
diff changeset
215 qend and timestamp(qend) or "now");
7568157bf998 mod_mam_muc: Add debug logging like in mod_mam
Kim Alvefur <zash@zash.se>
parents: 2718
diff changeset
216
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
217 -- RSM stuff
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
218 local qset = rsm.get(query);
2831
d65868fed77a mod_mam_muc: Copy max_archive_query_results option from mod_mam
Kim Alvefur <zash@zash.se>
parents: 2723
diff changeset
219 local qmax = m_min(qset and qset.max or default_max_items, max_max_items);
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
220 local reverse = qset and qset.before or false;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
221
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
222 local before, after = qset and qset.before, qset and qset.after;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
223 if type(before) ~= "string" then before = nil; end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
225 -- Load all the data!
1146
9fa89dc7a86f mod_mam_muc: Search the rooms archive correctly (copypaste error from mod_mam)
Kim Alvefur <zash@zash.se>
parents: 1145
diff changeset
226 local data, err = archive:find(room_node, {
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
227 start = qstart; ["end"] = qend; -- Time range
1691
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
228 limit = qmax + 1;
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
229 before = before; after = after;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
230 reverse = reverse;
1547
e98c335c6554 mod_mam_muc: Limit search to groupchat messages (we use the internal 'with' field stanza types)
Kim Alvefur <zash@zash.se>
parents: 1546
diff changeset
231 with = "message<groupchat";
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
232 });
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
233
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
234 if not data then
1865
db8b256f51ff mod_admin_web,mod_carbons,mod_csi_compat,mod_mam_muc,mod_tcpproxy: Explicitly return true
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
235 origin.send(st.error_reply(stanza, "cancel", "internal-server-error"));
db8b256f51ff mod_admin_web,mod_carbons,mod_csi_compat,mod_mam_muc,mod_tcpproxy: Explicitly return true
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
236 return true;
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
237 end
2832
15a9d508c5f7 mod_mam_muc: Pass along the size of the matching set, if returned from storage driver
Kim Alvefur <zash@zash.se>
parents: 2831
diff changeset
238 local total = tonumber(err);
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
239
1534
4dd6eebc8fbd mod_mam_muc: Minor moving about of variables
Kim Alvefur <zash@zash.se>
parents: 1533
diff changeset
240 local msg_reply_attr = { to = stanza.attr.from, from = stanza.attr.to };
4dd6eebc8fbd mod_mam_muc: Minor moving about of variables
Kim Alvefur <zash@zash.se>
parents: 1533
diff changeset
241
1691
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
242 local results = {};
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
243
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
244 -- Wrap it in stuff and deliver
1691
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
245 local first, last;
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
246 local count = 0;
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
247 local complete = "true";
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
248 for id, item, when in data do
1691
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
249 count = count + 1;
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
250 if count > qmax then
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
251 complete = nil;
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
252 break;
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
253 end
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
254 local fwd_st = st.message(msg_reply_attr)
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
255 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id })
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
256 :tag("forwarded", { xmlns = xmlns_forward })
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
257 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up();
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
258
2833
4f9466be7b46 mod_mam_muc: Add comment describing stripping of <x> tag in results
Kim Alvefur <zash@zash.se>
parents: 2832
diff changeset
259 -- Strip <x> tag, containing the original senders JID, unless the room makes this public
2721
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
260 if room:get_whois() ~= "anyone" then
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
261 item:maptags(function (tag)
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
262 if tag.name == "x" and tag.attr.xmlns == xmlns_muc_user then
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
263 return nil;
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
264 end
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
265 return tag;
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
266 end);
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
267 end
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
268 if not is_stanza(item) then
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
269 item = st.deserialize(item);
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
270 end
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
271 item.attr.xmlns = "jabber:client";
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
272 fwd_st:add_child(item);
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
274 if not first then first = id; end
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
275 last = id;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
276
1691
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
277 if reverse then
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
278 results[count] = fwd_st;
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
279 else
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
280 origin.send(fwd_st);
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
281 end
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
282 end
2500
ec046f915506 mod_mam_muc: Minor syntax change
Kim Alvefur <zash@zash.se>
parents: 2498
diff changeset
283
1691
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
284 if reverse then
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
285 for i = #results, 1, -1 do
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
286 origin.send(results[i]);
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
287 end
2502
f576ac1cad65 mod_mam_muc: Move swapping of reversed variables (like 5fb917b86838 for mod_mam)
Kim Alvefur <zash@zash.se>
parents: 2501
diff changeset
288 first, last = last, first;
1691
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
289 end
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
290
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
291 -- That's all folks!
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
292 module:log("debug", "Archive query %s completed", tostring(qid));
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293
2507
b8a66805459e mod_mam_muc: Update to XEP-0313 0.5.1
Kim Alvefur <zash@zash.se>
parents: 2502
diff changeset
294 origin.send(st.reply(stanza)
1691
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
295 :tag("fin", { xmlns = xmlns_mam, queryid = qid, complete = complete })
1535
efbb047c01e7 mod_mam_muc: Update to XEP-0313 v 0.3
Kim Alvefur <zash@zash.se>
parents: 1534
diff changeset
296 :add_child(rsm.generate {
2832
15a9d508c5f7 mod_mam_muc: Pass along the size of the matching set, if returned from storage driver
Kim Alvefur <zash@zash.se>
parents: 2831
diff changeset
297 first = first, last = last, count = total }));
1691
1a8c791d365f mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
Kim Alvefur <zash@zash.se>
parents: 1679
diff changeset
298 return true;
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299 end);
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
301 module:hook("muc-get-history", function (event)
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
302 local room = event.room;
2716
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
303 if not archiving_enabled(room) then return end
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
304 local room_jid = room.jid;
2723
ec02ee02a04b mod_mam_muc: Handle missing maxstanzas (thanks Martin)
Kim Alvefur <zash@zash.se>
parents: 2722
diff changeset
305 local maxstanzas = event.maxstanzas or math.huge;
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
306 local maxchars = event.maxchars;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
307 local since = event.since;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
308 local to = event.to;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
309
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
310 -- Load all the data!
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
311 local query = {
2718
02d688ba7739 mod_mam_muc: Make calculation of max history length more like in mod_muc
Kim Alvefur <zash@zash.se>
parents: 2716
diff changeset
312 limit = math.min(maxstanzas, get_historylength(room));
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
313 start = since;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
314 reverse = true;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
315 with = "message<groupchat";
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
316 }
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
317 local data, err = archive:find(jid_split(room_jid), query);
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
318
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
319 if not data then
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
320 module:log("error", "Could not fetch history: %s", tostring(err));
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
321 return
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
322 end
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
323
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
324 local history, i = {}, 1;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
325
2628
fbb26a56a667 mod_mam_muc: Include stanza-id in history replay
Kim Alvefur <zash@zash.se>
parents: 2627
diff changeset
326 for id, item, when in data do
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
327 item.attr.to = to;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
328 item:tag("delay", { xmlns = "urn:xmpp:delay", from = room_jid, stamp = timestamp(when) }):up(); -- XEP-0203
2628
fbb26a56a667 mod_mam_muc: Include stanza-id in history replay
Kim Alvefur <zash@zash.se>
parents: 2627
diff changeset
329 item:tag("stanza-id", { xmlns = xmlns_st_id, by = room_jid, id = id }):up();
2721
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
330 if room:get_whois() ~= "anyone" then
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
331 item:maptags(function (tag)
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
332 if tag.name == "x" and tag.attr.xmlns == xmlns_muc_user then
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
333 return nil;
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
334 end
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
335 return tag;
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
336 end);
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
337 end
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
338 if maxchars then
1649
ab4073468dfb mod_mam_muc: Fix character limit again(?)
Kim Alvefur <zash@zash.se>
parents: 1636
diff changeset
339 local chars = #tostring(item);
1636
7fa0c41792c7 mod_mam_muc: Fix character limit
Kim Alvefur <zash@zash.se>
parents: 1627
diff changeset
340 if maxchars - chars < 0 then
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
341 break
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
342 end
1649
ab4073468dfb mod_mam_muc: Fix character limit again(?)
Kim Alvefur <zash@zash.se>
parents: 1636
diff changeset
343 maxchars = maxchars - chars;
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
344 end
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
345 history[i], i = item, i+1;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
346 -- module:log("debug", tostring(item));
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
347 end
1650
66d67f0bae16 mod_mam_muc: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1649
diff changeset
348 function event.next_stanza()
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
349 i = i - 1;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
350 return history[i];
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
351 end
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
352 return true;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
353 end, 1);
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
354
1519
67c80abe742e mod_mam_muc: Fix add/removal of room method overrides
Kim Alvefur <zash@zash.se>
parents: 1430
diff changeset
355 function send_history(self, to, stanza)
1278
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
356 local maxchars, maxstanzas, seconds, since;
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
357 local history_tag = stanza:find("{http://jabber.org/protocol/muc}x/history")
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
358 if history_tag then
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
359 module:log("debug", tostring(history_tag));
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
360 local history_attr = history_tag.attr;
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
361 maxchars = tonumber(history_attr.maxchars);
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
362 maxstanzas = tonumber(history_attr.maxstanzas);
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
363 seconds = tonumber(history_attr.seconds);
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
364 since = history_attr.since;
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
365 if since then
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
366 since = timestamp_parse(since);
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
367 end
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
368 if seconds then
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
369 since = math.max(os.time() - seconds, since or 0);
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
370 end
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
371 end
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
372
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
373 local event = {
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
374 room = self;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
375 to = to; -- `to` is required to calculate the character count for `maxchars`
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
376 maxchars = maxchars, maxstanzas = maxstanzas, since = since;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
377 next_stanza = function() end; -- events should define this iterator
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
378 };
1278
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
379
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
380 module:fire_event("muc-get-history", event);
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
381
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
382 for msg in event.next_stanza, event do
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
383 self:_route_stanza(msg);
1278
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
384 end
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
385 end
40f077b18dfe mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents: 1277
diff changeset
386
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 -- Handle messages
1519
67c80abe742e mod_mam_muc: Fix add/removal of room method overrides
Kim Alvefur <zash@zash.se>
parents: 1430
diff changeset
388 function save_to_history(self, stanza)
2627
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
389 local room_node, room_host = jid_split(self.jid);
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
390
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
391 -- Filter out <stanza-id> that claim to be from us
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
392 stanza:maptags(function (tag)
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
393 if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
394 and jid_prep(tag.attr.by) == self.jid then
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
395 return nil;
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
396 end
2721
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
397 if tag.name == "x" and tag.attr.xmlns == xmlns_muc_user then
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
398 return nil;
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
399 end
2627
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
400 return tag;
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
401 end);
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
402
2721
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
403 local stored_stanza = stanza;
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
404
2722
e32bf5e19acd mod_mam_muc: Only collect original JID for messages (unavailable presence seems to cause an error)
Kim Alvefur <zash@zash.se>
parents: 2721
diff changeset
405 if stanza.name == "message" and self:get_whois() == "anyone" then
2721
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
406 stored_stanza = st.clone(stanza);
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
407 local actor = jid_bare(self._occupants[stanza.attr.from].jid);
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
408 local affiliation = self:get_affiliation(actor) or "none";
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
409 local role = self:get_role(actor) or self:get_default_role(affiliation);
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
410 stored_stanza:add_direct_child(st.stanza("x", { xmlns = xmlns_muc_user })
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
411 :tag("item", { affiliation = affiliation; role = role; jid = actor }));
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
412 end
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
413
1279
2118a2eeb1d5 mod_mam_muc: Override method for storing history messages instead of hooking stanza events
Kim Alvefur <zash@zash.se>
parents: 1278
diff changeset
414 -- Policy check
2716
02a7f9fe44fa mod_mam_muc: Rename various fields from "logging" to "archiving"
Kim Alvefur <zash@zash.se>
parents: 2628
diff changeset
415 if not archiving_enabled(self) then return end -- Don't log
1143
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
416
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
417 -- And stash it
1385
da9469e68dee mod_mam_muc: Place stanza type in 'with' column; when sending history, only send messages with type=groupchat
daurnimator <quae@daurnimator.com>
parents: 1382
diff changeset
418 local with = stanza.name
da9469e68dee mod_mam_muc: Place stanza type in 'with' column; when sending history, only send messages with type=groupchat
daurnimator <quae@daurnimator.com>
parents: 1382
diff changeset
419 if stanza.attr.type then
da9469e68dee mod_mam_muc: Place stanza type in 'with' column; when sending history, only send messages with type=groupchat
daurnimator <quae@daurnimator.com>
parents: 1382
diff changeset
420 with = with .. "<" .. stanza.attr.type
da9469e68dee mod_mam_muc: Place stanza type in 'with' column; when sending history, only send messages with type=groupchat
daurnimator <quae@daurnimator.com>
parents: 1382
diff changeset
421 end
2627
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
422
2721
391c508e0b75 mod_mam_muc: Store and return original JID, role and affiliation (fixes #903)
Kim Alvefur <zash@zash.se>
parents: 2720
diff changeset
423 local id = archive:append(room_node, nil, stored_stanza, time_now(), with);
2627
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
424
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
425 if id then
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
426 stanza:add_direct_child(st.stanza("stanza-id", { xmlns = xmlns_st_id, by = self.jid, id = id }));
3f166242b460 mod_mam_muc: Add stanza-id tags (updates to XEP-0313 v0.6)
Kim Alvefur <zash@zash.se>
parents: 2626
diff changeset
427 end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
428 end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
429
1543
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
430 module:hook("muc-broadcast-message", function (event)
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
431 local room, stanza = event.room, event.stanza;
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
432 if stanza:get_child("body") then
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
433 save_to_history(room, stanza);
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
434 end
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
435 end);
57fb9ce21f9c mod_mam_muc: Add compatibility with the new MUC code in trunk
Kim Alvefur <zash@zash.se>
parents: 1542
diff changeset
436
1616
28411e97db94 mod_mam_muc: support to disable presence logging via muc_log_presences
Stuart Carnie <stuart.carnie@gmail.com>
parents: 1571
diff changeset
437 if module:get_option_boolean("muc_log_presences", true) then
28411e97db94 mod_mam_muc: support to disable presence logging via muc_log_presences
Stuart Carnie <stuart.carnie@gmail.com>
parents: 1571
diff changeset
438 module:hook("muc-occupant-joined", function (event)
28411e97db94 mod_mam_muc: support to disable presence logging via muc_log_presences
Stuart Carnie <stuart.carnie@gmail.com>
parents: 1571
diff changeset
439 save_to_history(event.room, st.stanza("presence", { from = event.nick }));
28411e97db94 mod_mam_muc: support to disable presence logging via muc_log_presences
Stuart Carnie <stuart.carnie@gmail.com>
parents: 1571
diff changeset
440 end);
28411e97db94 mod_mam_muc: support to disable presence logging via muc_log_presences
Stuart Carnie <stuart.carnie@gmail.com>
parents: 1571
diff changeset
441 module:hook("muc-occupant-left", function (event)
28411e97db94 mod_mam_muc: support to disable presence logging via muc_log_presences
Stuart Carnie <stuart.carnie@gmail.com>
parents: 1571
diff changeset
442 save_to_history(event.room, st.stanza("presence", { type = "unavailable", from = event.nick }));
28411e97db94 mod_mam_muc: support to disable presence logging via muc_log_presences
Stuart Carnie <stuart.carnie@gmail.com>
parents: 1571
diff changeset
443 end);
28411e97db94 mod_mam_muc: support to disable presence logging via muc_log_presences
Stuart Carnie <stuart.carnie@gmail.com>
parents: 1571
diff changeset
444 end
1548
d3c847070618 mod_mam_muc: Store joins and leaves
Kim Alvefur <zash@zash.se>
parents: 1547
diff changeset
445
2835
b69ef062806e mod_mam_muc: Warn if the storage driver is missing the deletion API (instead of a traceback) (thanks asterix)
Kim Alvefur <zash@zash.se>
parents: 2833
diff changeset
446 if not archive.delete then
b69ef062806e mod_mam_muc: Warn if the storage driver is missing the deletion API (instead of a traceback) (thanks asterix)
Kim Alvefur <zash@zash.se>
parents: 2833
diff changeset
447 module:log("warn", "Storage driver %s does not support deletion", archive._provided_by);
b69ef062806e mod_mam_muc: Warn if the storage driver is missing the deletion API (instead of a traceback) (thanks asterix)
Kim Alvefur <zash@zash.se>
parents: 2833
diff changeset
448 module:log("warn", "Archived message will persist after a room has been destroyed");
b69ef062806e mod_mam_muc: Warn if the storage driver is missing the deletion API (instead of a traceback) (thanks asterix)
Kim Alvefur <zash@zash.se>
parents: 2833
diff changeset
449 else
b69ef062806e mod_mam_muc: Warn if the storage driver is missing the deletion API (instead of a traceback) (thanks asterix)
Kim Alvefur <zash@zash.se>
parents: 2833
diff changeset
450 module:hook("muc-room-destroyed", function(event)
b69ef062806e mod_mam_muc: Warn if the storage driver is missing the deletion API (instead of a traceback) (thanks asterix)
Kim Alvefur <zash@zash.se>
parents: 2833
diff changeset
451 local room_node = jid_split(event.room.jid);
b69ef062806e mod_mam_muc: Warn if the storage driver is missing the deletion API (instead of a traceback) (thanks asterix)
Kim Alvefur <zash@zash.se>
parents: 2833
diff changeset
452 archive:delete(room_node);
b69ef062806e mod_mam_muc: Warn if the storage driver is missing the deletion API (instead of a traceback) (thanks asterix)
Kim Alvefur <zash@zash.se>
parents: 2833
diff changeset
453 end);
b69ef062806e mod_mam_muc: Warn if the storage driver is missing the deletion API (instead of a traceback) (thanks asterix)
Kim Alvefur <zash@zash.se>
parents: 2833
diff changeset
454 end
1277
999891a9ae5d mod_mam_muc: Remove archives when a room is destroyed
Kim Alvefur <zash@zash.se>
parents: 1276
diff changeset
455
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
456 -- And role/affiliation changes?
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
457
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
458 module:add_feature(xmlns_mam);
1679
c77e9522dc66 mod_mam_muc: Advertise MAM feature on bare JID room disco#info (only effective after prosody 57bc52f67564)
Kim Alvefur <zash@zash.se>
parents: 1650
diff changeset
459
c77e9522dc66 mod_mam_muc: Advertise MAM feature on bare JID room disco#info (only effective after prosody 57bc52f67564)
Kim Alvefur <zash@zash.se>
parents: 1650
diff changeset
460 module:hook("muc-disco#info", function(event)
c77e9522dc66 mod_mam_muc: Advertise MAM feature on bare JID room disco#info (only effective after prosody 57bc52f67564)
Kim Alvefur <zash@zash.se>
parents: 1650
diff changeset
461 event.reply:tag("feature", {var=xmlns_mam}):up();
c77e9522dc66 mod_mam_muc: Advertise MAM feature on bare JID room disco#info (only effective after prosody 57bc52f67564)
Kim Alvefur <zash@zash.se>
parents: 1650
diff changeset
462 end);