f30b9d797928f06d10dda8c3638480c04df70d11
[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         if (len(c) > 1):
25                 print(commands.get(c[1],(None,'Can\'t find help for command %s'%(c[1])))[1])
26         else:
27                 print("Available commands: %s" % ", ".join(sorted(commands.keys())))
28                 print("Use 'help command' to get more information on the command 'command'")
29
30 def extcmd(cmd):
31         def run(c):
32                 ret = subprocess.call(cmd)
33                 if ret != 0:
34                         print("Command returned non-zero exit statis %d" % ret)
35         return run
36
37 def sendcmd(addr, cmd):
38         def run(c):
39                 print("206 Sending command %s..." % (cmd))
40                 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
41                 s.connect(addr)
42                 s.settimeout(60.0)
43                 s.send(cmd.encode())
44                 data = s.recv(256)
45                 s.close()
46                 print(data.decode('utf-8'))
47         return run
48
49 def exitcmd(c):
50         print("Bye")
51         return True
52
53 def whocmd(c):
54         for n in grp.getgrnam("tuer").gr_mem:
55                 p = pwd.getpwnam(n)
56                 print (p.pw_name, " - ", p.pw_gecos)
57
58 def alias (cmds, aliases):
59         for newname, oldname in aliases.items():
60                 cmds[newname] = cmds[oldname]
61         return cmds
62
63 commands = alias({
64         'exit': (exitcmd, 'Quits this shell'),
65         'help': (helpcmd, 'Helps you getting to know the available commands'),
66         'open': (sendcmd(tuerSock, 'unlock'), 'Will try to unlock the apartment door'),
67         'buzz': (sendcmd(tuerSock, 'buzz'), 'Will buzz the buzzer for the street door'),
68         'who': (whocmd, 'Shows the list of people, who are allowed to control this system'),
69 },{
70         # aliases
71         'unlock': 'open',
72 })
73
74 def complete_command(cmd):
75         '''returns a list of commands (as strings) starting with cmd'''
76         return list(filter(lambda x: x.startswith(cmd), commands.keys()))
77 readline.set_completer(lambda cmd, num: (complete_command(cmd)+[None])[num]) # wrap complete_command for readline's weird completer API
78 readline.parse_and_bind("tab: complete") # run completion on tab
79
80 # input loop
81 print("Welcome to tyshell. Use help to see what you can do.")
82 while True:
83         try:
84                 command = input("$ ")
85         except EOFError:
86                 print()
87                 break
88         command = shlex.split(command)
89         if not len(command): continue
90         # find suiting commands
91         if command[0] in commands: # needed in case a complete command is a prefix of another one
92                 cmdoptions = [command[0]]
93         else:
94                 cmdoptions = complete_command(command[0])
95         # check how many we found
96         if len(cmdoptions) == 0: # no commands fit prefix
97                 print("Command %s not found. Use help." % command[0])
98         elif len(cmdoptions) == 1: # exactly one command fits (prefix)
99                 try:
100                         res = commands[cmdoptions[0]][0](command)
101                         if res: break
102                 except Exception as e:
103                         print("Error while executing %s: %s" % (command[0], str(e)))
104         else: # multiple commands fit the prefix
105                 print("Ambiguous command prefix, please choose one of the following:")
106                 print("\t", " ".join(cmdoptions))
107                 # TODO: put current "command[0]" into the shell for the next command, but such that it is deletable with backspace
108