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