Annotate

plugins/muc/history.lib.lua @ 11883:97aa4b2fe4e0

doap: Bump XEP-0313 version for going Stable
author Kim Alvefur <zash@zash.se>
date Wed, 03 Nov 2021 14:36:58 +0100
parent 11711:6fbbfa4a1178
child 12977:74b9e05af71e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
1 -- Prosody IM
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
4 -- Copyright (C) 2014 Daurnimator
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
5 --
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
7 -- COPYING file in the source package for more information.
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
8 --
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
9
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
10 local gettime = os.time;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
11 local datetime = require "util.datetime";
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
12 local st = require "util.stanza";
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
13
6240
641756a6a5f7 plugins/muc: Move 'module:get_option_number("max_history_messages")' from mod_muc into history lib; remove from muclib exports
daurnimator <quae@daurnimator.com>
parents: 6231
diff changeset
14 local default_history_length = 20;
641756a6a5f7 plugins/muc: Move 'module:get_option_number("max_history_messages")' from mod_muc into history lib; remove from muclib exports
daurnimator <quae@daurnimator.com>
parents: 6231
diff changeset
15 local max_history_length = module:get_option_number("max_history_messages", math.huge);
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
16
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
17 local function set_max_history_length(_max_history_length)
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
18 max_history_length = _max_history_length or math.huge;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
19 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
20
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
21 local function get_historylength(room)
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
22 return math.min(room._data.history_length or default_history_length, max_history_length);
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
23 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
24
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
25 local function set_historylength(room, length)
6991
84e01dbb739e MUC: Update all config form handlers to take advantage of the new per-option events
Matthew Wild <mwild1@gmail.com>
parents: 6535
diff changeset
26 if length then
84e01dbb739e MUC: Update all config form handlers to take advantage of the new per-option events
Matthew Wild <mwild1@gmail.com>
parents: 6535
diff changeset
27 length = assert(tonumber(length), "Length not a valid number");
84e01dbb739e MUC: Update all config form handlers to take advantage of the new per-option events
Matthew Wild <mwild1@gmail.com>
parents: 6535
diff changeset
28 end
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
29 if length == default_history_length then length = nil; end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
30 room._data.history_length = length;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
31 return true;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
32 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
33
8794
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
34 -- Fix for clients who don't support XEP-0045 correctly
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
35 -- Default number of history messages the room returns
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
36 local function get_defaulthistorymessages(room)
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
37 return room._data.default_history_messages or default_history_length;
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
38 end
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
39 local function set_defaulthistorymessages(room, number)
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
40 number = math.min(tonumber(number) or default_history_length, room._data.history_length or default_history_length);
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
41 if number == default_history_length then
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
42 number = nil;
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
43 end
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
44 room._data.default_history_messages = number;
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
45 end
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
46
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
47 module:hook("muc-config-form", function(event)
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
48 table.insert(event.form, {
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
49 name = "muc#roomconfig_historylength";
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
50 type = "text-single";
10355
cb9755d7a36e MUC: Advertise history related fields as integers via XEP-0122
Kim Alvefur <zash@zash.se>
parents: 10230
diff changeset
51 datatype = "xs:integer";
9034
1c709e3d2e5e MUC: Improve labels of all config form items
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
52 label = "Maximum number of history messages returned by room";
1c709e3d2e5e MUC: Improve labels of all config form items
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
53 desc = "Specify the maximum number of previous messages that should be sent to users when they join the room";
10355
cb9755d7a36e MUC: Advertise history related fields as integers via XEP-0122
Kim Alvefur <zash@zash.se>
parents: 10230
diff changeset
54 value = get_historylength(event.room);
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
55 });
8794
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
56 table.insert(event.form, {
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
57 name = 'muc#roomconfig_defaulthistorymessages',
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
58 type = 'text-single',
10355
cb9755d7a36e MUC: Advertise history related fields as integers via XEP-0122
Kim Alvefur <zash@zash.se>
parents: 10230
diff changeset
59 datatype = "xs:integer";
9034
1c709e3d2e5e MUC: Improve labels of all config form items
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
60 label = 'Default number of history messages returned by room',
1c709e3d2e5e MUC: Improve labels of all config form items
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
61 desc = "Specify the number of previous messages sent to new users when they join the room";
10355
cb9755d7a36e MUC: Advertise history related fields as integers via XEP-0122
Kim Alvefur <zash@zash.se>
parents: 10230
diff changeset
62 value = get_defaulthistorymessages(event.room);
8794
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
63 });
9035
173c0e16e704 MUC: Add sections in room config form
Matthew Wild <mwild1@gmail.com>
parents: 9034
diff changeset
64 end, 70-5);
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
65
6991
84e01dbb739e MUC: Update all config form handlers to take advantage of the new per-option events
Matthew Wild <mwild1@gmail.com>
parents: 6535
diff changeset
66 module:hook("muc-config-submitted/muc#roomconfig_historylength", function(event)
84e01dbb739e MUC: Update all config form handlers to take advantage of the new per-option events
Matthew Wild <mwild1@gmail.com>
parents: 6535
diff changeset
67 if set_historylength(event.room, event.value) then
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
68 event.status_codes["104"] = true;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
69 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
70 end);
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
71
8794
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
72 module:hook("muc-config-submitted/muc#roomconfig_defaulthistorymessages", function(event)
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
73 if set_defaulthistorymessages(event.room, event.value) then
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
74 event.status_codes["104"] = true;
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
75 end
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
76 end);
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
77
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
78 local function parse_history(stanza)
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
79 local x_tag = stanza:get_child("x", "http://jabber.org/protocol/muc");
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
80 local history_tag = x_tag and x_tag:get_child("history", "http://jabber.org/protocol/muc");
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
81 if not history_tag then
8794
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
82 return nil, nil, nil;
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
83 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
84
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
85 local maxchars = tonumber(history_tag.attr.maxchars);
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
86
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
87 local maxstanzas = tonumber(history_tag.attr.maxstanzas);
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
88
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
89 -- messages received since the UTC datetime specified
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
90 local since = history_tag.attr.since;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
91 if since then
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
92 since = datetime.parse(since);
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
93 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
94
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
95 -- messages received in the last "X" seconds.
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
96 local seconds = tonumber(history_tag.attr.seconds);
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
97 if seconds then
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
98 seconds = gettime() - seconds;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
99 if since then
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
100 since = math.max(since, seconds);
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
101 else
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
102 since = seconds;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
103 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
104 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
105
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
106 return maxchars, maxstanzas, since;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
107 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
108
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
109 module:hook("muc-get-history", function(event)
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
110 local room = event.room;
7350
24e2369b67f9 MUC: Move history to room._history
Kim Alvefur <zash@zash.se>
parents: 7086
diff changeset
111 local history = room._history; -- send discussion history
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
112 if not history then return nil end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
113 local history_len = #history;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
114
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
115 local to = event.to;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
116 local maxchars = event.maxchars;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
117 local maxstanzas = event.maxstanzas or history_len;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
118 local since = event.since;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
119 local n = 0;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
120 local charcount = 0;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
121 for i=history_len,1,-1 do
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
122 local entry = history[i];
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
123 if maxchars then
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
124 if not entry.chars then
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
125 entry.stanza.attr.to = "";
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
126 entry.chars = #tostring(entry.stanza);
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
127 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
128 charcount = charcount + entry.chars + #to;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
129 if charcount > maxchars then break; end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
130 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
131 if since and since > entry.timestamp then break; end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
132 if n + 1 > maxstanzas then break; end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
133 n = n + 1;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
134 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
135
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
136 local i = history_len-n+1
7086
6cc7c9da29ed MUC: Rename variables to please luacheck
Kim Alvefur <zash@zash.se>
parents: 6991
diff changeset
137 function event.next_stanza()
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
138 if i > history_len then return nil end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
139 local entry = history[i];
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
140 local msg = entry.stanza;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
141 msg.attr.to = to;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
142 i = i + 1;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
143 return msg;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
144 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
145 return true;
8027
c2e7dfd87abb MUC: Decrement priority muc-get-history hook to standard for core modules
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
146 end, -1);
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
147
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
148 local function send_history(room, stanza)
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
149 local maxchars, maxstanzas, since = parse_history(stanza);
8794
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
150 if not(maxchars or maxstanzas or since) then
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
151 maxstanzas = get_defaulthistorymessages(room);
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
152 end
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
153 local event = {
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
154 room = room;
8028
adfc7f3b29ce MUC: Include original stanza in send history event
Kim Alvefur <zash@zash.se>
parents: 8027
diff changeset
155 stanza = stanza;
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
156 to = stanza.attr.from; -- `to` is required to calculate the character count for `maxchars`
8794
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
157 maxchars = maxchars,
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
158 maxstanzas = maxstanzas,
0e2c1c4d4f78 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8780
diff changeset
159 since = since;
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
160 next_stanza = function() end; -- events should define this iterator
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
161 };
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
162 module:fire_event("muc-get-history", event);
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
163 for msg in event.next_stanza, event do
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
164 room:route_stanza(msg);
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
165 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
166 end
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
167
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
168 -- Send history on join
6277
f2c9c36979b3 plugins/muc: Fix use of incorrect event on occupant join
daurnimator <quae@daurnimator.com>
parents: 6240
diff changeset
169 module:hook("muc-occupant-session-new", function(event)
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
170 send_history(event.room, event.stanza);
6231
bc12a8253f94 plugins/muc/muc.lib: Move sending of occupant list to joining user out of hook, and into main flow: It has to occur before publication of their status
daurnimator <quae@daurnimator.com>
parents: 6215
diff changeset
171 end, 50); -- Before subject(20)
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
172
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
173 -- add to history
6535
0f940a7ba489 mod_muc: Add muc-add-history event to allow modules to override default history storage
Matthew Wild <mwild1@gmail.com>
parents: 6277
diff changeset
174 module:hook("muc-add-history", function(event)
8779
11b4ae162db7 MUC: Move condition for what gets added to history so that other modules benefit (thanks jcbrand)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
175 local room = event.room
11711
6fbbfa4a1178 MUC: Skip adding to history when it's set to zero
Kim Alvefur <zash@zash.se>
parents: 11180
diff changeset
176 if get_historylength(room) == 0 then
6fbbfa4a1178 MUC: Skip adding to history when it's set to zero
Kim Alvefur <zash@zash.se>
parents: 11180
diff changeset
177 room._history = nil;
6fbbfa4a1178 MUC: Skip adding to history when it's set to zero
Kim Alvefur <zash@zash.se>
parents: 11180
diff changeset
178 return;
6fbbfa4a1178 MUC: Skip adding to history when it's set to zero
Kim Alvefur <zash@zash.se>
parents: 11180
diff changeset
179 end
8779
11b4ae162db7 MUC: Move condition for what gets added to history so that other modules benefit (thanks jcbrand)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
180 local history = room._history;
11b4ae162db7 MUC: Move condition for what gets added to history so that other modules benefit (thanks jcbrand)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
181 if not history then history = {}; room._history = history; end
11b4ae162db7 MUC: Move condition for what gets added to history so that other modules benefit (thanks jcbrand)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
182 local stanza = st.clone(event.stanza);
11b4ae162db7 MUC: Move condition for what gets added to history so that other modules benefit (thanks jcbrand)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
183 stanza.attr.to = "";
11b4ae162db7 MUC: Move condition for what gets added to history so that other modules benefit (thanks jcbrand)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
184 local ts = gettime();
11b4ae162db7 MUC: Move condition for what gets added to history so that other modules benefit (thanks jcbrand)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
185 local stamp = datetime.datetime(ts);
9081
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9035
diff changeset
186 stanza:tag("delay", { -- XEP-0203
10230
dd7e924c74ef MUC: Fix delay@from to be room JID (fixes #1416)
Kim Alvefur <zash@zash.se>
parents: 9081
diff changeset
187 xmlns = "urn:xmpp:delay", from = room.jid, stamp = stamp
9081
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9035
diff changeset
188 }):up();
8779
11b4ae162db7 MUC: Move condition for what gets added to history so that other modules benefit (thanks jcbrand)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
189 local entry = { stanza = stanza, timestamp = ts };
11b4ae162db7 MUC: Move condition for what gets added to history so that other modules benefit (thanks jcbrand)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
190 table.insert(history, entry);
11b4ae162db7 MUC: Move condition for what gets added to history so that other modules benefit (thanks jcbrand)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
191 while #history > get_historylength(room) do table.remove(history, 1) end
6535
0f940a7ba489 mod_muc: Add muc-add-history event to allow modules to override default history storage
Matthew Wild <mwild1@gmail.com>
parents: 6277
diff changeset
192 return true;
0f940a7ba489 mod_muc: Add muc-add-history event to allow modules to override default history storage
Matthew Wild <mwild1@gmail.com>
parents: 6277
diff changeset
193 end, -1);
0f940a7ba489 mod_muc: Add muc-add-history event to allow modules to override default history storage
Matthew Wild <mwild1@gmail.com>
parents: 6277
diff changeset
194
0f940a7ba489 mod_muc: Add muc-add-history event to allow modules to override default history storage
Matthew Wild <mwild1@gmail.com>
parents: 6277
diff changeset
195 -- Have a single muc-add-history event, so that plugins can mark it
0f940a7ba489 mod_muc: Add muc-add-history event to allow modules to override default history storage
Matthew Wild <mwild1@gmail.com>
parents: 6277
diff changeset
196 -- as handled without stopping other muc-broadcast-message handlers
0f940a7ba489 mod_muc: Add muc-add-history event to allow modules to override default history storage
Matthew Wild <mwild1@gmail.com>
parents: 6277
diff changeset
197 module:hook("muc-broadcast-message", function(event)
8780
4cab4ee5dfcc MUC: Introduce an event to allow plugins to influence which messages are added to history
Kim Alvefur <zash@zash.se>
parents: 8779
diff changeset
198 if module:fire_event("muc-message-is-historic", event) then
8779
11b4ae162db7 MUC: Move condition for what gets added to history so that other modules benefit (thanks jcbrand)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
199 module:fire_event("muc-add-history", event);
11b4ae162db7 MUC: Move condition for what gets added to history so that other modules benefit (thanks jcbrand)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
200 end
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
201 end);
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
202
8780
4cab4ee5dfcc MUC: Introduce an event to allow plugins to influence which messages are added to history
Kim Alvefur <zash@zash.se>
parents: 8779
diff changeset
203 module:hook("muc-message-is-historic", function (event)
10775
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
204 local stanza = event.stanza;
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
205 if stanza:get_child("no-store", "urn:xmpp:hints")
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
206 or stanza:get_child("no-permanent-store", "urn:xmpp:hints") then
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
207 -- XXX Experimental XEP
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
208 return false, "hint";
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
209 end
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
210 if stanza:get_child("store", "urn:xmpp:hints") then
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
211 return true, "hint";
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
212 end
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
213 if stanza:get_child("body") then
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
214 return true;
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
215 end
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
216 if stanza:get_child("encryption", "urn:xmpp:eme:0") then
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
217 -- Since we can't know what an encrypted message contains, we assume it's important
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
218 -- XXX Experimental XEP
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
219 return true, "encrypted";
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
220 end
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
221 if stanza:get_child(nil, "urn:xmpp:chat-markers:0") then
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
222 -- XXX Experimental XEP
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
223 return true, "marker";
1641e33f1d36 MUC: Adapt rules for what should be stored from mod_mam
Kim Alvefur <zash@zash.se>
parents: 10355
diff changeset
224 end
8780
4cab4ee5dfcc MUC: Introduce an event to allow plugins to influence which messages are added to history
Kim Alvefur <zash@zash.se>
parents: 8779
diff changeset
225 end, -1);
4cab4ee5dfcc MUC: Introduce an event to allow plugins to influence which messages are added to history
Kim Alvefur <zash@zash.se>
parents: 8779
diff changeset
226
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
227 return {
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
228 set_max_length = set_max_history_length;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
229 parse_history = parse_history;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
230 send = send_history;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
231 get_length = get_historylength;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
232 set_length = set_historylength;
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
233 };