Skip to content

Commit 85474e7

Browse files
committed
Add more tests for regex_chans
1 parent a1cb452 commit 85474e7

File tree

2 files changed

+698
-31
lines changed

2 files changed

+698
-31
lines changed

plugins/core/regex_chans.py

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import logging
2-
from typing import Dict, Tuple
2+
from typing import TYPE_CHECKING, Dict, Optional, Tuple
33

44
from sqlalchemy import Column, String, Table, UniqueConstraint
55

66
from cloudbot import hook
77
from cloudbot.util import database
88

9+
if TYPE_CHECKING:
10+
from cloudbot.bot import CloudBot
11+
from cloudbot.client import Client
12+
from cloudbot.event import Event
13+
from cloudbot.plugin_hooks import Hook
14+
915
table = Table(
1016
"regex_chans",
1117
database.metadata,
@@ -84,33 +90,8 @@ def delete_status(db, conn, chan):
8490
load_cache(db)
8591

8692

87-
@hook.sieve()
88-
def sieve_regex(bot, event, _hook):
89-
if (
90-
_hook.type == "regex"
91-
and event.chan.startswith("#")
92-
and _hook.plugin.title != "factoids"
93-
):
94-
status = status_cache.get(
95-
(event.conn.name, event.chan), default_enabled
96-
)
97-
if not status:
98-
logger.info(
99-
"[%s] Denying %s from %s",
100-
event.conn.name,
101-
_hook.function_name,
102-
event.chan,
103-
)
104-
return None
105-
106-
logger.info(
107-
"[%s] Allowing %s to %s",
108-
event.conn.name,
109-
_hook.function_name,
110-
event.chan,
111-
)
112-
113-
return event
93+
def get_status(conn: "Client", chan: str) -> bool:
94+
return status_cache.get((conn.name, chan), default_enabled)
11495

11596

11697
def parse_args(text: str, chan: str) -> str:
@@ -139,6 +120,34 @@ def change_status(db, event, status):
139120
set_status(db, event.conn.name, channel, status)
140121

141122

123+
@hook.sieve()
124+
def sieve_regex(
125+
bot: "CloudBot", event: "Event", _hook: "Hook"
126+
) -> Optional["Event"]:
127+
if (
128+
_hook.type == "regex"
129+
and event.chan.startswith("#")
130+
and _hook.plugin.title != "factoids"
131+
):
132+
if not get_status(event.conn, event.chan):
133+
logger.info(
134+
"[%s] Denying %s from %s",
135+
event.conn.name,
136+
_hook.function_name,
137+
event.chan,
138+
)
139+
return None
140+
141+
logger.info(
142+
"[%s] Allowing %s to %s",
143+
event.conn.name,
144+
_hook.function_name,
145+
event.chan,
146+
)
147+
148+
return event
149+
150+
142151
@hook.command(autohelp=False, permissions=["botcontrol"])
143152
def enableregex(db, event):
144153
"""[chan] - Enable regex hooks in [chan] (default: current channel)"""
@@ -172,15 +181,15 @@ def resetregex(text, db, conn, chan, nick, message, notice):
172181

173182

174183
@hook.command(autohelp=False, permissions=["botcontrol"])
175-
def regexstatus(text, conn, chan):
184+
def regexstatus(text: str, conn: "Client", chan: str) -> str:
176185
"""[chan] - Get status of regex hooks in [chan] (default: current channel)"""
177186
channel = parse_args(text, chan)
178-
status = status_cache.get((conn.name, chan), default_enabled)
187+
status = get_status(conn, channel)
179188
return f"Regex status for {channel}: {ENABLED if status else DISABLED}"
180189

181190

182191
@hook.command(autohelp=False, permissions=["botcontrol"])
183-
def listregex(conn):
192+
def listregex(conn: "Client") -> str:
184193
"""- List non-default regex statuses for channels"""
185194
values = []
186195
for (conn_name, chan), status in status_cache.items():

0 commit comments

Comments
 (0)