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;
53 function xml_handlers:StartElement(tagname, attr)
54 if stanza and #chardata > 0 then
55 -- We have some character data in the buffer
56 stanza:text(t_concat(chardata));
59 local curr_ns,name = tagname:match("^(.+)|([%w%-]+)$");
60 if curr_ns ~= "jabber:server" then
65 for i, k in ipairs(attr) do
66 if type(k) == "string" then
67 local ns, nm = k:match("^([^|]+)|?([^|]-)$")
71 attr[ns..":"..nm] = attr[k];
72 attr[i] = ns..":"..nm;
79 if not stanza then --if we are not currently inside a stanza
80 if session.notopen then
81 if tagname == stream_tag then
82 if cb_streamopened then
83 cb_streamopened(session, attr);
86 -- Garbage before stream?
87 cb_error(session, "no-stream");
91 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
92 cb_error(session, "invalid-top-level-element");
95 stanza = st.stanza(name, attr);
97 else -- we are inside a stanza, so add a tag
99 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then
100 attr.xmlns = curr_ns;
102 stanza:tag(name, attr);
105 function xml_handlers:CharacterData(data)
107 t_insert(chardata, data);
110 function xml_handlers:EndElement(tagname)
111 curr_ns,name = tagname:match("^(.+)|([%w%-]+)$");
112 if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then
113 if tagname == stream_tag then
114 if cb_streamclosed then
115 cb_streamclosed(session);
118 elseif name == "error" then
119 cb_error(session, "stream-error", stanza);
121 cb_error(session, "parse-error", "unexpected-element-close", name);
125 if #chardata > 0 then
126 -- We have some character data in the buffer
127 stanza:text(t_concat(chardata));
131 if #stanza.last_add == 0 then
132 cb_handlestanza(session, stanza);
142 return init_xmlhandlers;