Software /
code /
prosody
Comparison
util/prosodyctl/shell.lua @ 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 |
parent | 10874:98c535531450 |
child | 10877:2b015ef8cd06 |
comparison
equal
deleted
inserted
replaced
10874:98c535531450 | 10875:09674bbb833f |
---|---|
1 local have_unix, unix = pcall(require, "socket.unix"); | |
2 | |
3 if not have_unix or type(unix) ~= "table" then | |
4 print("** LuaSocket unix socket support not available or incompatible, ensure your"); | |
5 print("** version is up to date."); | |
6 os.exit(1); | |
7 end | |
8 | |
9 local config = require "core.configmanager"; | 1 local config = require "core.configmanager"; |
10 local server = require "net.server"; | 2 local server = require "net.server"; |
11 local st = require "util.stanza"; | 3 local st = require "util.stanza"; |
12 local path = require "util.paths"; | 4 local path = require "util.paths"; |
13 local parse_args = require "util.argparse".parse; | 5 local parse_args = require "util.argparse".parse; |
40 print(""); | 32 print(""); |
41 end | 33 end |
42 os.exit(); | 34 os.exit(); |
43 end | 35 end |
44 send_line(client, line); | 36 send_line(client, line); |
45 end | |
46 | |
47 local function connection(socket_path, listeners) | |
48 local conn, sock; | |
49 | |
50 return { | |
51 connect = function () | |
52 if sock or conn then | |
53 return nil, "already connected"; | |
54 end | |
55 sock = unix.stream(); | |
56 sock:settimeout(0); | |
57 local ok, err = sock:connect(socket_path); | |
58 if not ok then | |
59 return nil, err; | |
60 end | |
61 conn = server.wrapclient(sock, nil, nil, listeners, "*a"); | |
62 return true; | |
63 end; | |
64 disconnect = function () | |
65 if conn then | |
66 conn:close(); | |
67 conn = nil; | |
68 end | |
69 if sock then | |
70 sock:close(); | |
71 sock = nil; | |
72 end | |
73 return true; | |
74 end; | |
75 }; | |
76 end | 37 end |
77 | 38 |
78 local function printbanner() | 39 local function printbanner() |
79 print([[ | 40 print([[ |
80 ____ \ / _ | 41 ____ \ / _ |
115 repl(client); | 76 repl(client); |
116 end | 77 end |
117 end); | 78 end); |
118 | 79 |
119 local socket_path = path.resolve_relative_path(prosody.paths.data, opts.socket or config.get("*", "admin_socket") or "prosody.sock"); | 80 local socket_path = path.resolve_relative_path(prosody.paths.data, opts.socket or config.get("*", "admin_socket") or "prosody.sock"); |
120 local conn = connection(socket_path, client.listeners); | 81 local conn = adminstream.connection(socket_path, client.listeners); |
121 local ok, err = conn:connect(); | 82 local ok, err = conn:connect(); |
122 if not ok then | 83 if not ok then |
123 print("** Unable to connect to server - is it running? Is mod_admin_shell enabled?"); | 84 if err == "no unix socket support" then |
124 print("** Connection error: "..err); | 85 print("** LuaSocket unix socket support not available or incompatible, ensure your"); |
86 print("** version is up to date."); | |
87 else | |
88 print("** Unable to connect to server - is it running? Is mod_admin_shell enabled?"); | |
89 print("** Connection error: "..err); | |
90 end | |
125 os.exit(1); | 91 os.exit(1); |
126 end | 92 end |
127 server.loop(); | 93 server.loop(); |
128 end | 94 end |
129 | 95 |