0f80742dc706d693dd4785e7738952a2b2a4dbd3
[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 local stanza_mt = st.stanza_mt;
13
14 local error = error;
15 local tostring = tostring;
16 local t_insert = table.insert;
17 local t_concat = table.concat;
18 local t_remove = table.remove;
19 local setmetatable = setmetatable;
20
21 -- COMPAT: w/LuaExpat 1.1.0
22 local lxp_supports_doctype = pcall(lxp.new, { StartDoctypeDecl = false });
23
24 module "xmppstream"
25
26 local new_parser = lxp.new;
27
28 local ns_prefixes = {
29         ["http://www.w3.org/XML/1998/namespace"] = "xml";
30 };
31
32 local xmlns_streams = "http://etherx.jabber.org/streams";
33
34 local ns_separator = "\1";
35 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
36
37 _M.ns_separator = ns_separator;
38 _M.ns_pattern = ns_pattern;
39
40 function new_sax_handlers(session, stream_callbacks)
41         local xml_handlers = {};
42         
43         local cb_streamopened = stream_callbacks.streamopened;
44         local cb_streamclosed = stream_callbacks.streamclosed;
45         local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
46         local cb_handlestanza = stream_callbacks.handlestanza;
47         
48         local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
49         local stream_tag = stream_callbacks.stream_tag or "stream";
50         if stream_ns ~= "" then
51                 stream_tag = stream_ns..ns_separator..stream_tag;
52         end
53         local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
54         
55         local stream_default_ns = stream_callbacks.default_ns;
56         
57         local stack = {};
58         local chardata, stanza = {};
59         local non_streamns_depth = 0;
60         function xml_handlers:StartElement(tagname, attr)
61                 if stanza and #chardata > 0 then
62                         -- We have some character data in the buffer
63                         t_insert(stanza, t_concat(chardata));
64                         chardata = {};
65                 end
66                 local curr_ns,name = tagname:match(ns_pattern);
67                 if name == "" then
68                         curr_ns, name = "", curr_ns;
69                 end
70
71                 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
72                         attr.xmlns = curr_ns;
73                         non_streamns_depth = non_streamns_depth + 1;
74                 end
75                 
76                 -- FIXME !!!!!
77                 for i=1,#attr do
78                         local k = attr[i];
79                         attr[i] = nil;
80                         local ns, nm = k:match(ns_pattern);
81                         if nm ~= "" then
82                                 ns = ns_prefixes[ns];
83                                 if ns then
84                                         attr[ns..":"..nm] = attr[k];
85                                         attr[k] = nil;
86                                 end
87                         end
88                 end
89                 
90                 if not stanza then --if we are not currently inside a stanza
91                         if session.notopen then
92                                 if tagname == stream_tag then
93                                         non_streamns_depth = 0;
94                                         if cb_streamopened then
95                                                 cb_streamopened(session, attr);
96                                         end
97                                 else
98                                         -- Garbage before stream?
99                                         cb_error(session, "no-stream");
100                                 end
101                                 return;
102                         end
103                         if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
104                                 cb_error(session, "invalid-top-level-element");
105                         end
106                         
107                         stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
108                 else -- we are inside a stanza, so add a tag
109                         t_insert(stack, stanza);
110                         local oldstanza = stanza;
111                         stanza = setmetatable({ name = name, attr = attr, tags = {} }, stanza_mt);
112                         t_insert(oldstanza, stanza);
113                         t_insert(oldstanza.tags, stanza);
114                 end
115         end
116         function xml_handlers:CharacterData(data)
117                 if stanza then
118                         t_insert(chardata, data);
119                 end
120         end
121         function xml_handlers:EndElement(tagname)
122                 if non_streamns_depth > 0 then
123                         non_streamns_depth = non_streamns_depth - 1;
124                 end
125                 if stanza then
126                         if #chardata > 0 then
127                                 -- We have some character data in the buffer
128                                 t_insert(stanza, t_concat(chardata));
129                                 chardata = {};
130                         end
131                         -- Complete stanza
132                         if #stack == 0 then
133                                 if tagname ~= stream_error_tag then
134                                         cb_handlestanza(session, stanza);
135                                 else
136                                         cb_error(session, "stream-error", stanza);
137                                 end
138                                 stanza = nil;
139                         else
140                                 stanza = t_remove(stack);
141                         end
142                 else
143                         if tagname == stream_tag then
144                                 if cb_streamclosed then
145                                         cb_streamclosed(session);
146                                 end
147                         else
148                                 local curr_ns,name = tagname:match(ns_pattern);
149                                 if name == "" then
150                                         curr_ns, name = "", curr_ns;
151                                 end
152                                 cb_error(session, "parse-error", "unexpected-element-close", name);
153                         end
154                         stanza, chardata = nil, {};
155                         stack = {};
156                 end
157         end
158
159         local function restricted_handler(parser)
160                 cb_error(session, "parse-error", "restricted-xml", "Restricted XML, see RFC 6120 section 11.1.");
161                 if not parser.stop or not parser:stop() then
162                         error("Failed to abort parsing");
163                 end
164         end
165         
166         if lxp_supports_doctype then
167                 xml_handlers.StartDoctypeDecl = restricted_handler;
168         end
169         xml_handlers.Comment = restricted_handler;
170         xml_handlers.ProcessingInstruction = restricted_handler;
171         
172         local function reset()
173                 stanza, chardata = nil, {};
174                 stack = {};
175         end
176         
177         local function set_session(stream, new_session)
178                 session = new_session;
179         end
180         
181         return xml_handlers, { reset = reset, set_session = set_session };
182 end
183
184 function new(session, stream_callbacks)
185         local handlers, meta = new_sax_handlers(session, stream_callbacks);
186         local parser = new_parser(handlers, ns_separator);
187         local parse = parser.parse;
188
189         return {
190                 reset = function ()
191                         parser = new_parser(handlers, ns_separator);
192                         parse = parser.parse;
193                         meta.reset();
194                 end,
195                 feed = function (self, data)
196                         return parse(parser, data);
197                 end,
198                 set_session = meta.set_session;
199         };
200 end
201
202 return _M;