2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local t_insert = table.insert;
11 local t_remove = table.remove;
12 local t_concat = table.concat;
13 local s_format = string.format;
14 local s_match = string.match;
15 local tostring = tostring;
16 local setmetatable = setmetatable;
18 local ipairs = ipairs;
20 local s_gsub = string.gsub;
21 local s_find = string.find;
24 local do_pretty_printing = not os.getenv("WINDIR");
25 local getstyle, getstring;
26 if do_pretty_printing then
27 local ok, termcolours = pcall(require, "util.termcolours");
29 getstyle, getstring = termcolours.getstyle, termcolours.getstring;
31 do_pretty_printing = nil;
35 local xmlns_stanzas = "urn:ietf:params:xml:ns:xmpp-stanzas";
39 stanza_mt = { __type = "stanza" };
40 stanza_mt.__index = stanza_mt;
41 local stanza_mt = stanza_mt;
43 function stanza(name, attr)
44 local stanza = { name = name, attr = attr or {}, tags = {} };
45 return setmetatable(stanza, stanza_mt);
47 local stanza = stanza;
49 function stanza_mt:query(xmlns)
50 return self:tag("query", { xmlns = xmlns });
53 function stanza_mt:body(text, attr)
54 return self:tag("body", attr):text(text);
57 function stanza_mt:tag(name, attrs)
58 local s = stanza(name, attrs);
59 local last_add = self.last_add;
60 if not last_add then last_add = {}; self.last_add = last_add; end
61 (last_add[#last_add] or self):add_direct_child(s);
62 t_insert(last_add, s);
66 function stanza_mt:text(text)
67 local last_add = self.last_add;
68 (last_add and last_add[#last_add] or self):add_direct_child(text);
72 function stanza_mt:up()
73 local last_add = self.last_add;
74 if last_add then t_remove(last_add); end
78 function stanza_mt:reset()
83 function stanza_mt:add_direct_child(child)
84 if type(child) == "table" then
85 t_insert(self.tags, child);
87 t_insert(self, child);
90 function stanza_mt:add_child(child)
91 local last_add = self.last_add;
92 (last_add and last_add[#last_add] or self):add_direct_child(child);
96 function stanza_mt:get_child(name, xmlns)
97 for _, child in ipairs(self.tags) do
98 if (not name or child.name == name)
99 and ((not xmlns and self.attr.xmlns == child.attr.xmlns)
100 or child.attr.xmlns == xmlns) then
107 function stanza_mt:get_child_text(name, xmlns)
108 local tag = self:get_child(name, xmlns);
110 return tag:get_text();
115 function stanza_mt:child_with_name(name)
116 for _, child in ipairs(self.tags) do
117 if child.name == name then return child; end
121 function stanza_mt:child_with_ns(ns)
122 for _, child in ipairs(self.tags) do
123 if child.attr.xmlns == ns then return child; end
127 function stanza_mt:children()
135 function stanza_mt:childtags(name, xmlns)
136 local tags = self.tags;
137 local start_i, max_i = 1, #tags;
139 for i = start_i, max_i do
141 if (not name or v.name == name)
142 and ((not xmlns and self.attr.xmlns == v.attr.xmlns)
143 or v.attr.xmlns == xmlns) then
151 function stanza_mt:maptags(callback)
152 local tags, curr_tag = self.tags, 1;
153 local n_children, n_tags = #self, #tags;
156 while curr_tag <= n_tags and n_tags > 0 do
157 if self[i] == tags[curr_tag] then
158 local ret = callback(self[i]);
161 t_remove(tags, curr_tag);
162 n_children = n_children - 1;
165 curr_tag = curr_tag - 1;
170 curr_tag = curr_tag + 1;
179 local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" };
180 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end
181 _M.xml_escape = xml_escape;
184 local function _dostring(t, buf, self, xml_escape, parentns)
187 t_insert(buf, "<"..name);
188 for k, v in pairs(t.attr) do
189 if s_find(k, "\1", 1, true) then
190 local ns, attrk = s_match(k, "^([^\1]*)\1?(.*)$");
192 t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
193 elseif not(k == "xmlns" and v == parentns) then
194 t_insert(buf, " "..k.."='"..xml_escape(v).."'");
205 self(child, buf, self, xml_escape, t.attr.xmlns);
207 t_insert(buf, xml_escape(child));
210 t_insert(buf, "</"..name..">");
213 function stanza_mt.__tostring(t)
215 _dostring(t, buf, _dostring, xml_escape, nil);
216 return t_concat(buf);
219 function stanza_mt.top_tag(t)
220 local attr_string = "";
222 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(" %s='%s'", k, xml_escape(tostring(v))); end end
224 return s_format("<%s%s>", t.name, attr_string);
227 function stanza_mt.get_text(t)
233 function stanza_mt.get_error(stanza)
234 local type, condition, text;
236 local error_tag = stanza:get_child("error");
237 if not error_tag then
238 return nil, nil, nil;
240 type = error_tag.attr.type;
242 for _, child in ipairs(error_tag.tags) do
243 if child.attr.xmlns == xmlns_stanzas then
244 if not text and child.name == "text" then
245 text = child:get_text();
246 elseif not condition then
247 condition = child.name;
249 if condition and text then
254 return type, condition or "undefined-condition", text;
265 function preserialize(stanza)
266 local s = { name = stanza.name, attr = stanza.attr };
267 for _, child in ipairs(stanza) do
268 if type(child) == "table" then
269 t_insert(s, preserialize(child));
277 function deserialize(stanza)
280 local attr = stanza.attr;
281 for i=1,#attr do attr[i] = nil; end
283 for att in pairs(attr) do
284 if s_find(att, "|", 1, true) and not s_find(att, "\1", 1, true) then
285 local ns,na = s_match(att, "^([^|]+)|(.+)$");
286 attrx[ns.."\1"..na] = attr[att];
290 for a,v in pairs(attrx) do
293 setmetatable(stanza, stanza_mt);
294 for _, child in ipairs(stanza) do
295 if type(child) == "table" then
299 if not stanza.tags then
302 for _, child in ipairs(stanza) do
303 if type(child) == "table" then
304 t_insert(tags, child);
314 local function _clone(stanza)
315 local attr, tags = {}, {};
316 for k,v in pairs(stanza.attr) do attr[k] = v; end
317 local new = { name = stanza.name, attr = attr, tags = tags };
319 local child = stanza[i];
321 child = _clone(child);
322 t_insert(tags, child);
324 t_insert(new, child);
326 return setmetatable(new, stanza_mt);
330 function message(attr, body)
332 return stanza("message", attr);
334 return stanza("message", attr):tag("body"):text(body):up();
338 if attr and not attr.id then attr.id = new_id(); end
339 return stanza("iq", attr or { id = new_id() });
343 return stanza(orig.name, orig.attr and { to = orig.attr.from, from = orig.attr.to, id = orig.attr.id, type = ((orig.name == "iq" and "result") or orig.attr.type) });
347 local xmpp_stanzas_attr = { xmlns = xmlns_stanzas };
348 function error_reply(orig, type, condition, message)
349 local t = reply(orig);
350 t.attr.type = "error";
351 t:tag("error", {type = type}) --COMPAT: Some day xmlns:stanzas goes here
352 :tag(condition, xmpp_stanzas_attr):up();
353 if (message) then t:tag("text", xmpp_stanzas_attr):text(message):up(); end
354 return t; -- stanza ready for adding app-specific errors
358 function presence(attr)
359 return stanza("presence", attr);
362 if do_pretty_printing then
363 local style_attrk = getstyle("yellow");
364 local style_attrv = getstyle("red");
365 local style_tagname = getstyle("red");
366 local style_punc = getstyle("magenta");
368 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'");
369 local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">");
370 --local tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">").."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
371 local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
372 function stanza_mt.pretty_print(t)
373 local children_text = "";
374 for n, child in ipairs(t) do
375 if type(child) == "string" then
376 children_text = children_text .. xml_escape(child);
378 children_text = children_text .. child:pretty_print();
382 local attr_string = "";
384 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end
386 return s_format(tag_format, t.name, attr_string, children_text, t.name);
389 function stanza_mt.pretty_top_tag(t)
390 local attr_string = "";
392 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end
394 return s_format(top_tag_format, t.name, attr_string);
397 -- Sorry, fresh out of colours for you guys ;)
398 stanza_mt.pretty_print = stanza_mt.__tostring;
399 stanza_mt.pretty_top_tag = stanza_mt.top_tag;