Annotate

util/session.lua @ 8374:b52663586243

mod_pubsub: Update node config names (should have been in 2abbb01cd756) (thanks lovetox)
author Kim Alvefur <zash@zash.se>
date Mon, 06 Nov 2017 21:03:30 +0100
parent 7181:8af558965da3
child 9947:8ebca1240203
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6941
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
1 local initialize_filters = require "util.filters".initialize;
6939
a9ae0c6ac4f4 util.session: What does the session say?
Kim Alvefur <zash@zash.se>
parents: 6938
diff changeset
2 local logger = require "util.logger";
6937
f5d2e58fbefa util.session: What is a session?
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
f5d2e58fbefa util.session: What is a session?
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local function new_session(typ)
f5d2e58fbefa util.session: What is a session?
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local session = {
f5d2e58fbefa util.session: What is a session?
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 type = typ .. "_unauthed";
f5d2e58fbefa util.session: What is a session?
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 };
f5d2e58fbefa util.session: What is a session?
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 return session;
f5d2e58fbefa util.session: What is a session?
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 end
f5d2e58fbefa util.session: What is a session?
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
6938
9df70e9e006b util.session: What is the identity of a session?
Kim Alvefur <zash@zash.se>
parents: 6937
diff changeset
11 local function set_id(session)
7181
8af558965da3 util.session: Fix luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 6941
diff changeset
12 local id = session.type .. tostring(session):match("%x+$"):lower();
6938
9df70e9e006b util.session: What is the identity of a session?
Kim Alvefur <zash@zash.se>
parents: 6937
diff changeset
13 session.id = id;
9df70e9e006b util.session: What is the identity of a session?
Kim Alvefur <zash@zash.se>
parents: 6937
diff changeset
14 return session;
9df70e9e006b util.session: What is the identity of a session?
Kim Alvefur <zash@zash.se>
parents: 6937
diff changeset
15 end
9df70e9e006b util.session: What is the identity of a session?
Kim Alvefur <zash@zash.se>
parents: 6937
diff changeset
16
6939
a9ae0c6ac4f4 util.session: What does the session say?
Kim Alvefur <zash@zash.se>
parents: 6938
diff changeset
17 local function set_logger(session)
7181
8af558965da3 util.session: Fix luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 6941
diff changeset
18 local log = logger.init(session.id);
6939
a9ae0c6ac4f4 util.session: What does the session say?
Kim Alvefur <zash@zash.se>
parents: 6938
diff changeset
19 session.log = log;
a9ae0c6ac4f4 util.session: What does the session say?
Kim Alvefur <zash@zash.se>
parents: 6938
diff changeset
20 return session;
a9ae0c6ac4f4 util.session: What does the session say?
Kim Alvefur <zash@zash.se>
parents: 6938
diff changeset
21 end
a9ae0c6ac4f4 util.session: What does the session say?
Kim Alvefur <zash@zash.se>
parents: 6938
diff changeset
22
6940
2be5e19485aa util.session: How does a session relate do a connection?
Kim Alvefur <zash@zash.se>
parents: 6939
diff changeset
23 local function set_conn(session, conn)
2be5e19485aa util.session: How does a session relate do a connection?
Kim Alvefur <zash@zash.se>
parents: 6939
diff changeset
24 session.conn = conn;
2be5e19485aa util.session: How does a session relate do a connection?
Kim Alvefur <zash@zash.se>
parents: 6939
diff changeset
25 session.ip = conn:ip();
2be5e19485aa util.session: How does a session relate do a connection?
Kim Alvefur <zash@zash.se>
parents: 6939
diff changeset
26 return session;
2be5e19485aa util.session: How does a session relate do a connection?
Kim Alvefur <zash@zash.se>
parents: 6939
diff changeset
27 end
2be5e19485aa util.session: How does a session relate do a connection?
Kim Alvefur <zash@zash.se>
parents: 6939
diff changeset
28
6941
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
29 local function set_send(session)
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
30 local conn = session.conn;
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
31 if not conn then
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
32 function session.send(data)
7181
8af558965da3 util.session: Fix luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 6941
diff changeset
33 session.log("debug", "Discarding data sent to unconnected session: %s", tostring(data));
6941
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
34 return false;
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
35 end
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
36 return session;
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
37 end
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
38 local filter = initialize_filters(session);
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
39 local w = conn.write;
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
40 session.send = function (t)
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
41 if t.name then
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
42 t = filter("stanzas/out", t);
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
43 end
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
44 if t then
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
45 t = filter("bytes/out", tostring(t));
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
46 if t then
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
47 local ret, err = w(conn, t);
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
48 if not ret then
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
49 session.log("debug", "Error writing to connection: %s", tostring(err));
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
50 return false, err;
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
51 end
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
52 end
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
53 end
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
54 return true;
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
55 end
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
56 return session;
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
57 end
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
58
6937
f5d2e58fbefa util.session: What is a session?
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 return {
f5d2e58fbefa util.session: What is a session?
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 new = new_session;
6938
9df70e9e006b util.session: What is the identity of a session?
Kim Alvefur <zash@zash.se>
parents: 6937
diff changeset
61 set_id = set_id;
6939
a9ae0c6ac4f4 util.session: What does the session say?
Kim Alvefur <zash@zash.se>
parents: 6938
diff changeset
62 set_logger = set_logger;
6940
2be5e19485aa util.session: How does a session relate do a connection?
Kim Alvefur <zash@zash.se>
parents: 6939
diff changeset
63 set_conn = set_conn;
6941
33fbc835697d util.session: How would you even send anything to a session?
Kim Alvefur <zash@zash.se>
parents: 6940
diff changeset
64 set_send = set_send;
6937
f5d2e58fbefa util.session: What is a session?
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 }