Comparison

plugins/muc/muc.lib.lua @ 6100:c78ba94d3261

plugins/muc/muc.lib: Move all kick code into one place
author daurnimator <quae@daurnimator.com>
date Fri, 21 Feb 2014 18:04:38 -0500
parent 6099:c8a749298d51
child 6101:a861dc18e08d
comparison
equal deleted inserted replaced
6099:c8a749298d51 6100:c78ba94d3261
257 :tag('subject'):text(subject):up(); 257 :tag('subject'):text(subject):up();
258 self:broadcast_message(msg, false); 258 self:broadcast_message(msg, false);
259 return true; 259 return true;
260 end 260 end
261 261
262 local function build_unavailable_presence_from_error(stanza) 262 function room_mt:handle_kickable(origin, stanza)
263 local type, condition, text = stanza:get_error(); 263 local type, condition, text = stanza:get_error();
264 local error_message = "Kicked: "..(condition and condition:gsub("%-", " ") or "presence error"); 264 local error_message = "Kicked: "..(condition and condition:gsub("%-", " ") or "presence error");
265 if text then 265 if text then
266 error_message = error_message..": "..text; 266 error_message = error_message..": "..text;
267 end 267 end
268 return st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to}) 268 local kick_stanza = st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to})
269 :tag('status'):text(error_message); 269 :tag('status'):text(error_message);
270 self:handle_unavailable_to_occupant(origin, kick_stanza); -- send unavailable
271 return true;
270 end 272 end
271 273
272 function room_mt:set_name(name) 274 function room_mt:set_name(name)
273 if name == "" or type(name) ~= "string" or name == (jid_split(self.jid)) then name = nil; end 275 if name == "" or type(name) ~= "string" or name == (jid_split(self.jid)) then name = nil; end
274 if self._data.name ~= name then 276 if self._data.name ~= name then
376 end 378 end
377 end 379 end
378 380
379 function room_mt:get_whois() 381 function room_mt:get_whois()
380 return self._data.whois; 382 return self._data.whois;
381 end
382
383 function room_mt:handle_presence_error_to_occupant(origin, stanza)
384 local current_nick = self._jid_nick[stanza.attr.from];
385 if not current_nick then
386 return true -- discard
387 end
388 log("debug", "kicking %s from %s", current_nick, self.jid);
389 return self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza))
390 end 383 end
391 384
392 function room_mt:handle_unavailable_to_occupant(origin, stanza) 385 function room_mt:handle_unavailable_to_occupant(origin, stanza)
393 local from = stanza.attr.from; 386 local from = stanza.attr.from;
394 local current_nick = self._jid_nick[from]; 387 local current_nick = self._jid_nick[from];
564 end 557 end
565 558
566 function room_mt:handle_presence_to_occupant(origin, stanza) 559 function room_mt:handle_presence_to_occupant(origin, stanza)
567 local type = stanza.attr.type; 560 local type = stanza.attr.type;
568 if type == "error" then -- error, kick em out! 561 if type == "error" then -- error, kick em out!
569 return self:handle_presence_error_to_occupant(origin, stanza) 562 return self:handle_kickable(origin, stanza)
570 elseif type == "unavailable" then -- unavailable 563 elseif type == "unavailable" then -- unavailable
571 return self:handle_unavailable_to_occupant(origin, stanza) 564 return self:handle_unavailable_to_occupant(origin, stanza)
572 elseif not type then -- available 565 elseif not type then -- available
573 return self:handle_available_to_occupant(origin, stanza) 566 return self:handle_available_to_occupant(origin, stanza)
574 elseif type ~= 'result' then -- bad type 567 elseif type ~= 'result' then -- bad type
640 if type == "groupchat" then -- groupchat messages not allowed in PM 633 if type == "groupchat" then -- groupchat messages not allowed in PM
641 origin.send(st.error_reply(stanza, "modify", "bad-request")); 634 origin.send(st.error_reply(stanza, "modify", "bad-request"));
642 return true; 635 return true;
643 elseif type == "error" and is_kickable_error(stanza) then 636 elseif type == "error" and is_kickable_error(stanza) then
644 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid); 637 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid);
645 self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); -- send unavailable 638 return self:handle_kickable(origin, stanza); -- send unavailable
646 return true;
647 end 639 end
648 640
649 local o_data = self._occupants[to]; 641 local o_data = self._occupants[to];
650 if not o_data then 642 if not o_data then
651 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); 643 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
1015 stanza.attr.from = from; 1007 stanza.attr.from = from;
1016 return true; 1008 return true;
1017 end 1009 end
1018 end 1010 end
1019 1011
1020 function room_mt:handle_kickable_to_room(origin, stanza)
1021 local current_nick = self._jid_nick[stanza.attr.from];
1022 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid);
1023 self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); -- send unavailable
1024 return true;
1025 end
1026
1027 -- hack - some buggy clients send presence updates to the room rather than their nick 1012 -- hack - some buggy clients send presence updates to the room rather than their nick
1028 function room_mt:handle_presence_to_room(origin, stanza) 1013 function room_mt:handle_presence_to_room(origin, stanza)
1029 local type = stanza.attr.type;
1030 local current_nick = self._jid_nick[stanza.attr.from]; 1014 local current_nick = self._jid_nick[stanza.attr.from];
1015 local handled
1031 if current_nick then 1016 if current_nick then
1032 local to = stanza.attr.to; 1017 local to = stanza.attr.to;
1033 stanza.attr.to = current_nick; 1018 stanza.attr.to = current_nick;
1034 self:handle_to_occupant(origin, stanza); 1019 handled = self:handle_presence_to_occupant(origin, stanza);
1035 stanza.attr.to = to; 1020 stanza.attr.to = to;
1036 return true; 1021 end
1037 else 1022 return handled;
1038 return nil;
1039 end
1040 end 1023 end
1041 1024
1042 function room_mt:handle_invite_to_room(origin, stanza, payload) 1025 function room_mt:handle_invite_to_room(origin, stanza, payload)
1043 local _from, _to = stanza.attr.from, stanza.attr.to; 1026 local _from, _to = stanza.attr.from, stanza.attr.to;
1044 local _invitee = jid_prep(payload.attr.to); 1027 local _invitee = jid_prep(payload.attr.to);
1074 function room_mt:handle_message_to_room(origin, stanza) 1057 function room_mt:handle_message_to_room(origin, stanza)
1075 local type = stanza.attr.type; 1058 local type = stanza.attr.type;
1076 if type == "groupchat" then 1059 if type == "groupchat" then
1077 return self:handle_groupchat_to_room(origin, stanza) 1060 return self:handle_groupchat_to_room(origin, stanza)
1078 elseif type == "error" and is_kickable_error(stanza) then 1061 elseif type == "error" and is_kickable_error(stanza) then
1079 return self:handle_kickable_to_room(origin, stanza) 1062 return self:handle_kickable(origin, stanza)
1080 elseif not(type == "chat" or type == "error" or type == "groupchat" or type == "headline") and #stanza.tags == 1 1063 elseif not(type == "chat" or type == "error" or type == "groupchat" or type == "headline") and #stanza.tags == 1
1081 and self._jid_nick[stanza.attr.from] and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" then 1064 and self._jid_nick[stanza.attr.from] and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" then
1082 local x = stanza.tags[1]; 1065 local x = stanza.tags[1];
1083 local payload = (#x.tags == 1 and x.tags[1]); 1066 local payload = (#x.tags == 1 and x.tags[1]);
1084 if payload and payload.name == "invite" and payload.attr.to then 1067 if payload and payload.name == "invite" and payload.attr.to then