util.stanza: Iterate on childtags instead of all childs.
[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_ns..ns_separator..(stream_callbacks.stream_tag or "stream");
49         local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
50         
51         local stream_default_ns = stream_callbacks.default_ns;
52         
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));
59                         chardata = {};
60                 end
61                 local curr_ns,name = tagname:match(ns_pattern);
62                 if name == "" then
63                         curr_ns, name = "", curr_ns;
64                 end
65
66                 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
67                         attr.xmlns = curr_ns;
68                         non_streamns_depth = non_streamns_depth + 1;
69                 end
70                 
71                 -- FIXME !!!!!
72                 for i=1,#attr do
73                         local k = attr[i];
74                         attr[i] = nil;
75                         local ns, nm = k:match(ns_pattern);
76                         if nm ~= "" then
77                                 ns = ns_prefixes[ns];
78                                 if ns then
79                                         attr[ns..":"..nm] = attr[k];
80                                         attr[k] = nil;
81                                 end
82                         end
83                 end
84                 
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);
91                                         end
92                                 else
93                                         -- Garbage before stream?
94                                         cb_error(session, "no-stream");
95                                 end
96                                 return;
97                         end
98                         if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
99                                 cb_error(session, "invalid-top-level-element");
100                         end
101                         
102                         stanza = st.stanza(name, attr);
103                 else -- we are inside a stanza, so add a tag
104                         stanza:tag(name, attr);
105                 end
106         end
107         function xml_handlers:CharacterData(data)
108                 if stanza then
109                         t_insert(chardata, data);
110                 end
111         end
112         function xml_handlers:EndElement(tagname)
113                 if non_streamns_depth > 0 then
114                         non_streamns_depth = non_streamns_depth - 1;
115                 end
116                 if stanza then
117                         if #chardata > 0 then
118                                 -- We have some character data in the buffer
119                                 stanza:text(t_concat(chardata));
120                                 chardata = {};
121                         end
122                         -- Complete stanza
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);
127                                 else
128                                         cb_error(session, "stream-error", stanza);
129                                 end
130                                 stanza = nil;
131                         else
132                                 stanza:up();
133                         end
134                 else
135                         if tagname == stream_tag then
136                                 if cb_streamclosed then
137                                         cb_streamclosed(session);
138                                 end
139                         else
140                                 local curr_ns,name = tagname:match(ns_pattern);
141                                 if name == "" then
142                                         curr_ns, name = "", curr_ns;
143                                 end
144                                 cb_error(session, "parse-error", "unexpected-element-close", name);
145                         end
146                         stanza, chardata = nil, {};
147                 end
148         end
149         
150         local function reset()
151                 stanza, chardata = nil, {};
152         end
153         
154         local function set_session(stream, new_session)
155                 session = new_session;
156                 log = new_session.log or default_log;
157         end
158         
159         return xml_handlers, { reset = reset, set_session = set_session };
160 end
161
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;
166
167         return {
168                 reset = function ()
169                         parser = new_parser(handlers, ns_separator);
170                         parse = parser.parse;
171                         meta.reset();
172                 end,
173                 feed = function (self, data)
174                         return parse(parser, data);
175                 end,
176                 set_session = meta.set_session;
177         };
178 end
179
180 return _M;