| 1 | -- Prosody IM |
| 2 | -- Copyright (C) 2008-2010 Matthew Wild |
| 3 | -- Copyright (C) 2008-2010 Waqas Hussain |
| 4 | -- |
| 5 | -- This project is MIT/X11 licensed. Please see the |
| 6 | -- COPYING file in the source package for more information. |
| 7 | -- |
| 8 | |
| 9 | |
| 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; |
| 19 | local pairs = pairs; |
| 20 | local ipairs = ipairs; |
| 21 | local type = type; |
| 22 | local next = next; |
| 23 | local print = print; |
| 24 | local unpack = unpack; |
| 25 | local s_gsub = string.gsub; |
| 26 | local s_char = string.char; |
| 27 | local s_find = string.find; |
| 28 | local os = os; |
| 29 | |
| 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"); |
| 34 | if ok then |
| 35 | getstyle, getstring = termcolours.getstyle, termcolours.getstring; |
| 36 | else |
| 37 | do_pretty_printing = nil; |
| 38 | end |
| 39 | end |
| 40 | |
| 41 | local xmlns_stanzas = "urn:ietf:params:xml:ns:xmpp-stanzas"; |
| 42 | |
| 43 | module "stanza" |
| 44 | |
| 45 | stanza_mt = { __type = "stanza" }; |
| 46 | stanza_mt.__index = stanza_mt; |
| 47 | |
| 48 | function stanza(name, attr) |
| 49 | local stanza = { name = name, attr = attr or {}, tags = {} }; |
| 50 | return setmetatable(stanza, stanza_mt); |
| 51 | end |
| 52 | |
| 53 | function stanza_mt:query(xmlns) |
| 54 | return self:tag("query", { xmlns = xmlns }); |
| 55 | end |
| 56 | |
| 57 | function stanza_mt:body(text, attr) |
| 58 | return self:tag("body", attr):text(text); |
| 59 | end |
| 60 | |
| 61 | function stanza_mt:tag(name, attrs) |
| 62 | local s = stanza(name, attrs); |
| 63 | local last_add = self.last_add; |
| 64 | if not last_add then last_add = {}; self.last_add = last_add; end |
| 65 | (last_add[#last_add] or self):add_direct_child(s); |
| 66 | t_insert(last_add, s); |
| 67 | return self; |
| 68 | end |
| 69 | |
| 70 | function stanza_mt:text(text) |
| 71 | local last_add = self.last_add; |
| 72 | (last_add and last_add[#last_add] or self):add_direct_child(text); |
| 73 | return self; |
| 74 | end |
| 75 | |
| 76 | function stanza_mt:up() |
| 77 | local last_add = self.last_add; |
| 78 | if last_add then t_remove(last_add); end |
| 79 | return self; |
| 80 | end |
| 81 | |
| 82 | function stanza_mt:reset() |
| 83 | self.last_add = nil; |
| 84 | return self; |
| 85 | end |
| 86 | |
| 87 | function stanza_mt:add_direct_child(child) |
| 88 | if type(child) == "table" then |
| 89 | t_insert(self.tags, child); |
| 90 | end |
| 91 | t_insert(self, child); |
| 92 | end |
| 93 | |
| 94 | function stanza_mt:add_child(child) |
| 95 | local last_add = self.last_add; |
| 96 | (last_add and last_add[#last_add] or self):add_direct_child(child); |
| 97 | return self; |
| 98 | end |
| 99 | |
| 100 | function stanza_mt:get_child(name, xmlns) |
| 101 | for _, child in ipairs(self.tags) do |
| 102 | if (not name or child.name == name) |
| 103 | and ((not xmlns and self.attr.xmlns == child.attr.xmlns) |
| 104 | or child.attr.xmlns == xmlns) then |
| 105 | |
| 106 | return child; |
| 107 | end |
| 108 | end |
| 109 | end |
| 110 | |
| 111 | function stanza_mt:child_with_name(name) |
| 112 | for _, child in ipairs(self.tags) do |
| 113 | if child.name == name then return child; end |
| 114 | end |
| 115 | end |
| 116 | |
| 117 | function stanza_mt:child_with_ns(ns) |
| 118 | for _, child in ipairs(self.tags) do |
| 119 | if child.attr.xmlns == ns then return child; end |
| 120 | end |
| 121 | end |
| 122 | |
| 123 | function stanza_mt:children() |
| 124 | local i = 0; |
| 125 | return function (a) |
| 126 | i = i + 1 |
| 127 | return a[i]; |
| 128 | end, self, i; |
| 129 | end |
| 130 | |
| 131 | function stanza_mt:matching_tags(name, xmlns) |
| 132 | xmlns = xmlns or self.attr.xmlns; |
| 133 | local tags = self.tags; |
| 134 | local start_i, max_i = 1, #tags; |
| 135 | return function () |
| 136 | for i=start_i,max_i do |
| 137 | v = tags[i]; |
| 138 | if (not name or v.name == name) |
| 139 | and (not xmlns or xmlns == v.attr.xmlns) then |
| 140 | start_i = i+1; |
| 141 | return v; |
| 142 | end |
| 143 | end |
| 144 | end, tags, i; |
| 145 | end |
| 146 | |
| 147 | function stanza_mt:childtags() |
| 148 | local i = 0; |
| 149 | return function (a) |
| 150 | i = i + 1 |
| 151 | local v = self.tags[i] |
| 152 | if v then return v; end |
| 153 | end, self.tags[1], i; |
| 154 | end |
| 155 | |
| 156 | function stanza_mt:maptags(callback) |
| 157 | local tags, curr_tag = self.tags, 1; |
| 158 | local n_children, n_tags = #self, #tags; |
| 159 | |
| 160 | local i = 1; |
| 161 | while curr_tag <= n_tags do |
| 162 | if self[i] == tags[curr_tag] then |
| 163 | local ret = callback(self[i]); |
| 164 | if ret == nil then |
| 165 | t_remove(self, i); |
| 166 | t_remove(tags, curr_tag); |
| 167 | n_children = n_children - 1; |
| 168 | n_tags = n_tags - 1; |
| 169 | else |
| 170 | self[i] = ret; |
| 171 | tags[i] = ret; |
| 172 | end |
| 173 | i = i + 1; |
| 174 | curr_tag = curr_tag + 1; |
| 175 | end |
| 176 | end |
| 177 | return self; |
| 178 | end |
| 179 | |
| 180 | local xml_escape |
| 181 | do |
| 182 | local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; |
| 183 | function xml_escape(str) return (s_gsub(tostring(str), "['&<>\"]", escape_table)); end |
| 184 | _M.xml_escape = xml_escape; |
| 185 | end |
| 186 | |
| 187 | local function _dostring(t, buf, self, xml_escape, parentns) |
| 188 | local nsid = 0; |
| 189 | local name = t.name |
| 190 | t_insert(buf, "<"..name); |
| 191 | for k, v in pairs(t.attr) do |
| 192 | if s_find(k, "\1", 1, true) then |
| 193 | local ns, attrk = s_match(k, "^([^\1]*)\1?(.*)$"); |
| 194 | nsid = nsid + 1; |
| 195 | t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'"); |
| 196 | elseif not(k == "xmlns" and (v == parentns or not v)) then |
| 197 | t_insert(buf, " "..k.."='"..xml_escape(v).."'"); |
| 198 | end |
| 199 | end |
| 200 | local len = #t; |
| 201 | if len == 0 then |
| 202 | t_insert(buf, "/>"); |
| 203 | else |
| 204 | t_insert(buf, ">"); |
| 205 | for n=1,len do |
| 206 | local child = t[n]; |
| 207 | if child.name then |
| 208 | self(child, buf, self, xml_escape, t.attr.xmlns); |
| 209 | else |
| 210 | t_insert(buf, xml_escape(child)); |
| 211 | end |
| 212 | end |
| 213 | t_insert(buf, "</"..name..">"); |
| 214 | end |
| 215 | end |
| 216 | function stanza_mt.__tostring(t) |
| 217 | local buf = {}; |
| 218 | _dostring(t, buf, _dostring, xml_escape, nil); |
| 219 | return t_concat(buf); |
| 220 | end |
| 221 | |
| 222 | function stanza_mt.top_tag(t) |
| 223 | local attr_string = ""; |
| 224 | if t.attr then |
| 225 | 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 |
| 226 | end |
| 227 | return s_format("<%s%s>", t.name, attr_string); |
| 228 | end |
| 229 | |
| 230 | function stanza_mt.get_text(t) |
| 231 | if #t.tags == 0 then |
| 232 | local s = {}; |
| 233 | for i, v in ipairs(t) do |
| 234 | s[i] = tostring(v); |
| 235 | end |
| 236 | return t_concat(s); |
| 237 | end |
| 238 | end |
| 239 | |
| 240 | function stanza_mt.get_error(stanza) |
| 241 | local type, condition, text; |
| 242 | |
| 243 | local error_tag = stanza:get_child("error"); |
| 244 | if not error_tag then |
| 245 | return nil, nil, nil; |
| 246 | end |
| 247 | type = error_tag.attr.type; |
| 248 | |
| 249 | for child in error_tag:childtags() do |
| 250 | if child.attr.xmlns == xmlns_stanzas then |
| 251 | if not text and child.name == "text" then |
| 252 | text = child:get_text(); |
| 253 | elseif not condition then |
| 254 | condition = child.name; |
| 255 | end |
| 256 | if condition and text then |
| 257 | break; |
| 258 | end |
| 259 | end |
| 260 | end |
| 261 | return type, condition or "undefined-condition", text; |
| 262 | end |
| 263 | |
| 264 | function stanza_mt.__add(s1, s2) |
| 265 | return s1:add_direct_child(s2); |
| 266 | end |
| 267 | |
| 268 | |
| 269 | do |
| 270 | local id = 0; |
| 271 | function new_id() |
| 272 | id = id + 1; |
| 273 | return "lx"..id; |
| 274 | end |
| 275 | end |
| 276 | |
| 277 | function preserialize(stanza) |
| 278 | local s = { name = stanza.name, attr = stanza.attr }; |
| 279 | for _, child in ipairs(stanza) do |
| 280 | if child.tags then |
| 281 | t_insert(s, preserialize(child)); |
| 282 | else |
| 283 | t_insert(s, tostring(child)); |
| 284 | end |
| 285 | end |
| 286 | return s; |
| 287 | end |
| 288 | |
| 289 | function deserialize(stanza) |
| 290 | -- Set metatable |
| 291 | if stanza then |
| 292 | local attr = stanza.attr; |
| 293 | for i=1,#attr do attr[i] = nil; end |
| 294 | local attrx = {}; |
| 295 | for att in pairs(attr) do |
| 296 | if s_find(att, "|", 1, true) and not s_find(att, "\1", 1, true) then |
| 297 | local ns,na = s_match(att, "^([^|]+)|(.+)$"); |
| 298 | attrx[ns.."\1"..na] = attr[att]; |
| 299 | attr[att] = nil; |
| 300 | end |
| 301 | end |
| 302 | for a,v in pairs(attrx) do |
| 303 | attr[a] = v; |
| 304 | end |
| 305 | setmetatable(stanza, stanza_mt); |
| 306 | for _, child in ipairs(stanza) do |
| 307 | if child.tags then |
| 308 | deserialize(child); |
| 309 | end |
| 310 | end |
| 311 | if not stanza.tags then |
| 312 | -- Rebuild tags |
| 313 | local tags = {}; |
| 314 | for _, child in ipairs(stanza) do |
| 315 | if type(child) == "table" then |
| 316 | t_insert(tags, child); |
| 317 | end |
| 318 | end |
| 319 | stanza.tags = tags; |
| 320 | end |
| 321 | end |
| 322 | |
| 323 | return stanza; |
| 324 | end |
| 325 | |
| 326 | function clone(stanza) |
| 327 | local lookup_table = {}; |
| 328 | local function _copy(object) |
| 329 | if type(object) ~= "table" then |
| 330 | return object; |
| 331 | elseif lookup_table[object] then |
| 332 | return lookup_table[object]; |
| 333 | end |
| 334 | local new_table = {}; |
| 335 | lookup_table[object] = new_table; |
| 336 | for index, value in pairs(object) do |
| 337 | new_table[_copy(index)] = _copy(value); |
| 338 | end |
| 339 | return setmetatable(new_table, getmetatable(object)); |
| 340 | end |
| 341 | |
| 342 | return _copy(stanza) |
| 343 | end |
| 344 | |
| 345 | function message(attr, body) |
| 346 | if not body then |
| 347 | return stanza("message", attr); |
| 348 | else |
| 349 | return stanza("message", attr):tag("body"):text(body):up(); |
| 350 | end |
| 351 | end |
| 352 | function iq(attr) |
| 353 | if attr and not attr.id then attr.id = new_id(); end |
| 354 | return stanza("iq", attr or { id = new_id() }); |
| 355 | end |
| 356 | |
| 357 | function reply(orig) |
| 358 | 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) }); |
| 359 | end |
| 360 | |
| 361 | do |
| 362 | local xmpp_stanzas_attr = { xmlns = xmlns_stanzas }; |
| 363 | function error_reply(orig, type, condition, message) |
| 364 | local t = reply(orig); |
| 365 | t.attr.type = "error"; |
| 366 | t:tag("error", {type = type}) --COMPAT: Some day xmlns:stanzas goes here |
| 367 | :tag(condition, xmpp_stanzas_attr):up(); |
| 368 | if (message) then t:tag("text", xmpp_stanzas_attr):text(message):up(); end |
| 369 | return t; -- stanza ready for adding app-specific errors |
| 370 | end |
| 371 | end |
| 372 | |
| 373 | function presence(attr) |
| 374 | return stanza("presence", attr); |
| 375 | end |
| 376 | |
| 377 | if do_pretty_printing then |
| 378 | local style_attrk = getstyle("yellow"); |
| 379 | local style_attrv = getstyle("red"); |
| 380 | local style_tagname = getstyle("red"); |
| 381 | local style_punc = getstyle("magenta"); |
| 382 | |
| 383 | local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'"); |
| 384 | local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">"); |
| 385 | --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, ">"); |
| 386 | local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">"); |
| 387 | function stanza_mt.pretty_print(t) |
| 388 | local children_text = ""; |
| 389 | for n, child in ipairs(t) do |
| 390 | if type(child) == "string" then |
| 391 | children_text = children_text .. xml_escape(child); |
| 392 | else |
| 393 | children_text = children_text .. child:pretty_print(); |
| 394 | end |
| 395 | end |
| 396 | |
| 397 | local attr_string = ""; |
| 398 | if t.attr then |
| 399 | 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 |
| 400 | end |
| 401 | return s_format(tag_format, t.name, attr_string, children_text, t.name); |
| 402 | end |
| 403 | |
| 404 | function stanza_mt.pretty_top_tag(t) |
| 405 | local attr_string = ""; |
| 406 | if t.attr then |
| 407 | 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 |
| 408 | end |
| 409 | return s_format(top_tag_format, t.name, attr_string); |
| 410 | end |
| 411 | else |
| 412 | -- Sorry, fresh out of colours for you guys ;) |
| 413 | stanza_mt.pretty_print = stanza_mt.__tostring; |
| 414 | stanza_mt.pretty_top_tag = stanza_mt.top_tag; |
| 415 | end |
| 416 | |
| 417 | return _M; |