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 = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" };
206 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end
207 _M.xml_escape = xml_escape;
210 local function _dostring(t, buf, self, xml_escape, parentns)
213 t_insert(buf, "<"..name);
214 for k, v in pairs(t.attr) do
215 if s_find(k, "\1", 1, true) then
216 local ns, attrk = s_match(k, "^([^\1]*)\1?(.*)$");
218 t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
219 elseif not(k == "xmlns" and v == parentns) then
220 t_insert(buf, " "..k.."='"..xml_escape(v).."'");
231 self(child, buf, self, xml_escape, t.attr.xmlns);
233 t_insert(buf, xml_escape(child));
236 t_insert(buf, "</"..name..">");
239 function stanza_mt.__tostring(t)
241 _dostring(t, buf, _dostring, xml_escape, nil);
242 return t_concat(buf);
245 function stanza_mt.top_tag(t)
246 local attr_string = "";
248 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
250 return s_format("<%s%s>", t.name, attr_string);
253 function stanza_mt.get_text(t)
259 function stanza_mt.get_error(stanza)
260 local type, condition, text;
262 local error_tag = stanza:get_child("error");
263 if not error_tag then
264 return nil, nil, nil;
266 type = error_tag.attr.type;
268 for _, child in ipairs(error_tag.tags) do
269 if child.attr.xmlns == xmlns_stanzas then
270 if not text and child.name == "text" then
271 text = child:get_text();
272 elseif not condition then
273 condition = child.name;
275 if condition and text then
280 return type, condition or "undefined-condition", text;
291 function preserialize(stanza)
292 local s = { name = stanza.name, attr = stanza.attr };
293 for _, child in ipairs(stanza) do
294 if type(child) == "table" then
295 t_insert(s, preserialize(child));
303 function deserialize(stanza)
306 local attr = stanza.attr;
307 for i=1,#attr do attr[i] = nil; end
309 for att in pairs(attr) do
310 if s_find(att, "|", 1, true) and not s_find(att, "\1", 1, true) then
311 local ns,na = s_match(att, "^([^|]+)|(.+)$");
312 attrx[ns.."\1"..na] = attr[att];
316 for a,v in pairs(attrx) do
319 setmetatable(stanza, stanza_mt);
320 for _, child in ipairs(stanza) do
321 if type(child) == "table" then
325 if not stanza.tags then
328 for _, child in ipairs(stanza) do
329 if type(child) == "table" then
330 t_insert(tags, child);
340 local function _clone(stanza)
341 local attr, tags = {}, {};
342 for k,v in pairs(stanza.attr) do attr[k] = v; end
343 local new = { name = stanza.name, attr = attr, tags = tags };
345 local child = stanza[i];
347 child = _clone(child);
348 t_insert(tags, child);
350 t_insert(new, child);
352 return setmetatable(new, stanza_mt);
356 function message(attr, body)
358 return stanza("message", attr);
360 return stanza("message", attr):tag("body"):text(body):up();
364 if attr and not attr.id then attr.id = new_id(); end
365 return stanza("iq", attr or { id = new_id() });
369 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) });
373 local xmpp_stanzas_attr = { xmlns = xmlns_stanzas };
374 function error_reply(orig, type, condition, message)
375 local t = reply(orig);
376 t.attr.type = "error";
377 t:tag("error", {type = type}) --COMPAT: Some day xmlns:stanzas goes here
378 :tag(condition, xmpp_stanzas_attr):up();
379 if (message) then t:tag("text", xmpp_stanzas_attr):text(message):up(); end
380 return t; -- stanza ready for adding app-specific errors
384 function presence(attr)
385 return stanza("presence", attr);
388 if do_pretty_printing then
389 local style_attrk = getstyle("yellow");
390 local style_attrv = getstyle("red");
391 local style_tagname = getstyle("red");
392 local style_punc = getstyle("magenta");
394 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'");
395 local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">");
396 --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, ">");
397 local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
398 function stanza_mt.pretty_print(t)
399 local children_text = "";
400 for n, child in ipairs(t) do
401 if type(child) == "string" then
402 children_text = children_text .. xml_escape(child);
404 children_text = children_text .. child:pretty_print();
408 local attr_string = "";
410 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
412 return s_format(tag_format, t.name, attr_string, children_text, t.name);
415 function stanza_mt.pretty_top_tag(t)
416 local attr_string = "";
418 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
420 return s_format(top_tag_format, t.name, attr_string);
423 -- Sorry, fresh out of colours for you guys ;)
424 stanza_mt.pretty_print = stanza_mt.__tostring;
425 stanza_mt.pretty_top_tag = stanza_mt.top_tag;