mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-27 21:19:31 +01:00
Add basic command hooks (ref #18)
This commit is contained in:
parent
28e7b52ef4
commit
6370ad492f
14
plugins/hooks.py
Normal file
14
plugins/hooks.py
Normal file
@ -0,0 +1,14 @@
|
||||
# hooks.py: test of PyLink hooks
|
||||
import sys, os
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
import utils
|
||||
|
||||
def hook_join(irc, source, command, args):
|
||||
print('%s joined channel %s (join hook caught)' % (source, args[0]))
|
||||
utils.add_hook(hook_join, 'join')
|
||||
|
||||
def hook_msg(irc, source, command, args):
|
||||
if utils.isChannel(args[0]) and irc.pseudoclient.nick in args[1]:
|
||||
utils.msg(irc, args[0], 'hi there!')
|
||||
print('%s said my name on channel %s (msg hook caught)' % (source, args[0]))
|
||||
utils.add_hook(hook_msg, 'msg')
|
@ -9,6 +9,11 @@ import traceback
|
||||
from classes import *
|
||||
|
||||
uidgen = {}
|
||||
# Map raw commands to protocol-independent hooks
|
||||
hook_map = {'FJOIN': 'join',
|
||||
'PART': 'part',
|
||||
'QUIT': 'quit',
|
||||
'PRIVMSG': 'msg',}
|
||||
|
||||
def _sendFromServer(irc, sid, msg):
|
||||
irc.send(':%s %s' % (sid, msg))
|
||||
@ -373,12 +378,23 @@ def handle_events(irc, data):
|
||||
except IndexError:
|
||||
return
|
||||
|
||||
# We will do wildcard event handling here. Unhandled events are just ignored, yay!
|
||||
cmd = command.lower()
|
||||
# We will do wildcard event handling here. Unhandled events are just ignored.
|
||||
try:
|
||||
func = globals()['handle_'+command.lower()]
|
||||
func = globals()['handle_'+cmd]
|
||||
func(irc, numeric, command, args)
|
||||
except KeyError: # unhandled event
|
||||
pass
|
||||
else:
|
||||
# All is well; we'll let our hooks do work now.
|
||||
if command in hook_map: # If this is a hook-enabled event
|
||||
# Iterate over hooked functions, catching errors accordingly
|
||||
for hook_func in utils.command_hooks[hook_map[command]]:
|
||||
try:
|
||||
hook_func(irc, numeric, command, args)
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
continue
|
||||
|
||||
def spawnServer(irc, name, sid, uplink=None, desc='PyLink Server'):
|
||||
# -> :0AL SERVER test.server * 1 0AM :some silly pseudoserver
|
||||
|
9
utils.py
9
utils.py
@ -1,9 +1,11 @@
|
||||
import string
|
||||
import re
|
||||
from collections import defaultdict
|
||||
|
||||
global bot_commands
|
||||
global bot_commands, command_hooks
|
||||
# This should be a mapping of command names to functions
|
||||
bot_commands = {}
|
||||
command_hooks = defaultdict(list)
|
||||
|
||||
class TS6UIDGenerator():
|
||||
"""TS6 UID Generator module, adapted from InspIRCd source
|
||||
@ -45,6 +47,11 @@ def add_cmd(func, name=None):
|
||||
name = name.lower()
|
||||
bot_commands[name] = func
|
||||
|
||||
def add_hook(func, command):
|
||||
"""Add a hook <func> for command <command>."""
|
||||
command = command.lower()
|
||||
command_hooks[command].append(func)
|
||||
|
||||
def nickToUid(irc, nick):
|
||||
for k, v in irc.users.items():
|
||||
if v.nick == nick:
|
||||
|
Loading…
Reference in New Issue
Block a user