From fdea348cbe0ec23ed7d64c9fb0c4a8f5876871df Mon Sep 17 00:00:00 2001 From: James Lu Date: Fri, 17 Jul 2015 16:09:50 -0700 Subject: [PATCH] move hook calling out of pr/insp and into Irc --- main.py | 30 +++++++++++++++++++++++++++++- protocols/inspircd.py | 23 +---------------------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/main.py b/main.py index 9c6eca8..8d847de 100755 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ import time import sys from collections import defaultdict import threading +import traceback from log import log import conf @@ -111,7 +112,34 @@ class Irc(): while '\n' in buf: line, buf = buf.split('\n', 1) log.debug("(%s) <- %s", self.name, line) - proto.handle_events(self, line) + hook_args = self.proto.handle_events(self, line) + # Only call our hooks if there's data to process. Handlers that support + # hooks will return a dict of parsed arguments, which can be passed on + # to plugins and the like. For example, the JOIN handler will return + # something like: {'channel': '#whatever', 'users': ['UID1', 'UID2', + # 'UID3']}, etc. + if hook_args is not None: + self.callHooks(hook_args) + + def callHooks(self, hook_args): + numeric, command, parsed_args = hook_args + # Always make sure TS is sent. + if 'ts' not in parsed_args: + parsed_args['ts'] = int(time.time()) + hook_cmd = command + hook_map = self.proto.hook_map + if command in hook_map: + hook_cmd = hook_map[command] + log.debug('Parsed args %r received from %s handler (calling hook %s)', parsed_args, command, hook_cmd) + # Iterate over hooked functions, catching errors accordingly + for hook_func in utils.command_hooks[hook_cmd]: + try: + log.debug('Calling function %s', hook_func) + hook_func(self, numeric, command, parsed_args) + except Exception: + # We don't want plugins to crash our servers... + traceback.print_exc() + continue def send(self, data): # Safeguard against newlines in input!! Otherwise, each line gets diff --git a/protocols/inspircd.py b/protocols/inspircd.py index 49d9f33..d571537 100644 --- a/protocols/inspircd.py +++ b/protocols/inspircd.py @@ -1,7 +1,6 @@ import time import sys import os -import traceback import re from copy import copy @@ -570,28 +569,8 @@ def handle_events(irc, data): pass else: parsed_args = func(irc, numeric, command, args) - # Only call our hooks if there's data to process. Handlers that support - # hooks will return a dict of parsed arguments, which can be passed on - # to plugins and the like. For example, the JOIN handler will return - # something like: {'channel': '#whatever', 'users': ['UID1', 'UID2', - # 'UID3']}, etc. if parsed_args is not None: - # Always make sure TS is sent. - if 'ts' not in parsed_args: - parsed_args['ts'] = int(time.time()) - hook_cmd = command - if command in hook_map: - hook_cmd = hook_map[command] - log.debug('Parsed args %r received from %s handler (calling hook %s)', parsed_args, command, hook_cmd) - # Iterate over hooked functions, catching errors accordingly - for hook_func in utils.command_hooks[hook_cmd]: - try: - log.debug('Calling function %s', hook_func) - hook_func(irc, numeric, command, parsed_args) - except Exception: - # We don't want plugins to crash our servers... - traceback.print_exc() - continue + return [numeric, command, parsed_args] def spawnServer(irc, name, sid=None, uplink=None, desc='PyLink Server', endburst=True): # -> :0AL SERVER test.server * 1 0AM :some silly pseudoserver