Comparison

util/stanza.lua @ 2526:401ff68413a1

util.stanza: Add stanza:get_error() to return type, condition and text of a stanza error
author Matthew Wild <mwild1@gmail.com>
date Fri, 29 Jan 2010 14:22:03 +0000
parent 2482:a1570e371258
child 2557:e5254ccd5ef8
comparison
equal deleted inserted replaced
2525:e385511b3a54 2526:401ff68413a1
36 else 36 else
37 do_pretty_printing = nil; 37 do_pretty_printing = nil;
38 end 38 end
39 end 39 end
40 40
41 local xmlns_stanzas = "urn:ietf:params:xml:ns:xmpp-stanzas";
42
41 module "stanza" 43 module "stanza"
42 44
43 stanza_mt = { __type = "stanza" }; 45 stanza_mt = { __type = "stanza" };
44 stanza_mt.__index = stanza_mt; 46 stanza_mt.__index = stanza_mt;
45 47
187 if #t.tags == 0 then 189 if #t.tags == 0 then
188 return t_concat(t); 190 return t_concat(t);
189 end 191 end
190 end 192 end
191 193
194 function stanza_mt.get_error(stanza)
195 local type, condition, text;
196
197 local error_tag = stanza:get_child("error");
198 if not error_tag then
199 return nil, nil, nil;
200 end
201 type = error_tag.attr.type;
202
203 for child in error_tag:children() do
204 if child.attr.xmlns == xmlns_stanzas then
205 if not text and child.name == "text" then
206 text = child:get_text();
207 elseif not condition then
208 condition = child.name;
209 end
210 if condition and text then
211 break;
212 end
213 end
214 end
215 return type, condition or "undefined-condition", text or "";
216 end
217
192 function stanza_mt.__add(s1, s2) 218 function stanza_mt.__add(s1, s2)
193 return s1:add_direct_child(s2); 219 return s1:add_direct_child(s2);
194 end 220 end
195 221
196 222