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_sub = string.sub;
22 local s_find = string.find;
25 local do_pretty_printing = not os.getenv("WINDIR");
26 local getstyle, getstring;
27 if do_pretty_printing then
28 local ok, termcolours = pcall(require, "util.termcolours");
30 getstyle, getstring = termcolours.getstyle, termcolours.getstring;
32 do_pretty_printing = nil;
36 local xmlns_stanzas = "urn:ietf:params:xml:ns:xmpp-stanzas";
40 local stanza_mt = { __type = "stanza" };
41 stanza_mt.__index = stanza_mt;
43 local function new_stanza(name, attr)
44 local stanza = { name = name, attr = attr or {}, tags = {} };
45 return setmetatable(stanza, stanza_mt);
48 function stanza_mt:query(xmlns)
49 return self:tag("query", { xmlns = xmlns });
52 function stanza_mt:body(text, attr)
53 return self:tag("body", attr):text(text);
56 function stanza_mt:tag(name, attrs)
57 local s = new_stanza(name, attrs);
58 local last_add = self.last_add;
59 if not last_add then last_add = {}; self.last_add = last_add; end
60 (last_add[#last_add] or self):add_direct_child(s);
61 t_insert(last_add, s);
65 function stanza_mt:text(text)
66 local last_add = self.last_add;
67 (last_add and last_add[#last_add] or self):add_direct_child(text);
71 function stanza_mt:up()
72 local last_add = self.last_add;
73 if last_add then t_remove(last_add); end
77 function stanza_mt:reset()
82 function stanza_mt:add_direct_child(child)
83 if type(child) == "table" then
84 t_insert(self.tags, child);
86 t_insert(self, child);
89 function stanza_mt:add_child(child)
90 local last_add = self.last_add;
91 (last_add and last_add[#last_add] or self):add_direct_child(child);
95 function stanza_mt:get_child(name, xmlns)
96 for _, child in ipairs(self.tags) do
97 if (not name or child.name == name)
98 and ((not xmlns and self.attr.xmlns == child.attr.xmlns)
99 or child.attr.xmlns == xmlns) then
106 function stanza_mt:get_child_text(name, xmlns)
107 local tag = self:get_child(name, xmlns);
109 return tag:get_text();
114 function stanza_mt:child_with_name(name)
115 for _, child in ipairs(self.tags) do
116 if child.name == name then return child; end
120 function stanza_mt:child_with_ns(ns)
121 for _, child in ipairs(self.tags) do
122 if child.attr.xmlns == ns then return child; end
126 function stanza_mt:children()
134 function stanza_mt:childtags(name, xmlns)
135 local tags = self.tags;
136 local start_i, max_i = 1, #tags;
138 for i = start_i, max_i do
140 if (not name or v.name == name)
141 and ((not xmlns and self.attr.xmlns == v.attr.xmlns)
142 or v.attr.xmlns == xmlns) then
150 function stanza_mt:maptags(callback)
151 local tags, curr_tag = self.tags, 1;
152 local n_children, n_tags = #self, #tags;
155 while curr_tag <= n_tags and n_tags > 0 do
156 if self[i] == tags[curr_tag] then
157 local ret = callback(self[i]);
160 t_remove(tags, curr_tag);
161 n_children = n_children - 1;
164 curr_tag = curr_tag - 1;
167 tags[curr_tag] = ret;
169 curr_tag = curr_tag + 1;
176 function stanza_mt:find(path)
178 local len = #path + 1;
181 local xmlns, name, text;
182 local char = s_sub(path, pos, pos);
184 return self.attr[s_sub(path, pos + 1)];
185 elseif char == "{" then
186 xmlns, pos = s_match(path, "^([^}]+)}()", pos + 1);
188 name, text, pos = s_match(path, "^([^@/#]*)([/#]?)()", pos);
189 name = name ~= "" and name or nil;
192 return self:get_child_text(name, xmlns);
194 return self:get_child(name, xmlns);
196 self = self:get_child(name, xmlns);
201 local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" };
202 local function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end
204 local function _dostring(t, buf, self, _xml_escape, parentns)
207 t_insert(buf, "<"..name);
208 for k, v in pairs(t.attr) do
209 if s_find(k, "\1", 1, true) then
210 local ns, attrk = s_match(k, "^([^\1]*)\1?(.*)$");
212 t_insert(buf, " xmlns:ns"..nsid.."='".._xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='".._xml_escape(v).."'");
213 elseif not(k == "xmlns" and v == parentns) then
214 t_insert(buf, " "..k.."='".._xml_escape(v).."'");
225 self(child, buf, self, _xml_escape, t.attr.xmlns);
227 t_insert(buf, _xml_escape(child));
230 t_insert(buf, "</"..name..">");
233 function stanza_mt.__tostring(t)
235 _dostring(t, buf, _dostring, xml_escape, nil);
236 return t_concat(buf);
239 function stanza_mt.top_tag(t)
240 local attr_string = "";
242 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
244 return s_format("<%s%s>", t.name, attr_string);
247 function stanza_mt.get_text(t)
253 function stanza_mt.get_error(stanza)
254 local error_type, condition, text;
256 local error_tag = stanza:get_child("error");
257 if not error_tag then
258 return nil, nil, nil;
260 error_type = error_tag.attr.type;
262 for _, child in ipairs(error_tag.tags) do
263 if child.attr.xmlns == xmlns_stanzas then
264 if not text and child.name == "text" then
265 text = child:get_text();
266 elseif not condition then
267 condition = child.name;
269 if condition and text then
274 return error_type, condition or "undefined-condition", text;
278 local function new_id()
283 local function preserialize(stanza)
284 local s = { name = stanza.name, attr = stanza.attr };
285 for _, child in ipairs(stanza) do
286 if type(child) == "table" then
287 t_insert(s, preserialize(child));
295 local function deserialize(stanza)
298 local attr = stanza.attr;
299 for i=1,#attr do attr[i] = nil; end
301 for att in pairs(attr) do
302 if s_find(att, "|", 1, true) and not s_find(att, "\1", 1, true) then
303 local ns,na = s_match(att, "^([^|]+)|(.+)$");
304 attrx[ns.."\1"..na] = attr[att];
308 for a,v in pairs(attrx) do
311 setmetatable(stanza, stanza_mt);
312 for _, child in ipairs(stanza) do
313 if type(child) == "table" then
317 if not stanza.tags then
320 for _, child in ipairs(stanza) do
321 if type(child) == "table" then
322 t_insert(tags, child);
332 local function clone(stanza)
333 local attr, tags = {}, {};
334 for k,v in pairs(stanza.attr) do attr[k] = v; end
335 local new = { name = stanza.name, attr = attr, tags = tags };
337 local child = stanza[i];
339 child = clone(child);
340 t_insert(tags, child);
342 t_insert(new, child);
344 return setmetatable(new, stanza_mt);
347 local function message(attr, body)
349 return new_stanza("message", attr);
351 return new_stanza("message", attr):tag("body"):text(body):up();
354 local function iq(attr)
355 if attr and not attr.id then attr.id = new_id(); end
356 return new_stanza("iq", attr or { id = new_id() });
359 local function reply(orig)
360 return new_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) });
363 local xmpp_stanzas_attr = { xmlns = xmlns_stanzas };
364 local function error_reply(orig, error_type, condition, error_message)
365 local t = reply(orig);
366 t.attr.type = "error";
367 t:tag("error", {type = error_type}) --COMPAT: Some day xmlns:stanzas goes here
368 :tag(condition, xmpp_stanzas_attr):up();
369 if error_message then t:tag("text", xmpp_stanzas_attr):text(error_message):up(); end
370 return t; -- stanza ready for adding app-specific errors
373 local function presence(attr)
374 return new_stanza("presence", attr);
377 if do_pretty_printing then
378 local style_attrk = getstyle("yellow");
379 local style_attrv = getstyle("red");
380 local style_tagname = getstyle("red");
381 local style_punc = getstyle("magenta");
383 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'");
384 local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">");
385 --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, ">");
386 local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
387 function stanza_mt.pretty_print(t)
388 local children_text = "";
389 for _, child in ipairs(t) do
390 if type(child) == "string" then
391 children_text = children_text .. xml_escape(child);
393 children_text = children_text .. child:pretty_print();
397 local attr_string = "";
399 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
401 return s_format(tag_format, t.name, attr_string, children_text, t.name);
404 function stanza_mt.pretty_top_tag(t)
405 local attr_string = "";
407 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
409 return s_format(top_tag_format, t.name, attr_string);
412 -- Sorry, fresh out of colours for you guys ;)
413 stanza_mt.pretty_print = stanza_mt.__tostring;
414 stanza_mt.pretty_top_tag = stanza_mt.top_tag;
418 stanza_mt = stanza_mt;
421 preserialize = preserialize;
422 deserialize = deserialize;
427 error_reply = error_reply;
429 xml_escape = xml_escape;