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 ,
15
21
UniqueConstraint ("connection" , "channel" ),
16
22
)
17
23
24
+ ENABLED = "ENABLED"
25
+ DISABLED = "DISABLED"
26
+
18
27
# Default value.
19
28
# If True, all channels without a setting will have regex enabled
20
29
# If False, all channels without a setting will have regex disabled
21
30
default_enabled = True
22
- status_cache : Dict [Tuple [str , str ], str ] = {}
31
+ status_cache : Dict [Tuple [str , str ], bool ] = {}
23
32
logger = logging .getLogger ("cloudbot" )
24
33
25
34
@@ -30,27 +39,44 @@ def load_cache(db):
30
39
conn = row ["connection" ]
31
40
chan = row ["channel" ]
32
41
status = row ["status" ]
33
- new_cache [(conn , chan )] = status
42
+ if status == ENABLED :
43
+ value = True
44
+ elif status == DISABLED :
45
+ value = False
46
+ else :
47
+ # Unknown values just use the default (existing behavior)
48
+ logger .warning (
49
+ "[regex_chans] Unknown status: %s, falling back to default" ,
50
+ tuple (row ),
51
+ )
52
+ continue
53
+
54
+ new_cache [(conn , chan )] = value
34
55
35
56
status_cache .clear ()
36
57
status_cache .update (new_cache )
37
58
38
59
39
- def set_status (db , conn , chan , status ):
60
+ def set_status (db , conn , chan , status : bool ):
61
+ status_value = ENABLED if status else DISABLED
40
62
if (conn , chan ) in status_cache :
41
63
# if we have a set value, update
42
64
db .execute (
43
65
table .update ()
44
- .values (status = status )
66
+ .values (status = status_value )
45
67
.where (table .c .connection == conn )
46
68
.where (table .c .channel == chan )
47
69
)
48
70
else :
49
71
# otherwise, insert
50
72
db .execute (
51
- table .insert ().values (connection = conn , channel = chan , status = status )
73
+ table .insert ().values (
74
+ connection = conn , channel = chan , status = status_value
75
+ )
52
76
)
77
+
53
78
db .commit ()
79
+ load_cache (db )
54
80
55
81
56
82
def delete_status (db , conn , chan ):
@@ -59,27 +85,59 @@ def delete_status(db, conn, chan):
59
85
.where (table .c .connection == conn )
60
86
.where (table .c .channel == chan )
61
87
)
88
+
62
89
db .commit ()
90
+ load_cache (db )
91
+
92
+
93
+ def get_status (conn : "Client" , chan : str ) -> bool :
94
+ return status_cache .get ((conn .name , chan ), default_enabled )
95
+
96
+
97
+ def parse_args (text : str , chan : str ) -> str :
98
+ text = text .strip ().lower ()
99
+ if not text :
100
+ channel = chan
101
+ elif text .startswith ("#" ):
102
+ channel = text
103
+ else :
104
+ channel = f"#{ text } "
105
+
106
+ return channel
107
+
108
+
109
+ def change_status (db , event , status ):
110
+ channel = parse_args (event .text , event .chan )
111
+ action = "Enabling" if status else "Disabling"
112
+ event .message (
113
+ "{} regex matching (youtube, etc) (issued by {})" .format (
114
+ action , event .nick
115
+ ),
116
+ target = channel ,
117
+ )
118
+
119
+ event .notice (f"{ action } regex matching (youtube, etc) in channel { channel } " )
120
+ set_status (db , event .conn .name , channel , status )
63
121
64
122
65
123
@hook .sieve ()
66
- def sieve_regex (bot , event , _hook ):
124
+ def sieve_regex (
125
+ bot : "CloudBot" , event : "Event" , _hook : "Hook"
126
+ ) -> Optional ["Event" ]:
67
127
if (
68
128
_hook .type == "regex"
69
129
and event .chan .startswith ("#" )
70
130
and _hook .plugin .title != "factoids"
71
131
):
72
- status = status_cache .get ((event .conn .name , event .chan ))
73
- if status != "ENABLED" and (
74
- status == "DISABLED" or not default_enabled
75
- ):
132
+ if not get_status (event .conn , event .chan ):
76
133
logger .info (
77
134
"[%s] Denying %s from %s" ,
78
135
event .conn .name ,
79
136
_hook .function_name ,
80
137
event .chan ,
81
138
)
82
139
return None
140
+
83
141
logger .info (
84
142
"[%s] Allowing %s to %s" ,
85
143
event .conn .name ,
@@ -90,29 +148,6 @@ def sieve_regex(bot, event, _hook):
90
148
return event
91
149
92
150
93
- def change_status (db , event , status ):
94
- text = event .text .strip ().lower ()
95
- if not text :
96
- channel = event .chan
97
- elif text .startswith ("#" ):
98
- channel = text
99
- else :
100
- channel = f"#{ text } "
101
-
102
- action = "Enabling" if status else "Disabling"
103
- event .message (
104
- "{} regex matching (youtube, etc) (issued by {})" .format (
105
- action , event .nick
106
- ),
107
- target = channel ,
108
- )
109
- event .notice (f"{ action } regex matching (youtube, etc) in channel { channel } " )
110
- set_status (
111
- db , event .conn .name , channel , "ENABLED" if status else "DISABLED"
112
- )
113
- load_cache (db )
114
-
115
-
116
151
@hook .command (autohelp = False , permissions = ["botcontrol" ])
117
152
def enableregex (db , event ):
118
153
"""[chan] - Enable regex hooks in [chan] (default: current channel)"""
@@ -128,54 +163,39 @@ def disableregex(db, event):
128
163
@hook .command (autohelp = False , permissions = ["botcontrol" ])
129
164
def resetregex (text , db , conn , chan , nick , message , notice ):
130
165
"""[chan] - Reset regex hook status in [chan] (default: current channel)"""
131
- text = text .strip ().lower ()
132
- if not text :
133
- channel = chan
134
- elif text .startswith ("#" ):
135
- channel = text
136
- else :
137
- channel = f"#{ text } "
138
-
166
+ channel = parse_args (text , chan )
139
167
message (
140
168
"Resetting regex matching setting (youtube, etc) (issued by {})" .format (
141
169
nick
142
170
),
143
171
target = channel ,
144
172
)
173
+
145
174
notice (
146
175
"Resetting regex matching setting (youtube, etc) in channel {}" .format (
147
176
channel
148
177
)
149
178
)
179
+
150
180
delete_status (db , conn .name , channel )
151
- load_cache (db )
152
181
153
182
154
183
@hook .command (autohelp = False , permissions = ["botcontrol" ])
155
- def regexstatus (text , conn , chan ) :
184
+ def regexstatus (text : str , conn : "Client" , chan : str ) -> str :
156
185
"""[chan] - Get status of regex hooks in [chan] (default: current channel)"""
157
- text = text .strip ().lower ()
158
- if not text :
159
- channel = chan
160
- elif text .startswith ("#" ):
161
- channel = text
162
- else :
163
- channel = f"#{ text } "
164
- status = status_cache .get ((conn .name , chan ))
165
- if status is None :
166
- if default_enabled :
167
- status = "ENABLED"
168
- else :
169
- status = "DISABLED"
170
- return f"Regex status for { channel } : { status } "
186
+ channel = parse_args (text , chan )
187
+ status = get_status (conn , channel )
188
+ return f"Regex status for { channel } : { ENABLED if status else DISABLED } "
171
189
172
190
173
191
@hook .command (autohelp = False , permissions = ["botcontrol" ])
174
- def listregex (conn ) :
192
+ def listregex (conn : "Client" ) -> str :
175
193
"""- List non-default regex statuses for channels"""
176
194
values = []
177
195
for (conn_name , chan ), status in status_cache .items ():
178
196
if conn_name != conn .name :
179
197
continue
180
- values .append (f"{ chan } : { status } " )
198
+
199
+ values .append (f"{ chan } : { ENABLED if status else DISABLED } " )
200
+
181
201
return ", " .join (values )
0 commit comments