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_ns..ns_separator..(stream_callbacks.stream_tag or "stream");
49 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
51 local stream_default_ns = stream_callbacks.default_ns;
53 local chardata, stanza = {};
54 local non_streamns_depth = 0;
55 function xml_handlers:StartElement(tagname, attr)
56 if stanza and #chardata > 0 then
57 -- We have some character data in the buffer
58 stanza:text(t_concat(chardata));
61 local curr_ns,name = tagname:match(ns_pattern);
63 curr_ns, name = "", curr_ns;
66 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
68 non_streamns_depth = non_streamns_depth + 1;
75 local ns, nm = k:match(ns_pattern);
79 attr[ns..":"..nm] = attr[k];
85 if not stanza then --if we are not currently inside a stanza
86 if session.notopen then
87 if tagname == stream_tag then
88 non_streamns_depth = 0;
89 if cb_streamopened then
90 cb_streamopened(session, attr);
93 -- Garbage before stream?
94 cb_error(session, "no-stream");
98 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
99 cb_error(session, "invalid-top-level-element");
102 stanza = st.stanza(name, attr);
103 else -- we are inside a stanza, so add a tag
104 stanza:tag(name, attr);
107 function xml_handlers:CharacterData(data)
109 t_insert(chardata, data);
112 function xml_handlers:EndElement(tagname)
113 if non_streamns_depth > 0 then
114 non_streamns_depth = non_streamns_depth - 1;
117 if #chardata > 0 then
118 -- We have some character data in the buffer
119 stanza:text(t_concat(chardata));
123 local last_add = stanza.last_add;
124 if not last_add or #last_add == 0 then
125 if tagname ~= stream_error_tag then
126 cb_handlestanza(session, stanza);
128 cb_error(session, "stream-error", stanza);
135 if tagname == stream_tag then
136 if cb_streamclosed then
137 cb_streamclosed(session);
140 local curr_ns,name = tagname:match(ns_pattern);
142 curr_ns, name = "", curr_ns;
144 cb_error(session, "parse-error", "unexpected-element-close", name);
146 stanza, chardata = nil, {};
150 local function reset()
151 stanza, chardata = nil, {};
154 local function set_session(stream, new_session)
155 session = new_session;
156 log = new_session.log or default_log;
159 return xml_handlers, { reset = reset, set_session = set_session };
162 function new(session, stream_callbacks)
163 local handlers, meta = new_sax_handlers(session, stream_callbacks);
164 local parser = new_parser(handlers, ns_separator);
165 local parse = parser.parse;
169 parser = new_parser(handlers, ns_separator);
170 parse = parser.parse;
173 feed = function (self, data)
174 return parse(parser, data);
176 set_session = meta.set_session;