Comparison

plugins/muc/muc.lib.lua @ 1999:05054e360d89

MUC: Improved handling of error stanzas and made error messages concise.
author Waqas Hussain <waqas20@gmail.com>
date Sun, 18 Oct 2009 06:49:40 +0500
parent 1998:40792c18a8e4
child 2005:478ba7e85862
comparison
equal deleted inserted replaced
1998:40792c18a8e4 1999:05054e360d89
43 end 43 end
44 return stanza, 0; 44 return stanza, 0;
45 end 45 end
46 local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true}; 46 local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true};
47 local function get_filtered_presence(stanza) 47 local function get_filtered_presence(stanza)
48 return filter_xmlns_from_stanza(st.clone(stanza), presence_filters); 48 return filter_xmlns_from_stanza(st.clone(stanza):reset(), presence_filters);
49 end 49 end
50 local kickable_error_conditions = { 50 local kickable_error_conditions = {
51 ["gone"] = true; 51 ["gone"] = true;
52 ["internal-server-error"] = true; 52 ["internal-server-error"] = true;
53 ["item-not-found"] = true; 53 ["item-not-found"] = true;
55 ["recipient-unavailable"] = true; 55 ["recipient-unavailable"] = true;
56 ["redirect"] = true; 56 ["redirect"] = true;
57 ["remote-server-not-found"] = true; 57 ["remote-server-not-found"] = true;
58 ["remote-server-timeout"] = true; 58 ["remote-server-timeout"] = true;
59 ["service-unavailable"] = true; 59 ["service-unavailable"] = true;
60 ["malformed error"] = true;
60 }; 61 };
61 local function get_kickable_error(stanza) 62 local function get_error_condition(stanza)
62 for _, tag in ipairs(stanza.tags) do 63 for _, tag in ipairs(stanza.tags) do
63 if tag.name == "error" and tag.attr.xmlns == "jabber:client" then 64 if tag.name == "error" and (not(tag.attr.xmlns) or tag.attr.xmlns == "jabber:client") then
64 for _, cond in ipairs(tag.tags) do 65 for _, cond in ipairs(tag.tags) do
65 if cond.attr.xmlns == "urn:ietf:params:xml:ns:xmpp-stanzas" then 66 if cond.attr.xmlns == "urn:ietf:params:xml:ns:xmpp-stanzas" then
66 return kickable_error_conditions[cond.name] and cond.name; 67 return cond.name;
67 end 68 end
68 end 69 end
69 return true; -- malformed error message 70 return "malformed error";
70 end 71 end
71 end 72 end
72 return true; -- malformed error message 73 return "malformed error";
74 end
75 local function is_kickable_error(stanza)
76 local cond = get_error_condition(stanza);
77 return kickable_error_conditions[cond] and cond;
73 end 78 end
74 local function getUsingPath(stanza, path, getText) 79 local function getUsingPath(stanza, path, getText)
75 local tag = stanza; 80 local tag = stanza;
76 for _, name in ipairs(path) do 81 for _, name in ipairs(path) do
77 if type(tag) ~= 'table' then return; end 82 if type(tag) ~= 'table' then return; end
207 pr.attr.from = current_nick; 212 pr.attr.from = current_nick;
208 if type == "error" then -- error, kick em out! 213 if type == "error" then -- error, kick em out!
209 if current_nick then 214 if current_nick then
210 log("debug", "kicking %s from %s", current_nick, room); 215 log("debug", "kicking %s from %s", current_nick, room);
211 self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}) 216 self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to})
212 :tag('status'):text('This participant is kicked from the room because he sent an error presence')); -- send unavailable 217 :tag('status'):text('Kicked: '..get_error_condition(stanza))); -- send unavailable
213 end 218 end
214 elseif type == "unavailable" then -- unavailable 219 elseif type == "unavailable" then -- unavailable
215 if current_nick then 220 if current_nick then
216 log("debug", "%s leaving %s", current_nick, room); 221 log("debug", "%s leaving %s", current_nick, room);
217 local occupant = self._occupants[current_nick]; 222 local occupant = self._occupants[current_nick];
351 else 356 else
352 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); 357 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
353 end 358 end
354 elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM 359 elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM
355 origin.send(st.error_reply(stanza, "modify", "bad-request")); 360 origin.send(st.error_reply(stanza, "modify", "bad-request"));
356 elseif current_nick and stanza.name == "message" and type == "error" and get_kickable_error(stanza) then 361 elseif current_nick and stanza.name == "message" and type == "error" and is_kickable_error(stanza) then
357 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid); 362 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid);
358 self:handle_to_occupant(origin, st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to}) 363 self:handle_to_occupant(origin, st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to})
359 :tag('status'):text('This participant is kicked from the room because he sent an error message to another occupant')); -- send unavailable 364 :tag('status'):text('Kicked: '..get_error_condition(stanza))); -- send unavailable
360 else -- private stanza 365 else -- private stanza
361 local o_data = self._occupants[to]; 366 local o_data = self._occupants[to];
362 if o_data then 367 if o_data then
363 log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid); 368 log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
364 local jid = o_data.jid; 369 local jid = o_data.jid;
516 self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza 521 self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza
517 else 522 else
518 self:broadcast_message(stanza, true); 523 self:broadcast_message(stanza, true);
519 end 524 end
520 end 525 end
521 elseif stanza.name == "message" and type == "error" and get_kickable_error(stanza) then 526 elseif stanza.name == "message" and type == "error" and is_kickable_error(stanza) then
522 local current_nick = self._jid_nick[stanza.attr.from]; 527 local current_nick = self._jid_nick[stanza.attr.from];
523 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid); 528 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid);
524 self:handle_to_occupant(origin, st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to}) 529 self:handle_to_occupant(origin, st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to})
525 :tag('status'):text('This participant is kicked from the room because he sent an error message')); -- send unavailable 530 :tag('status'):text('Kicked: '..get_error_condition(stanza))); -- send unavailable
526 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick 531 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick
527 local to = stanza.attr.to; 532 local to = stanza.attr.to;
528 local current_nick = self._jid_nick[stanza.attr.from]; 533 local current_nick = self._jid_nick[stanza.attr.from];
529 if current_nick then 534 if current_nick then
530 stanza.attr.to = current_nick; 535 stanza.attr.to = current_nick;