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 stanza_mt = { __type = "stanza" };
41 stanza_mt.__index = stanza_mt;
42 local stanza_mt = stanza_mt;
44 function stanza(name, attr)
45 local stanza = { name = name, attr = attr or {}, tags = {} };
46 return setmetatable(stanza, stanza_mt);
48 local stanza = stanza;
50 function stanza_mt:query(xmlns)
51 return self:tag("query", { xmlns = xmlns });
54 function stanza_mt:body(text, attr)
55 return self:tag("body", attr):text(text);
58 function stanza_mt:tag(name, attrs)
59 local s = stanza(name, attrs);
60 local last_add = self.last_add;
61 if not last_add then last_add = {}; self.last_add = last_add; end
62 (last_add[#last_add] or self):add_direct_child(s);
63 t_insert(last_add, s);
67 function stanza_mt:text(text)
68 local last_add = self.last_add;
69 (last_add and last_add[#last_add] or self):add_direct_child(text);
73 function stanza_mt:up()
74 local last_add = self.last_add;
75 if last_add then t_remove(last_add); end
79 function stanza_mt:reset()
84 function stanza_mt:add_direct_child(child)
85 if type(child) == "table" then
86 t_insert(self.tags, child);
88 t_insert(self, child);
91 function stanza_mt:add_child(child)
92 local last_add = self.last_add;
93 (last_add and last_add[#last_add] or self):add_direct_child(child);
97 function stanza_mt:get_child(name, xmlns)
98 for _, child in ipairs(self.tags) do
99 if (not name or child.name == name)
100 and ((not xmlns and self.attr.xmlns == child.attr.xmlns)
101 or child.attr.xmlns == xmlns) then
108 function stanza_mt:get_child_text(name, xmlns)
109 local tag = self:get_child(name, xmlns);
111 return tag:get_text();
116 function stanza_mt:child_with_name(name)
117 for _, child in ipairs(self.tags) do
118 if child.name == name then return child; end
122 function stanza_mt:child_with_ns(ns)
123 for _, child in ipairs(self.tags) do
124 if child.attr.xmlns == ns then return child; end
128 function stanza_mt:children()
136 function stanza_mt:childtags(name, xmlns)
137 local tags = self.tags;
138 local start_i, max_i = 1, #tags;
140 for i = start_i, max_i do
142 if (not name or v.name == name)
143 and ((not xmlns and self.attr.xmlns == v.attr.xmlns)
144 or v.attr.xmlns == xmlns) then
152 function stanza_mt:maptags(callback)
153 local tags, curr_tag = self.tags, 1;
154 local n_children, n_tags = #self, #tags;
157 while curr_tag <= n_tags and n_tags > 0 do
158 if self[i] == tags[curr_tag] then
159 local ret = callback(self[i]);
162 t_remove(tags, curr_tag);
163 n_children = n_children - 1;
166 curr_tag = curr_tag - 1;
169 tags[curr_tag] = ret;
171 curr_tag = curr_tag + 1;
178 function stanza_mt:find(path)
180 local len = #path + 1;
183 local xmlns, name, text;
184 local char = s_sub(path, pos, pos);
186 return self.attr[s_sub(path, pos + 1)];
187 elseif char == "{" then
188 xmlns, pos = s_match(path, "^([^}]+)}()", pos + 1);
190 name, text, pos = s_match(path, "^([^@/#]*)([/#]?)()", pos);
191 name = name ~= "" and name or nil;
194 return self:get_child_text(name, xmlns);
196 return self:get_child(name, xmlns);
198 self = self:get_child(name, xmlns);
205 local escape_table = {
211 -- escape this whitespace because [\r\n\t] change into spaces in attributes
212 -- and \r\n changes into \n in text, and we want to preserve original bytes
217 function xml_escape(str) return (s_gsub(str, "['&<>\"\t\n\r]", escape_table)); end
218 _M.xml_escape = xml_escape;
221 local function _dostring(t, buf, self, xml_escape, parentns)
224 t_insert(buf, "<"..name);
225 for k, v in pairs(t.attr) do
226 if s_find(k, "\1", 1, true) then
227 local ns, attrk = s_match(k, "^([^\1]*)\1?(.*)$");
229 t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
230 elseif not(k == "xmlns" and v == parentns) then
231 t_insert(buf, " "..k.."='"..xml_escape(v).."'");
242 self(child, buf, self, xml_escape, t.attr.xmlns);
244 t_insert(buf, xml_escape(child));
247 t_insert(buf, "</"..name..">");
250 function stanza_mt.__tostring(t)
252 _dostring(t, buf, _dostring, xml_escape, nil);
253 return t_concat(buf);
256 function stanza_mt.top_tag(t)
257 local attr_string = "";
259 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
261 return s_format("<%s%s>", t.name, attr_string);
264 function stanza_mt.get_text(t)
270 function stanza_mt.get_error(stanza)
271 local type, condition, text;
273 local error_tag = stanza:get_child("error");
274 if not error_tag then
275 return nil, nil, nil;
277 type = error_tag.attr.type;
279 for _, child in ipairs(error_tag.tags) do
280 if child.attr.xmlns == xmlns_stanzas then
281 if not text and child.name == "text" then
282 text = child:get_text();
283 elseif not condition then
284 condition = child.name;
286 if condition and text then
291 return type, condition or "undefined-condition", text;
302 function preserialize(stanza)
303 local s = { name = stanza.name, attr = stanza.attr };
304 for _, child in ipairs(stanza) do
305 if type(child) == "table" then
306 t_insert(s, preserialize(child));
314 function deserialize(stanza)
317 local attr = stanza.attr;
318 for i=1,#attr do attr[i] = nil; end
320 for att in pairs(attr) do
321 if s_find(att, "|", 1, true) and not s_find(att, "\1", 1, true) then
322 local ns,na = s_match(att, "^([^|]+)|(.+)$");
323 attrx[ns.."\1"..na] = attr[att];
327 for a,v in pairs(attrx) do
330 setmetatable(stanza, stanza_mt);
331 for _, child in ipairs(stanza) do
332 if type(child) == "table" then
336 if not stanza.tags then
339 for _, child in ipairs(stanza) do
340 if type(child) == "table" then
341 t_insert(tags, child);
351 local function _clone(stanza)
352 local attr, tags = {}, {};
353 for k,v in pairs(stanza.attr) do attr[k] = v; end
354 local new = { name = stanza.name, attr = attr, tags = tags };
356 local child = stanza[i];
358 child = _clone(child);
359 t_insert(tags, child);
361 t_insert(new, child);
363 return setmetatable(new, stanza_mt);
367 function message(attr, body)
369 return stanza("message", attr);
371 return stanza("message", attr):tag("body"):text(body):up();
375 if attr and not attr.id then attr.id = new_id(); end
376 return stanza("iq", attr or { id = new_id() });
380 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) });
384 local xmpp_stanzas_attr = { xmlns = xmlns_stanzas };
385 function error_reply(orig, type, condition, message)
386 local t = reply(orig);
387 t.attr.type = "error";
388 t:tag("error", {type = type}) --COMPAT: Some day xmlns:stanzas goes here
389 :tag(condition, xmpp_stanzas_attr):up();
390 if (message) then t:tag("text", xmpp_stanzas_attr):text(message):up(); end
391 return t; -- stanza ready for adding app-specific errors
395 function presence(attr)
396 return stanza("presence", attr);
399 if do_pretty_printing then
400 local style_attrk = getstyle("yellow");
401 local style_attrv = getstyle("red");
402 local style_tagname = getstyle("red");
403 local style_punc = getstyle("magenta");
405 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'");
406 local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">");
407 --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, ">");
408 local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
409 function stanza_mt.pretty_print(t)
410 local children_text = "";
411 for n, child in ipairs(t) do
412 if type(child) == "string" then
413 children_text = children_text .. xml_escape(child);
415 children_text = children_text .. child:pretty_print();
419 local attr_string = "";
421 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
423 return s_format(tag_format, t.name, attr_string, children_text, t.name);
426 function stanza_mt.pretty_top_tag(t)
427 local attr_string = "";
429 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
431 return s_format(top_tag_format, t.name, attr_string);
434 -- Sorry, fresh out of colours for you guys ;)
435 stanza_mt.pretty_print = stanza_mt.__tostring;
436 stanza_mt.pretty_top_tag = stanza_mt.top_tag;