Changeset

6941:33fbc835697d

util.session: How would you even send anything to a session?
author Kim Alvefur <zash@zash.se>
date Tue, 24 Nov 2015 19:31:37 +0100
parents 6940:2be5e19485aa
children 6942:f12deb882148
files util/session.lua
diffstat 1 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/util/session.lua	Tue Nov 24 19:26:27 2015 +0100
+++ b/util/session.lua	Tue Nov 24 19:31:37 2015 +0100
@@ -1,3 +1,4 @@
+local initialize_filters = require "util.filters".initialize;
 local logger = require "util.logger";
 
 local function new_session(typ)
@@ -25,9 +26,40 @@
 	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;
 }