Changeset

10875:09674bbb833f

util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
author Matthew Wild <mwild1@gmail.com>
date Tue, 02 Jun 2020 08:28:39 +0100
parents 10874:98c535531450
children 10876:c01c39a2c7a2
files util/adminstream.lua util/prosodyctl/shell.lua
diffstat 2 files changed, 47 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/util/adminstream.lua	Tue Jun 02 09:19:07 2020 +0200
+++ b/util/adminstream.lua	Tue Jun 02 08:28:39 2020 +0100
@@ -136,6 +136,44 @@
 
 --- Public methods
 
+local function new_connection(socket_path, listeners)
+	local have_unix, unix = pcall(require, "socket.unix");
+	if type(unix) ~= "table" then
+		have_unix = false;
+	end
+	local conn, sock;
+
+	return {
+		connect = function ()
+			if not have_unix then
+				return nil, "no unix socket support";
+			end
+			if sock or conn then
+				return nil, "already connected";
+			end
+			sock = unix.stream();
+			sock:settimeout(0);
+			local ok, err = sock:connect(socket_path);
+			if not ok then
+				return nil, err;
+			end
+			conn = server.wrapclient(sock, nil, nil, listeners, "*a");
+			return true;
+		end;
+		disconnect = function ()
+			if conn then
+				conn:close();
+				conn = nil;
+			end
+			if sock then
+				sock:close();
+				sock = nil;
+			end
+			return true;
+		end;
+	};
+end
+
 local function new_server(sessions, stanza_handler)
 	local listeners = {};
 
@@ -280,6 +318,7 @@
 end
 
 return {
+	connection = new_connection;
 	server = new_server;
 	client = new_client;
 };
--- a/util/prosodyctl/shell.lua	Tue Jun 02 09:19:07 2020 +0200
+++ b/util/prosodyctl/shell.lua	Tue Jun 02 08:28:39 2020 +0100
@@ -1,11 +1,3 @@
-local have_unix, unix = pcall(require, "socket.unix");
-
-if not have_unix or type(unix) ~= "table" then
-	print("** LuaSocket unix socket support not available or incompatible, ensure your");
-	print("** version is up to date.");
-	os.exit(1);
-end
-
 local config = require "core.configmanager";
 local server = require "net.server";
 local st = require "util.stanza";
@@ -44,37 +36,6 @@
 	send_line(client, line);
 end
 
-local function connection(socket_path, listeners)
-	local conn, sock;
-
-	return {
-		connect = function ()
-			if sock or conn then
-				return nil, "already connected";
-			end
-			sock = unix.stream();
-			sock:settimeout(0);
-			local ok, err = sock:connect(socket_path);
-			if not ok then
-				return nil, err;
-			end
-			conn = server.wrapclient(sock, nil, nil, listeners, "*a");
-			return true;
-		end;
-		disconnect = function ()
-			if conn then
-				conn:close();
-				conn = nil;
-			end
-			if sock then
-				sock:close();
-				sock = nil;
-			end
-			return true;
-		end;
-	};
-end
-
 local function printbanner()
 	print([[
                      ____                \   /     _
@@ -117,11 +78,16 @@
 	end);
 
 	local socket_path = path.resolve_relative_path(prosody.paths.data, opts.socket or config.get("*", "admin_socket") or "prosody.sock");
-	local conn = connection(socket_path, client.listeners);
+	local conn = adminstream.connection(socket_path, client.listeners);
 	local ok, err = conn:connect();
 	if not ok then
-		print("** Unable to connect to server - is it running? Is mod_admin_shell enabled?");
-		print("** Connection error: "..err);
+		if err == "no unix socket support" then
+			print("** LuaSocket unix socket support not available or incompatible, ensure your");
+			print("** version is up to date.");
+		else
+			print("** Unable to connect to server - is it running? Is mod_admin_shell enabled?");
+			print("** Connection error: "..err);
+		end
 		os.exit(1);
 	end
 	server.loop();