2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 local tostring = tostring;
27 local ipairs = ipairs;
30 local format = string.format;
31 local m_random = math.random;
32 local t_insert = table.insert;
33 local t_remove = table.remove;
34 local t_concat = table.concat;
35 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
36 local sm_destroy_session = import("core.sessionmanager", "destroy_session");
38 local default_log = require "util.logger".init("xmlhandlers");
45 ["http://www.w3.org/XML/1998/namespace"] = "xml";
48 function init_xmlhandlers(session, stream_callbacks)
49 local ns_stack = { "" };
53 local xml_handlers = {};
54 local log = session.log or default_log;
56 local send = session.send;
58 local cb_streamopened = stream_callbacks.streamopened;
59 local cb_streamclosed = stream_callbacks.streamclosed;
60 local cb_error = stream_callbacks.error or function (session, e) error("XML stream error: "..tostring(e)); end;
61 local cb_handlestanza = stream_callbacks.handlestanza;
63 local stream_ns = stream_callbacks.ns;
66 function xml_handlers:StartElement(name, attr)
67 if stanza and #chardata > 0 then
68 -- We have some character data in the buffer
69 stanza:text(t_concat(chardata));
72 curr_ns,name = name:match("^(.+)|([%w%-]+)$");
73 if curr_ns ~= "jabber:server" then
78 for i, k in ipairs(attr) do
79 if type(k) == "string" then
80 local ns, nm = k:match("^([^|]+)|?([^|]-)$")
84 attr[ns..":"..nm] = attr[k];
85 attr[i] = ns..":"..nm;
92 if not stanza then --if we are not currently inside a stanza
93 if session.notopen then
94 if name == "stream" and curr_ns == stream_ns then
95 if cb_streamopened then
96 cb_streamopened(session, attr);
99 -- Garbage before stream?
100 cb_error(session, "no-stream");
104 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
105 cb_error(session, "invalid-top-level-element");
108 stanza = st.stanza(name, attr);
110 else -- we are inside a stanza, so add a tag
112 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then
113 attr.xmlns = curr_ns;
115 stanza:tag(name, attr);
118 function xml_handlers:CharacterData(data)
120 t_insert(chardata, data);
123 function xml_handlers:EndElement(name)
124 curr_ns,name = name:match("^(.+)|([%w%-]+)$");
125 if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then
126 if name == "stream" then
127 if cb_streamclosed then
128 cb_streamclosed(session);
131 elseif name == "error" then
132 cb_error(session, "stream-error", stanza);
134 cb_error(session, "parse-error", "unexpected-element-close", name);
137 if stanza and #chardata > 0 then
138 -- We have some character data in the buffer
139 stanza:text(t_concat(chardata));
143 if #stanza.last_add == 0 then
144 cb_handlestanza(session, stanza);
153 return init_xmlhandlers;