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";
12 local stanza_mt = st.stanza_mt;
14 local tostring = tostring;
15 local t_insert = table.insert;
16 local t_concat = table.concat;
17 local t_remove = table.remove;
18 local setmetatable = setmetatable;
20 local default_log = require "util.logger".init("xmppstream");
26 local new_parser = lxp.new;
29 ["http://www.w3.org/XML/1998/namespace"] = "xml";
32 local xmlns_streams = "http://etherx.jabber.org/streams";
34 local ns_separator = "\1";
35 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
37 _M.ns_separator = ns_separator;
38 _M.ns_pattern = ns_pattern;
40 function new_sax_handlers(session, stream_callbacks)
41 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_ns = stream_callbacks.stream_ns or xmlns_streams;
51 local stream_tag = stream_callbacks.stream_tag or "stream";
52 if stream_ns ~= "" then
53 stream_tag = stream_ns..ns_separator..stream_tag;
55 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
57 local stream_default_ns = stream_callbacks.default_ns;
60 local chardata, stanza = {};
61 local non_streamns_depth = 0;
62 function xml_handlers:StartElement(tagname, attr)
63 if stanza and #chardata > 0 then
64 -- We have some character data in the buffer
65 t_insert(stanza, t_concat(chardata));
68 local curr_ns,name = tagname:match(ns_pattern);
70 curr_ns, name = "", curr_ns;
73 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
75 non_streamns_depth = non_streamns_depth + 1;
82 local ns, nm = k:match(ns_pattern);
86 attr[ns..":"..nm] = attr[k];
92 if not stanza then --if we are not currently inside a stanza
93 if session.notopen then
94 if tagname == stream_tag then
95 non_streamns_depth = 0;
96 if cb_streamopened then
97 cb_streamopened(session, attr);
100 -- Garbage before stream?
101 cb_error(session, "no-stream");
105 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
106 cb_error(session, "invalid-top-level-element");
109 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
110 else -- we are inside a stanza, so add a tag
111 t_insert(stack, stanza);
112 local oldstanza = stanza;
113 stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
114 t_insert(oldstanza, stanza);
115 t_insert(oldstanza.tags, stanza);
118 function xml_handlers:CharacterData(data)
120 t_insert(chardata, data);
123 function xml_handlers:EndElement(tagname)
124 if non_streamns_depth > 0 then
125 non_streamns_depth = non_streamns_depth - 1;
128 if #chardata > 0 then
129 -- We have some character data in the buffer
130 t_insert(stanza, t_concat(chardata));
135 if tagname ~= stream_error_tag then
136 cb_handlestanza(session, stanza);
138 cb_error(session, "stream-error", stanza);
142 stanza = t_remove(stack);
145 if tagname == stream_tag then
146 if cb_streamclosed then
147 cb_streamclosed(session);
150 local curr_ns,name = tagname:match(ns_pattern);
152 curr_ns, name = "", curr_ns;
154 cb_error(session, "parse-error", "unexpected-element-close", name);
156 stanza, chardata = nil, {};
161 local function reset()
162 stanza, chardata = nil, {};
166 local function set_session(stream, new_session)
167 session = new_session;
168 log = new_session.log or default_log;
171 return xml_handlers, { reset = reset, set_session = set_session };
174 function new(session, stream_callbacks)
175 local handlers, meta = new_sax_handlers(session, stream_callbacks);
176 local parser = new_parser(handlers, ns_separator);
177 local parse = parser.parse;
181 parser = new_parser(handlers, ns_separator);
182 parse = parser.parse;
185 feed = function (self, data)
186 return parse(parser, data);
188 set_session = meta.set_session;