Move src/IRCLineHandler.py to modules/line_handler.py

This commit is contained in:
jesopo 2018-09-30 19:43:20 +01:00
parent 1f2c2b719b
commit c87728a87d
4 changed files with 23 additions and 21 deletions

View file

@ -1,5 +1,5 @@
import re, threading import re, threading
from . import Utils from src import Utils
RE_PREFIXES = re.compile(r"\bPREFIX=\((\w+)\)(\W+)(?:\b|$)") RE_PREFIXES = re.compile(r"\bPREFIX=\((\w+)\)(\W+)(?:\b|$)")
RE_CHANMODES = re.compile( RE_CHANMODES = re.compile(
@ -13,11 +13,12 @@ CAPABILITIES = {"multi-prefix", "chghost", "invite-notify", "account-tag",
"draft/message-tags-0.2", "server-time", "cap-notify", "draft/message-tags-0.2", "server-time", "cap-notify",
"batch", "draft/labeled-response"} "batch", "draft/labeled-response"}
class LineHandler(object): class Module(object):
def __init__(self, events, timers): def __init__(self, bot, events, exports):
self.bot = bot
self.events = events self.events = events
self.timers = timers
events.on("raw.PING").hook(self.ping) events.on("raw").hook(self.handle)
events.on("raw.001").hook(self.handle_001, default_event=True) events.on("raw.001").hook(self.handle_001, default_event=True)
events.on("raw.005").hook(self.handle_005) events.on("raw.005").hook(self.handle_005)
@ -34,6 +35,7 @@ class LineHandler(object):
events.on("raw.433").hook(self.handle_433, default_event=True) events.on("raw.433").hook(self.handle_433, default_event=True)
events.on("raw.477").hook(self.handle_477, default_event=True) events.on("raw.477").hook(self.handle_477, default_event=True)
events.on("raw.PING").hook(self.ping)
events.on("raw.JOIN").hook(self.join) events.on("raw.JOIN").hook(self.join)
events.on("raw.PART").hook(self.part) events.on("raw.PART").hook(self.part)
events.on("raw.QUIT").hook(self.quit) events.on("raw.QUIT").hook(self.quit)
@ -53,8 +55,8 @@ class LineHandler(object):
events.on("raw.AWAY").hook(self.away) events.on("raw.AWAY").hook(self.away)
events.on("raw.BATCH").hook(self.batch) events.on("raw.BATCH").hook(self.batch)
def handle(self, server, line): def handle(self, event):
original_line = line line = original_line = event["line"]
tags = {} tags = {}
prefix = None prefix = None
command = None command = None
@ -65,7 +67,7 @@ class LineHandler(object):
if tag: if tag:
tag_split = tag.split("=", 1) tag_split = tag.split("=", 1)
tags[tag_split[0]] = "".join(tag_split[1:]) tags[tag_split[0]] = "".join(tag_split[1:])
if "batch" in tags and tags["batch"] in server.batches: if "batch" in tags and tags["batch"] in event["server"].batches:
server.batches[tag["batch"]].append(line) server.batches[tag["batch"]].append(line)
return return
@ -96,17 +98,20 @@ class LineHandler(object):
last = arbitrary or args[-1] last = arbitrary or args[-1]
#server, prefix, command, args, arbitrary #server, prefix, command, args, arbitrary
self.events.on("raw").on(command).call(server=server, last=last, self.events.on("raw").on(command).call(server=event["server"],
prefix=prefix, args=args, arbitrary=arbitrary, tags=tags) last=last, prefix=prefix, args=args, arbitrary=arbitrary,
tags=tags)
if default_event or not hooks: if default_event or not hooks:
if command.isdigit(): if command.isdigit():
self.events.on("received.numeric").on(command).call( self.events.on("received.numeric").on(command).call(
line=original_line, server=server, tags=tags, last=last, line=original_line, server=event["server"], tags=tags,
line_split=original_line.split(" "), number=command) last=last, line_split=original_line.split(" "),
number=command)
else: else:
self.events.on("received").on(command).call( self.events.on("received").on(command).call(
line=original_line, line_split=original_line.split(" "), line=original_line, line_split=original_line.split(" "),
command=command, server=server, tags=tags, last=last) command=command, server=event["server"], tags=tags,
last=last)
# ping from the server # ping from the server
def ping(self, event): def ping(self, event):

View file

@ -1,10 +1,9 @@
import os, select, sys, threading, time, traceback, uuid import os, select, sys, threading, time, traceback, uuid
from . import EventManager, Exports, IRCLineHandler, IRCServer, Logging from . import EventManager, Exports, IRCServer, Logging, ModuleManager
from . import ModuleManager
class Bot(object): class Bot(object):
def __init__(self, directory, args, cache, config, database, events, def __init__(self, directory, args, cache, config, database, events,
exports, line_handler, log, modules, timers): exports, log, modules, timers):
self.directory = directory self.directory = directory
self.args = args self.args = args
self.cache = cache self.cache = cache
@ -12,7 +11,6 @@ class Bot(object):
self.database = database self.database = database
self._events = events self._events = events
self._exports = exports self._exports = exports
self.line_handler = line_handler
self.log = log self.log = log
self.modules = modules self.modules = modules
self.timers = timers self.timers = timers

View file

@ -198,7 +198,7 @@ class Server(object):
def parse_line(self, line): def parse_line(self, line):
if not line: if not line:
return return
self.bot.line_handler.handle(self, line) self.events.on("raw").call(server=self, line=line)
self.check_users() self.check_users()
def check_users(self): def check_users(self):
for user in self.new_users: for user in self.new_users:

View file

@ -2,7 +2,7 @@
import argparse, os, sys, time import argparse, os, sys, time
from src import Cache, Config, Database, EventManager, Exports, IRCBot from src import Cache, Config, Database, EventManager, Exports, IRCBot
from src import IRCLineHandler, Logging, ModuleManager, Timers from src import Logging, ModuleManager, Timers
def bool_input(s): def bool_input(s):
result = input("%s (Y/n): " % s) result = input("%s (Y/n): " % s)
@ -37,12 +37,11 @@ database = Database.Database(log, args.database)
events = events = EventManager.EventHook(log) events = events = EventManager.EventHook(log)
exports = exports = Exports.Exports() exports = exports = Exports.Exports()
timers = Timers.Timers(database, events, log) timers = Timers.Timers(database, events, log)
line_handler = IRCLineHandler.LineHandler(events, timers)
modules = modules = ModuleManager.ModuleManager(events, exports, config, log, modules = modules = ModuleManager.ModuleManager(events, exports, config, log,
os.path.join(directory, "modules")) os.path.join(directory, "modules"))
bot = IRCBot.Bot(directory, args, cache, config, database, events, bot = IRCBot.Bot(directory, args, cache, config, database, events,
exports, line_handler, log, modules, timers) exports, log, modules, timers)
whitelist = bot.get_setting("module-whitelist", []) whitelist = bot.get_setting("module-whitelist", [])
blacklist = bot.get_setting("module-blacklist", []) blacklist = bot.get_setting("module-blacklist", [])