c9a09273c96d837f1e3a018320aa8122e961e1b8
[saartuer.git] / tyshell
1 #!/usr/bin/python3
2 import os
3 import readline
4 import shlex
5 import sys
6 import subprocess
7 import socket
8 import pwd
9
10 tuerSock = "/run/tuer.sock"
11
12 # use a histfile
13 histfile = os.path.join(os.path.expanduser("~"), ".tyshellhist")
14 try:
15     readline.read_history_file(histfile)
16 except IOError:
17     pass
18 import atexit
19 atexit.register(readline.write_history_file, histfile)
20
21 # available commands
22 def helpcmd(c):
23         print("Available commands: %s" % ", ".join(sorted(commands.keys())))
24
25 def extcmd(cmd):
26         def run(c):
27                 ret = subprocess.call(cmd)
28                 if ret != 0:
29                         print("Command returned non-zero exit statis %d" % ret)
30         return run
31
32 def sendcmd(addr, cmd):
33         def run(c):
34                 print("Running %s..." % (cmd))
35                 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
36                 s.connect(addr)
37                 s.settimeout(10.0)
38                 s.send(cmd.encode())
39                 data = s.recv(4)
40                 s.close()
41                 print(data.decode('utf-8'))
42         return run
43
44 def exitcmd(c):
45         print("Bye")
46         return True
47
48 def whocmd(c):
49         for p in filter(lambda x:x.pw_shell=="/opt/tuer/tyshell",pwd.getpwall()):
50                 print (p.pw_name, " - ", p.pw_gecos)
51
52 commands = {
53         'exit': exitcmd,
54         'help': helpcmd,
55         'open': sendcmd(tuerSock, 'unlock'),
56         'unlock': sendcmd(tuerSock, 'unlock'),
57         'buzz': sendcmd(tuerSock, 'buzz'),
58         'who': whocmd,
59 }
60
61 def complete_command(cmd):
62         '''returns a list of commands (as strings) starting with cmd'''
63         return list(filter(lambda x: x.startswith(cmd), commands.keys()))
64 readline.set_completer(lambda cmd, num: (complete_command(cmd)+[None])[num]) # wrap complete_command for readline's weird completer API
65 readline.parse_and_bind("tab: complete") # run completion on tab
66
67 # input loop
68 print("Welcome to tyshell. Use help to see what you can do.")
69 while True:
70         try:
71                 command = input("$ ")
72         except EOFError:
73                 print()
74                 break
75         command = shlex.split(command)
76         if not len(command): continue
77         # find suiting commands
78         if command[0] in commands: # needed in case a complete command is a prefix of another one
79                 cmdoptions = [command[0]]
80         else:
81                 cmdoptions = complete_command(command[0])
82         # check how many we found
83         if len(cmdoptions) == 0: # no commands fit prefix
84                 print("Command %s not found. Use help." % command[0])
85         elif len(cmdoptions) == 1: # exactly one command fits (prefix)
86                 try:
87                         res = commands[cmdoptions[0]](command)
88                         if res: break
89                 except Exception as e:
90                         print("Error while executing %s: %s" % (command[0], str(e)))
91         else: # multiple commands fit the prefix
92                 print("Ambiguous command prefix, please choose one of the following:")
93                 print("\t", " ".join(cmdoptions))
94                 # TODO: put current "command[0]" into the shell for the next command, but such that it is deletable with backspace
95