aboutsummaryrefslogtreecommitdiffstats
path: root/util/adminstream.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2020-06-02 08:28:39 +0100
committerMatthew Wild <mwild1@gmail.com>2020-06-02 08:28:39 +0100
commit78a8bfc31dbe40fb4e0d073b7eb86d7c78f6a183 (patch)
tree821c1204b631d73fd3e5d3d143e189a45d7233f0 /util/adminstream.lua
parent425c9cb979bfaae53b4ccf2a19ad1ec5fddac34c (diff)
downloadprosody-78a8bfc31dbe40fb4e0d073b7eb86d7c78f6a183.tar.gz
prosody-78a8bfc31dbe40fb4e0d073b7eb86d7c78f6a183.zip
util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Diffstat (limited to 'util/adminstream.lua')
-rw-r--r--util/adminstream.lua39
1 files changed, 39 insertions, 0 deletions
diff --git a/util/adminstream.lua b/util/adminstream.lua
index 186cb0e9..b1bc0e64 100644
--- a/util/adminstream.lua
+++ b/util/adminstream.lua
@@ -136,6 +136,44 @@ end
--- 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 @@ local function new_client()
end
return {
+ connection = new_connection;
server = new_server;
client = new_client;
};