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("^(.-)|?([^%|]-)$");
62 curr_ns, name = "", curr_ns;
65 if curr_ns ~= stream_default_ns then
70 for i, k in ipairs(attr) do
71 if type(k) == "string" then
72 local ns, nm = k:match("^([^|]+)|?([^|]-)$")
76 attr[ns..":"..nm] = attr[k];
77 attr[i] = ns..":"..nm;
84 if not stanza then --if we are not currently inside a stanza
85 if session.notopen then
86 if tagname == stream_tag then
87 if cb_streamopened then
88 cb_streamopened(session, attr);
91 -- Garbage before stream?
92 cb_error(session, "no-stream");
96 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
97 cb_error(session, "invalid-top-level-element");
100 stanza = st.stanza(name, attr);
102 else -- we are inside a stanza, so add a tag
104 if curr_ns ~= stream_default_ns then
105 attr.xmlns = curr_ns;
107 stanza:tag(name, attr);
110 function xml_handlers:CharacterData(data)
112 t_insert(chardata, data);
115 function xml_handlers:EndElement(tagname)
116 curr_ns,name = tagname:match("^(.-)|?([^%|]-)$");
118 curr_ns, name = "", curr_ns;
120 if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then
121 if tagname == stream_tag then
122 if cb_streamclosed then
123 cb_streamclosed(session);
126 elseif name == "error" then
127 cb_error(session, "stream-error", stanza);
129 cb_error(session, "parse-error", "unexpected-element-close", name);
133 if #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 cb_handlestanza(session, stanza);
150 return init_xmlhandlers;