2019-10-11 14:16:56 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-10-11 14:31:22 +00:00
|
|
|
import argparse, os, sys
|
2019-10-11 14:16:56 +00:00
|
|
|
|
|
|
|
directory = os.path.dirname(os.path.realpath(__file__))
|
2019-10-17 12:47:58 +00:00
|
|
|
home = os.path.expanduser("~")
|
|
|
|
default_data = os.path.join(home, ".bitbot")
|
2019-10-11 14:16:56 +00:00
|
|
|
|
|
|
|
arg_parser = argparse.ArgumentParser(
|
|
|
|
description="BitBot CLI control utility")
|
|
|
|
|
2019-10-17 12:47:58 +00:00
|
|
|
arg_parser.add_argument("--socket", "-s",
|
|
|
|
help="Location of bitbotd's unix domain socket",
|
|
|
|
default=os.path.join(default_data, "bot.sock"))
|
2019-10-11 14:16:56 +00:00
|
|
|
|
2019-10-11 14:31:22 +00:00
|
|
|
arg_parser.add_argument("command")
|
|
|
|
|
2019-10-11 14:54:43 +00:00
|
|
|
args, unknown = arg_parser.parse_known_args()
|
2019-10-11 14:31:22 +00:00
|
|
|
|
2019-11-11 11:52:35 +00:00
|
|
|
def _die(code: int, s: str):
|
2019-10-11 14:31:22 +00:00
|
|
|
sys.stderr.write("%s\n" % s)
|
2019-11-11 11:52:35 +00:00
|
|
|
sys.exit(code)
|
2019-10-11 14:31:22 +00:00
|
|
|
|
2019-10-17 15:53:48 +00:00
|
|
|
SIMPLE = ["rehash", "reload", "stop"]
|
2019-10-11 14:31:22 +00:00
|
|
|
if args.command == "log":
|
|
|
|
arg_parser.add_argument("--level", "-l", help="Log level",
|
|
|
|
default="INFO")
|
2019-10-17 15:53:48 +00:00
|
|
|
elif args.command in SIMPLE:
|
2019-10-11 14:54:43 +00:00
|
|
|
pass
|
2019-10-11 14:31:22 +00:00
|
|
|
else:
|
2019-11-11 11:52:35 +00:00
|
|
|
_die(1, "Unknown command '%s'" % args.command)
|
2019-10-11 14:31:22 +00:00
|
|
|
|
2019-10-11 14:16:56 +00:00
|
|
|
args = arg_parser.parse_args()
|
|
|
|
|
2019-10-11 14:54:43 +00:00
|
|
|
import json, socket, signal
|
|
|
|
|
|
|
|
def _sigint(_1, _2):
|
|
|
|
print("")
|
|
|
|
sys.exit(0)
|
|
|
|
signal.signal(signal.SIGINT, _sigint)
|
|
|
|
|
2019-10-11 14:16:56 +00:00
|
|
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
2019-11-11 11:52:35 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
sock.connect(args.socket)
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
_die(2, "Failed to connect to BitBot instance. Is bitbotd running?")
|
2019-10-11 14:31:22 +00:00
|
|
|
|
|
|
|
def _send(s):
|
|
|
|
sock.send(("%s\n" % s).encode("utf8"))
|
|
|
|
|
2019-10-14 15:51:36 +00:00
|
|
|
# protocol: <msgid> <command> [arg [arg ..]]
|
2019-10-11 14:31:22 +00:00
|
|
|
_send("0 version 0")
|
|
|
|
|
|
|
|
if args.command == "log":
|
|
|
|
_send("1 log %s" % args.level)
|
2019-10-17 15:53:48 +00:00
|
|
|
elif args.command in SIMPLE:
|
|
|
|
_send("1 %s" % args.command)
|
2019-10-11 14:16:56 +00:00
|
|
|
|
|
|
|
read_buffer = b""
|
|
|
|
|
|
|
|
while True:
|
|
|
|
data = sock.recv(1024)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
|
|
|
|
data = read_buffer+data
|
|
|
|
lines = [line.strip(b"\r") for line in data.split(b"\n")]
|
|
|
|
read_buffer = lines.pop(-1)
|
|
|
|
for line in lines:
|
|
|
|
line = json.loads(line)
|
|
|
|
if line["action"] == "log":
|
|
|
|
print(line["data"])
|
2019-10-14 13:52:58 +00:00
|
|
|
elif line["action"] == "ack" and line["data"]:
|
|
|
|
print(line["data"])
|