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