mirror of
https://github.com/jlu5/PyLink.git
synced 2024-12-28 13:42:37 +01:00
core: keep track of where last command was called & make command calling a shared function
Prerequisite for FANTASY command implementation (#111).
This commit is contained in:
parent
5afa621654
commit
822544e3cc
22
classes.py
22
classes.py
@ -11,6 +11,7 @@ from copy import deepcopy
|
||||
from log import log
|
||||
from conf import conf
|
||||
import world
|
||||
import utils
|
||||
|
||||
### Exceptions
|
||||
|
||||
@ -26,6 +27,10 @@ class Irc():
|
||||
self.pseudoclient = None
|
||||
self.lastping = time.time()
|
||||
|
||||
# Internal variable to set the place the last command was called (in PM
|
||||
# or in a channel), used by fantasy command support.
|
||||
self.called_by = None
|
||||
|
||||
# Server, channel, and user indexes to be populated by our protocol module
|
||||
self.servers = {self.sid: IrcServer(None, self.serverdata['hostname'],
|
||||
internal=True, desc=self.serverdata.get('serverdesc')
|
||||
@ -166,6 +171,23 @@ class Irc():
|
||||
else:
|
||||
return
|
||||
|
||||
def callCommand(self, source, text):
|
||||
cmd_args = text.strip().split(' ')
|
||||
cmd = cmd_args[0].lower()
|
||||
cmd_args = cmd_args[1:]
|
||||
if cmd not in world.bot_commands:
|
||||
self.msg(self.called_by or source, 'Error: Unknown command %r.' % cmd)
|
||||
return
|
||||
log.info('(%s) Calling command %r for %s', self.name, cmd, utils.getHostmask(self, source))
|
||||
for func in world.bot_commands[cmd]:
|
||||
try:
|
||||
func(self, source, cmd_args)
|
||||
except utils.NotAuthenticatedError:
|
||||
self.msg(self.called_by or source, 'Error: You are not authorized to perform this operation.')
|
||||
except Exception as e:
|
||||
log.exception('Unhandled exception caught in command %r', cmd)
|
||||
self.msg(self.called_by or source, 'Uncaught exception in command %r: %s: %s' % (cmd, type(e).__name__, str(e)))
|
||||
|
||||
def msg(self, target, text, notice=False, source=None):
|
||||
"""Handy function to send messages/notices to clients. Source
|
||||
is optional, and defaults to the main PyLink client if not specified."""
|
||||
|
@ -21,22 +21,9 @@ utils.add_hook(handle_kick, 'KICK')
|
||||
def handle_commands(irc, source, command, args):
|
||||
"""Handle commands sent to the PyLink client (PRIVMSG)."""
|
||||
if args['target'] == irc.pseudoclient.uid:
|
||||
text = args['text'].strip()
|
||||
cmd_args = text.split(' ')
|
||||
cmd = cmd_args[0].lower()
|
||||
cmd_args = cmd_args[1:]
|
||||
if cmd not in world.bot_commands:
|
||||
irc.msg(source, 'Error: Unknown command %r.' % cmd)
|
||||
return
|
||||
log.info('(%s) Calling command %r for %s', irc.name, cmd, utils.getHostmask(irc, source))
|
||||
for func in world.bot_commands[cmd]:
|
||||
try:
|
||||
func(irc, source, cmd_args)
|
||||
except utils.NotAuthenticatedError:
|
||||
irc.msg(source, 'Error: You are not authorized to perform this operation.')
|
||||
except Exception as e:
|
||||
log.exception('Unhandled exception caught in command %r', cmd)
|
||||
irc.msg(source, 'Uncaught exception in command %r: %s: %s' % (cmd, type(e).__name__, str(e)))
|
||||
irc.called_by = source
|
||||
irc.callCommand(source, args['text'])
|
||||
|
||||
utils.add_hook(handle_commands, 'PRIVMSG')
|
||||
|
||||
def handle_whois(irc, source, command, args):
|
||||
|
Loading…
Reference in New Issue
Block a user