Prepare required data folder for integration tests
[prosody.git] / util / session.lua
blob25b22faf3affb6f416eae8d82481cae8dd3e3966
1 local initialize_filters = require "util.filters".initialize;
2 local logger = require "util.logger";
4 local function new_session(typ)
5 local session = {
6 type = typ .. "_unauthed";
7 base_type = typ;
8 };
9 return session;
10 end
12 local function set_id(session)
13 local id = session.base_type .. tostring(session):match("%x+$"):lower();
14 session.id = id;
15 return session;
16 end
18 local function set_logger(session)
19 local log = logger.init(session.id);
20 session.log = log;
21 return session;
22 end
24 local function set_conn(session, conn)
25 session.conn = conn;
26 session.ip = conn:ip();
27 return session;
28 end
30 local function set_send(session)
31 local conn = session.conn;
32 if not conn then
33 function session.send(data)
34 session.log("debug", "Discarding data sent to unconnected session: %s", data);
35 return false;
36 end
37 return session;
38 end
39 local filter = initialize_filters(session);
40 local w = conn.write;
41 session.send = function (t)
42 if t.name then
43 t = filter("stanzas/out", t);
44 end
45 if t then
46 t = filter("bytes/out", tostring(t));
47 if t then
48 local ret, err = w(conn, t);
49 if not ret then
50 session.log("debug", "Error writing to connection: %s", err);
51 return false, err;
52 end
53 end
54 end
55 return true;
56 end
57 return session;
58 end
60 return {
61 new = new_session;
62 set_id = set_id;
63 set_logger = set_logger;
64 set_conn = set_conn;
65 set_send = set_send;