2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2014 Daurnimator
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 local gettime = os.time;
11 local datetime = require "util.datetime";
12 local st = require "util.stanza";
14 local default_history_length = 20;
15 local max_history_length = module:get_option_number("max_history_messages", math.huge);
17 local function set_max_history_length(_max_history_length)
18 max_history_length = _max_history_length or math.huge;
21 local function get_historylength(room)
22 return math.min(room._data.history_length or default_history_length, max_history_length);
25 local function set_historylength(room, length)
26 length = assert(tonumber(length), "Length not a valid number");
27 if length == default_history_length then length = nil; end
28 room._data.history_length = length;
32 module:hook("muc-config-form", function(event)
33 table.insert(event.form, {
34 name = "muc#roomconfig_historylength";
36 label = "Maximum Number of History Messages Returned by Room";
37 value = tostring(get_historylength(event.room));
41 module:hook("muc-config-submitted", function(event)
42 local new = event.fields["muc#roomconfig_historylength"];
43 if new ~= nil and set_historylength(event.room, new) then
44 event.status_codes["104"] = true;
48 local function parse_history(stanza)
49 local x_tag = stanza:get_child("x", "http://jabber.org/protocol/muc");
50 local history_tag = x_tag and x_tag:get_child("history", "http://jabber.org/protocol/muc");
51 if not history_tag then
52 return nil, default_history_length, nil;
55 local maxchars = tonumber(history_tag.attr.maxchars);
57 local maxstanzas = tonumber(history_tag.attr.maxstanzas);
59 -- messages received since the UTC datetime specified
60 local since = history_tag.attr.since;
62 since = datetime.parse(since);
65 -- messages received in the last "X" seconds.
66 local seconds = tonumber(history_tag.attr.seconds);
68 seconds = gettime() - seconds;
70 since = math.max(since, seconds);
76 return maxchars, maxstanzas, since;
79 module:hook("muc-get-history", function(event)
80 local room = event.room;
81 local history = room._data["history"]; -- send discussion history
82 if not history then return nil end
83 local history_len = #history;
86 local maxchars = event.maxchars;
87 local maxstanzas = event.maxstanzas or history_len;
88 local since = event.since;
91 for i=history_len,1,-1 do
92 local entry = history[i];
94 if not entry.chars then
95 entry.stanza.attr.to = "";
96 entry.chars = #tostring(entry.stanza);
98 charcount = charcount + entry.chars + #to;
99 if charcount > maxchars then break; end
101 if since and since > entry.timestamp then break; end
102 if n + 1 > maxstanzas then break; end
106 local i = history_len-n+1
107 function event:next_stanza()
108 if i > history_len then return nil end
109 local entry = history[i];
110 local msg = entry.stanza;
118 local function send_history(room, stanza)
119 local maxchars, maxstanzas, since = parse_history(stanza);
122 to = stanza.attr.from; -- `to` is required to calculate the character count for `maxchars`
123 maxchars = maxchars, maxstanzas = maxstanzas, since = since;
124 next_stanza = function() end; -- events should define this iterator
126 module:fire_event("muc-get-history", event);
127 for msg in event.next_stanza, event do
128 room:route_stanza(msg);
132 -- Send history on join
133 module:hook("muc-occupant-session-new", function(event)
134 send_history(event.room, event.stanza);
135 end, 50); -- Before subject(20)
138 module:hook("muc-add-history", function(event)
139 local historic = event.stanza:get_child("body");
141 local room = event.room
142 local history = room._data["history"];
143 if not history then history = {}; room._data["history"] = history; end
144 local stanza = st.clone(event.stanza);
146 local ts = gettime();
147 local stamp = datetime.datetime(ts);
148 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = module.host, stamp = stamp}):up(); -- XEP-0203
149 stanza:tag("x", {xmlns = "jabber:x:delay", from = module.host, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
150 local entry = { stanza = stanza, timestamp = ts };
151 table.insert(history, entry);
152 while #history > get_historylength(room) do table.remove(history, 1) end
157 -- Have a single muc-add-history event, so that plugins can mark it
158 -- as handled without stopping other muc-broadcast-message handlers
159 module:hook("muc-broadcast-message", function(event)
160 module:fire_event("muc-add-history", event);
164 set_max_length = set_max_history_length;
165 parse_history = parse_history;
167 get_length = get_historylength;
168 set_length = set_historylength;