Comparison

net/xmppclient_listener.lua @ 3034:b9ebd44a35f9

net.xmppclient_listener: Port to util.xmppstream \o/
author Matthew Wild <mwild1@gmail.com>
date Sun, 09 May 2010 20:48:21 +0100
parent 2925:692b3c6c5bd2
child 3136:9630cb97cd3e
comparison
equal deleted inserted replaced
3033:0194d7eab8a5 3034:b9ebd44a35f9
9 9
10 10
11 local logger = require "logger"; 11 local logger = require "logger";
12 local log = logger.init("xmppclient_listener"); 12 local log = logger.init("xmppclient_listener");
13 local lxp = require "lxp" 13 local lxp = require "lxp"
14 local init_xmlhandlers = require "core.xmlhandlers" 14 local new_xmpp_stream = require "util.xmppstream".new;
15 local sm_new_session = require "core.sessionmanager".new_session; 15 local sm_new_session = require "core.sessionmanager".new_session;
16 16
17 local connlisteners_register = require "net.connlisteners".register; 17 local connlisteners_register = require "net.connlisteners".register;
18 18
19 local t_insert = table.insert; 19 local t_insert = table.insert;
70 local sessions = {}; 70 local sessions = {};
71 local xmppclient = { default_port = 5222, default_mode = "*a" }; 71 local xmppclient = { default_port = 5222, default_mode = "*a" };
72 72
73 -- These are session methods -- 73 -- These are session methods --
74 74
75 local function session_reset_stream(session)
76 -- Reset stream
77 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "\1");
78 session.parser = parser;
79
80 session.notopen = true;
81
82 function session.data(conn, data)
83 local ok, err = parser:parse(data);
84 if ok then return; end
85 log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
86 session:close("xml-not-well-formed");
87 end
88
89 return true;
90 end
91
92 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; 75 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
93 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" }; 76 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
94 local function session_close(session, reason) 77 local function session_close(session, reason)
95 local log = session.log or log; 78 local log = session.log or log;
96 if session.conn then 79 if session.conn then
143 126
144 if opt_keepalives ~= nil then 127 if opt_keepalives ~= nil then
145 conn:setoption("keepalive", opt_keepalives); 128 conn:setoption("keepalive", opt_keepalives);
146 end 129 end
147 130
148 session.reset_stream = session_reset_stream;
149 session.close = session_close; 131 session.close = session_close;
150 132
151 session_reset_stream(session); -- Initialise, ready for use 133 local stream = new_xmpp_stream(session, stream_callbacks);
134 session.stream = stream;
135
136 session.notopen = true;
137
138 function session.reset_stream()
139 session.notopen = true;
140 session.stream:reset();
141 end
142
143 function session.data(data)
144 local ok, err = stream:feed(data);
145 if ok then return; end
146 log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
147 session:close("xml-not-well-formed");
148 end
152 149
153 session.dispatch_stanza = stream_callbacks.handlestanza; 150 session.dispatch_stanza = stream_callbacks.handlestanza;
154 end 151 end
155 if data then 152 if data then
156 session.data(conn, data); 153 session.data(data);
157 end 154 end
158 end 155 end
159 156
160 function xmppclient.ondisconnect(conn, err) 157 function xmppclient.ondisconnect(conn, err)
161 local session = sessions[conn]; 158 local session = sessions[conn];