comparison plugins/muc/history.lib.lua @ 6609:d2faaaca695d

Merge 0.10->trunk
author Matthew Wild <mwild1@gmail.com>
date Fri, 27 Mar 2015 22:24:57 +0000
parents 0f940a7ba489
children 84e01dbb739e
comparison
equal deleted inserted replaced
6608:b6e558febb7a 6609:d2faaaca695d
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2014 Daurnimator
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
10 local gettime = os.time;
11 local datetime = require "util.datetime";
12 local st = require "util.stanza";
13
14 local default_history_length = 20;
15 local max_history_length = module:get_option_number("max_history_messages", math.huge);
16
17 local function set_max_history_length(_max_history_length)
18 max_history_length = _max_history_length or math.huge;
19 end
20
21 local function get_historylength(room)
22 return math.min(room._data.history_length or default_history_length, max_history_length);
23 end
24
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;
29 return true;
30 end
31
32 module:hook("muc-config-form", function(event)
33 table.insert(event.form, {
34 name = "muc#roomconfig_historylength";
35 type = "text-single";
36 label = "Maximum Number of History Messages Returned by Room";
37 value = tostring(get_historylength(event.room));
38 });
39 end);
40
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;
45 end
46 end);
47
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;
53 end
54
55 local maxchars = tonumber(history_tag.attr.maxchars);
56
57 local maxstanzas = tonumber(history_tag.attr.maxstanzas);
58
59 -- messages received since the UTC datetime specified
60 local since = history_tag.attr.since;
61 if since then
62 since = datetime.parse(since);
63 end
64
65 -- messages received in the last "X" seconds.
66 local seconds = tonumber(history_tag.attr.seconds);
67 if seconds then
68 seconds = gettime() - seconds;
69 if since then
70 since = math.max(since, seconds);
71 else
72 since = seconds;
73 end
74 end
75
76 return maxchars, maxstanzas, since;
77 end
78
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;
84
85 local to = event.to;
86 local maxchars = event.maxchars;
87 local maxstanzas = event.maxstanzas or history_len;
88 local since = event.since;
89 local n = 0;
90 local charcount = 0;
91 for i=history_len,1,-1 do
92 local entry = history[i];
93 if maxchars then
94 if not entry.chars then
95 entry.stanza.attr.to = "";
96 entry.chars = #tostring(entry.stanza);
97 end
98 charcount = charcount + entry.chars + #to;
99 if charcount > maxchars then break; end
100 end
101 if since and since > entry.timestamp then break; end
102 if n + 1 > maxstanzas then break; end
103 n = n + 1;
104 end
105
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;
111 msg.attr.to = to;
112 i = i + 1;
113 return msg;
114 end
115 return true;
116 end);
117
118 local function send_history(room, stanza)
119 local maxchars, maxstanzas, since = parse_history(stanza);
120 local event = {
121 room = room;
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
125 };
126 module:fire_event("muc-get-history", event);
127 for msg in event.next_stanza, event do
128 room:route_stanza(msg);
129 end
130 end
131
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)
136
137 -- add to history
138 module:hook("muc-add-history", function(event)
139 local historic = event.stanza:get_child("body");
140 if historic then
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);
145 stanza.attr.to = "";
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
153 end
154 return true;
155 end, -1);
156
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);
161 end);
162
163 return {
164 set_max_length = set_max_history_length;
165 parse_history = parse_history;
166 send = send_history;
167 get_length = get_historylength;
168 set_length = set_historylength;
169 };