-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoudabot.py
executable file
·73 lines (51 loc) · 1.56 KB
/
goudabot.py
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
#! /usr/bin/python
import SocketServer
import json
from subprocess import check_output, CalledProcessError
import os
import shlex
PORT = 50007
script_dir = os.environ['GOUDABOT'] + '/scripts/'
def execute(commands):
if len(commands) <= 1:
return usage()
command = script_dir + commands[0]
args = commands[1:]
args.insert(0, command)
if os.path.isfile(command):
try:
run = shlex.split(combine(args))
print "Running:"
print run
return check_output(run)
except CalledProcessError as err:
return err.output
return usage()
def usage():
dirList = os.listdir(script_dir)
cmds = ''
for cmd in dirList:
cmds += cmd + ", "
return "Command not found. Avaliable commands are: " + cmds[:-2]
def combine(lis):
result = ''
for i in lis:
result += str(i) + " "
return result[:-1]
class GoudaHandler(SocketServer.BaseRequestHandler):
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
result = execute(json.loads(self.data))
self.request.sendall(result)
class GoudaBot(SocketServer.TCPServer):
allow_reuse_address = True
def main():
HOST = "localhost"
# Create the server, binding to localhost on port 9999
server = GoudaBot((HOST, PORT), GoudaHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()
if __name__ == "__main__":
main()