Skip to content

Commit

Permalink
Add more tests for regex_chans
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed Mar 17, 2024
1 parent a1cb452 commit 85474e7
Show file tree
Hide file tree
Showing 2 changed files with 698 additions and 31 deletions.
71 changes: 40 additions & 31 deletions plugins/core/regex_chans.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import logging
from typing import Dict, Tuple
from typing import TYPE_CHECKING, Dict, Optional, Tuple

from sqlalchemy import Column, String, Table, UniqueConstraint

from cloudbot import hook
from cloudbot.util import database

if TYPE_CHECKING:
from cloudbot.bot import CloudBot
from cloudbot.client import Client
from cloudbot.event import Event
from cloudbot.plugin_hooks import Hook

table = Table(
"regex_chans",
database.metadata,
Expand Down Expand Up @@ -84,33 +90,8 @@ def delete_status(db, conn, chan):
load_cache(db)


@hook.sieve()
def sieve_regex(bot, event, _hook):
if (
_hook.type == "regex"
and event.chan.startswith("#")
and _hook.plugin.title != "factoids"
):
status = status_cache.get(
(event.conn.name, event.chan), default_enabled
)
if not status:
logger.info(
"[%s] Denying %s from %s",
event.conn.name,
_hook.function_name,
event.chan,
)
return None

logger.info(
"[%s] Allowing %s to %s",
event.conn.name,
_hook.function_name,
event.chan,
)

return event
def get_status(conn: "Client", chan: str) -> bool:
return status_cache.get((conn.name, chan), default_enabled)


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


@hook.sieve()
def sieve_regex(
bot: "CloudBot", event: "Event", _hook: "Hook"
) -> Optional["Event"]:
if (
_hook.type == "regex"
and event.chan.startswith("#")
and _hook.plugin.title != "factoids"
):
if not get_status(event.conn, event.chan):
logger.info(
"[%s] Denying %s from %s",
event.conn.name,
_hook.function_name,
event.chan,
)
return None

logger.info(
"[%s] Allowing %s to %s",
event.conn.name,
_hook.function_name,
event.chan,
)

return event


@hook.command(autohelp=False, permissions=["botcontrol"])
def enableregex(db, event):
"""[chan] - Enable regex hooks in [chan] (default: current channel)"""
Expand Down Expand Up @@ -172,15 +181,15 @@ def resetregex(text, db, conn, chan, nick, message, notice):


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


@hook.command(autohelp=False, permissions=["botcontrol"])
def listregex(conn):
def listregex(conn: "Client") -> str:
"""- List non-default regex statuses for channels"""
values = []
for (conn_name, chan), status in status_cache.items():
Expand Down
Loading

0 comments on commit 85474e7

Please sign in to comment.