util.xmppstream: Allow stream_ns = "" for parsing streams with no xmlns
[prosody.git] / util / xmppstream.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local lxp = require "lxp";
11 local st = require "util.stanza";
12
13 local tostring = tostring;
14 local t_insert = table.insert;
15 local t_concat = table.concat;
16
17 local default_log = require "util.logger".init("xmppstream");
18
19 local error = error;
20
21 module "xmppstream"
22
23 local new_parser = lxp.new;
24
25 local ns_prefixes = {
26         ["http://www.w3.org/XML/1998/namespace"] = "xml";
27 };
28
29 local xmlns_streams = "http://etherx.jabber.org/streams";
30
31 local ns_separator = "\1";
32 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
33
34 _M.ns_separator = ns_separator;
35 _M.ns_pattern = ns_pattern;
36
37 function new_sax_handlers(session, stream_callbacks)
38         local xml_handlers = {};
39         
40         local log = session.log or default_log;
41         
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;
46         
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;
51         end
52         local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
53         
54         local stream_default_ns = stream_callbacks.default_ns;
55         
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));
62                         chardata = {};
63                 end
64                 local curr_ns,name = tagname:match(ns_pattern);
65                 if name == "" then
66                         curr_ns, name = "", curr_ns;
67                 end
68
69                 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
70                         attr.xmlns = curr_ns;
71                         non_streamns_depth = non_streamns_depth + 1;
72                 end
73                 
74                 -- FIXME !!!!!
75                 for i=1,#attr do
76                         local k = attr[i];
77                         attr[i] = nil;
78                         local ns, nm = k:match(ns_pattern);
79                         if nm ~= "" then
80                                 ns = ns_prefixes[ns];
81                                 if ns then
82                                         attr[ns..":"..nm] = attr[k];
83                                         attr[k] = nil;
84                                 end
85                         end
86                 end
87                 
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);
94                                         end
95                                 else
96                                         -- Garbage before stream?
97                                         cb_error(session, "no-stream");
98                                 end
99                                 return;
100                         end
101                         if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
102                                 cb_error(session, "invalid-top-level-element");
103                         end
104                         
105                         stanza = st.stanza(name, attr);
106                 else -- we are inside a stanza, so add a tag
107                         stanza:tag(name, attr);
108                 end
109         end
110         function xml_handlers:CharacterData(data)
111                 if stanza then
112                         t_insert(chardata, data);
113                 end
114         end
115         function xml_handlers:EndElement(tagname)
116                 if non_streamns_depth > 0 then
117                         non_streamns_depth = non_streamns_depth - 1;
118                 end
119                 if stanza then
120                         if #chardata > 0 then
121                                 -- We have some character data in the buffer
122                                 stanza:text(t_concat(chardata));
123                                 chardata = {};
124                         end
125                         -- Complete stanza
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);
130                                 else
131                                         cb_error(session, "stream-error", stanza);
132                                 end
133                                 stanza = nil;
134                         else
135                                 stanza:up();
136                         end
137                 else
138                         if tagname == stream_tag then
139                                 if cb_streamclosed then
140                                         cb_streamclosed(session);
141                                 end
142                         else
143                                 local curr_ns,name = tagname:match(ns_pattern);
144                                 if name == "" then
145                                         curr_ns, name = "", curr_ns;
146                                 end
147                                 cb_error(session, "parse-error", "unexpected-element-close", name);
148                         end
149                         stanza, chardata = nil, {};
150                 end
151         end
152         
153         local function reset()
154                 stanza, chardata = nil, {};
155         end
156         
157         local function set_session(stream, new_session)
158                 session = new_session;
159                 log = new_session.log or default_log;
160         end
161         
162         return xml_handlers, { reset = reset, set_session = set_session };
163 end
164
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;
169
170         return {
171                 reset = function ()
172                         parser = new_parser(handlers, ns_separator);
173                         parse = parser.parse;
174                         meta.reset();
175                 end,
176                 feed = function (self, data)
177                         return parse(parser, data);
178                 end,
179                 set_session = meta.set_session;
180         };
181 end
182
183 return _M;