util/stanza.lua

changeset 2526
401ff68413a1
parent 2482
a1570e371258
child 2557
e5254ccd5ef8
--- a/util/stanza.lua	Fri Jan 29 18:16:39 2010 +0500
+++ b/util/stanza.lua	Fri Jan 29 14:22:03 2010 +0000
@@ -38,6 +38,8 @@
 	end
 end
 
+local xmlns_stanzas = "urn:ietf:params:xml:ns:xmpp-stanzas";
+
 module "stanza"
 
 stanza_mt = { __type = "stanza" };
@@ -189,6 +191,30 @@
 	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

mercurial