|
1 | 1 | import logging
|
2 |
| -from typing import Dict, Tuple |
| 2 | +from typing import TYPE_CHECKING, Dict, Optional, Tuple |
3 | 3 |
|
4 | 4 | from sqlalchemy import Column, String, Table, UniqueConstraint
|
5 | 5 |
|
6 | 6 | from cloudbot import hook
|
7 | 7 | from cloudbot.util import database
|
8 | 8 |
|
| 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 | + |
9 | 15 | table = Table(
|
10 | 16 | "regex_chans",
|
11 | 17 | database.metadata,
|
@@ -84,33 +90,8 @@ def delete_status(db, conn, chan):
|
84 | 90 | load_cache(db)
|
85 | 91 |
|
86 | 92 |
|
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) |
114 | 95 |
|
115 | 96 |
|
116 | 97 | def parse_args(text: str, chan: str) -> str:
|
@@ -139,6 +120,34 @@ def change_status(db, event, status):
|
139 | 120 | set_status(db, event.conn.name, channel, status)
|
140 | 121 |
|
141 | 122 |
|
| 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 | + |
142 | 151 | @hook.command(autohelp=False, permissions=["botcontrol"])
|
143 | 152 | def enableregex(db, event):
|
144 | 153 | """[chan] - Enable regex hooks in [chan] (default: current channel)"""
|
@@ -172,15 +181,15 @@ def resetregex(text, db, conn, chan, nick, message, notice):
|
172 | 181 |
|
173 | 182 |
|
174 | 183 | @hook.command(autohelp=False, permissions=["botcontrol"])
|
175 |
| -def regexstatus(text, conn, chan): |
| 184 | +def regexstatus(text: str, conn: "Client", chan: str) -> str: |
176 | 185 | """[chan] - Get status of regex hooks in [chan] (default: current channel)"""
|
177 | 186 | channel = parse_args(text, chan)
|
178 |
| - status = status_cache.get((conn.name, chan), default_enabled) |
| 187 | + status = get_status(conn, channel) |
179 | 188 | return f"Regex status for {channel}: {ENABLED if status else DISABLED}"
|
180 | 189 |
|
181 | 190 |
|
182 | 191 | @hook.command(autohelp=False, permissions=["botcontrol"])
|
183 |
| -def listregex(conn): |
| 192 | +def listregex(conn: "Client") -> str: |
184 | 193 | """- List non-default regex statuses for channels"""
|
185 | 194 | values = []
|
186 | 195 | for (conn_name, chan), status in status_cache.items():
|
|
0 commit comments