From 35d65ca55b849e4cd36853dbee802fa1b78e7856 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 29 Jan 2010 14:22:03 +0000 Subject: util.stanza: Add stanza:get_error() to return type, condition and text of a stanza error --- util/stanza.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/util/stanza.lua b/util/stanza.lua index c817f93b..d223d53f 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -38,6 +38,8 @@ if do_pretty_printing then end end +local xmlns_stanzas = "urn:ietf:params:xml:ns:xmpp-stanzas"; + module "stanza" stanza_mt = { __type = "stanza" }; @@ -189,6 +191,30 @@ function stanza_mt.get_text(t) end end +function stanza_mt.get_error(stanza) + local type, condition, text; + + local error_tag = stanza:get_child("error"); + if not error_tag then + return nil, nil, nil; + end + type = error_tag.attr.type; + + for child in error_tag:children() do + if child.attr.xmlns == xmlns_stanzas then + if not text and child.name == "text" then + text = child:get_text(); + elseif not condition then + condition = child.name; + end + if condition and text then + break; + end + end + end + return type, condition or "undefined-condition", text or ""; +end + function stanza_mt.__add(s1, s2) return s1:add_direct_child(s2); end -- cgit v1.2.3 From 119072d7d8a3a5e2c60229786eb746a8e1d04670 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 29 Jan 2010 14:26:54 +0000 Subject: MUC: Have get_error_condition() use the new stanza:get_error() (muc.lib.lua 11 lines shorter \o/) --- plugins/muc/muc.lib.lua | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/plugins/muc/muc.lib.lua b/plugins/muc/muc.lib.lua index f27cf191..896a977f 100644 --- a/plugins/muc/muc.lib.lua +++ b/plugins/muc/muc.lib.lua @@ -59,19 +59,12 @@ local kickable_error_conditions = { ["service-unavailable"] = true; ["malformed error"] = true; }; + local function get_error_condition(stanza) - for _, tag in ipairs(stanza.tags) do - if tag.name == "error" and (not(tag.attr.xmlns) or tag.attr.xmlns == "jabber:client") then - for _, cond in ipairs(tag.tags) do - if cond.attr.xmlns == "urn:ietf:params:xml:ns:xmpp-stanzas" then - return cond.name; - end - end - return "malformed error"; - end - end - return "malformed error"; + local _, condition = stanza:get_error(); + return condition or "malformed error"; end + local function is_kickable_error(stanza) local cond = get_error_condition(stanza); return kickable_error_conditions[cond] and cond; -- cgit v1.2.3 From 71a08805224b3693573fade6b1f2c1f6d71bef6d Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 29 Jan 2010 14:40:37 +0000 Subject: MUC: Show error message texts when participants are kicked for stanza errors --- plugins/muc/muc.lib.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/muc/muc.lib.lua b/plugins/muc/muc.lib.lua index 896a977f..103227f0 100644 --- a/plugins/muc/muc.lib.lua +++ b/plugins/muc/muc.lib.lua @@ -200,9 +200,14 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc pr.attr.from = current_nick; if type == "error" then -- error, kick em out! if current_nick then - log("debug", "kicking %s from %s", current_nick, room); + local type, condition, text = stanza:get_error(); + local error_message = "Kicked: "..condition:gsub("%-", " "); + if text then + error_message = error_message..": "..text; + end + log("debug", "kicking %s from %s for %s", current_nick, room, condition); self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}) - :tag('status'):text('Kicked: '..get_error_condition(stanza))); -- send unavailable + :tag('status'):text(error_message)); -- send unavailable end elseif type == "unavailable" then -- unavailable if current_nick then -- cgit v1.2.3 From a24c95d2c90c47339ae396cf7ae5132769269f14 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 29 Jan 2010 15:13:06 +0000 Subject: MUC: Fixes and refactoring for the previous commit to work in all cases, text of error stanzas is now broadcast --- plugins/muc/muc.lib.lua | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/plugins/muc/muc.lib.lua b/plugins/muc/muc.lib.lua index 103227f0..0544cfe3 100644 --- a/plugins/muc/muc.lib.lua +++ b/plugins/muc/muc.lib.lua @@ -188,6 +188,16 @@ function room_mt:set_subject(current_nick, subject) return true; end +local function build_unavailable_presence_from_error(stanza) + local type, condition, text = stanza:get_error(); + local error_message = "Kicked: "..condition:gsub("%-", " "); + if text then + error_message = error_message..": "..text; + end + return st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to}) + :tag('status'):text(error_message); +end + function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc local from, to = stanza.attr.from, stanza.attr.to; local room = jid_bare(to); @@ -200,14 +210,8 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc pr.attr.from = current_nick; if type == "error" then -- error, kick em out! if current_nick then - local type, condition, text = stanza:get_error(); - local error_message = "Kicked: "..condition:gsub("%-", " "); - if text then - error_message = error_message..": "..text; - end - log("debug", "kicking %s from %s for %s", current_nick, room, condition); - self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}) - :tag('status'):text(error_message)); -- send unavailable + log("debug", "kicking %s from %s", current_nick, room); + self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); end elseif type == "unavailable" then -- unavailable if current_nick then @@ -354,8 +358,7 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc origin.send(st.error_reply(stanza, "modify", "bad-request")); elseif current_nick and stanza.name == "message" and type == "error" and is_kickable_error(stanza) then log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid); - self:handle_to_occupant(origin, st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to}) - :tag('status'):text('Kicked: '..get_error_condition(stanza))); -- send unavailable + self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); -- send unavailable else -- private stanza local o_data = self._occupants[to]; if o_data then @@ -614,8 +617,7 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha elseif stanza.name == "message" and type == "error" and is_kickable_error(stanza) then local current_nick = self._jid_nick[stanza.attr.from]; log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid); - self:handle_to_occupant(origin, st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to}) - :tag('status'):text('Kicked: '..get_error_condition(stanza))); -- send unavailable + self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); -- send unavailable elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick local to = stanza.attr.to; local current_nick = self._jid_nick[stanza.attr.from]; -- cgit v1.2.3