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("xmppstream");
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 _M.ns_separator = ns_separator;
35 _M.ns_pattern = ns_pattern;
37 function new_sax_handlers(session, stream_callbacks)
38 local xml_handlers = {};
40 local log = session.log or default_log;
42 local cb_streamopened = stream_callbacks.streamopened;
43 local cb_streamclosed = stream_callbacks.streamclosed;
44 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
45 local cb_handlestanza = stream_callbacks.handlestanza;
47 local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
48 local stream_tag = stream_callbacks.stream_tag or "stream";
49 if stream_ns ~= "" then
50 stream_tag = stream_ns..ns_separator..stream_tag;
52 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
54 local stream_default_ns = stream_callbacks.default_ns;
56 local chardata, stanza = {};
57 local non_streamns_depth = 0;
58 function xml_handlers:StartElement(tagname, attr)
59 if stanza and #chardata > 0 then
60 -- We have some character data in the buffer
61 stanza:text(t_concat(chardata));
64 local curr_ns,name = tagname:match(ns_pattern);
66 curr_ns, name = "", curr_ns;
69 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
71 non_streamns_depth = non_streamns_depth + 1;
78 local ns, nm = k:match(ns_pattern);
82 attr[ns..":"..nm] = attr[k];
88 if not stanza then --if we are not currently inside a stanza
89 if session.notopen then
90 if tagname == stream_tag then
91 non_streamns_depth = 0;
92 if cb_streamopened then
93 cb_streamopened(session, attr);
96 -- Garbage before stream?
97 cb_error(session, "no-stream");
101 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
102 cb_error(session, "invalid-top-level-element");
105 stanza = st.stanza(name, attr);
106 else -- we are inside a stanza, so add a tag
107 stanza:tag(name, attr);
110 function xml_handlers:CharacterData(data)
112 t_insert(chardata, data);
115 function xml_handlers:EndElement(tagname)
116 if non_streamns_depth > 0 then
117 non_streamns_depth = non_streamns_depth - 1;
120 if #chardata > 0 then
121 -- We have some character data in the buffer
122 stanza:text(t_concat(chardata));
126 local last_add = stanza.last_add;
127 if not last_add or #last_add == 0 then
128 if tagname ~= stream_error_tag then
129 cb_handlestanza(session, stanza);
131 cb_error(session, "stream-error", stanza);
138 if tagname == stream_tag then
139 if cb_streamclosed then
140 cb_streamclosed(session);
143 local curr_ns,name = tagname:match(ns_pattern);
145 curr_ns, name = "", curr_ns;
147 cb_error(session, "parse-error", "unexpected-element-close", name);
149 stanza, chardata = nil, {};
153 local function reset()
154 stanza, chardata = nil, {};
157 local function set_session(stream, new_session)
158 session = new_session;
159 log = new_session.log or default_log;
162 return xml_handlers, { reset = reset, set_session = set_session };
165 function new(session, stream_callbacks)
166 local handlers, meta = new_sax_handlers(session, stream_callbacks);
167 local parser = new_parser(handlers, ns_separator);
168 local parse = parser.parse;
172 parser = new_parser(handlers, ns_separator);
173 parse = parser.parse;
176 feed = function (self, data)
177 return parse(parser, data);
179 set_session = meta.set_session;