net/xmppclient_listener.lua

Wed, 05 Jan 2011 03:05:13 +0000

author
daurnimator <quae@daurnimator.com>
date
Wed, 05 Jan 2011 03:05:13 +0000
changeset 3998
13a5a8df7c34
parent 3756
db7183f07114
permissions
-rw-r--r--

stanza_router: Replace xmlns == nil checks with 'not xmlns'

1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1490
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2816
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2816
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 625
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 625
diff changeset
6 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
7 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
9
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local logger = require "logger";
1040
4c79b28bce64 xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents: 901
diff changeset
12 local log = logger.init("xmppclient_listener");
3034
b9ebd44a35f9 net.xmppclient_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
13 local new_xmpp_stream = require "util.xmppstream".new;
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local connlisteners_register = require "net.connlisteners".register;
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
1040
4c79b28bce64 xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents: 901
diff changeset
17 local sessionmanager = require "core.sessionmanager";
4c79b28bce64 xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents: 901
diff changeset
18 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session;
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local sm_streamopened = sessionmanager.streamopened;
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
20 local sm_streamclosed = sessionmanager.streamclosed;
1040
4c79b28bce64 xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents: 901
diff changeset
21 local st = require "util.stanza";
3756
db7183f07114 net.xmppclient_listener: Imports and global cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
22 local xpcall = xpcall;
db7183f07114 net.xmppclient_listener: Imports and global cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
23 local tostring = tostring;
db7183f07114 net.xmppclient_listener: Imports and global cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
24 local type = type;
db7183f07114 net.xmppclient_listener: Imports and global cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
25 local traceback = debug.traceback;
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
2454
e4818c49192d xmppclient_listener: Allow the tcp_keepalives option in the config to control whether keepalives are used
Matthew Wild <mwild1@gmail.com>
parents: 2170
diff changeset
27 local config = require "core.configmanager";
e4818c49192d xmppclient_listener: Allow the tcp_keepalives option in the config to control whether keepalives are used
Matthew Wild <mwild1@gmail.com>
parents: 2170
diff changeset
28 local opt_keepalives = config.get("*", "core", "tcp_keepalives");
e4818c49192d xmppclient_listener: Allow the tcp_keepalives option in the config to control whether keepalives are used
Matthew Wild <mwild1@gmail.com>
parents: 2170
diff changeset
29
2466
0e44b6035210 net.xmpp{client,server,component}: Update for new xmlhandlers syntax
Matthew Wild <mwild1@gmail.com>
parents: 2454
diff changeset
30 local stream_callbacks = { default_ns = "jabber:client",
901
0e8934d3c4cb net.xmppclient_listener: Set default namespace to jabber:client
Matthew Wild <mwild1@gmail.com>
parents: 894
diff changeset
31 streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza };
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
32
2753
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
33 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
34
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
35 function stream_callbacks.error(session, error, data)
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
36 if error == "no-stream" then
837
86d14e7ef60c net/xmppclient_listener: Add some logging and handle unestablished sessions error'ing
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
37 session.log("debug", "Invalid opening stream header");
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
38 session:close("invalid-namespace");
2753
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
39 elseif error == "parse-error" then
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
40 (session.log or log)("debug", "Client XML parse error: %s", tostring(data));
3634
0d6b57edb7bd net.xmpp{client,server,component}_listener: s/xml-not-well-formed/not-well-formed/ as per latest bis drafts.
Waqas Hussain <waqas20@gmail.com>
parents: 3423
diff changeset
41 session:close("not-well-formed");
2753
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
42 elseif error == "stream-error" then
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
43 local condition, text = "undefined-condition";
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
44 for child in data:children() do
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
45 if child.attr.xmlns == xmlns_xmpp_streams then
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
46 if child.name ~= "text" then
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
47 condition = child.name;
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
48 else
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
49 text = child:get_text();
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
50 end
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
51 if condition ~= "undefined-condition" and text then
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
52 break;
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
53 end
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
54 end
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
55 end
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
56 text = condition .. (text and (" ("..text..")") or "");
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
57 session.log("info", "Session closed by remote with error: %s", text);
305428b14f76 net.xmppclient_listener: Fix to correctly handle stream errors from clients
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
58 session:close(nil, text);
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
59 end
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
60 end
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
61
3756
db7183f07114 net.xmppclient_listener: Imports and global cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
62 local function handleerr(err) log("error", "Traceback[c2s]: %s: %s", tostring(err), traceback()); end
3332
c941d1191709 xmpp{client,server}_listener: Put stanzas/in filtering code in the correct place to make it actually work :)
Matthew Wild <mwild1@gmail.com>
parents: 3144
diff changeset
63 function stream_callbacks.handlestanza(session, stanza)
c941d1191709 xmpp{client,server}_listener: Put stanzas/in filtering code in the correct place to make it actually work :)
Matthew Wild <mwild1@gmail.com>
parents: 3144
diff changeset
64 stanza = session.filter("stanzas/in", stanza);
c941d1191709 xmpp{client,server}_listener: Put stanzas/in filtering code in the correct place to make it actually work :)
Matthew Wild <mwild1@gmail.com>
parents: 3144
diff changeset
65 if stanza then
3345
8520cd88b84c xmpp{client,server}_listener: Convert some calls to tail calls for efficiency
Matthew Wild <mwild1@gmail.com>
parents: 3332
diff changeset
66 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
3332
c941d1191709 xmpp{client,server}_listener: Put stanzas/in filtering code in the correct place to make it actually work :)
Matthew Wild <mwild1@gmail.com>
parents: 3144
diff changeset
67 end
559
fa4a51fe6442 Remove an incorrect line which I didn't add, and fix the proper way. Corrects the sending of stanzas over unauthed s2sout's. Also fixes mod_dialback to send stanzas and not strings.
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
68 end
fa4a51fe6442 Remove an incorrect line which I didn't add, and fix the proper way. Corrects the sending of stanzas over unauthed s2sout's. Also fixes mod_dialback to send stanzas and not strings.
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
69
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 local sessions = {};
451
e9f269e5204e No more reading 1 byte at a time from sockets
Matthew Wild <mwild1@gmail.com>
parents: 333
diff changeset
71 local xmppclient = { default_port = 5222, default_mode = "*a" };
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 -- These are session methods --
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
329
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
75 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
2466
0e44b6035210 net.xmpp{client,server,component}: Update for new xmlhandlers syntax
Matthew Wild <mwild1@gmail.com>
parents: 2454
diff changeset
76 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
333
8d15b073fdbe session:disconnect() -> session:close() for consistency with other Lua APIs
Matthew Wild <mwild1@gmail.com>
parents: 331
diff changeset
77 local function session_close(session, reason)
329
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
78 local log = session.log or log;
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
79 if session.conn then
1617
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
80 if session.notopen then
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
81 session.send("<?xml version='1.0'?>");
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
82 session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
83 end
329
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
84 if reason then
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
85 if type(reason) == "string" then -- assume stream error
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
86 log("info", "Disconnecting client, <stream:error> is: %s", reason);
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
87 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
88 elseif type(reason) == "table" then
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
89 if reason.condition then
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
90 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
91 if reason.text then
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
92 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
93 end
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
94 if reason.extra then
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
95 stanza:add_child(reason.extra);
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
96 end
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
97 log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
98 session.send(stanza);
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
99 elseif reason.name then -- a stanza
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
100 log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
101 session.send(reason);
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
102 end
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
103 end
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
104 end
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
105 session.send("</stream:stream>");
2104
90751c0ca690 xmppclient_listener: Use : syntax for connection methods, to be compatible with new server.lua
Matthew Wild <mwild1@gmail.com>
parents: 2103
diff changeset
106 session.conn:close();
2126
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 2104
diff changeset
107 xmppclient.ondisconnect(session.conn, (reason and (reason.text or reason.condition)) or reason or "session closed");
329
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
108 end
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
109 end
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
110
3be63719428e Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm)
Matthew Wild <mwild1@gmail.com>
parents: 318
diff changeset
111
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 -- End of session methods --
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113
3136
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
114 function xmppclient.onconnect(conn)
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
115 local session = sm_new_session(conn);
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
116 sessions[conn] = session;
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
117
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
118 session.log("info", "Client connected");
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
119
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
120 -- Client is using legacy SSL (otherwise mod_tls sets this flag)
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
121 if conn:ssl() then
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
122 session.secure = true;
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
123 end
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
124
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
125 if opt_keepalives ~= nil then
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
126 conn:setoption("keepalive", opt_keepalives);
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
127 end
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
128
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
129 session.close = session_close;
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
130
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
131 local stream = new_xmpp_stream(session, stream_callbacks);
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
132 session.stream = stream;
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
133
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
134 session.notopen = true;
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
135
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
136 function session.reset_stream()
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
137 session.notopen = true;
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
138 session.stream:reset();
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
139 end
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
140
3144
9e16012c2155 xmppclient_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3136
diff changeset
141 local filter = session.filter;
3136
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
142 function session.data(data)
3144
9e16012c2155 xmppclient_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3136
diff changeset
143 data = filter("bytes/in", data);
9e16012c2155 xmppclient_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3136
diff changeset
144 if data then
9e16012c2155 xmppclient_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3136
diff changeset
145 local ok, err = stream:feed(data);
9e16012c2155 xmppclient_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3136
diff changeset
146 if ok then return; end
9e16012c2155 xmppclient_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3136
diff changeset
147 log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
3634
0d6b57edb7bd net.xmpp{client,server,component}_listener: s/xml-not-well-formed/not-well-formed/ as per latest bis drafts.
Waqas Hussain <waqas20@gmail.com>
parents: 3423
diff changeset
148 session:close("not-well-formed");
3144
9e16012c2155 xmppclient_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3136
diff changeset
149 end
3136
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
150 end
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
151
3144
9e16012c2155 xmppclient_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3136
diff changeset
152 local handlestanza = stream_callbacks.handlestanza;
9e16012c2155 xmppclient_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3136
diff changeset
153 function session.dispatch_stanza(session, stanza)
3332
c941d1191709 xmpp{client,server}_listener: Put stanzas/in filtering code in the correct place to make it actually work :)
Matthew Wild <mwild1@gmail.com>
parents: 3144
diff changeset
154 return handlestanza(session, stanza);
3144
9e16012c2155 xmppclient_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3136
diff changeset
155 end
3136
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
156 end
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
157
2103
0737b167a997 xmppclient_listener: Switch to .onincoming listener method, to be compatible with new server_event.lua
Matthew Wild <mwild1@gmail.com>
parents: 2077
diff changeset
158 function xmppclient.onincoming(conn, data)
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 local session = sessions[conn];
3136
9630cb97cd3e xmppclient_listener: Port to onconnect handler
Matthew Wild <mwild1@gmail.com>
parents: 3034
diff changeset
160 if session then
3034
b9ebd44a35f9 net.xmppclient_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
161 session.data(data);
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 end
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164
2126
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 2104
diff changeset
165 function xmppclient.ondisconnect(conn, err)
123
ebd65feb188c Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
166 local session = sessions[conn];
ebd65feb188c Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
167 if session then
318
cc20ea4a8697 Fix logging in some cases for client disconnects
Matthew Wild <mwild1@gmail.com>
parents: 275
diff changeset
168 (session.log or log)("info", "Client disconnected: %s", err);
1422
db9cb1f24f38 xmppclient_listener: Pass session close reason to destroy_session (to be used in unavailable presence)
Matthew Wild <mwild1@gmail.com>
parents: 1255
diff changeset
169 sm_destroy_session(session, err);
123
ebd65feb188c Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
170 sessions[conn] = nil;
ebd65feb188c Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
171 session = nil;
ebd65feb188c Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
172 end
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 end
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174
3423
929fb7b65d2c net.xmppclient_listener: Add associate_session(conn, session) to change the session a connection is associated with
Matthew Wild <mwild1@gmail.com>
parents: 3345
diff changeset
175 function xmppclient.associate_session(conn, session)
929fb7b65d2c net.xmppclient_listener: Add associate_session(conn, session) to change the session a connection is associated with
Matthew Wild <mwild1@gmail.com>
parents: 3345
diff changeset
176 sessions[conn] = session;
929fb7b65d2c net.xmppclient_listener: Add associate_session(conn, session) to change the session a connection is associated with
Matthew Wild <mwild1@gmail.com>
parents: 3345
diff changeset
177 end
929fb7b65d2c net.xmppclient_listener: Add associate_session(conn, session) to change the session a connection is associated with
Matthew Wild <mwild1@gmail.com>
parents: 3345
diff changeset
178
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 connlisteners_register("xmppclient", xmppclient);

mercurial