2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local lxp = require "lxp";
11 local st = require "util.stanza";
13 local tostring = tostring;
14 local t_insert = table.insert;
15 local t_concat = table.concat;
17 local default_log = require "util.logger".init("xmlhandlers");
23 local new_parser = lxp.new;
26 ["http://www.w3.org/XML/1998/namespace"] = "xml";
29 local xmlns_streams = "http://etherx.jabber.org/streams";
31 local ns_separator = "\1";
32 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
34 function new_sax_handlers(session, stream_callbacks)
35 local xml_handlers = {};
37 local log = session.log or default_log;
39 local cb_streamopened = stream_callbacks.streamopened;
40 local cb_streamclosed = stream_callbacks.streamclosed;
41 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
42 local cb_handlestanza = stream_callbacks.handlestanza;
44 local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
45 local stream_tag = stream_ns..ns_separator..(stream_callbacks.stream_tag or "stream");
46 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
48 local stream_default_ns = stream_callbacks.default_ns;
50 local chardata, stanza = {};
51 function xml_handlers:StartElement(tagname, attr)
52 if stanza and #chardata > 0 then
53 -- We have some character data in the buffer
54 stanza:text(t_concat(chardata));
57 local curr_ns,name = tagname:match(ns_pattern);
59 curr_ns, name = "", curr_ns;
62 if curr_ns ~= stream_default_ns then
70 local ns, nm = k:match(ns_pattern);
74 attr[ns..":"..nm] = attr[k];
80 if not stanza then --if we are not currently inside a stanza
81 if session.notopen then
82 if tagname == stream_tag then
83 if cb_streamopened then
84 cb_streamopened(session, attr);
87 -- Garbage before stream?
88 cb_error(session, "no-stream");
92 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
93 cb_error(session, "invalid-top-level-element");
96 stanza = st.stanza(name, attr);
97 else -- we are inside a stanza, so add a tag
98 stanza:tag(name, attr);
101 function xml_handlers:CharacterData(data)
103 t_insert(chardata, data);
106 function xml_handlers:EndElement(tagname)
108 if #chardata > 0 then
109 -- We have some character data in the buffer
110 stanza:text(t_concat(chardata));
114 if #stanza.last_add == 0 then
115 if tagname ~= stream_error_tag then
116 cb_handlestanza(session, stanza);
118 cb_error(session, "stream-error", stanza);
125 if tagname == stream_tag then
126 if cb_streamclosed then
127 cb_streamclosed(session);
130 local curr_ns,name = tagname:match(ns_pattern);
132 curr_ns, name = "", curr_ns;
134 cb_error(session, "parse-error", "unexpected-element-close", name);
136 stanza, chardata = nil, {};
140 local function reset()
141 stanza, chardata = nil, {};
144 local function set_session(stream, new_session)
145 session = new_session;
146 log = new_session.log or default_log;
149 return xml_handlers, { reset = reset, set_session = set_session };
152 function new(session, stream_callbacks)
153 local handlers, meta = new_sax_handlers(session, stream_callbacks);
154 local parser = new_parser(handlers, ns_separator);
155 local parse = parser.parse;
159 parser = new_parser(handlers, ns_separator);
160 parse = parser.parse;
163 feed = function (self, data)
164 return parse(parser, data);
166 set_session = meta.set_session;