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_concat = table.concat;
12 local t_remove = table.remove;
13 local t_concat = table.concat;
14 local s_format = string.format;
15 local s_match = string.match;
16 local tostring = tostring;
17 local setmetatable = setmetatable;
18 local getmetatable = getmetatable;
20 local ipairs = ipairs;
24 local unpack = unpack;
25 local s_gsub = string.gsub;
26 local s_char = string.char;
27 local s_find = string.find;
30 local do_pretty_printing = not os.getenv("WINDIR");
31 local getstyle, getstring;
32 if do_pretty_printing then
33 local ok, termcolours = pcall(require, "util.termcolours");
35 getstyle, getstring = termcolours.getstyle, termcolours.getstring;
37 do_pretty_printing = nil;
41 local xmlns_stanzas = "urn:ietf:params:xml:ns:xmpp-stanzas";
45 stanza_mt = { __type = "stanza" };
46 stanza_mt.__index = stanza_mt;
47 local stanza_mt = stanza_mt;
49 function stanza(name, attr)
50 local stanza = { name = name, attr = attr or {}, tags = {} };
51 return setmetatable(stanza, stanza_mt);
53 local stanza = stanza;
55 function stanza_mt:query(xmlns)
56 return self:tag("query", { xmlns = xmlns });
59 function stanza_mt:body(text, attr)
60 return self:tag("body", attr):text(text);
63 function stanza_mt:tag(name, attrs)
64 local s = stanza(name, attrs);
65 local last_add = self.last_add;
66 if not last_add then last_add = {}; self.last_add = last_add; end
67 (last_add[#last_add] or self):add_direct_child(s);
68 t_insert(last_add, s);
72 function stanza_mt:text(text)
73 local last_add = self.last_add;
74 (last_add and last_add[#last_add] or self):add_direct_child(text);
78 function stanza_mt:up()
79 local last_add = self.last_add;
80 if last_add then t_remove(last_add); end
84 function stanza_mt:reset()
89 function stanza_mt:add_direct_child(child)
90 if type(child) == "table" then
91 t_insert(self.tags, child);
93 t_insert(self, child);
96 function stanza_mt:add_child(child)
97 local last_add = self.last_add;
98 (last_add and last_add[#last_add] or self):add_direct_child(child);
102 function stanza_mt:get_child(name, xmlns)
103 for _, child in ipairs(self.tags) do
104 if (not name or child.name == name)
105 and ((not xmlns and self.attr.xmlns == child.attr.xmlns)
106 or child.attr.xmlns == xmlns) then
113 function stanza_mt:get_child_text(name, xmlns)
114 local tag = self:get_child(name, xmlns);
116 return tag:get_text();
121 function stanza_mt:child_with_name(name)
122 for _, child in ipairs(self.tags) do
123 if child.name == name then return child; end
127 function stanza_mt:child_with_ns(ns)
128 for _, child in ipairs(self.tags) do
129 if child.attr.xmlns == ns then return child; end
133 function stanza_mt:children()
141 function stanza_mt:matching_tags(name, xmlns)
142 xmlns = xmlns or self.attr.xmlns;
143 local tags = self.tags;
144 local start_i, max_i = 1, #tags;
146 for i=start_i,max_i do
148 if (not name or v.name == name)
149 and (not xmlns or xmlns == v.attr.xmlns) then
157 function stanza_mt:childtags()
161 local v = self.tags[i]
162 if v then return v; end
163 end, self.tags[1], i;
166 function stanza_mt:maptags(callback)
167 local tags, curr_tag = self.tags, 1;
168 local n_children, n_tags = #self, #tags;
171 while curr_tag <= n_tags do
172 if self[i] == tags[curr_tag] then
173 local ret = callback(self[i]);
176 t_remove(tags, curr_tag);
177 n_children = n_children - 1;
184 curr_tag = curr_tag + 1;
192 local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" };
193 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end
194 _M.xml_escape = xml_escape;
197 local function _dostring(t, buf, self, xml_escape, parentns)
200 t_insert(buf, "<"..name);
201 for k, v in pairs(t.attr) do
202 if s_find(k, "\1", 1, true) then
203 local ns, attrk = s_match(k, "^([^\1]*)\1?(.*)$");
205 t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
206 elseif not(k == "xmlns" and v == parentns) then
207 t_insert(buf, " "..k.."='"..xml_escape(v).."'");
218 self(child, buf, self, xml_escape, t.attr.xmlns);
220 t_insert(buf, xml_escape(child));
223 t_insert(buf, "</"..name..">");
226 function stanza_mt.__tostring(t)
228 _dostring(t, buf, _dostring, xml_escape, nil);
229 return t_concat(buf);
232 function stanza_mt.top_tag(t)
233 local attr_string = "";
235 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
237 return s_format("<%s%s>", t.name, attr_string);
240 function stanza_mt.get_text(t)
246 function stanza_mt.get_error(stanza)
247 local type, condition, text;
249 local error_tag = stanza:get_child("error");
250 if not error_tag then
251 return nil, nil, nil;
253 type = error_tag.attr.type;
255 for child in error_tag:childtags() do
256 if child.attr.xmlns == xmlns_stanzas then
257 if not text and child.name == "text" then
258 text = child:get_text();
259 elseif not condition then
260 condition = child.name;
262 if condition and text then
267 return type, condition or "undefined-condition", text;
270 function stanza_mt.__add(s1, s2)
271 return s1:add_direct_child(s2);
283 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 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 function clone(stanza)
333 local lookup_table = {};
334 local function _copy(object)
335 if type(object) ~= "table" then
337 elseif lookup_table[object] then
338 return lookup_table[object];
340 local new_table = {};
341 lookup_table[object] = new_table;
342 for index, value in pairs(object) do
343 new_table[_copy(index)] = _copy(value);
345 return setmetatable(new_table, getmetatable(object));
351 function message(attr, body)
353 return stanza("message", attr);
355 return stanza("message", attr):tag("body"):text(body):up();
359 if attr and not attr.id then attr.id = new_id(); end
360 return stanza("iq", attr or { id = new_id() });
364 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) });
368 local xmpp_stanzas_attr = { xmlns = xmlns_stanzas };
369 function error_reply(orig, type, condition, message)
370 local t = reply(orig);
371 t.attr.type = "error";
372 t:tag("error", {type = type}) --COMPAT: Some day xmlns:stanzas goes here
373 :tag(condition, xmpp_stanzas_attr):up();
374 if (message) then t:tag("text", xmpp_stanzas_attr):text(message):up(); end
375 return t; -- stanza ready for adding app-specific errors
379 function presence(attr)
380 return stanza("presence", attr);
383 if do_pretty_printing then
384 local style_attrk = getstyle("yellow");
385 local style_attrv = getstyle("red");
386 local style_tagname = getstyle("red");
387 local style_punc = getstyle("magenta");
389 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'");
390 local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">");
391 --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, ">");
392 local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
393 function stanza_mt.pretty_print(t)
394 local children_text = "";
395 for n, child in ipairs(t) do
396 if type(child) == "string" then
397 children_text = children_text .. xml_escape(child);
399 children_text = children_text .. child:pretty_print();
403 local attr_string = "";
405 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
407 return s_format(tag_format, t.name, attr_string, children_text, t.name);
410 function stanza_mt.pretty_top_tag(t)
411 local attr_string = "";
413 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
415 return s_format(top_tag_format, t.name, attr_string);
418 -- Sorry, fresh out of colours for you guys ;)
419 stanza_mt.pretty_print = stanza_mt.__tostring;
420 stanza_mt.pretty_top_tag = stanza_mt.top_tag;