Skip to content

Commit 9cf8537

Browse files
committed
Add client type filter for hooks
1 parent 755de8b commit 9cf8537

5 files changed

Lines changed: 16 additions & 6 deletions

File tree

cloudbot/bot.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ def add_hook(hook, _event, _run_before=False):
239239
if halted:
240240
return False
241241

242+
if hook.clients and _event.conn.type not in hook.clients:
243+
return True
244+
242245
coro = self.plugin_manager.launch(hook, _event)
243246
if _run_before:
244247
run_before_tasks.append(coro)

cloudbot/clients/irc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,15 @@ def describe_server(self):
104104

105105
@asyncio.coroutine
106106
def try_connect(self):
107-
timeout = self.config["connection"].get("timeout", 30)
108107
while True:
109108
try:
110-
yield from self.connect(timeout)
109+
yield from self.connect(self.timeout)
111110
except (asyncio.TimeoutError, OSError):
112111
logger.exception("[%s] Error occurred while connecting", self.name)
113112
else:
114113
break
115114

116-
yield from asyncio.sleep(random.randrange(timeout))
115+
yield from asyncio.sleep(random.randrange(self.timeout))
117116

118117
@asyncio.coroutine
119118
def connect(self, timeout=None):
@@ -148,6 +147,7 @@ def connect(self, timeout=None):
148147
tasks = [
149148
self.bot.plugin_manager.launch(hook, Event(bot=self.bot, conn=self, hook=hook))
150149
for hook in self.bot.plugin_manager.connect_hooks
150+
if not hook.clients or self.type in hook.clients
151151
]
152152
# TODO stop connecting if a connect hook fails?
153153
yield from asyncio.gather(*tasks)

cloudbot/plugin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,13 @@ def __init__(self, _type, plugin, func_hook):
730730
self.action = func_hook.kwargs.pop("action", Action.CONTINUE)
731731
self.priority = func_hook.kwargs.pop("priority", Priority.NORMAL)
732732

733+
clients = func_hook.kwargs.pop("clients", [])
734+
735+
if isinstance(clients, str):
736+
clients = [clients]
737+
738+
self.clients = clients
739+
733740
if func_hook.kwargs:
734741
# we should have popped all the args, so warn if there are any left
735742
logger.warning("Ignoring extra args {} from {}".format(func_hook.kwargs, self.description))

plugins/core/cap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from cloudbot.util.parsers.irc import CapList
99

1010

11-
@hook.connect(priority=-10)
11+
@hook.connect(priority=-10, clients="irc")
1212
def send_cap_ls(conn):
1313
conn.cmd("CAP", "LS", "302")
1414
conn.memory.setdefault("available_caps", set()).clear()

plugins/core/core_connect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from cloudbot import hook
22

33

4-
@hook.connect(priority=0)
4+
@hook.connect(priority=0, clients="irc")
55
def conn_pass(conn):
66
conn.set_pass(conn.config["connection"].get("password"))
77

@@ -11,7 +11,7 @@ def conn_nick(conn):
1111
conn.set_nick(conn.nick)
1212

1313

14-
@hook.connect(priority=20)
14+
@hook.connect(priority=20, clients="irc")
1515
def conn_user(conn):
1616
conn.cmd(
1717
"USER", conn.config.get('user', 'cloudbot'), "3", "*",

0 commit comments

Comments
 (0)