diff options
author | Matthew Wild <mwild1@gmail.com> | 2015-11-26 00:19:31 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2015-11-26 00:19:31 +0000 |
commit | ab8006e6152db2531cfc65fbfa4de95efa570d6d (patch) | |
tree | 1584719c9dc1939806c4e9f35c3cc4bd7e368320 /util/session.lua | |
parent | 237847b4d5a439a19d128f14f898f224a97bdf6e (diff) | |
parent | 451180d8a42fd3869be9b7002142467147da3203 (diff) | |
download | prosody-ab8006e6152db2531cfc65fbfa4de95efa570d6d.tar.gz prosody-ab8006e6152db2531cfc65fbfa4de95efa570d6d.zip |
Merge 0.10->trunk
Diffstat (limited to 'util/session.lua')
-rw-r--r-- | util/session.lua | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/util/session.lua b/util/session.lua new file mode 100644 index 00000000..fda7fccd --- /dev/null +++ b/util/session.lua @@ -0,0 +1,65 @@ +local initialize_filters = require "util.filters".initialize; +local logger = require "util.logger"; + +local function new_session(typ) + local session = { + type = typ .. "_unauthed"; + }; + return session; +end + +local function set_id(session) + local id = typ .. tostring(session):match("%x+$"):lower(); + session.id = id; + return session; +end + +local function set_logger(session) + local log = logger.init(id); + session.log = log; + return session; +end + +local function set_conn(session, conn) + session.conn = conn; + session.ip = conn:ip(); + return session; +end + +local function set_send(session) + local conn = session.conn; + if not conn then + function session.send(data) + log("debug", "Discarding data sent to unconnected session: %s", tostring(data)); + return false; + end + return session; + end + local filter = initialize_filters(session); + local w = conn.write; + session.send = function (t) + if t.name then + t = filter("stanzas/out", t); + end + if t then + t = filter("bytes/out", tostring(t)); + if t then + local ret, err = w(conn, t); + if not ret then + session.log("debug", "Error writing to connection: %s", tostring(err)); + return false, err; + end + end + end + return true; + end + return session; +end + +return { + new = new_session; + set_id = set_id; + set_logger = set_logger; + set_conn = set_conn; + set_send = set_send; +} |