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 = { "" };
50 local curr_ns, name = "";
53 local xml_handlers = {};
54 local log = session.log or default_log;
56 local cb_streamopened = stream_callbacks.streamopened;
57 local cb_streamclosed = stream_callbacks.streamclosed;
58 local cb_error = stream_callbacks.error or function (session, e) error("XML stream error: "..tostring(e)); end;
59 local cb_handlestanza = stream_callbacks.handlestanza;
61 local stream_tag = stream_callbacks.stream_tag;
64 function xml_handlers:StartElement(tagname, attr)
65 if stanza and #chardata > 0 then
66 -- We have some character data in the buffer
67 stanza:text(t_concat(chardata));
70 local curr_ns,name = tagname:match("^(.+)|([%w%-]+)$");
71 if curr_ns ~= "jabber:server" then
76 for i, k in ipairs(attr) do
77 if type(k) == "string" then
78 local ns, nm = k:match("^([^|]+)|?([^|]-)$")
82 attr[ns..":"..nm] = attr[k];
83 attr[i] = ns..":"..nm;
90 if not stanza then --if we are not currently inside a stanza
91 if session.notopen then
92 if tagname == stream_tag then
93 if cb_streamopened then
94 cb_streamopened(session, attr);
97 -- Garbage before stream?
98 cb_error(session, "no-stream");
102 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
103 cb_error(session, "invalid-top-level-element");
106 stanza = st.stanza(name, attr);
108 else -- we are inside a stanza, so add a tag
110 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then
111 attr.xmlns = curr_ns;
113 stanza:tag(name, attr);
116 function xml_handlers:CharacterData(data)
118 t_insert(chardata, data);
121 function xml_handlers:EndElement(tagname)
122 curr_ns,name = tagname:match("^(.+)|([%w%-]+)$");
123 if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then
124 if tagname == stream_tag then
125 if cb_streamclosed then
126 cb_streamclosed(session);
129 elseif name == "error" then
130 cb_error(session, "stream-error", stanza);
132 cb_error(session, "parse-error", "unexpected-element-close", name);
135 if stanza and #chardata > 0 then
136 -- We have some character data in the buffer
137 stanza:text(t_concat(chardata));
141 if #stanza.last_add == 0 then
142 cb_handlestanza(session, stanza);
151 return init_xmlhandlers;