7
7
from .base import BaseRoute
8
8
9
9
10
+ def match_prefix (all_address : list [str ], all_prefix : list [str ]) -> bool :
11
+ for address in all_address :
12
+ for prefix in all_prefix :
13
+ if address .find (prefix ) == 0 :
14
+ return True
15
+ return False
16
+
17
+
18
+ def match_suffix (all_address : list [str ], all_suffix : list [str ]) -> bool :
19
+ for address in all_address :
20
+ for suffix in all_suffix :
21
+ length_suffix = len (suffix )
22
+ address_suffix = address [- length_suffix :]
23
+ if address_suffix == suffix :
24
+ return True
25
+ return False
26
+
27
+
28
+ def match_prefix_and_suffix (all_address : list [str ], all_prefix : list [str ], all_suffix : list [str ]) -> bool :
29
+ for address in all_address :
30
+ for prefix in all_prefix :
31
+ for suffix in all_suffix :
32
+ length_suffix = len (suffix )
33
+ address_suffix = address [- length_suffix :]
34
+ if address .find (prefix ) == 0 and address_suffix == suffix :
35
+ return True
36
+ return False
37
+
38
+
10
39
class SESRoute (BaseRoute ):
11
40
mail_to : list [str ] | None
41
+ mail_to_prefix : list [str ] | None
42
+ mail_to_suffix : list [str ] | None
12
43
mail_from : list [str ] | None
44
+ mail_from_prefix : list [str ] | None
45
+ mail_from_suffix : list [str ] | None
13
46
mail_subject : str | None
47
+ mail_subject_prefix : str | None
14
48
15
49
def __init__ (
16
50
self ,
17
51
func : Callable ,
18
52
mail_to : str | list [str ] | None = None ,
53
+ mail_to_prefix : str | list [str ] | None = None ,
54
+ mail_to_suffix : str | list [str ] | None = None ,
19
55
mail_from : str | list [str ] | None = None ,
56
+ mail_from_prefix : str | list [str ] | None = None ,
57
+ mail_from_suffix : str | list [str ] | None = None ,
20
58
mail_subject : str | None = None ,
59
+ mail_subject_prefix : str | None = None ,
21
60
):
22
61
self .func = func
23
62
@@ -26,76 +65,92 @@ def __init__(
26
65
else :
27
66
self .mail_to = mail_to
28
67
68
+ if isinstance (mail_to_prefix , str ):
69
+ self .mail_to_prefix = [mail_to_prefix ]
70
+ else :
71
+ self .mail_to_prefix = mail_to_prefix
72
+
73
+ if isinstance (mail_to_suffix , str ):
74
+ self .mail_to_suffix = [mail_to_suffix ]
75
+ else :
76
+ self .mail_to_suffix = mail_to_suffix
77
+
29
78
if isinstance (mail_from , str ):
30
79
self .mail_from = [mail_from ]
31
80
else :
32
81
self .mail_from = mail_from
33
82
83
+ if isinstance (mail_from_prefix , str ):
84
+ self .mail_from_prefix = [mail_from_prefix ]
85
+ else :
86
+ self .mail_from_prefix = mail_from_prefix
87
+
88
+ if isinstance (mail_from_suffix , str ):
89
+ self .mail_from_suffix = [mail_from_suffix ]
90
+ else :
91
+ self .mail_from_suffix = mail_from_suffix
92
+
34
93
self .mail_subject = mail_subject
94
+ self .mail_subject_prefix = mail_subject_prefix
35
95
36
96
if not self .mail_to and not self .mail_from and not self .mail_subject :
37
- raise ValueError ("mail_to, mail_from, or mail_subject must be not null" )
97
+ raise ValueError (
98
+ (
99
+ "mail_to, mail_to_prefix, mail_to_suffix, mail_from, mail_from_prefix, mail_from_suffix, "
100
+ "mail_subject, or mail_subject_prefix must be not null"
101
+ ),
102
+ )
38
103
39
104
def is_target_with_mail_to (self , mail_to : list [str ] | None ) -> bool :
40
- if not mail_to or not self . mail_to :
105
+ if not mail_to :
41
106
return False
42
-
43
- for address in self .mail_to :
44
- if address in mail_to :
45
- return True
107
+ elif self .mail_to :
108
+ for address in mail_to :
109
+ if address in self .mail_to :
110
+ return True
111
+ elif self .mail_to_prefix and self .mail_to_suffix :
112
+ return match_prefix_and_suffix (
113
+ all_address = mail_to ,
114
+ all_prefix = self .mail_to_prefix ,
115
+ all_suffix = self .mail_to_suffix ,
116
+ )
117
+ elif self .mail_to_prefix :
118
+ return match_prefix (all_address = mail_to , all_prefix = self .mail_to_prefix )
119
+ elif self .mail_to_suffix :
120
+ return match_suffix (all_address = mail_to , all_suffix = self .mail_to_suffix )
46
121
47
122
return False
48
123
49
124
def is_target_with_mail_from (self , mail_from : list [str ] | None ) -> bool :
50
- if not mail_from or not self . mail_from :
125
+ if not mail_from :
51
126
return False
52
-
53
- for address in self .mail_from :
54
- if address in mail_from :
55
- return True
127
+ elif self .mail_from :
128
+ for address in mail_from :
129
+ if address in self .mail_from :
130
+ return True
131
+ elif self .mail_from_prefix and self .mail_from_suffix :
132
+ return match_prefix_and_suffix (
133
+ all_address = mail_from ,
134
+ all_prefix = self .mail_from_prefix ,
135
+ all_suffix = self .mail_from_suffix ,
136
+ )
137
+ elif self .mail_from_prefix :
138
+ return match_prefix (all_address = mail_from , all_prefix = self .mail_from_prefix )
139
+ elif self .mail_from_suffix :
140
+ return match_suffix (all_address = mail_from , all_suffix = self .mail_from_suffix )
56
141
57
142
return False
58
143
59
144
def is_target_with_mail_subject (self , mail_subject : str | None ) -> bool :
60
- if mail_subject and self .mail_subject :
145
+ if not mail_subject :
146
+ return False
147
+ elif self .mail_subject :
61
148
return self .mail_subject == mail_subject
149
+ elif self .mail_subject_prefix :
150
+ return mail_subject .find (self .mail_subject_prefix ) == 0
62
151
else :
63
152
return False
64
153
65
- def is_target (self , mail_to : list [str ] | None , mail_from : list [str ] | None , mail_subject : str | None ) -> bool :
66
- flag_mail_to = self .is_target_with_mail_to (mail_to = mail_to )
67
- flag_mail_from = self .is_target_with_mail_from (mail_from = mail_from )
68
- flag_mail_subject = self .is_target_with_mail_subject (mail_subject = mail_subject )
69
-
70
- if mail_to and mail_from and mail_subject :
71
- text = "mail_to, mail_from, mail_subject"
72
- elif mail_to and mail_from and not mail_subject :
73
- text = "mail_to, mail_from"
74
- elif mail_to and not mail_from and mail_subject :
75
- text = "mail_to, mail_subject"
76
- elif not mail_to and mail_from and mail_subject :
77
- text = "mail_from, mail_subject"
78
- elif mail_to and not mail_from and not mail_subject :
79
- text = "mail_to"
80
- elif not mail_to and mail_from and not mail_subject :
81
- text = "mail_from"
82
- elif not mail_to and not mail_from and mail_subject :
83
- text = "mail_subject"
84
- else : # not mail_to and not mail_from and not mail_subject
85
- text = ""
86
-
87
- mapping = {
88
- "mail_to, mail_from, mail_subject" : flag_mail_to and flag_mail_from and flag_mail_subject ,
89
- "mail_to, mail_from" : flag_mail_to and flag_mail_from ,
90
- "mail_to, mail_subject" : flag_mail_to and flag_mail_subject ,
91
- "mail_from, mail_subject" : flag_mail_from and flag_mail_subject ,
92
- "mail_to" : flag_mail_to ,
93
- "mail_from" : flag_mail_from ,
94
- "mail_subject" : flag_mail_subject ,
95
- "" : False ,
96
- }
97
- return mapping [text ]
98
-
99
154
def match (self , event : dict [str , Any ]) -> tuple [Callable , SESEvent ] | None :
100
155
if not isinstance (event , dict ):
101
156
return None
@@ -110,22 +165,43 @@ def match(self, event: dict[str, Any]) -> tuple[Callable, SESEvent] | None:
110
165
if common_header is None :
111
166
return None
112
167
113
- if self .mail_to :
114
- mail_to = common_header .get ("to" )
115
- else :
168
+ mail_to = common_header .get ("to" )
169
+ mail_from = common_header .get ("from" )
170
+ mail_subject = common_header .get ("subject" )
171
+
172
+ if not self .mail_to and not self .mail_to_prefix and not self .mail_to_suffix :
116
173
mail_to = None
117
174
118
- if self .mail_from :
119
- mail_from = common_header .get ("from" )
120
- else :
175
+ if not self .mail_from and not self .mail_from_prefix and not self .mail_from_suffix :
121
176
mail_from = None
122
177
123
- if self .mail_subject :
124
- mail_subject = common_header .get ("subject" )
125
- else :
178
+ if not self .mail_subject and not self .mail_subject_prefix :
126
179
mail_subject = None
127
180
128
- if self .is_target (mail_to , mail_from , mail_subject ):
181
+ flag_mail_to = self .is_target_with_mail_to (mail_to = mail_to )
182
+ flag_mail_from = self .is_target_with_mail_from (mail_from = mail_from )
183
+ flag_mail_subject = self .is_target_with_mail_subject (mail_subject = mail_subject )
184
+
185
+ text = ", " .join (
186
+ [
187
+ "mail_to: x" if mail_to is None else "mail_to: o" ,
188
+ "mail_from: x" if mail_from is None else "mail_from: o" ,
189
+ "mail_subject: x" if mail_subject is None else "mail_subject: o" ,
190
+ ],
191
+ )
192
+
193
+ mapping = {
194
+ "mail_to: o, mail_from: o, mail_subject: o" : flag_mail_to and flag_mail_from and flag_mail_subject ,
195
+ "mail_to: o, mail_from: o, mail_subject: x" : flag_mail_to and flag_mail_from ,
196
+ "mail_to: o, mail_from: x, mail_subject: o" : flag_mail_to and flag_mail_subject ,
197
+ "mail_to: x, mail_from: o, mail_subject: o" : flag_mail_from and flag_mail_subject ,
198
+ "mail_to: o, mail_from: x, mail_subject: x" : flag_mail_to ,
199
+ "mail_to: x, mail_from: o, mail_subject: x" : flag_mail_from ,
200
+ "mail_to: x, mail_from: x, mail_subject: o" : flag_mail_subject ,
201
+ "mail_to: x, mail_from: x, mail_subject: x" : False ,
202
+ }
203
+
204
+ if mapping [text ]:
129
205
return self .func , SESEvent (event )
130
206
else :
131
207
return None
0 commit comments