Mercurial > prosody-modules
annotate mod_mam_muc/mod_mam_muc.lua @ 1278:40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 18 Jan 2014 21:14:40 +0100 |
parents | 999891a9ae5d |
children | 2118a2eeb1d5 |
rev | line source |
---|---|
820 | 1 -- XEP-0313: Message Archive Management for Prosody |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
2 -- Copyright (C) 2011-2013 Kim Alvefur |
820 | 3 -- |
4 -- This file is MIT/X11 licensed. | |
5 | |
6 local xmlns_mam = "urn:xmpp:mam:tmp"; | |
7 local xmlns_delay = "urn:xmpp:delay"; | |
8 local xmlns_forward = "urn:xmpp:forward:0"; | |
1143
8098683b6d6f
mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents:
1142
diff
changeset
|
9 local muc_form_config_option = "muc#roomconfig_enablelogging" |
820 | 10 |
11 local st = require "util.stanza"; | |
12 local rsm = module:require "mod_mam/rsm"; | |
13 local jid_bare = require "util.jid".bare; | |
14 local jid_split = require "util.jid".split; | |
1278
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
15 local room_mt = module:depends"muc".room_mt; |
820 | 16 |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
17 local getmetatable = getmetatable; |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
18 local function is_stanza(x) |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
19 return getmetatable(x) == st.stanza_mt; |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
20 end |
820 | 21 |
22 local tostring = tostring; | |
23 local time_now = os.time; | |
24 local m_min = math.min; | |
25 local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse; | |
26 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50); | |
1278
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
27 local max_history_length = module:get_option_number("max_history_messages", 1000); |
820 | 28 |
1143
8098683b6d6f
mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents:
1142
diff
changeset
|
29 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
|
30 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
|
31 local advertise_archive = module:get_option_boolean("muc_log_advertise", true); |
fabdaa0d99e3
mod_mam_muc: Stap archived messages
Kim Alvefur <zash@zash.se>
parents:
1141
diff
changeset
|
32 |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
33 local archive = module:open_store("archive2", "archive"); |
1139
b32d65e41755
mod_mam_muc: Get room objects in a less awkward fashion
Kim Alvefur <zash@zash.se>
parents:
1138
diff
changeset
|
34 local rooms = hosts[module.host].modules.muc.rooms; |
820 | 35 |
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
|
36 if not log_all_rooms then |
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
|
37 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
|
38 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
|
39 local logging_enabled = room._data.logging; |
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
|
40 if logging_enabled == nil then |
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
|
41 logging_enabled = log_by_default; |
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
|
42 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
|
43 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
|
44 { |
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
|
45 name = muc_form_config_option, |
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
|
46 type = "boolean", |
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
|
47 label = "Enable Logging?", |
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
|
48 value = logging_enabled, |
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
|
49 } |
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
|
50 ); |
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
|
51 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
|
52 |
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
|
53 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
|
54 local room, fields, changed = event.room, event.fields, event.changed; |
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
|
55 local new = fields[muc_form_config_option]; |
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
|
56 if new ~= room._data.logging then |
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
|
57 room._data.logging = new; |
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
|
58 if type(changed) == "table" then |
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
|
59 changed[muc_form_config_option] = 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 |
1278
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
67 local _send_history = room_mt.send_history; |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
68 function module.unload() |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
69 room_mt.send_history = _send_history; |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
70 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
|
71 |
820 | 72 -- Handle archive queries |
1138
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
73 module:hook("iq-get/bare/"..xmlns_mam..":query", function(event) |
820 | 74 local origin, stanza = event.origin, event.stanza; |
1144
ccb0c5afe658
mod_mam_muc: Fix room lookup, should be indexed by bare jid
Kim Alvefur <zash@zash.se>
parents:
1143
diff
changeset
|
75 local room = stanza.attr.to; |
1146
9fa89dc7a86f
mod_mam_muc: Search the rooms archive correctly (copypaste error from mod_mam)
Kim Alvefur <zash@zash.se>
parents:
1145
diff
changeset
|
76 local room_node = jid_split(room); |
820 | 77 local query = stanza.tags[1]; |
78 | |
1139
b32d65e41755
mod_mam_muc: Get room objects in a less awkward fashion
Kim Alvefur <zash@zash.se>
parents:
1138
diff
changeset
|
79 local room_obj = rooms[room]; |
820 | 80 if not room_obj then |
1145
5a00f9bec6e7
mod_mam_muc: Send item-not-found if the requested room does not exist
Kim Alvefur <zash@zash.se>
parents:
1144
diff
changeset
|
81 return origin.send(st.error_reply(stanza, "cancel", "item-not-found")) |
820 | 82 end |
83 local from = jid_bare(stanza.attr.from); | |
84 | |
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
|
85 -- Banned or not a member of a members-only room? |
820 | 86 if room_obj._affiliations[from] == "outcast" |
87 or room_obj._data.members_only and not room_obj._affiliations[from] then | |
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
|
88 return origin.send(st.error_reply(stanza, "auth", "forbidden")) |
820 | 89 end |
90 | |
1138
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
91 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
|
92 |
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
93 -- Search query parameters |
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
94 local qstart = query:get_child_text("start"); |
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
95 local qend = query:get_child_text("end"); |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
96 module:log("debug", "Archive query, id %s from %s until %s)", |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
97 tostring(qid), qstart or "the dawn of time", qend or "now"); |
820 | 98 |
1138
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
99 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
|
100 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
|
101 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
|
102 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp")) |
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
103 return true |
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
104 end |
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
105 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
|
106 end |
820 | 107 |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
108 -- RSM stuff |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
109 local qset = rsm.get(query); |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
110 local qmax = m_min(qset and qset.max or default_max_items, max_max_items); |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
111 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
|
112 |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
113 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
|
114 if type(before) ~= "string" then before = nil; end |
820 | 115 |
1138
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
116 -- 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
|
117 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
|
118 start = qstart; ["end"] = qend; -- Time range |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
119 limit = qmax; |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
120 before = before; after = after; |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
121 reverse = reverse; |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
122 total = true; |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
123 }); |
1138
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
124 |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
125 if not data then |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
126 return origin.send(st.error_reply(stanza, "cancel", "internal-server-error")); |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
127 end |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
128 local count = err; |
1138
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
129 |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
130 -- Wrap it in stuff and deliver |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
131 local first, last; |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
132 for id, item, when in data do |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
133 local fwd_st = st.message{ to = origin.full_jid } |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
134 :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
|
135 :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
|
136 :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
|
137 |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
138 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
|
139 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
|
140 end |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
141 item.attr.xmlns = "jabber:client"; |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
142 fwd_st:add_child(item); |
820 | 143 |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
144 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
|
145 last = id; |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
146 |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
147 origin.send(fwd_st); |
1138
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
148 end |
5c97ee75cadb
mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents:
820
diff
changeset
|
149 -- 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
|
150 module:log("debug", "Archive query %s completed", tostring(qid)); |
820 | 151 |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
152 if reverse then first, last = last, first; end |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
153 return origin.send(st.reply(stanza) |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
154 :query(xmlns_mam):add_child(rsm.generate { |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
155 first = first, last = last, count = count })); |
820 | 156 end); |
157 | |
1278
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
158 function room_mt:send_history(to, stanza) |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 if since then |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
169 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
|
170 end |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
171 if seconds then |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
172 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
|
173 end |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
174 end |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
175 |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
176 -- Load all the data! |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
177 local data, err = archive:find(jid_split(self.jid), { |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
178 limit = m_min(maxstanzas or 20, max_history_length); |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
179 after = since; |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
180 reverse = true; |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
181 }); |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
182 |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
183 if not data then |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
184 module:log("error", "Could not fetch history: %s", tostring(err)); |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
185 return |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
186 end |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
187 |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
188 local to_send = {}; |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
189 local charcount = 0; |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
190 local chars; |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
191 for id, item, when in data do |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
192 item.attr.to = to; |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
193 item:tag("delay", { xmlns = "urn:xmpp:delay", from = self.jid, stamp = timestamp(when) }):up(); -- XEP-0203 |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
194 if maxchars then |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
195 chars = #tostring(item); |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
196 if chars + charcount > maxchars then break end |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
197 charcount = charcount + chars; |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
198 end |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
199 to_send[1+#to_send] = item; |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
200 end |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
201 for i = #to_send,1,-1 do |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
202 self:_route_stanza(to_send[i]); |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
203 end |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
204 end |
40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
Kim Alvefur <zash@zash.se>
parents:
1277
diff
changeset
|
205 |
820 | 206 -- Handle messages |
207 local function message_handler(event) | |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
208 local stanza = event.stanza; |
820 | 209 local orig_type = stanza.attr.type or "normal"; |
210 local orig_to = stanza.attr.to; | |
211 local orig_from = stanza.attr.from; | |
212 | |
213 -- Only store groupchat messages | |
214 if not (orig_type == "groupchat" and (stanza:get_child("body") or stanza:get_child("subject"))) then | |
215 return; | |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
216 -- Chat states and other non-content messages, what TODO? |
820 | 217 end |
218 | |
219 local room = jid_split(orig_to); | |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
220 local room_obj = rooms[orig_to] |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
221 if not room_obj then return end -- No such room |
820 | 222 |
1143
8098683b6d6f
mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents:
1142
diff
changeset
|
223 if not ( log_all_rooms == true -- Logging forced on all rooms |
8098683b6d6f
mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents:
1142
diff
changeset
|
224 or (room_obj._data.logging == nil and log_by_default == true) |
8098683b6d6f
mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents:
1142
diff
changeset
|
225 or room_obj._data.logging ) then return end -- Don't log |
8098683b6d6f
mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents:
1142
diff
changeset
|
226 |
820 | 227 local nick = room_obj._jid_nick[orig_from]; |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
228 if not nick then return end -- Message from someone not in the room? |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
229 |
820 | 230 stanza.attr.from = nick; |
231 -- And stash it | |
1142
fabdaa0d99e3
mod_mam_muc: Stap archived messages
Kim Alvefur <zash@zash.se>
parents:
1141
diff
changeset
|
232 local ok, id = archive:append(room, time_now(), "", stanza); |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
233 stanza.attr.from = orig_from; |
1142
fabdaa0d99e3
mod_mam_muc: Stap archived messages
Kim Alvefur <zash@zash.se>
parents:
1141
diff
changeset
|
234 if ok and advertise_archive then |
fabdaa0d99e3
mod_mam_muc: Stap archived messages
Kim Alvefur <zash@zash.se>
parents:
1141
diff
changeset
|
235 stanza:tag("archived", { xmlns = xmlns_mam, by = jid_bare(orig_to), id = id }):up(); |
fabdaa0d99e3
mod_mam_muc: Stap archived messages
Kim Alvefur <zash@zash.se>
parents:
1141
diff
changeset
|
236 end |
820 | 237 end |
238 | |
239 module:hook("message/bare", message_handler, 2); | |
240 | |
1277
999891a9ae5d
mod_mam_muc: Remove archives when a room is destroyed
Kim Alvefur <zash@zash.se>
parents:
1276
diff
changeset
|
241 module:hook("muc-room-destroyed", function(event) |
999891a9ae5d
mod_mam_muc: Remove archives when a room is destroyed
Kim Alvefur <zash@zash.se>
parents:
1276
diff
changeset
|
242 archive:delete(jid_split(event.room.jid)); |
999891a9ae5d
mod_mam_muc: Remove archives when a room is destroyed
Kim Alvefur <zash@zash.se>
parents:
1276
diff
changeset
|
243 end); |
999891a9ae5d
mod_mam_muc: Remove archives when a room is destroyed
Kim Alvefur <zash@zash.se>
parents:
1276
diff
changeset
|
244 |
1141
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
245 -- TODO should we perhaps log presence as well? |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
246 -- And role/affiliation changes? |
1091be1c3aba
mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents:
1140
diff
changeset
|
247 |
820 | 248 module:add_feature(xmlns_mam); |