2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local t_insert = table.insert;
10 local t_concat = table.concat;
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;
17 local getmetatable = getmetatable;
19 local ipairs = ipairs;
23 local unpack = unpack;
24 local s_gsub = string.gsub;
25 local s_char = string.char;
26 local s_find = string.find;
29 local do_pretty_printing = not os.getenv("WINDIR");
30 local getstyle, getstring;
31 if do_pretty_printing then
32 local ok, termcolours = pcall(require, "util.termcolours");
34 getstyle, getstring = termcolours.getstyle, termcolours.getstring;
36 do_pretty_printing = nil;
42 stanza_mt = { __type = "stanza" };
43 stanza_mt.__index = stanza_mt;
45 function stanza(name, attr)
46 local stanza = { name = name, attr = attr or {}, tags = {}, last_add = {}};
47 return setmetatable(stanza, stanza_mt);
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 (self.last_add[#self.last_add] or self):add_direct_child(s);
61 t_insert(self.last_add, s);
65 function stanza_mt:text(text)
66 (self.last_add[#self.last_add] or self):add_direct_child(text);
70 function stanza_mt:up()
71 t_remove(self.last_add);
75 function stanza_mt:reset()
76 local last_add = self.last_add;
77 for i = 1,#last_add do
83 function stanza_mt:add_direct_child(child)
84 if type(child) == "table" then
85 t_insert(self.tags, child);
87 t_insert(self, child);
90 function stanza_mt:add_child(child)
91 (self.last_add[#self.last_add] or self):add_direct_child(child);
95 function stanza_mt:child_with_name(name)
96 for _, child in ipairs(self.tags) do
97 if child.name == name then return child; end
101 function stanza_mt:child_with_ns(ns)
102 for _, child in ipairs(self.tags) do
103 if child.attr.xmlns == ns then return child; end
107 function stanza_mt:children()
112 if v then return v; end
116 function stanza_mt:childtags()
120 local v = self.tags[i]
121 if v then return v; end
122 end, self.tags[1], i;
128 local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" };
129 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end
130 _M.xml_escape = xml_escape;
133 local function _dostring(t, buf, self, xml_escape)
136 t_insert(buf, "<"..name);
137 for k, v in pairs(t.attr) do
138 if s_find(k, "|", 1, true) then
139 local ns, attrk = s_match(k, "^([^|]+)|(.+)$");
141 t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
143 t_insert(buf, " "..k.."='"..xml_escape(v).."'");
154 self(child, buf, self, xml_escape);
156 t_insert(buf, xml_escape(child));
159 t_insert(buf, "</"..name..">");
162 function stanza_mt.__tostring(t)
164 _dostring(t, buf, _dostring, xml_escape);
165 return t_concat(buf);
168 function stanza_mt.top_tag(t)
169 local attr_string = "";
171 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
173 return s_format("<%s%s>", t.name, attr_string);
176 function stanza_mt.get_text(t)
182 function stanza_mt.__add(s1, s2)
183 return s1:add_direct_child(s2);
195 function preserialize(stanza)
196 local s = { name = stanza.name, attr = stanza.attr };
197 for _, child in ipairs(stanza) do
198 if type(child) == "table" then
199 t_insert(s, preserialize(child));
207 function deserialize(stanza)
210 local attr = stanza.attr;
211 for i=1,#attr do attr[i] = nil; end
212 setmetatable(stanza, stanza_mt);
213 for _, child in ipairs(stanza) do
214 if type(child) == "table" then
218 if not stanza.tags then
221 for _, child in ipairs(stanza) do
222 if type(child) == "table" then
223 t_insert(tags, child);
227 if not stanza.last_add then
228 stanza.last_add = {};
236 function clone(stanza)
237 local lookup_table = {};
238 local function _copy(object)
239 if type(object) ~= "table" then
241 elseif lookup_table[object] then
242 return lookup_table[object];
244 local new_table = {};
245 lookup_table[object] = new_table;
246 for index, value in pairs(object) do
247 new_table[_copy(index)] = _copy(value);
249 return setmetatable(new_table, getmetatable(object));
255 function message(attr, body)
257 return stanza("message", attr);
259 return stanza("message", attr):tag("body"):text(body);
263 if attr and not attr.id then attr.id = new_id(); end
264 return stanza("iq", attr or { id = new_id() });
268 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) });
271 function error_reply(orig, type, condition, message)
272 local t = reply(orig);
273 t.attr.type = "error";
274 t:tag("error", {type = type})
275 :tag(condition, {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}):up();
276 if (message) then t:tag("text"):text(message):up(); end
277 return t; -- stanza ready for adding app-specific errors
280 function presence(attr)
281 return stanza("presence", attr);
284 if do_pretty_printing then
285 local style_attrk = getstyle("yellow");
286 local style_attrv = getstyle("red");
287 local style_tagname = getstyle("red");
288 local style_punc = getstyle("magenta");
290 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'");
291 local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">");
292 --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, ">");
293 local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
294 function stanza_mt.pretty_print(t)
295 local children_text = "";
296 for n, child in ipairs(t) do
297 if type(child) == "string" then
298 children_text = children_text .. xml_escape(child);
300 children_text = children_text .. child:pretty_print();
304 local attr_string = "";
306 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
308 return s_format(tag_format, t.name, attr_string, children_text, t.name);
311 function stanza_mt.pretty_top_tag(t)
312 local attr_string = "";
314 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
316 return s_format(top_tag_format, t.name, attr_string);
319 -- Sorry, fresh out of colours for you guys ;)
320 stanza_mt.pretty_print = stanza_mt.__tostring;
321 stanza_mt.pretty_top_tag = stanza_mt.top_tag;