-
Notifications
You must be signed in to change notification settings - Fork 2
/
thudshell.py
66 lines (61 loc) · 2.62 KB
/
thudshell.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
class ThudCommand(object):
def __init__(self,shell,name,description=""):
self.shell = shell
self.name = name
self.description = description
def help(self,args):
self._help(args)
def _help(self,args):
self.shell.respond("help: %s" % self.description)
def run(self, args):
self._run(args)
def _run(self, args):
pass
class HelpCommand(ThudCommand):
def __init__(self,shell):
super(HelpCommand,self).__init__(shell,"help","command help")
def _run(self,args):
if not len(args):
self.shell.respond("thud shell help. commands available:")
for name, cmd in self.shell.commands.items():
self.shell.respond(" %s - %s" % (name,cmd.description))
else:
cmd = args[0].lower()
if cmd in self.shell.commands:
self.shell.commands[cmd].help(args[1:])
else:
self.shell.respond("help: unknown command")
class ListCommand(ThudCommand):
def __init__(self,shell):
super(ListCommand,self).__init__(shell,"list","list things (servers, client-resources, etc)")
def _run(self,args):
if len(args) != 1:
return self.help(args)
kind = args[0].lower()
if "servers".startswith(kind) or "servers".startswith(kind):
self.shell.respond("connected server servers:")
server_connections = self.shell.cache.server.config.user.server_connections
for name, server in server_connections.items():
self.shell.respond(" %s - %s" % (name, server.config.uri))
elif "clients".startswith(kind) or "resources".startswith(kind):
self.shell.respond("connected downstream client resources:")
clients = self.shell.cache.server.config.user.clients
for resource, client in clients.items():
self.shell.respond(" %s connected to %s last seen at %s" % (resource,client.serverref,self.shell.cache.last_seen[resource]))
class ThudShell(object):
def __init__(self, cache, client):
self.cache,self.client = cache,client
self.commands = {
"help": HelpCommand(self),
"list": ListCommand(self),
}
def respond(self, message):
messages = message.split("\n")
for m in messages:
self.client.sendLine(":[email protected] NOTICE %s :%s" % (self.cache.server.config.nick,m))
def handle(self, args):
cmd = args[0].lower()
if cmd in self.commands:
self.commands[cmd].run(args[1:])
else:
self.respond("unknown command: %s" % args)