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;
55 --local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end
57 local send = session.send;
59 local cb_streamopened = stream_callbacks.streamopened;
60 local cb_streamclosed = stream_callbacks.streamclosed;
63 function xml_handlers:StartElement(name, attr)
64 if stanza and #chardata > 0 then
65 -- We have some character data in the buffer
66 stanza:text(t_concat(chardata));
69 curr_ns,name = name:match("^(.+)|([%w%-]+)$");
70 if curr_ns ~= "jabber:server" then
75 for i, k in ipairs(attr) do
76 if type(k) == "string" then
77 local ns, nm = k:match("^([^|]+)|?([^|]-)$")
81 attr[ns..":"..nm] = attr[k];
82 attr[i] = ns..":"..nm;
89 if not stanza then --if we are not currently inside a stanza
90 if session.notopen then
91 if name == "stream" then
92 if cb_streamopened then
93 cb_streamopened(session, attr);
97 error("Client failed to open stream successfully");
99 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
100 error("Client sent invalid top-level stanza");
103 stanza = st.stanza(name, attr);
105 else -- we are inside a stanza, so add a tag
107 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then
108 attr.xmlns = curr_ns;
110 stanza:tag(name, attr);
113 function xml_handlers:CharacterData(data)
115 t_insert(chardata, data);
118 function xml_handlers:EndElement(name)
119 curr_ns,name = name:match("^(.+)|([%w%-]+)$");
120 if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then
121 if name == "stream" then
122 log("debug", "Stream closed");
123 if cb_streamclosed then
124 cb_streamclosed(session);
127 elseif name == "error" then
128 error("Stream error: "..tostring(name)..": "..tostring(stanza));
130 error("XML parse error in client stream with element: "..name);
133 if stanza and #chardata > 0 then
134 -- We have some character data in the buffer
135 stanza:text(t_concat(chardata));
139 if #stanza.last_add == 0 then
140 session.stanza_dispatch(stanza);
149 return init_xmlhandlers;