Comparison

util/xmppstream.lua @ 4425:6f5ed0f4a3e6

util.xmppstream: A little cleanup.
author Waqas Hussain <waqas20@gmail.com>
date Tue, 22 Nov 2011 01:44:28 +0500
parent 4306:a101c59772e7
child 4426:ee65aa40ef60
comparison
equal deleted inserted replaced
4424:1cbf3744300c 4425:6f5ed0f4a3e6
9 9
10 local lxp = require "lxp"; 10 local lxp = require "lxp";
11 local st = require "util.stanza"; 11 local st = require "util.stanza";
12 local stanza_mt = st.stanza_mt; 12 local stanza_mt = st.stanza_mt;
13 13
14 local error = error;
14 local tostring = tostring; 15 local tostring = tostring;
15 local t_insert = table.insert; 16 local t_insert = table.insert;
16 local t_concat = table.concat; 17 local t_concat = table.concat;
17 local t_remove = table.remove; 18 local t_remove = table.remove;
18 local setmetatable = setmetatable; 19 local setmetatable = setmetatable;
19 20
20 local default_log = require "util.logger".init("xmppstream");
21
22 -- COMPAT: w/LuaExpat 1.1.0 21 -- COMPAT: w/LuaExpat 1.1.0
23 local lxp_supports_doctype = pcall(lxp.new, { StartDoctypeDecl = false }); 22 local lxp_supports_doctype = pcall(lxp.new, { StartDoctypeDecl = false });
24
25 if not lxp_supports_doctype then 23 if not lxp_supports_doctype then
24 local default_log = require "util.logger".init("xmppstream");
26 default_log("warn", "The version of LuaExpat on your system leaves Prosody " 25 default_log("warn", "The version of LuaExpat on your system leaves Prosody "
27 .."vulnerable to denial-of-service attacks. You should upgrade to " 26 .."vulnerable to denial-of-service attacks. You should upgrade to "
28 .."LuaExpat 1.1.1 or higher as soon as possible. See " 27 .."LuaExpat 1.1.1 or higher as soon as possible. See "
29 .."http://prosody.im/doc/depends#luaexpat for more information."); 28 .."http://prosody.im/doc/depends#luaexpat for more information.");
30 end 29 end
31 30
32 local error = error;
33
34 module "xmppstream" 31 module "xmppstream"
35 32
36 local new_parser = lxp.new; 33 local new_parser = lxp.new;
37 34
38 local ns_prefixes = { 35 local ns_prefixes = {
47 _M.ns_separator = ns_separator; 44 _M.ns_separator = ns_separator;
48 _M.ns_pattern = ns_pattern; 45 _M.ns_pattern = ns_pattern;
49 46
50 function new_sax_handlers(session, stream_callbacks) 47 function new_sax_handlers(session, stream_callbacks)
51 local xml_handlers = {}; 48 local xml_handlers = {};
52
53 local log = session.log or default_log;
54 49
55 local cb_streamopened = stream_callbacks.streamopened; 50 local cb_streamopened = stream_callbacks.streamopened;
56 local cb_streamclosed = stream_callbacks.streamclosed; 51 local cb_streamclosed = stream_callbacks.streamclosed;
57 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end; 52 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
58 local cb_handlestanza = stream_callbacks.handlestanza; 53 local cb_handlestanza = stream_callbacks.handlestanza;
186 stack = {}; 181 stack = {};
187 end 182 end
188 183
189 local function set_session(stream, new_session) 184 local function set_session(stream, new_session)
190 session = new_session; 185 session = new_session;
191 log = new_session.log or default_log;
192 end 186 end
193 187
194 return xml_handlers, { reset = reset, set_session = set_session }; 188 return xml_handlers, { reset = reset, set_session = set_session };
195 end 189 end
196 190