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;
48 function stanza(name, attr)
49 local stanza = { name = name, attr = attr or {}, tags = {}, last_add = {}};
50 return setmetatable(stanza, stanza_mt);
53 function stanza_mt:query(xmlns)
54 return self:tag("query", { xmlns = xmlns });
57 function stanza_mt:body(text, attr)
58 return self:tag("body", attr):text(text);
61 function stanza_mt:tag(name, attrs)
62 local s = stanza(name, attrs);
63 (self.last_add[#self.last_add] or self):add_direct_child(s);
64 t_insert(self.last_add, s);
68 function stanza_mt:text(text)
69 (self.last_add[#self.last_add] or self):add_direct_child(text);
73 function stanza_mt:up()
74 t_remove(self.last_add);
78 function stanza_mt:reset()
79 local last_add = self.last_add;
80 for i = 1,#last_add do
86 function stanza_mt:add_direct_child(child)
87 if type(child) == "table" then
88 t_insert(self.tags, child);
90 t_insert(self, child);
93 function stanza_mt:add_child(child)
94 (self.last_add[#self.last_add] or self):add_direct_child(child);
98 function stanza_mt:get_child(name, xmlns)
99 for _, child in ipairs(self.tags) do
100 if (not name or child.name == name)
101 and ((not xmlns and self.attr.xmlns == child.attr.xmlns)
102 or child.attr.xmlns == xmlns) then
109 function stanza_mt:child_with_name(name)
110 for _, child in ipairs(self.tags) do
111 if child.name == name then return child; end
115 function stanza_mt:child_with_ns(ns)
116 for _, child in ipairs(self.tags) do
117 if child.attr.xmlns == ns then return child; end
121 function stanza_mt:children()
126 if v then return v; end
129 function stanza_mt:childtags()
133 local v = self.tags[i]
134 if v then return v; end
135 end, self.tags[1], i;
140 local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" };
141 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end
142 _M.xml_escape = xml_escape;
145 local function _dostring(t, buf, self, xml_escape, parentns)
148 t_insert(buf, "<"..name);
149 for k, v in pairs(t.attr) do
150 if s_find(k, "\1", 1, true) then
151 local ns, attrk = s_match(k, "^([^\1]*)\1?(.*)$");
153 t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
154 elseif not(k == "xmlns" and v == parentns) then
155 t_insert(buf, " "..k.."='"..xml_escape(v).."'");
166 self(child, buf, self, xml_escape, t.attr.xmlns);
168 t_insert(buf, xml_escape(child));
171 t_insert(buf, "</"..name..">");
174 function stanza_mt.__tostring(t)
176 _dostring(t, buf, _dostring, xml_escape, nil);
177 return t_concat(buf);
180 function stanza_mt.top_tag(t)
181 local attr_string = "";
183 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
185 return s_format("<%s%s>", t.name, attr_string);
188 function stanza_mt.get_text(t)
194 function stanza_mt.get_error(stanza)
195 local type, condition, text;
197 local error_tag = stanza:get_child("error");
198 if not error_tag then
199 return nil, nil, nil;
201 type = error_tag.attr.type;
203 for child in error_tag:children() do
204 if child.attr.xmlns == xmlns_stanzas then
205 if not text and child.name == "text" then
206 text = child:get_text();
207 elseif not condition then
208 condition = child.name;
210 if condition and text then
215 return type, condition or "undefined-condition", text or "";
218 function stanza_mt.__add(s1, s2)
219 return s1:add_direct_child(s2);
231 function preserialize(stanza)
232 local s = { name = stanza.name, attr = stanza.attr };
233 for _, child in ipairs(stanza) do
234 if type(child) == "table" then
235 t_insert(s, preserialize(child));
243 function deserialize(stanza)
246 local attr = stanza.attr;
247 for i=1,#attr do attr[i] = nil; end
249 for att in pairs(attr) do
250 if s_find(att, "|", 1, true) and not s_find(att, "\1", 1, true) then
251 local ns,na = s_match(att, "^([^|]+)|(.+)$");
252 attrx[ns.."\1"..na] = attr[att];
256 for a,v in pairs(attrx) do
259 setmetatable(stanza, stanza_mt);
260 for _, child in ipairs(stanza) do
261 if type(child) == "table" then
265 if not stanza.tags then
268 for _, child in ipairs(stanza) do
269 if type(child) == "table" then
270 t_insert(tags, child);
274 if not stanza.last_add then
275 stanza.last_add = {};
283 function clone(stanza)
284 local lookup_table = {};
285 local function _copy(object)
286 if type(object) ~= "table" then
288 elseif lookup_table[object] then
289 return lookup_table[object];
291 local new_table = {};
292 lookup_table[object] = new_table;
293 for index, value in pairs(object) do
294 new_table[_copy(index)] = _copy(value);
296 return setmetatable(new_table, getmetatable(object));
302 function message(attr, body)
304 return stanza("message", attr);
306 return stanza("message", attr):tag("body"):text(body);
310 if attr and not attr.id then attr.id = new_id(); end
311 return stanza("iq", attr or { id = new_id() });
315 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) });
319 local xmpp_stanzas_attr = { xmlns = xmlns_stanzas };
320 function error_reply(orig, type, condition, message)
321 local t = reply(orig);
322 t.attr.type = "error";
323 t:tag("error", {type = type}) --COMPAT: Some day xmlns:stanzas goes here
324 :tag(condition, xmpp_stanzas_attr):up();
325 if (message) then t:tag("text", xmpp_stanzas_attr):text(message):up(); end
326 return t; -- stanza ready for adding app-specific errors
330 function presence(attr)
331 return stanza("presence", attr);
334 if do_pretty_printing then
335 local style_attrk = getstyle("yellow");
336 local style_attrv = getstyle("red");
337 local style_tagname = getstyle("red");
338 local style_punc = getstyle("magenta");
340 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'");
341 local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">");
342 --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, ">");
343 local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
344 function stanza_mt.pretty_print(t)
345 local children_text = "";
346 for n, child in ipairs(t) do
347 if type(child) == "string" then
348 children_text = children_text .. xml_escape(child);
350 children_text = children_text .. child:pretty_print();
354 local attr_string = "";
356 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
358 return s_format(tag_format, t.name, attr_string, children_text, t.name);
361 function stanza_mt.pretty_top_tag(t)
362 local attr_string = "";
364 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
366 return s_format(top_tag_format, t.name, attr_string);
369 -- Sorry, fresh out of colours for you guys ;)
370 stanza_mt.pretty_print = stanza_mt.__tostring;
371 stanza_mt.pretty_top_tag = stanza_mt.top_tag;