Comparison

plugins/muc/muc.lib.lua @ 6206:f937bb5c83c3

plugins/muc: Move locking to seperate module
author daurnimator <quae@daurnimator.com>
date Wed, 02 Apr 2014 15:48:25 -0400
parent 6204:c3254827698d
child 6208:d724289a5226
comparison
equal deleted inserted replaced
6205:49dd381666f3 6206:f937bb5c83c3
85 return self:get_moderated() and "visitor" or "participant"; 85 return self:get_moderated() and "visitor" or "participant";
86 end 86 end
87 end 87 end
88 end 88 end
89 89
90 function room_mt:lock()
91 module:fire_event("muc-room-locked", { room = self });
92 self.locked = true
93 end
94 function room_mt:unlock()
95 module:fire_event("muc-room-unlocked", { room = self });
96 self.locked = nil
97 end
98 function room_mt:is_locked()
99 return not not self.locked
100 end
101
102 --- Occupant functions 90 --- Occupant functions
103 function room_mt:new_occupant(bare_real_jid, nick) 91 function room_mt:new_occupant(bare_real_jid, nick)
104 local occupant = occupant_lib.new(bare_real_jid, nick); 92 local occupant = occupant_lib.new(bare_real_jid, nick);
105 local affiliation = self:get_affiliation(bare_real_jid); 93 local affiliation = self:get_affiliation(bare_real_jid);
106 occupant.role = self:get_default_role(affiliation); 94 occupant.role = self:get_default_role(affiliation);
581 569
582 function room_mt:get_whois() 570 function room_mt:get_whois()
583 return self._data.whois; 571 return self._data.whois;
584 end 572 end
585 573
586 module:hook("muc-room-pre-create", function(event)
587 local room = event.room;
588 if room:is_locked() and not event.stanza:get_child("x", "http://jabber.org/protocol/muc") then
589 room:unlock(); -- Older groupchat protocol doesn't lock
590 end
591 end, 10);
592
593 -- Give the room creator owner affiliation 574 -- Give the room creator owner affiliation
594 module:hook("muc-room-pre-create", function(event) 575 module:hook("muc-room-pre-create", function(event)
595 event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner"); 576 event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner");
596 end, -1); 577 end, -1);
597 578
607 reply.tags[1].attr.code = "401"; 588 reply.tags[1].attr.code = "401";
608 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); 589 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
609 return true; 590 return true;
610 end 591 end
611 end, -20); 592 end, -20);
612
613 module:hook("muc-occupant-pre-join", function(event)
614 if event.room:is_locked() then -- Deny entry
615 event.origin.send(st.error_reply(event.stanza, "cancel", "item-not-found"));
616 return true;
617 end
618 end, -30);
619 593
620 -- registration required for entering members-only room 594 -- registration required for entering members-only room
621 module:hook("muc-occupant-pre-join", function(event) 595 module:hook("muc-occupant-pre-join", function(event)
622 local room, stanza = event.room, event.stanza; 596 local room, stanza = event.room, event.stanza;
623 local affiliation = room:get_affiliation(stanza.attr.from); 597 local affiliation = room:get_affiliation(stanza.attr.from);
1004 return true; 978 return true;
1005 end 979 end
1006 module:fire_event("muc-config-submitted", event); 980 module:fire_event("muc-config-submitted", event);
1007 981
1008 if self.save then self:save(true); end 982 if self.save then self:save(true); end
1009 if self:is_locked() then
1010 self:unlock();
1011 end
1012 origin.send(st.reply(stanza)); 983 origin.send(st.reply(stanza));
1013 984
1014 if next(event.status_codes) then 985 if next(event.status_codes) then
1015 local msg = st.message({type='groupchat', from=self.jid}) 986 local msg = st.message({type='groupchat', from=self.jid})
1016 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}) 987 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
1541 local _M = {}; -- module "muc" 1512 local _M = {}; -- module "muc"
1542 1513
1543 function _M.new_room(jid, config) 1514 function _M.new_room(jid, config)
1544 return setmetatable({ 1515 return setmetatable({
1545 jid = jid; 1516 jid = jid;
1546 locked = nil;
1547 _jid_nick = {}; 1517 _jid_nick = {};
1548 _occupants = {}; 1518 _occupants = {};
1549 _data = { 1519 _data = {
1550 whois = 'moderators'; 1520 whois = 'moderators';
1551 history_length = math.min((config and config.history_length) 1521 history_length = math.min((config and config.history_length)