Comparison

plugins/mod_muc_mam.lua @ 8723:f3bdb20214ab

mod_muc_mam: Remove 0.10 compat code
author Kim Alvefur <zash@zash.se>
date Tue, 12 Dec 2017 20:25:56 +0100
parent 8722:7ee93b3fa160
child 8724:cfcc78c50905
comparison
equal deleted inserted replaced
8722:7ee93b3fa160 8723:f3bdb20214ab
19 local rsm = require "util.rsm"; 19 local rsm = require "util.rsm";
20 local jid_bare = require "util.jid".bare; 20 local jid_bare = require "util.jid".bare;
21 local jid_split = require "util.jid".split; 21 local jid_split = require "util.jid".split;
22 local jid_prep = require "util.jid".prep; 22 local jid_prep = require "util.jid".prep;
23 local dataform = require "util.dataforms".new; 23 local dataform = require "util.dataforms".new;
24 local it = require"util.iterators"; 24
25
26 -- Support both old and new MUC code
27 local mod_muc = module:depends"muc"; 25 local mod_muc = module:depends"muc";
28 local rooms = rawget(mod_muc, "rooms"); 26 local get_room_from_jid = mod_muc.get_room_from_jid;
29 local each_room = rawget(mod_muc, "each_room") or function() return it.values(rooms); end;
30 local new_muc = not rooms;
31 if new_muc then
32 rooms = module:shared"muc/rooms";
33 else
34 -- COMPAT: We don't (currently?) support injecting stanza-id
35 -- on Prosody 0.10 and prior, which is required by mam:2
36 xmlns_mam = "urn:xmpp:mam:1";
37 end
38 local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
39 function (jid)
40 return rooms[jid];
41 end
42 27
43 local is_stanza = st.is_stanza; 28 local is_stanza = st.is_stanza;
44 local tostring = tostring; 29 local tostring = tostring;
45 local time_now = os.time; 30 local time_now = os.time;
46 local m_min = math.min; 31 local m_min = math.min;
79 local enabled = room._data.archiving; 64 local enabled = room._data.archiving;
80 if enabled == nil then 65 if enabled == nil then
81 return log_by_default; 66 return log_by_default;
82 end 67 end
83 return enabled; 68 return enabled;
84 end
85
86 local send_history, save_to_history;
87
88 -- Override history methods for all rooms.
89 if not new_muc then -- 0.10 or older
90 module:hook("muc-room-created", function (event)
91 local room = event.room;
92 if archiving_enabled(room) then
93 room.send_history = send_history;
94 room.save_to_history = save_to_history;
95 end
96 end);
97
98 function module.load()
99 for room in each_room() do
100 if archiving_enabled(room) then
101 room.send_history = send_history;
102 room.save_to_history = save_to_history;
103 end
104 end
105 end
106 function module.unload()
107 for room in each_room() do
108 if room.send_history == send_history then
109 room.send_history = nil;
110 room.save_to_history = nil;
111 end
112 end
113 end
114 end 69 end
115 70
116 if not log_all_rooms then 71 if not log_all_rooms then
117 module:hook("muc-config-form", function(event) 72 module:hook("muc-config-form", function(event)
118 local room, form = event.room, event.form; 73 local room, form = event.room, event.form;
124 value = archiving_enabled(room), 79 value = archiving_enabled(room),
125 } 80 }
126 ); 81 );
127 end); 82 end);
128 83
129 module:hook("muc-config-submitted", function(event) 84 module:hook("muc-config-submitted/"..muc_form_enable, function(event)
130 local room, fields, changed = event.room, event.fields, event.changed; 85 event.room._data.archiving = event.value;
131 local new = fields[muc_form_enable];
132 if new ~= room._data.archiving then
133 room._data.archiving = new;
134 if type(changed) == "table" then
135 changed[muc_form_enable] = true;
136 else
137 event.changed = true;
138 end
139 if new then
140 room.send_history = send_history;
141 room.save_to_history = save_to_history;
142 else
143 room.send_history = nil;
144 room.save_to_history = nil;
145 end
146 end
147 end); 86 end);
148 end 87 end
149 88
150 -- Note: We ignore the 'with' field as this is internally used for stanza types 89 -- Note: We ignore the 'with' field as this is internally used for stanza types
151 local query_form = dataform { 90 local query_form = dataform {
350 return history[i]; 289 return history[i];
351 end 290 end
352 return true; 291 return true;
353 end, 1); 292 end, 1);
354 293
355 function send_history(self, to, stanza)
356 local maxchars, maxstanzas, seconds, since;
357 local history_tag = stanza:find("{http://jabber.org/protocol/muc}x/history")
358 if history_tag then
359 module:log("debug", tostring(history_tag));
360 local history_attr = history_tag.attr;
361 maxchars = tonumber(history_attr.maxchars);
362 maxstanzas = tonumber(history_attr.maxstanzas);
363 seconds = tonumber(history_attr.seconds);
364 since = history_attr.since;
365 if since then
366 since = timestamp_parse(since);
367 end
368 if seconds then
369 since = math.max(os.time() - seconds, since or 0);
370 end
371 end
372
373 local event = {
374 room = self;
375 to = to; -- `to` is required to calculate the character count for `maxchars`
376 maxchars = maxchars, maxstanzas = maxstanzas, since = since;
377 next_stanza = function() end; -- events should define this iterator
378 };
379
380 module:fire_event("muc-get-history", event);
381
382 for msg in event.next_stanza, event do
383 self:_route_stanza(msg);
384 end
385 end
386
387 -- Handle messages 294 -- Handle messages
388 function save_to_history(self, stanza) 295 local function save_to_history(self, stanza)
389 local room_node, room_host = jid_split(self.jid); 296 local room_node, room_host = jid_split(self.jid);
390 297
391 -- Filter out <stanza-id> that claim to be from us 298 -- Filter out <stanza-id> that claim to be from us
392 stanza:maptags(function (tag) 299 stanza:maptags(function (tag)
393 if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id 300 if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id