"""This file contains one class, Command, which is instanced whenever
a command is typed into the talker"""
import time,re
class Command:
def __init__(self,session,msg):
'''Set up initial variables, parse the command, update timestamp
and execute it'''
self.session = session
self.user = self.session.user
self.get_cmd_and_args(msg)
self.user.time = time.time()
self.execute_command()
def get_cmd_and_args(self,msg):
'''Parses the command. Sets command, args and a variable
called rawargs, which is the args un-strip()ped.
It first checks if the command is alphanumeric or symbolic (by
its first char) and uses a different regexp to parse each. The
symbolic commands are all one char in length atm, but this is
likely to change in the future.'''
if msg[0].isalnum():
pattern = re.compile(r'^(\w+)(.*)$')
else:
pattern = re.compile(r'^(.)(.*)$')
result = pattern.match(msg)
rawcommand = result.group(1)
self.command = result.group(1).strip().lower()
self.rawargs = result.group(2)
self.args = self.rawargs.strip()
def execute_command(self):
'''This is the function that decides what command is in
self.command and executes the appropriate method for it. It
uses a dispatcher function, with some special cases for the
aliases of each command. Thanks to Robin Munn on
comp.lang.python for the idea.'''
special_cases = {
"'": self.do_say,
'"': self.do_say,
';': self.do_emote,
':': self.do_emote,
'~': self.do_think,
')': self.do_sing
}
if special_cases.has_key(self.command):
special_cases[self.command]()
elif hasattr(self,'do_' + self.command):
getattr(self,'do_' + self.command)()
else:
self.do_ERROR()
def do_ERROR(self):
'This command executed when unknown command typed'
self.session.send_user(
"I'm sorry, I don't know how to %s. I'm very primitive." %
self.command)
def do_who(self):
'''This command iterates over all the users and displays them
to the user who typed the command'''
users = self.session.allusers.userdict
self.session.send_user("-WHO (name, idle time)-")
for user in users:
str = "%s, " % users[user].name
str += `int(time.time() - users[user].time)`
self.session.send_user(str)
self.session.send_user("-----------------------")
def do_say(self):
'The basic "say" command'
if self.args == "":
self.session.send_user("Format: say <line>")
return
if self.args.endswith('?'): typeofsay = 'asks'
elif self.args.endswith('!'): typeofsay = 'exclaims'
else: typeofsay = 'says'
self.session.send_all("%s %s '%s'" % (self.user.name,typeofsay,
self.args))
def do_emote(self):
'Emote is something like "Owl waves"'
if self.args == "":
self.session.send_user("Format: emote <action>")
return
if self.rawargs[0] in ";:":
self.args = self.args[1:]
self.do_pemote()
return
if not self.args[0] in "',":
self.args = " " + self.args
self.session.send_all("%s%s" % (self.user.name, self.args))
def do_pemote(self):
"This is an emote with 's , eg \"Owl's computer is slow\""
if self.args == "":
self.session.send_user("Format: pemote <action>")
return
self.session.send_all("%s's %s" % (self.user.name, self.args))
def do_think(self):
'Self explanatory :)'
if self.args == "":
self.session.send_user("Format: think <thought>")
return
self.session.send_all("%s thinks . o O ( %s )" %
(self.user.name, self.args))
def do_sing(self):
'This too :)'
if self.args == "":
self.session.send_user("Format: sing <lyrics>")
return
self.session.send_all("%s sings o/~ %s o/~" % (self.user.name,
self.args))
def do_hug(self):
'''This code will be abstracted into a more generalised
"social" command later'''
if self.args == "":
self.session.send_user("Format: hug <username>")
return
users = self.session.allusers.userdict
usertyped = self.args.strip().lower()
if usertyped in users:
self.session.send_all("%s extends arms and hugs %s" %
(self.user.name, users[usertyped].name))
else:
self.session.send_user("I can't find anyone called %s." %
self.args)