-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathbitbotctl
executable file
·78 lines (60 loc) · 1.95 KB
/
bitbotctl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
import argparse, os, sys
directory = os.path.dirname(os.path.realpath(__file__))
home = os.path.expanduser("~")
default_data = os.path.join(home, ".bitbot")
arg_parser = argparse.ArgumentParser(
description="BitBot CLI control utility")
arg_parser.add_argument("--socket", "-s",
help="Location of bitbotd's unix domain socket",
default=os.path.join(default_data, "bot.sock"))
arg_parser.add_argument("command")
args, unknown = arg_parser.parse_known_args()
def _die(code: int, s: str):
sys.stderr.write("%s\n" % s)
sys.exit(code)
SIMPLE = ["rehash", "reload", "stop"]
if args.command == "log":
arg_parser.add_argument("--level", "-l", help="Log level",
default="INFO")
elif args.command == "command":
arg_parser.add_argument("subcommand")
elif args.command in SIMPLE:
pass
else:
_die(1, "Unknown command '%s'" % args.command)
args = arg_parser.parse_args()
import json, socket, signal
def _sigint(_1, _2):
print("")
sys.exit(0)
signal.signal(signal.SIGINT, _sigint)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
sock.connect(args.socket)
except ConnectionRefusedError:
_die(2, "Failed to connect to BitBot instance. Is bitbotd running?")
def _send(s):
sock.send(("%s\n" % s).encode("utf8"))
# protocol: <msgid> <command> [arg [arg ..]]
_send("0 version 0")
if args.command == "log":
_send("1 log %s" % args.level)
elif args.command == "command":
_send("1 command %s" % args.subcommand)
elif args.command in SIMPLE:
_send("1 %s" % args.command)
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"])
elif line["action"] == "ack" and line["data"]:
print(line["data"])