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