|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+import re
|
|
1
|
2
|
from abc import ABC, abstractmethod
|
|
2
|
3
|
from discord import Guild, Member, Message
|
|
3
|
4
|
from discord.ext import commands
|
|
4
|
|
-from datetime import timedelta
|
|
5
|
|
-import re
|
|
6
|
5
|
|
|
7
|
6
|
from cogs.basecog import BaseCog, BotMessage, BotMessageReaction
|
|
8
|
7
|
from config import CONFIG
|
|
|
@@ -10,42 +9,58 @@ from rbutils import parse_timedelta
|
|
10
|
9
|
from storage import Storage
|
|
11
|
10
|
|
|
12
|
11
|
class PatternAction:
|
|
13
|
|
- def __init__(self, type: str, args: list):
|
|
14
|
|
- self.type = type
|
|
|
12
|
+ """
|
|
|
13
|
+ Describes one action to take on a matched message or its author.
|
|
|
14
|
+ """
|
|
|
15
|
+ def __init__(self, action: str, args: list):
|
|
|
16
|
+ self.action = action
|
|
15
|
17
|
self.arguments = list(args)
|
|
16
|
18
|
|
|
17
|
19
|
def __str__(self) -> str:
|
|
18
|
20
|
arg_str = ', '.join(self.arguments)
|
|
19
|
|
- return f'{self.type}({arg_str})'
|
|
|
21
|
+ return f'{self.action}({arg_str})'
|
|
20
|
22
|
|
|
21
|
23
|
class PatternExpression(ABC):
|
|
|
24
|
+ """
|
|
|
25
|
+ Abstract message matching expression.
|
|
|
26
|
+ """
|
|
22
|
27
|
def __init__(self):
|
|
23
|
28
|
pass
|
|
24
|
29
|
|
|
25
|
30
|
@abstractmethod
|
|
26
|
31
|
def matches(self, message: Message) -> bool:
|
|
|
32
|
+ """
|
|
|
33
|
+ Whether a message matches this expression.
|
|
|
34
|
+ """
|
|
27
|
35
|
return False
|
|
28
|
36
|
|
|
29
|
37
|
class PatternSimpleExpression(PatternExpression):
|
|
|
38
|
+ """
|
|
|
39
|
+ Message matching expression with a simple "<field> <operator> <value>"
|
|
|
40
|
+ structure.
|
|
|
41
|
+ """
|
|
30
|
42
|
def __init__(self, field: str, operator: str, value):
|
|
|
43
|
+ super().__init__()
|
|
31
|
44
|
self.field = field
|
|
32
|
45
|
self.operator = operator
|
|
33
|
46
|
self.value = value
|
|
34
|
47
|
|
|
35
|
|
- def matches(self, message: Message) -> bool:
|
|
36
|
|
- field_value = None
|
|
|
48
|
+ def __field_value(self, message: Message):
|
|
37
|
49
|
if self.field == 'content':
|
|
38
|
|
- field_value = message.content
|
|
39
|
|
- elif self.field == 'author':
|
|
40
|
|
- field_value = str(message.author.id)
|
|
41
|
|
- elif self.field == 'author.id':
|
|
42
|
|
- field_value = str(message.author.id)
|
|
43
|
|
- elif self.field == 'author.joinage':
|
|
44
|
|
- field_value = message.created_at - message.author.joined_at
|
|
45
|
|
- elif self.field == 'author.name':
|
|
46
|
|
- field_value = message.author.name
|
|
|
50
|
+ return message.content
|
|
|
51
|
+ if self.field == 'author':
|
|
|
52
|
+ return str(message.author.id)
|
|
|
53
|
+ if self.field == 'author.id':
|
|
|
54
|
+ return str(message.author.id)
|
|
|
55
|
+ if self.field == 'author.joinage':
|
|
|
56
|
+ return message.created_at - message.author.joined_at
|
|
|
57
|
+ if self.field == 'author.name':
|
|
|
58
|
+ return message.author.name
|
|
47
|
59
|
else:
|
|
48
|
60
|
raise ValueError(f'Bad field name {self.field}')
|
|
|
61
|
+
|
|
|
62
|
+ def matches(self, message: Message) -> bool:
|
|
|
63
|
+ field_value = self.__field_value(message)
|
|
49
|
64
|
if self.operator == '==':
|
|
50
|
65
|
if isinstance(field_value, str) and isinstance(self.value, str):
|
|
51
|
66
|
return field_value.lower() == self.value.lower()
|
|
|
@@ -78,35 +93,42 @@ class PatternSimpleExpression(PatternExpression):
|
|
78
|
93
|
return f'({self.field} {self.operator} {self.value})'
|
|
79
|
94
|
|
|
80
|
95
|
class PatternCompoundExpression(PatternExpression):
|
|
|
96
|
+ """
|
|
|
97
|
+ Message matching expression that combines several child expressions with
|
|
|
98
|
+ a boolean operator.
|
|
|
99
|
+ """
|
|
81
|
100
|
def __init__(self, operator: str, operands: list):
|
|
|
101
|
+ super().__init__()
|
|
82
|
102
|
self.operator = operator
|
|
83
|
103
|
self.operands = list(operands)
|
|
84
|
104
|
|
|
85
|
105
|
def matches(self, message: Message) -> bool:
|
|
86
|
106
|
if self.operator == '!':
|
|
87
|
107
|
return not self.operands[0].matches(message)
|
|
88
|
|
- elif self.operator == 'and':
|
|
|
108
|
+ if self.operator == 'and':
|
|
89
|
109
|
for op in self.operands:
|
|
90
|
110
|
if not op.matches(message):
|
|
91
|
111
|
return False
|
|
92
|
112
|
return True
|
|
93
|
|
- elif self.operator == 'or':
|
|
|
113
|
+ if self.operator == 'or':
|
|
94
|
114
|
for op in self.operands:
|
|
95
|
115
|
if op.matches(message):
|
|
96
|
116
|
return True
|
|
97
|
117
|
return False
|
|
98
|
|
- else:
|
|
99
|
|
- raise RuntimeError(f'Bad operator "{self.operator}"')
|
|
|
118
|
+ raise RuntimeError(f'Bad operator "{self.operator}"')
|
|
100
|
119
|
|
|
101
|
120
|
def __str__(self) -> str:
|
|
102
|
121
|
if self.operator == '!':
|
|
103
|
122
|
return f'(!( {self.operands[0]} ))'
|
|
104
|
|
- else:
|
|
105
|
|
- strs = map(str, self.operands)
|
|
106
|
|
- joined = f' {self.operator} '.join(strs)
|
|
107
|
|
- return f'( {joined} )'
|
|
|
123
|
+ strs = map(str, self.operands)
|
|
|
124
|
+ joined = f' {self.operator} '.join(strs)
|
|
|
125
|
+ return f'( {joined} )'
|
|
108
|
126
|
|
|
109
|
127
|
class PatternStatement:
|
|
|
128
|
+ """
|
|
|
129
|
+ A full message match statement. If a message matches the given expression,
|
|
|
130
|
+ the given actions should be performed.
|
|
|
131
|
+ """
|
|
110
|
132
|
def __init__(self, name: str, actions: list, expression: PatternExpression, original: str):
|
|
111
|
133
|
self.name = name
|
|
112
|
134
|
self.actions = list(actions) # PatternAction[]
|
|
|
@@ -114,6 +136,10 @@ class PatternStatement:
|
|
114
|
136
|
self.original = original
|
|
115
|
137
|
|
|
116
|
138
|
class PatternContext:
|
|
|
139
|
+ """
|
|
|
140
|
+ Data about a message that has matched a configured statement and what
|
|
|
141
|
+ actions have been carried out.
|
|
|
142
|
+ """
|
|
117
|
143
|
def __init__(self, message: Message, statement: PatternStatement):
|
|
118
|
144
|
self.message = message
|
|
119
|
145
|
self.statement = statement
|
|
|
@@ -121,20 +147,11 @@ class PatternContext:
|
|
121
|
147
|
self.is_kicked = False
|
|
122
|
148
|
self.is_banned = False
|
|
123
|
149
|
|
|
124
|
|
-class PatternCog(BaseCog):
|
|
125
|
|
- def __init__(self, bot):
|
|
126
|
|
- super().__init__(bot)
|
|
127
|
|
-
|
|
128
|
|
- # def __patterns(self, guild: Guild) -> list:
|
|
129
|
|
- # patterns = Storage.get_state_value(guild, 'pattern_patterns')
|
|
130
|
|
- # if patterns is None:
|
|
131
|
|
- # patterns_encoded = Storage.get_config_value(guild, 'pattern_patterns')
|
|
132
|
|
- # if patterns_encoded:
|
|
133
|
|
- # patterns = []
|
|
134
|
|
- # for pe in patterns_encoded:
|
|
135
|
|
- # patterns.append(Pattern.decode(pe))
|
|
136
|
|
- # Storage.set_state_value(guild, 'pattern_patterns', patterns)
|
|
137
|
|
- # return patterns
|
|
|
150
|
+class PatternCog(BaseCog, name='Pattern Matching'):
|
|
|
151
|
+ """
|
|
|
152
|
+ Highly flexible cog for performing various actions on messages that match
|
|
|
153
|
+ various critera. Patterns can be defined by mods for each guild.
|
|
|
154
|
+ """
|
|
138
|
155
|
|
|
139
|
156
|
def __get_patterns(self, guild: Guild) -> dict:
|
|
140
|
157
|
patterns = Storage.get_state_value(guild, 'PatternCog.patterns')
|
|
|
@@ -148,12 +165,13 @@ class PatternCog(BaseCog):
|
|
148
|
165
|
try:
|
|
149
|
166
|
ps = PatternCompiler.parse_statement(name, statement)
|
|
150
|
167
|
patterns[name] = ps
|
|
151
|
|
- except RuntimeError as e:
|
|
152
|
|
- self.log(guild, f'Error parsing saved statement "{name}". Skipping: {statement}')
|
|
|
168
|
+ except Exception as e:
|
|
|
169
|
+ self.log(guild, f'Error parsing saved statement "{name}". Skipping: {statement}. Error: {e}')
|
|
153
|
170
|
Storage.set_state_value(guild, 'PatternCog.patterns', patterns)
|
|
154
|
171
|
return patterns
|
|
155
|
172
|
|
|
156
|
|
- def __save_patterns(self, guild: Guild, patterns: dict) -> None:
|
|
|
173
|
+ @classmethod
|
|
|
174
|
+ def __save_patterns(cls, guild: Guild, patterns: dict) -> None:
|
|
157
|
175
|
to_save = []
|
|
158
|
176
|
for name, statement in patterns.items():
|
|
159
|
177
|
to_save.append({
|
|
|
@@ -164,6 +182,7 @@ class PatternCog(BaseCog):
|
|
164
|
182
|
|
|
165
|
183
|
@commands.Cog.listener()
|
|
166
|
184
|
async def on_message(self, message: Message) -> None:
|
|
|
185
|
+ 'Event listener'
|
|
167
|
186
|
if message.author is None or \
|
|
168
|
187
|
message.author.bot or \
|
|
169
|
188
|
message.channel is None or \
|
|
|
@@ -176,7 +195,7 @@ class PatternCog(BaseCog):
|
|
176
|
195
|
return
|
|
177
|
196
|
|
|
178
|
197
|
patterns = self.__get_patterns(message.guild)
|
|
179
|
|
- for name, statement in patterns.items():
|
|
|
198
|
+ for _, statement in patterns.items():
|
|
180
|
199
|
if statement.expression.matches(message):
|
|
181
|
200
|
await self.__trigger_actions(message, statement)
|
|
182
|
201
|
break
|
|
|
@@ -188,7 +207,7 @@ class PatternCog(BaseCog):
|
|
188
|
207
|
action_descriptions = []
|
|
189
|
208
|
self.log(message.guild, f'Message from {message.author.name} matched pattern "{statement.name}"')
|
|
190
|
209
|
for action in statement.actions:
|
|
191
|
|
- if action.type == 'ban':
|
|
|
210
|
+ if action.action == 'ban':
|
|
192
|
211
|
await message.author.ban(
|
|
193
|
212
|
reason=f'Rocketbot: Message matched custom pattern named "{statement.name}"',
|
|
194
|
213
|
delete_message_days=0)
|
|
|
@@ -196,21 +215,21 @@ class PatternCog(BaseCog):
|
|
196
|
215
|
context.is_kicked = True
|
|
197
|
216
|
action_descriptions.append('Author banned')
|
|
198
|
217
|
self.log(message.guild, f'{message.author.name} banned')
|
|
199
|
|
- elif action.type == 'delete':
|
|
|
218
|
+ elif action.action == 'delete':
|
|
200
|
219
|
await message.delete()
|
|
201
|
220
|
context.is_deleted = True
|
|
202
|
221
|
action_descriptions.append('Message deleted')
|
|
203
|
222
|
self.log(message.guild, f'{message.author.name}\'s message deleted')
|
|
204
|
|
- elif action.type == 'kick':
|
|
|
223
|
+ elif action.action == 'kick':
|
|
205
|
224
|
await message.author.kick(
|
|
206
|
225
|
reason=f'Rocketbot: Message matched custom pattern named "{statement.name}"')
|
|
207
|
226
|
context.is_kicked = True
|
|
208
|
227
|
action_descriptions.append('Author kicked')
|
|
209
|
228
|
self.log(message.guild, f'{message.author.name} kicked')
|
|
210
|
|
- elif action.type == 'modwarn':
|
|
|
229
|
+ elif action.action == 'modwarn':
|
|
211
|
230
|
should_alert_mods = True
|
|
212
|
231
|
action_descriptions.append('Mods alerted')
|
|
213
|
|
- elif action.type == 'reply':
|
|
|
232
|
+ elif action.action == 'reply':
|
|
214
|
233
|
await message.reply(
|
|
215
|
234
|
f'{action.arguments[0]}',
|
|
216
|
235
|
mention_author=False)
|
|
|
@@ -240,13 +259,13 @@ class PatternCog(BaseCog):
|
|
240
|
259
|
context.is_deleted = True
|
|
241
|
260
|
elif reaction.emoji == CONFIG['kick_emoji']:
|
|
242
|
261
|
await context.message.author.kick(
|
|
243
|
|
- reason=f'Rocketbot: Message matched custom pattern named ' + \
|
|
244
|
|
- '"{statement.name}". Kicked by {reacted_by.name}.')
|
|
|
262
|
+ reason='Rocketbot: Message matched custom pattern named ' + \
|
|
|
263
|
+ f'"{context.statement.name}". Kicked by {reacted_by.name}.')
|
|
245
|
264
|
context.is_kicked = True
|
|
246
|
265
|
elif reaction.emoji == CONFIG['ban_emoji']:
|
|
247
|
266
|
await context.message.author.ban(
|
|
248
|
|
- reason=f'Rocketbot: Message matched custom pattern named ' + \
|
|
249
|
|
- '"{statement.name}". Banned by {reacted_by.name}.',
|
|
|
267
|
+ reason='Rocketbot: Message matched custom pattern named ' + \
|
|
|
268
|
+ f'"{context.statement.name}". Banned by {reacted_by.name}.',
|
|
250
|
269
|
delete_message_days=1)
|
|
251
|
270
|
context.is_banned = True
|
|
252
|
271
|
await bot_message.set_reactions(BotMessageReaction.standard_set(
|
|
|
@@ -260,7 +279,7 @@ class PatternCog(BaseCog):
|
|
260
|
279
|
@commands.has_permissions(ban_members=True)
|
|
261
|
280
|
@commands.guild_only()
|
|
262
|
281
|
async def pattern(self, context: commands.Context):
|
|
263
|
|
- 'Message pattern matching'
|
|
|
282
|
+ 'Message pattern matching command group'
|
|
264
|
283
|
if context.invoked_subcommand is None:
|
|
265
|
284
|
await context.send_help()
|
|
266
|
285
|
|
|
|
@@ -273,6 +292,7 @@ class PatternCog(BaseCog):
|
|
273
|
292
|
ignore_extra=True
|
|
274
|
293
|
)
|
|
275
|
294
|
async def add(self, context: commands.Context, name: str):
|
|
|
295
|
+ 'Command handler'
|
|
276
|
296
|
pattern_str = PatternCompiler.expression_str_from_context(context, name)
|
|
277
|
297
|
try:
|
|
278
|
298
|
statement = PatternCompiler.parse_statement(name, pattern_str)
|
|
|
@@ -292,6 +312,7 @@ class PatternCog(BaseCog):
|
|
292
|
312
|
usage='<pattern_name>'
|
|
293
|
313
|
)
|
|
294
|
314
|
async def remove(self, context: commands.Context, name: str):
|
|
|
315
|
+ 'Command handler'
|
|
295
|
316
|
patterns = self.__get_patterns(context.guild)
|
|
296
|
317
|
if patterns.get(name) is not None:
|
|
297
|
318
|
del patterns[name]
|
|
|
@@ -308,6 +329,7 @@ class PatternCog(BaseCog):
|
|
308
|
329
|
brief='Lists all patterns'
|
|
309
|
330
|
)
|
|
310
|
331
|
async def list(self, context: commands.Context) -> None:
|
|
|
332
|
+ 'Command handler'
|
|
311
|
333
|
patterns = self.__get_patterns(context.guild)
|
|
312
|
334
|
if len(patterns) == 0:
|
|
313
|
335
|
await context.message.reply('No patterns defined.', mention_author=False)
|
|
|
@@ -318,6 +340,9 @@ class PatternCog(BaseCog):
|
|
318
|
340
|
await context.message.reply(msg, mention_author=False)
|
|
319
|
341
|
|
|
320
|
342
|
class PatternCompiler:
|
|
|
343
|
+ """
|
|
|
344
|
+ Parses a user-provided message filter statement into a PatternStatement.
|
|
|
345
|
+ """
|
|
321
|
346
|
TYPE_ID = 'id'
|
|
322
|
347
|
TYPE_MEMBER = 'Member'
|
|
323
|
348
|
TYPE_TEXT = 'text'
|
|
|
@@ -364,6 +389,9 @@ class PatternCompiler:
|
|
364
|
389
|
|
|
365
|
390
|
@classmethod
|
|
366
|
391
|
def expression_str_from_context(cls, context: commands.Context, name: str) -> str:
|
|
|
392
|
+ """
|
|
|
393
|
+ Extracts the statement string from an "add" command context.
|
|
|
394
|
+ """
|
|
367
|
395
|
pattern_str = context.message.content
|
|
368
|
396
|
command_chain = [ name ]
|
|
369
|
397
|
cmd = context.command
|
|
|
@@ -380,6 +408,9 @@ class PatternCompiler:
|
|
380
|
408
|
|
|
381
|
409
|
@classmethod
|
|
382
|
410
|
def parse_statement(cls, name: str, statement: str) -> PatternStatement:
|
|
|
411
|
+ """
|
|
|
412
|
+ Parses a user-provided message filter statement into a PatternStatement.
|
|
|
413
|
+ """
|
|
383
|
414
|
tokens = cls.tokenize(statement)
|
|
384
|
415
|
token_index = 0
|
|
385
|
416
|
actions, token_index = cls.read_actions(tokens, token_index)
|
|
|
@@ -388,6 +419,9 @@ class PatternCompiler:
|
|
388
|
419
|
|
|
389
|
420
|
@classmethod
|
|
390
|
421
|
def tokenize(cls, statement: str) -> list:
|
|
|
422
|
+ """
|
|
|
423
|
+ Converts a message filter statement into a list of tokens.
|
|
|
424
|
+ """
|
|
391
|
425
|
tokens = []
|
|
392
|
426
|
in_quote = False
|
|
393
|
427
|
in_escape = False
|
|
|
@@ -476,6 +510,11 @@ class PatternCompiler:
|
|
476
|
510
|
|
|
477
|
511
|
@classmethod
|
|
478
|
512
|
def read_actions(cls, tokens: list, token_index: int) -> tuple:
|
|
|
513
|
+ """
|
|
|
514
|
+ Reads the actions from a list of statement tokens. Returns a tuple
|
|
|
515
|
+ containing a list of PatternActions and the token index this method
|
|
|
516
|
+ left off at (the token after the "if").
|
|
|
517
|
+ """
|
|
479
|
518
|
actions = []
|
|
480
|
519
|
current_action_tokens = []
|
|
481
|
520
|
while token_index < len(tokens):
|
|
|
@@ -501,43 +540,47 @@ class PatternCompiler:
|
|
501
|
540
|
|
|
502
|
541
|
@classmethod
|
|
503
|
542
|
def __validate_action(cls, action: PatternAction) -> None:
|
|
504
|
|
- args = cls.ACTION_TO_ARGS.get(action.type)
|
|
|
543
|
+ args = cls.ACTION_TO_ARGS.get(action.action)
|
|
505
|
544
|
if args is None:
|
|
506
|
|
- raise RuntimeError(f'Unknown action "{action.type}"')
|
|
|
545
|
+ raise RuntimeError(f'Unknown action "{action.action}"')
|
|
507
|
546
|
if len(action.arguments) != len(args):
|
|
508
|
|
- arg_list = ', '.join(args)
|
|
509
|
547
|
if len(args) == 0:
|
|
510
|
|
- raise RuntimeError(f'Action "{action.type}" expects no arguments, got {len(action.arguments)}.')
|
|
|
548
|
+ raise RuntimeError(f'Action "{action.action}" expects no arguments, ' + \
|
|
|
549
|
+ f'got {len(action.arguments)}.')
|
|
511
|
550
|
else:
|
|
512
|
|
- raise RuntimeError(f'Action "{action.type}" expects {len(args)} arguments, got {len(action.arguments)}.')
|
|
513
|
|
- for i in range(len(args)):
|
|
514
|
|
- datatype = args[i]
|
|
|
551
|
+ raise RuntimeError(f'Action "{action.action}" expects {len(args)} ' + \
|
|
|
552
|
+ f'arguments, got {len(action.arguments)}.')
|
|
|
553
|
+ for i, datatype in enumerate(args):
|
|
515
|
554
|
action.arguments[i] = cls.parse_value(action.arguments[i], datatype)
|
|
516
|
555
|
|
|
517
|
556
|
@classmethod
|
|
518
|
|
- def read_expression(cls, tokens: list, token_index: int, depth: int = 0, one_subexpression: bool = False) -> tuple:
|
|
519
|
|
- # field op value
|
|
520
|
|
- # (field op value)
|
|
521
|
|
- # !(field op value)
|
|
522
|
|
- # field op value and field op value
|
|
523
|
|
- # (field op value and field op value) or field op value
|
|
524
|
|
- indent = '\t' * depth
|
|
|
557
|
+ def read_expression(cls,
|
|
|
558
|
+ tokens: list,
|
|
|
559
|
+ token_index: int,
|
|
|
560
|
+ depth: int = 0,
|
|
|
561
|
+ one_subexpression: bool = False) -> tuple:
|
|
|
562
|
+ """
|
|
|
563
|
+ Reads an expression from a list of statement tokens. Returns a tuple
|
|
|
564
|
+ containing the PatternExpression and the token index it left off at.
|
|
|
565
|
+ If one_subexpression is True then it will return after reading a
|
|
|
566
|
+ single expression instead of joining multiples (for readong the
|
|
|
567
|
+ subject of a NOT expression).
|
|
|
568
|
+ """
|
|
525
|
569
|
subexpressions = []
|
|
526
|
570
|
last_compound_operator = None
|
|
527
|
571
|
while token_index < len(tokens):
|
|
528
|
572
|
if one_subexpression:
|
|
529
|
573
|
if len(subexpressions) == 1:
|
|
530
|
574
|
return (subexpressions[0], token_index)
|
|
531
|
|
- elif len(subexpressions) > 1:
|
|
|
575
|
+ if len(subexpressions) > 1:
|
|
532
|
576
|
raise RuntimeError('Too many subexpressions')
|
|
533
|
577
|
compound_operator = None
|
|
534
|
578
|
if tokens[token_index] == ')':
|
|
535
|
579
|
if len(subexpressions) == 0:
|
|
536
|
580
|
raise RuntimeError('No subexpressions')
|
|
537
|
|
- elif len(subexpressions) == 1:
|
|
|
581
|
+ if len(subexpressions) == 1:
|
|
538
|
582
|
return (subexpressions[0], token_index)
|
|
539
|
|
- else:
|
|
540
|
|
- return (PatternCompoundExpression(last_compound_operator, subexpressions), token_index)
|
|
|
583
|
+ return (PatternCompoundExpression(last_compound_operator, subexpressions), token_index)
|
|
541
|
584
|
if tokens[token_index] in set(["and", "or"]):
|
|
542
|
585
|
compound_operator = tokens[token_index]
|
|
543
|
586
|
if last_compound_operator and compound_operator != last_compound_operator:
|
|
|
@@ -547,7 +590,8 @@ class PatternCompiler:
|
|
547
|
590
|
last_compound_operator = compound_operator
|
|
548
|
591
|
token_index += 1
|
|
549
|
592
|
if tokens[token_index] == '!':
|
|
550
|
|
- (exp, next_index) = cls.read_expression(tokens, token_index + 1, depth + 1, one_subexpression=True)
|
|
|
593
|
+ (exp, next_index) = cls.read_expression(tokens, token_index + 1, \
|
|
|
594
|
+ depth + 1, one_subexpression=True)
|
|
551
|
595
|
subexpressions.append(PatternCompoundExpression('!', [exp]))
|
|
552
|
596
|
token_index = next_index
|
|
553
|
597
|
elif tokens[token_index] == '(':
|
|
|
@@ -569,8 +613,13 @@ class PatternCompiler:
|
|
569
|
613
|
|
|
570
|
614
|
@classmethod
|
|
571
|
615
|
def read_simple_expression(cls, tokens: list, token_index: int, depth: int = 0) -> tuple:
|
|
572
|
|
- indent = '\t' * depth
|
|
573
|
|
-
|
|
|
616
|
+ """
|
|
|
617
|
+ Reads a simple expression consisting of a field name, operator, and
|
|
|
618
|
+ comparison value. Returns a tuple of the PatternSimpleExpression and
|
|
|
619
|
+ the token index it left off at.
|
|
|
620
|
+ """
|
|
|
621
|
+ if depth > 8:
|
|
|
622
|
+ raise RuntimeError('Expression nests too deeply')
|
|
574
|
623
|
if token_index >= len(tokens):
|
|
575
|
624
|
raise RuntimeError('Expected field name, found EOL')
|
|
576
|
625
|
field = tokens[token_index]
|
|
|
@@ -609,20 +658,23 @@ class PatternCompiler:
|
|
609
|
658
|
return (exp, token_index)
|
|
610
|
659
|
|
|
611
|
660
|
@classmethod
|
|
612
|
|
- def parse_value(cls, value: str, type: str):
|
|
613
|
|
- if type == cls.TYPE_ID:
|
|
|
661
|
+ def parse_value(cls, value: str, datatype: str):
|
|
|
662
|
+ """
|
|
|
663
|
+ Converts a value token to its Python value.
|
|
|
664
|
+ """
|
|
|
665
|
+ if datatype == cls.TYPE_ID:
|
|
614
|
666
|
p = re.compile('^[0-9]+$')
|
|
615
|
667
|
if p.match(value) is None:
|
|
616
|
668
|
raise ValueError(f'Illegal id value "{value}"')
|
|
617
|
669
|
# Store it as a str so it can be larger than an int
|
|
618
|
670
|
return value
|
|
619
|
|
- if type == cls.TYPE_MEMBER:
|
|
|
671
|
+ if datatype == cls.TYPE_MEMBER:
|
|
620
|
672
|
p = re.compile('^<@!?([0-9]+)>$')
|
|
621
|
673
|
m = p.match(value)
|
|
622
|
674
|
if m is None:
|
|
623
|
|
- raise ValueError(f'Illegal member value. Must be an @ mention.')
|
|
|
675
|
+ raise ValueError('Illegal member value. Must be an @ mention.')
|
|
624
|
676
|
return m.group(1)
|
|
625
|
|
- if type == cls.TYPE_TEXT:
|
|
|
677
|
+ if datatype == cls.TYPE_TEXT:
|
|
626
|
678
|
# Must be quoted.
|
|
627
|
679
|
if len(value) < 2 or \
|
|
628
|
680
|
value[0:1] not in cls.STRING_QUOTE_CHARS or \
|
|
|
@@ -630,10 +682,10 @@ class PatternCompiler:
|
|
630
|
682
|
value[0:1] != value[-1:]:
|
|
631
|
683
|
raise ValueError(f'Not a quoted string value: {value}')
|
|
632
|
684
|
return value[1:-1]
|
|
633
|
|
- if type == cls.TYPE_INT:
|
|
|
685
|
+ if datatype == cls.TYPE_INT:
|
|
634
|
686
|
return int(value)
|
|
635
|
|
- if type == cls.TYPE_FLOAT:
|
|
|
687
|
+ if datatype == cls.TYPE_FLOAT:
|
|
636
|
688
|
return float(value)
|
|
637
|
|
- if type == cls.TYPE_TIMESPAN:
|
|
|
689
|
+ if datatype == cls.TYPE_TIMESPAN:
|
|
638
|
690
|
return parse_timedelta(value)
|
|
639
|
691
|
raise ValueError(f'Unhandled datatype {datatype}')
|