comparison plugins/muc/muc.lib.lua @ 4797:e239668aa6d2

Merge 0.9->trunk
author Matthew Wild <mwild1@gmail.com>
date Sun, 29 Apr 2012 02:10:55 +0100
parents 36234dc4b177
children 4905aed00382
comparison
equal deleted inserted replaced
4796:04a34287dc12 4797:e239668aa6d2
7 -- 7 --
8 8
9 local select = select; 9 local select = select;
10 local pairs, ipairs = pairs, ipairs; 10 local pairs, ipairs = pairs, ipairs;
11 11
12 local datamanager = require "util.datamanager";
13 local datetime = require "util.datetime"; 12 local datetime = require "util.datetime";
14 13
15 local dataform = require "util.dataforms"; 14 local dataform = require "util.dataforms";
16 15
17 local jid_split = require "util.jid".split; 16 local jid_split = require "util.jid".split;
18 local jid_bare = require "util.jid".bare; 17 local jid_bare = require "util.jid".bare;
19 local jid_prep = require "util.jid".prep; 18 local jid_prep = require "util.jid".prep;
20 local st = require "util.stanza"; 19 local st = require "util.stanza";
21 local log = require "util.logger".init("mod_muc"); 20 local log = require "util.logger".init("mod_muc");
22 local multitable_new = require "util.multitable".new;
23 local t_insert, t_remove = table.insert, table.remove; 21 local t_insert, t_remove = table.insert, table.remove;
24 local setmetatable = setmetatable; 22 local setmetatable = setmetatable;
25 local base64 = require "util.encodings".base64; 23 local base64 = require "util.encodings".base64;
26 local md5 = require "util.hashes".md5; 24 local md5 = require "util.hashes".md5;
27 25
131 local history = self._data['history']; 129 local history = self._data['history'];
132 if not history then history = {}; self._data['history'] = history; end 130 if not history then history = {}; self._data['history'] = history; end
133 stanza = st.clone(stanza); 131 stanza = st.clone(stanza);
134 stanza.attr.to = ""; 132 stanza.attr.to = "";
135 local stamp = datetime.datetime(); 133 local stamp = datetime.datetime();
136 local chars = #tostring(stanza);
137 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = stamp}):up(); -- XEP-0203 134 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = stamp}):up(); -- XEP-0203
138 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated) 135 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
139 local entry = { stanza = stanza, stamp = stamp }; 136 local entry = { stanza = stanza, stamp = stamp };
140 t_insert(history, entry); 137 t_insert(history, entry);
141 while #history > (self._data.history_length or default_history_length) do t_remove(history, 1) end 138 while #history > self._data.history_length do t_remove(history, 1) end
142 end 139 end
143 end 140 end
144 function room_mt:broadcast_except_nick(stanza, nick) 141 function room_mt:broadcast_except_nick(stanza, nick)
145 for rnick, occupant in pairs(self._occupants) do 142 for rnick, occupant in pairs(self._occupants) do
146 if rnick ~= nick then 143 if rnick ~= nick then
183 if since then since = datetime.parse(since); since = since and datetime.datetime(since); end 180 if since then since = datetime.parse(since); since = since and datetime.datetime(since); end
184 if seconds and (not since or since < seconds) then since = seconds; end 181 if seconds and (not since or since < seconds) then since = seconds; end
185 182
186 local n = 0; 183 local n = 0;
187 local charcount = 0; 184 local charcount = 0;
188 local stanzacount = 0;
189 185
190 for i=#history,1,-1 do 186 for i=#history,1,-1 do
191 local entry = history[i]; 187 local entry = history[i];
192 if maxchars then 188 if maxchars then
193 if not entry.chars then 189 if not entry.chars then
337 end 333 end
338 end 334 end
339 function room_mt:get_changesubject() 335 function room_mt:get_changesubject()
340 return self._data.changesubject; 336 return self._data.changesubject;
341 end 337 end
338 function room_mt:get_historylength()
339 return self._data.history_length or default_history_length;
340 end
341 function room_mt:set_historylength(length)
342 if tonumber(length) == nil then
343 return
344 end
345 length = tonumber(length);
346 log("debug", "max_history_length %s", self._data.max_history_length or "nil");
347 if self._data.max_history_length and length > self._data.max_history_length then
348 length = self._data.max_history_length
349 end
350 self._data.history_length = length;
351 end
352
342 353
343 function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc 354 function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
344 local from, to = stanza.attr.from, stanza.attr.to; 355 local from, to = stanza.attr.from, stanza.attr.to;
345 local room = jid_bare(to); 356 local room = jid_bare(to);
346 local current_nick = self._jid_nick[from]; 357 local current_nick = self._jid_nick[from];
606 { 617 {
607 name = 'muc#roomconfig_membersonly', 618 name = 'muc#roomconfig_membersonly',
608 type = 'boolean', 619 type = 'boolean',
609 label = 'Make Room Members-Only?', 620 label = 'Make Room Members-Only?',
610 value = self:is_members_only() 621 value = self:is_members_only()
622 },
623 {
624 name = 'muc#roomconfig_historylength',
625 type = 'text-single',
626 label = 'Maximum Number of History Messages Returned by Room',
627 value = tostring(self:get_historylength())
611 } 628 }
612 }); 629 });
613 end 630 end
614 631
615 local valid_whois = { 632 local valid_whois = {
657 674
658 local changesubject = fields['muc#roomconfig_changesubject']; 675 local changesubject = fields['muc#roomconfig_changesubject'];
659 dirty = dirty or (self:get_changesubject() ~= (not changesubject and true or nil)) 676 dirty = dirty or (self:get_changesubject() ~= (not changesubject and true or nil))
660 module:log('debug', 'changesubject=%s', changesubject and "true" or "false") 677 module:log('debug', 'changesubject=%s', changesubject and "true" or "false")
661 678
679 local historylength = fields['muc#roomconfig_historylength'];
680 dirty = dirty or (self:get_historylength() ~= (historylength and true or nil))
681 module:log('debug', 'historylength=%s', historylength)
682
683
662 local whois = fields['muc#roomconfig_whois']; 684 local whois = fields['muc#roomconfig_whois'];
663 if not valid_whois[whois] then 685 if not valid_whois[whois] then
664 origin.send(st.error_reply(stanza, 'cancel', 'bad-request', "Invalid value for 'whois'")); 686 origin.send(st.error_reply(stanza, 'cancel', 'bad-request', "Invalid value for 'whois'"));
665 return; 687 return;
666 end 688 end
675 self:set_moderated(moderated); 697 self:set_moderated(moderated);
676 self:set_members_only(membersonly); 698 self:set_members_only(membersonly);
677 self:set_persistent(persistent); 699 self:set_persistent(persistent);
678 self:set_hidden(not public); 700 self:set_hidden(not public);
679 self:set_changesubject(changesubject); 701 self:set_changesubject(changesubject);
702 self:set_historylength(historylength);
680 703
681 if self.save then self:save(true); end 704 if self.save then self:save(true); end
682 origin.send(st.reply(stanza)); 705 origin.send(st.reply(stanza));
683 706
684 if dirty or whois_changed then 707 if dirty or whois_changed then
826 elseif type == "set" or type == "get" then 849 elseif type == "set" or type == "get" then
827 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); 850 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
828 end 851 end
829 elseif stanza.name == "message" and type == "groupchat" then 852 elseif stanza.name == "message" and type == "groupchat" then
830 local from, to = stanza.attr.from, stanza.attr.to; 853 local from, to = stanza.attr.from, stanza.attr.to;
831 local room = jid_bare(to);
832 local current_nick = self._jid_nick[from]; 854 local current_nick = self._jid_nick[from];
833 local occupant = self._occupants[current_nick]; 855 local occupant = self._occupants[current_nick];
834 if not occupant then -- not in room 856 if not occupant then -- not in room
835 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); 857 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
836 elseif occupant.role == "visitor" then 858 elseif occupant.role == "visitor" then
846 else 868 else
847 stanza.attr.from = from; 869 stanza.attr.from = from;
848 origin.send(st.error_reply(stanza, "cancel", "forbidden")); 870 origin.send(st.error_reply(stanza, "cancel", "forbidden"));
849 end 871 end
850 else 872 else
851 self:broadcast_message(stanza, true); 873 self:broadcast_message(stanza, self:get_historylength() > 0);
852 end 874 end
853 stanza.attr.from = from; 875 stanza.attr.from = from;
854 end 876 end
855 elseif stanza.name == "message" and type == "error" and is_kickable_error(stanza) then 877 elseif stanza.name == "message" and type == "error" and is_kickable_error(stanza) then
856 local current_nick = self._jid_nick[stanza.attr.from]; 878 local current_nick = self._jid_nick[stanza.attr.from];
1100 jid = jid; 1122 jid = jid;
1101 _jid_nick = {}; 1123 _jid_nick = {};
1102 _occupants = {}; 1124 _occupants = {};
1103 _data = { 1125 _data = {
1104 whois = 'moderators'; 1126 whois = 'moderators';
1105 history_length = (config and config.history_length); 1127 history_length = (config and config.max_history_length) or default_history_length;
1128 max_history_length = (config and config.max_history_length) or default_history_length;
1106 }; 1129 };
1107 _affiliations = {}; 1130 _affiliations = {};
1108 }, room_mt); 1131 }, room_mt);
1109 end 1132 end
1110 1133