Change modules/line_handler.py to use @Utils.hook
This commit is contained in:
parent
31c19aec69
commit
300eadf9b6
1 changed files with 35 additions and 43 deletions
|
@ -1,5 +1,5 @@
|
||||||
import re, threading
|
import re, threading
|
||||||
from src import Utils
|
from src import ModuleManager, 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,48 +13,8 @@ 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 Module(object):
|
class Module(ModuleManager.BaseModule):
|
||||||
def __init__(self, bot, events, exports):
|
@Utils.hook("raw")
|
||||||
self.bot = bot
|
|
||||||
self.events = events
|
|
||||||
|
|
||||||
events.on("raw").hook(self.handle)
|
|
||||||
|
|
||||||
events.on("raw.001").hook(self.handle_001, default_event=True)
|
|
||||||
events.on("raw.005").hook(self.handle_005)
|
|
||||||
events.on("raw.311").hook(self.handle_311, default_event=True)
|
|
||||||
events.on("raw.332").hook(self.handle_332)
|
|
||||||
events.on("raw.333").hook(self.handle_333)
|
|
||||||
events.on("raw.353").hook(self.handle_353, default_event=True)
|
|
||||||
events.on("raw.366").hook(self.handle_366, default_event=True)
|
|
||||||
events.on("raw.421").hook(self.handle_421, default_event=True)
|
|
||||||
events.on("raw.352").hook(self.handle_352, default_event=True)
|
|
||||||
events.on("raw.354").hook(self.handle_354, default_event=True)
|
|
||||||
events.on("raw.324").hook(self.handle_324, default_event=True)
|
|
||||||
events.on("raw.329").hook(self.handle_329, 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.PING").hook(self.ping)
|
|
||||||
events.on("raw.JOIN").hook(self.join)
|
|
||||||
events.on("raw.PART").hook(self.part)
|
|
||||||
events.on("raw.QUIT").hook(self.quit)
|
|
||||||
events.on("raw.NICK").hook(self.nick)
|
|
||||||
events.on("raw.MODE").hook(self.mode)
|
|
||||||
events.on("raw.KICK").hook(self.kick)
|
|
||||||
events.on("raw.INVITE").hook(self.invite)
|
|
||||||
events.on("raw.TOPIC").hook(self.topic)
|
|
||||||
events.on("raw.PRIVMSG").hook(self.privmsg)
|
|
||||||
events.on("raw.NOTICE").hook(self.notice)
|
|
||||||
|
|
||||||
events.on("raw.CAP").hook(self.cap)
|
|
||||||
events.on("raw.AUTHENTICATE").hook(self.authenticate)
|
|
||||||
events.on("raw.CHGHOST").hook(self.chghost)
|
|
||||||
events.on("raw.ACCOUNT").hook(self.account)
|
|
||||||
events.on("raw.TAGMSG").hook(self.tagmsg)
|
|
||||||
events.on("raw.AWAY").hook(self.away)
|
|
||||||
events.on("raw.BATCH").hook(self.batch)
|
|
||||||
|
|
||||||
def handle(self, event):
|
def handle(self, event):
|
||||||
line = original_line = event["line"]
|
line = original_line = event["line"]
|
||||||
tags = {}
|
tags = {}
|
||||||
|
@ -114,16 +74,19 @@ class Module(object):
|
||||||
last=last)
|
last=last)
|
||||||
|
|
||||||
# ping from the server
|
# ping from the server
|
||||||
|
@Utils.hook("raw.ping")
|
||||||
def ping(self, event):
|
def ping(self, event):
|
||||||
event["server"].send_pong(event["last"])
|
event["server"].send_pong(event["last"])
|
||||||
|
|
||||||
# first numeric line the server sends
|
# first numeric line the server sends
|
||||||
|
@Utils.hook("raw.001", default_event=True)
|
||||||
def handle_001(self, event):
|
def handle_001(self, event):
|
||||||
event["server"].name = event["prefix"].nickname
|
event["server"].name = event["prefix"].nickname
|
||||||
event["server"].set_own_nickname(event["args"][0])
|
event["server"].set_own_nickname(event["args"][0])
|
||||||
event["server"].send_whois(event["server"].nickname)
|
event["server"].send_whois(event["server"].nickname)
|
||||||
|
|
||||||
# server telling us what it supports
|
# server telling us what it supports
|
||||||
|
@Utils.hook("raw.005")
|
||||||
def handle_005(self, event):
|
def handle_005(self, event):
|
||||||
isupport_line = " ".join(event["args"][1:])
|
isupport_line = " ".join(event["args"][1:])
|
||||||
|
|
||||||
|
@ -153,6 +116,7 @@ class Module(object):
|
||||||
isupport=isupport_line, server=event["server"])
|
isupport=isupport_line, server=event["server"])
|
||||||
|
|
||||||
# whois respose (nickname, username, realname, hostname)
|
# whois respose (nickname, username, realname, hostname)
|
||||||
|
@Utils.hook("raw.311", default_event=True)
|
||||||
def handle_311(self, event):
|
def handle_311(self, event):
|
||||||
nickname = event["args"][1]
|
nickname = event["args"][1]
|
||||||
if event["server"].is_own_nickname(nickname):
|
if event["server"].is_own_nickname(nickname):
|
||||||
|
@ -164,6 +128,7 @@ class Module(object):
|
||||||
target.realname = event["arbitrary"]
|
target.realname = event["arbitrary"]
|
||||||
|
|
||||||
# on-join channel topic line
|
# on-join channel topic line
|
||||||
|
@Utils.hook("raw.332")
|
||||||
def handle_332(self, event):
|
def handle_332(self, event):
|
||||||
channel = event["server"].get_channel(event["args"][1])
|
channel = event["server"].get_channel(event["args"][1])
|
||||||
|
|
||||||
|
@ -172,6 +137,7 @@ class Module(object):
|
||||||
server=event["server"], topic=event["arbitrary"])
|
server=event["server"], topic=event["arbitrary"])
|
||||||
|
|
||||||
# channel topic changed
|
# channel topic changed
|
||||||
|
@Utils.hook("raw.topic")
|
||||||
def topic(self, event):
|
def topic(self, event):
|
||||||
user = event["server"].get_user(event["prefix"].nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
channel = event["server"].get_channel(event["args"][0])
|
channel = event["server"].get_channel(event["args"][0])
|
||||||
|
@ -180,6 +146,7 @@ class Module(object):
|
||||||
server=event["server"], topic=event["arbitrary"], user=user)
|
server=event["server"], topic=event["arbitrary"], user=user)
|
||||||
|
|
||||||
# on-join channel topic set by/at
|
# on-join channel topic set by/at
|
||||||
|
@Utils.hook("raw.333")
|
||||||
def handle_333(self, event):
|
def handle_333(self, event):
|
||||||
channel = event["server"].get_channel(event["args"][1])
|
channel = event["server"].get_channel(event["args"][1])
|
||||||
|
|
||||||
|
@ -196,6 +163,7 @@ class Module(object):
|
||||||
server=event["server"])
|
server=event["server"])
|
||||||
|
|
||||||
# /names response, also on-join user list
|
# /names response, also on-join user list
|
||||||
|
@Utils.hook("raw.353", default_event=True)
|
||||||
def handle_353(self, event):
|
def handle_353(self, event):
|
||||||
channel = event["server"].get_channel(event["args"][2])
|
channel = event["server"].get_channel(event["args"][2])
|
||||||
nicknames = event["arbitrary"].split()
|
nicknames = event["arbitrary"].split()
|
||||||
|
@ -221,10 +189,12 @@ class Module(object):
|
||||||
channel.add_mode(mode, nickname)
|
channel.add_mode(mode, nickname)
|
||||||
|
|
||||||
# on-join user list has finished
|
# on-join user list has finished
|
||||||
|
@Utils.hook("raw.366", default_event=True)
|
||||||
def handle_366(self, event):
|
def handle_366(self, event):
|
||||||
event["server"].send_whox(event["args"][1], "n", "ahnrtu", "111")
|
event["server"].send_whox(event["args"][1], "n", "ahnrtu", "111")
|
||||||
|
|
||||||
# on user joining channel
|
# on user joining channel
|
||||||
|
@Utils.hook("raw.join")
|
||||||
def join(self, event):
|
def join(self, event):
|
||||||
account = None
|
account = None
|
||||||
realname = None
|
realname = None
|
||||||
|
@ -262,6 +232,7 @@ class Module(object):
|
||||||
channel.send_mode()
|
channel.send_mode()
|
||||||
|
|
||||||
# on user parting channel
|
# on user parting channel
|
||||||
|
@Utils.hook("raw.part")
|
||||||
def part(self, event):
|
def part(self, event):
|
||||||
channel = event["server"].get_channel(event["args"][0])
|
channel = event["server"].get_channel(event["args"][0])
|
||||||
reason = event["arbitrary"] or ""
|
reason = event["arbitrary"] or ""
|
||||||
|
@ -280,10 +251,12 @@ class Module(object):
|
||||||
event["server"].remove_channel(channel)
|
event["server"].remove_channel(channel)
|
||||||
|
|
||||||
# unknown command sent by us, oops!
|
# unknown command sent by us, oops!
|
||||||
|
@Utils.hook("raw.421", default_event=True)
|
||||||
def handle_421(self, event):
|
def handle_421(self, event):
|
||||||
print("warning: unknown command '%s'." % event["args"][1])
|
print("warning: unknown command '%s'." % event["args"][1])
|
||||||
|
|
||||||
# a user has disconnected!
|
# a user has disconnected!
|
||||||
|
@Utils.hook("raw.quit")
|
||||||
def quit(self, event):
|
def quit(self, event):
|
||||||
reason = event["arbitrary"] or ""
|
reason = event["arbitrary"] or ""
|
||||||
|
|
||||||
|
@ -296,6 +269,7 @@ class Module(object):
|
||||||
event["server"].disconnect()
|
event["server"].disconnect()
|
||||||
|
|
||||||
# the server is telling us about its capabilities!
|
# the server is telling us about its capabilities!
|
||||||
|
@Utils.hook("raw.cap")
|
||||||
def cap(self, event):
|
def cap(self, event):
|
||||||
capabilities_list = (event["arbitrary"] or "").split(" ")
|
capabilities_list = (event["arbitrary"] or "").split(" ")
|
||||||
capabilities = {}
|
capabilities = {}
|
||||||
|
@ -346,11 +320,13 @@ class Module(object):
|
||||||
event["server"].send_capability_end()
|
event["server"].send_capability_end()
|
||||||
|
|
||||||
# the server is asking for authentication
|
# the server is asking for authentication
|
||||||
|
@Utils.hook("raw.authenticate")
|
||||||
def authenticate(self, event):
|
def authenticate(self, event):
|
||||||
self.events.on("received.authenticate").call(
|
self.events.on("received.authenticate").call(
|
||||||
message=event["args"][0], server=event["server"])
|
message=event["args"][0], server=event["server"])
|
||||||
|
|
||||||
# someone has changed their nickname
|
# someone has changed their nickname
|
||||||
|
@Utils.hook("raw.nick")
|
||||||
def nick(self, event):
|
def nick(self, event):
|
||||||
new_nickname = event["arbitrary"]
|
new_nickname = event["arbitrary"]
|
||||||
if not event["server"].is_own_nickname(event["prefix"].nickname):
|
if not event["server"].is_own_nickname(event["prefix"].nickname):
|
||||||
|
@ -369,6 +345,7 @@ class Module(object):
|
||||||
new_nickname=new_nickname, old_nickname=old_nickname)
|
new_nickname=new_nickname, old_nickname=old_nickname)
|
||||||
|
|
||||||
# something's mode has changed
|
# something's mode has changed
|
||||||
|
@Utils.hook("raw.mode")
|
||||||
def mode(self, event):
|
def mode(self, event):
|
||||||
user = event["server"].get_user(event["prefix"].nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
target = event["args"][0]
|
target = event["args"][0]
|
||||||
|
@ -402,6 +379,7 @@ class Module(object):
|
||||||
server=event["server"])
|
server=event["server"])
|
||||||
|
|
||||||
# someone (maybe me!) has been invited somewhere
|
# someone (maybe me!) has been invited somewhere
|
||||||
|
@Utils.hook("raw.invite")
|
||||||
def invite(self, event):
|
def invite(self, event):
|
||||||
target_channel = event["last"]
|
target_channel = event["last"]
|
||||||
user = event["server"].get_user(event["prefix"].nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
|
@ -411,6 +389,7 @@ class Module(object):
|
||||||
target_user=target_user)
|
target_user=target_user)
|
||||||
|
|
||||||
# we've received a message
|
# we've received a message
|
||||||
|
@Utils.hook("raw.privmsg")
|
||||||
def privmsg(self, event):
|
def privmsg(self, event):
|
||||||
user = event["server"].get_user(event["prefix"].nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
message = event["arbitrary"] or ""
|
message = event["arbitrary"] or ""
|
||||||
|
@ -442,6 +421,7 @@ class Module(object):
|
||||||
user.buffer.add_line(user.nickname, message, action)
|
user.buffer.add_line(user.nickname, message, action)
|
||||||
|
|
||||||
# we've received a notice
|
# we've received a notice
|
||||||
|
@Utils.hook("raw.notice")
|
||||||
def notice(self, event):
|
def notice(self, event):
|
||||||
message = event["arbitrary"] or ""
|
message = event["arbitrary"] or ""
|
||||||
message_split = message.split(" ")
|
message_split = message.split(" ")
|
||||||
|
@ -470,6 +450,7 @@ class Module(object):
|
||||||
server=event["server"], tags=event["tags"])
|
server=event["server"], tags=event["tags"])
|
||||||
|
|
||||||
# IRCv3 TAGMSG, used to send tags without any other information
|
# IRCv3 TAGMSG, used to send tags without any other information
|
||||||
|
@Utils.hook("raw.tagmsg")
|
||||||
def tagmsg(self, event):
|
def tagmsg(self, event):
|
||||||
user = event["server"].get_user(event["prefix"].nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
target = event["args"][0]
|
target = event["args"][0]
|
||||||
|
@ -483,6 +464,7 @@ class Module(object):
|
||||||
user=user, tags=event["tags"], server=event["server"])
|
user=user, tags=event["tags"], server=event["server"])
|
||||||
|
|
||||||
# IRCv3 AWAY, used to notify us that a client we can see has changed /away
|
# IRCv3 AWAY, used to notify us that a client we can see has changed /away
|
||||||
|
@Utils.hook("raw.away")
|
||||||
def away(self, event):
|
def away(self, event):
|
||||||
user = event["server"].get_user(event["prefix"].nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
message = event["arbitrary"]
|
message = event["arbitrary"]
|
||||||
|
@ -495,6 +477,7 @@ class Module(object):
|
||||||
self.events.on("received.away.off").call(user=user,
|
self.events.on("received.away.off").call(user=user,
|
||||||
server=event["server"])
|
server=event["server"])
|
||||||
|
|
||||||
|
@Utils.hook("raw.batch")
|
||||||
def batch(self, event):
|
def batch(self, event):
|
||||||
identifier = event["args"][0]
|
identifier = event["args"][0]
|
||||||
modifier, identifier = identifier[0], identifier[1:]
|
modifier, identifier = identifier[0], identifier[1:]
|
||||||
|
@ -507,6 +490,7 @@ class Module(object):
|
||||||
self.handle(event["server"], line)
|
self.handle(event["server"], line)
|
||||||
|
|
||||||
# IRCv3 CHGHOST, a user's username and/or hostname has changed
|
# IRCv3 CHGHOST, a user's username and/or hostname has changed
|
||||||
|
@Utils.hook("raw.chghost")
|
||||||
def chghost(self, event):
|
def chghost(self, event):
|
||||||
username = event["args"][0]
|
username = event["args"][0]
|
||||||
hostname = event["args"][1]
|
hostname = event["args"][1]
|
||||||
|
@ -518,6 +502,7 @@ class Module(object):
|
||||||
target.username = username
|
target.username = username
|
||||||
target.hostname = hostname
|
target.hostname = hostname
|
||||||
|
|
||||||
|
@Utils.hook("raw.account")
|
||||||
def account(self, event):
|
def account(self, event):
|
||||||
user = event["server"].get_user(event["prefix"].nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
|
|
||||||
|
@ -534,11 +519,13 @@ class Module(object):
|
||||||
server=event["server"])
|
server=event["server"])
|
||||||
|
|
||||||
# response to a WHO command for user information
|
# response to a WHO command for user information
|
||||||
|
@Utils.hook("raw.352", default_event=True)
|
||||||
def handle_352(self, event):
|
def handle_352(self, event):
|
||||||
user = event["server"].get_user(event["args"][5])
|
user = event["server"].get_user(event["args"][5])
|
||||||
user.username = event["args"][2]
|
user.username = event["args"][2]
|
||||||
user.hostname = event["args"][3]
|
user.hostname = event["args"][3]
|
||||||
# response to a WHOX command for user information, including account name
|
# response to a WHOX command for user information, including account name
|
||||||
|
@Utils.hook("raw.354", default_event=True)
|
||||||
def handle_354(self, event):
|
def handle_354(self, event):
|
||||||
if event["args"][1] == "111":
|
if event["args"][1] == "111":
|
||||||
username = event["args"][2]
|
username = event["args"][2]
|
||||||
|
@ -555,6 +542,7 @@ class Module(object):
|
||||||
user.identified_account = account
|
user.identified_account = account
|
||||||
|
|
||||||
# response to an empty mode command
|
# response to an empty mode command
|
||||||
|
@Utils.hook("raw.324", default_event=True)
|
||||||
def handle_324(self, event):
|
def handle_324(self, event):
|
||||||
channel = event["server"].get_channel(event["args"][1])
|
channel = event["server"].get_channel(event["args"][1])
|
||||||
modes = event["args"][2]
|
modes = event["args"][2]
|
||||||
|
@ -564,15 +552,18 @@ class Module(object):
|
||||||
channel.add_mode(mode)
|
channel.add_mode(mode)
|
||||||
|
|
||||||
# channel creation unix timestamp
|
# channel creation unix timestamp
|
||||||
|
@Utils.hook("raw.329", default_event=True)
|
||||||
def handle_329(self, event):
|
def handle_329(self, event):
|
||||||
channel = event["server"].get_channel(event["args"][1])
|
channel = event["server"].get_channel(event["args"][1])
|
||||||
channel.creation_timestamp = int(event["args"][2])
|
channel.creation_timestamp = int(event["args"][2])
|
||||||
|
|
||||||
# nickname already in use
|
# nickname already in use
|
||||||
|
@Utils.hook("raw.433", default_event=True)
|
||||||
def handle_433(self, event):
|
def handle_433(self, event):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# we need a registered nickname for this channel
|
# we need a registered nickname for this channel
|
||||||
|
@Utils.hook("raw.477", default_event=True)
|
||||||
def handle_477(self, event):
|
def handle_477(self, event):
|
||||||
channel_name = Utils.irc_lower(event["server"], event["args"][1])
|
channel_name = Utils.irc_lower(event["server"], event["args"][1])
|
||||||
if channel_name in event["server"]:
|
if channel_name in event["server"]:
|
||||||
|
@ -581,6 +572,7 @@ class Module(object):
|
||||||
server_id=event["server"].id)
|
server_id=event["server"].id)
|
||||||
|
|
||||||
# someone's been kicked from a channel
|
# someone's been kicked from a channel
|
||||||
|
@Utils.hook("raw.kick")
|
||||||
def kick(self, event):
|
def kick(self, event):
|
||||||
user = event["server"].get_user(event["prefix"].nickname)
|
user = event["server"].get_user(event["prefix"].nickname)
|
||||||
target = event["args"][1]
|
target = event["args"][1]
|
||||||
|
|
Loading…
Reference in a new issue