MUC: Fix delay@from to be room JID (fixes #1416)
[prosody.git] / util / session.lua
blobb2a726ce27cf95ce34cf133a51efad3f3be62748
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 };
8 return session;
9 end
11 local function set_id(session)
12 local id = session.type .. tostring(session):match("%x+$"):lower();
13 session.id = id;
14 return session;
15 end
17 local function set_logger(session)
18 local log = logger.init(session.id);
19 session.log = log;
20 return session;
21 end
23 local function set_conn(session, conn)
24 session.conn = conn;
25 session.ip = conn:ip();
26 return session;
27 end
29 local function set_send(session)
30 local conn = session.conn;
31 if not conn then
32 function session.send(data)
33 session.log("debug", "Discarding data sent to unconnected session: %s", tostring(data));
34 return false;
35 end
36 return session;
37 end
38 local filter = initialize_filters(session);
39 local w = conn.write;
40 session.send = function (t)
41 if t.name then
42 t = filter("stanzas/out", t);
43 end
44 if t then
45 t = filter("bytes/out", tostring(t));
46 if t then
47 local ret, err = w(conn, t);
48 if not ret then
49 session.log("debug", "Error writing to connection: %s", tostring(err));
50 return false, err;
51 end
52 end
53 end
54 return true;
55 end
56 return session;
57 end
59 return {
60 new = new_session;
61 set_id = set_id;
62 set_logger = set_logger;
63 set_conn = set_conn;
64 set_send = set_send;