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.
14 local tostring = tostring;
16 local ipairs = ipairs;
19 local format = string.format;
20 local m_random = math.random;
21 local t_insert = table.insert;
22 local t_remove = table.remove;
23 local t_concat = table.concat;
24 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end
25 local sm_destroy_session = import("core.sessionmanager", "destroy_session");
27 local default_log = require "util.logger".init("xmlhandlers");
34 ["http://www.w3.org/XML/1998/namespace"] = "xml";
37 function init_xmlhandlers(session, stream_callbacks)
38 local ns_stack = { "" };
39 local curr_ns, name = "";
42 local xml_handlers = {};
43 local log = session.log or default_log;
45 local cb_streamopened = stream_callbacks.streamopened;
46 local cb_streamclosed = stream_callbacks.streamclosed;
47 local cb_error = stream_callbacks.error or function (session, e) error("XML stream error: "..tostring(e)); end;
48 local cb_handlestanza = stream_callbacks.handlestanza;
50 local stream_tag = stream_callbacks.stream_tag;
51 local stream_default_ns = stream_callbacks.default_ns;
54 function xml_handlers:StartElement(tagname, attr)
55 if stanza and #chardata > 0 then
56 -- We have some character data in the buffer
57 stanza:text(t_concat(chardata));
60 local curr_ns,name = tagname:match("^(.+)|([%w%-]+)$");
61 if curr_ns ~= stream_default_ns then
66 for i, k in ipairs(attr) do
67 if type(k) == "string" then
68 local ns, nm = k:match("^([^|]+)|?([^|]-)$")
72 attr[ns..":"..nm] = attr[k];
73 attr[i] = ns..":"..nm;
80 if not stanza then --if we are not currently inside a stanza
81 if session.notopen then
82 if tagname == stream_tag then
83 if cb_streamopened then
84 cb_streamopened(session, attr);
87 -- Garbage before stream?
88 cb_error(session, "no-stream");
92 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
93 cb_error(session, "invalid-top-level-element");
96 stanza = st.stanza(name, attr);
98 else -- we are inside a stanza, so add a tag
100 if curr_ns ~= stream_default_ns then
101 attr.xmlns = curr_ns;
103 stanza:tag(name, attr);
106 function xml_handlers:CharacterData(data)
108 t_insert(chardata, data);
111 function xml_handlers:EndElement(tagname)
112 curr_ns,name = tagname:match("^(.+)|([%w%-]+)$");
113 if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then
114 if tagname == stream_tag then
115 if cb_streamclosed then
116 cb_streamclosed(session);
119 elseif name == "error" then
120 cb_error(session, "stream-error", stanza);
122 cb_error(session, "parse-error", "unexpected-element-close", name);
126 if #chardata > 0 then
127 -- We have some character data in the buffer
128 stanza:text(t_concat(chardata));
132 if #stanza.last_add == 0 then
133 cb_handlestanza(session, stanza);
143 return init_xmlhandlers;