util.datetime: Fixes for more liberal timezone parsing - colon and minutes are both...
[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("xmlhandlers");
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 function new_sax_handlers(session, stream_callbacks)
35         local xml_handlers = {};
36         
37         local log = session.log or default_log;
38         
39         local cb_streamopened = stream_callbacks.streamopened;
40         local cb_streamclosed = stream_callbacks.streamclosed;
41         local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
42         local cb_handlestanza = stream_callbacks.handlestanza;
43         
44         local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
45         local stream_tag = stream_ns..ns_separator..(stream_callbacks.stream_tag or "stream");
46         local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
47         
48         local stream_default_ns = stream_callbacks.default_ns;
49         
50         local chardata, stanza = {};
51         local non_streamns_depth = 0;
52         function xml_handlers:StartElement(tagname, attr)
53                 if stanza and #chardata > 0 then
54                         -- We have some character data in the buffer
55                         stanza:text(t_concat(chardata));
56                         chardata = {};
57                 end
58                 local curr_ns,name = tagname:match(ns_pattern);
59                 if name == "" then
60                         curr_ns, name = "", curr_ns;
61                 end
62
63                 if curr_ns ~= stream_default_ns or non_streamns_depth > 0 then
64                         attr.xmlns = curr_ns;
65                         non_streamns_depth = non_streamns_depth + 1;
66                 end
67                 
68                 -- FIXME !!!!!
69                 for i=1,#attr do
70                         local k = attr[i];
71                         attr[i] = nil;
72                         local ns, nm = k:match(ns_pattern);
73                         if nm ~= "" then
74                                 ns = ns_prefixes[ns];
75                                 if ns then
76                                         attr[ns..":"..nm] = attr[k];
77                                         attr[k] = nil;
78                                 end
79                         end
80                 end
81                 
82                 if not stanza then --if we are not currently inside a stanza
83                         if session.notopen then
84                                 if tagname == stream_tag then
85                                         non_streamns_depth = 0;
86                                         if cb_streamopened then
87                                                 cb_streamopened(session, attr);
88                                         end
89                                 else
90                                         -- Garbage before stream?
91                                         cb_error(session, "no-stream");
92                                 end
93                                 return;
94                         end
95                         if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
96                                 cb_error(session, "invalid-top-level-element");
97                         end
98                         
99                         stanza = st.stanza(name, attr);
100                 else -- we are inside a stanza, so add a tag
101                         stanza:tag(name, attr);
102                 end
103         end
104         function xml_handlers:CharacterData(data)
105                 if stanza then
106                         t_insert(chardata, data);
107                 end
108         end
109         function xml_handlers:EndElement(tagname)
110                 if non_streamns_depth > 0 then
111                         non_streamns_depth = non_streamns_depth - 1;
112                 end
113                 if stanza then
114                         if #chardata > 0 then
115                                 -- We have some character data in the buffer
116                                 stanza:text(t_concat(chardata));
117                                 chardata = {};
118                         end
119                         -- Complete stanza
120                         local last_add = stanza.last_add;
121                         if not last_add or #last_add == 0 then
122                                 if tagname ~= stream_error_tag then
123                                         cb_handlestanza(session, stanza);
124                                 else
125                                         cb_error(session, "stream-error", stanza);
126                                 end
127                                 stanza = nil;
128                         else
129                                 stanza:up();
130                         end
131                 else
132                         if tagname == stream_tag then
133                                 if cb_streamclosed then
134                                         cb_streamclosed(session);
135                                 end
136                         else
137                                 local curr_ns,name = tagname:match(ns_pattern);
138                                 if name == "" then
139                                         curr_ns, name = "", curr_ns;
140                                 end
141                                 cb_error(session, "parse-error", "unexpected-element-close", name);
142                         end
143                         stanza, chardata = nil, {};
144                 end
145         end
146         
147         local function reset()
148                 stanza, chardata = nil, {};
149         end
150         
151         local function set_session(stream, new_session)
152                 session = new_session;
153                 log = new_session.log or default_log;
154         end
155         
156         return xml_handlers, { reset = reset, set_session = set_session };
157 end
158
159 function new(session, stream_callbacks)
160         local handlers, meta = new_sax_handlers(session, stream_callbacks);
161         local parser = new_parser(handlers, ns_separator);
162         local parse = parser.parse;
163
164         return {
165                 reset = function ()
166                         parser = new_parser(handlers, ns_separator);
167                         parse = parser.parse;
168                         meta.reset();
169                 end,
170                 feed = function (self, data)
171                         return parse(parser, data);
172                 end,
173                 set_session = meta.set_session;
174         };
175 end
176
177 return _M;