def evaluate(self, message: Message, state: EvaluationState) -> bool: if not isinstance(self.keylist, list): raise ValueError('TestAddress keylist not iterable') if not isinstance(self.headers, list): raise ValueError('TestAddress headers not iterable') header_values: List[Text] = [] for header in self.headers: header = expand_variables(header, state) # TODO: section 5.1: we should restrict the allowed headers to # those headers that contain an "address-list". this includes at # least: from, to, cc, bcc, sender, resent-from, resent-to. header_values.extend(message.get_all(header, [])) addresses: List[Text] = [] for msg_address in email.utils.getaddresses(header_values): if msg_address[1] != '': addresses.append( sifter.grammar.string.address_part( msg_address[1], cast(Text, self.address_part))) for address in addresses: for key in self.keylist: key = expand_variables(key, state) if sifter.grammar.string.compare(address, key, state, self.comparator, cast(Text, self.match_type)): return True return False
def evaluate(self, message: Message, state: EvaluationState) -> None: state.check_required_extension('rewrite', 'REWRITE') search = self.positional_args[0][0] # type: ignore replace = self.positional_args[1][0] # type: ignore search = expand_variables(search, state) replace = expand_variables(replace, state) state.actions.append('rewrite', (search, replace)) # type: ignore
def evaluate(self, message: Message, state: EvaluationState) -> bool: if not isinstance(self.headers, list): raise ValueError("TestHeader.headers is not a list") if not isinstance(self.keylist, list): raise ValueError("TestHeader.keylist is not a list") for header in self.headers: header = expand_variables(header, state) for value in message.get_all(header, []): value = self._newline_re.sub(" ", value) for key in self.keylist: key = expand_variables(key, state) if string_compare(str(value), key, state, self.comparator, self.match_type): return True return False
def evaluate(self, message: Message, state: EvaluationState) -> None: notify_from = notify_importance = self.notify_message = None notify_options = [] # type: ignore if 'from' in self.tagged_args: notify_from = self.tagged_args['from'][1][0] # type: ignore if 'importance' in self.tagged_args: notify_importance = self.tagged_args['importance'][1][ 0] # type: ignore if 'options' in self.tagged_args: notify_options = self.tagged_args['options'][1] # type: ignore notify_message = None if 'message' in self.tagged_args: notify_message = self.tagged_args['message'][1][0] # type: ignore notify_method = self.positional_args[0][0] # type: ignore state.check_required_extension('enotify', 'NOTIFY') notify_from = expand_variables(notify_from, state) # type: ignore notify_importance = expand_variables(notify_importance, state) # type: ignore notify_options = list( map(lambda s: expand_variables(s, state), notify_options)) notify_message = expand_variables(notify_message, state) # type: ignore notify_method = expand_variables(notify_method, state) m = re.match('^([A-Za-z][A-Za-z0-9.+-]*):', notify_method) if not m: raise RuleSyntaxError( "Notification method must be an URI, e.g. 'mailto:[email protected]'" ) if notify_importance and notify_importance not in ["1", "2", "3"]: raise RuleSyntaxError( "Illegal notify importance '%s' encountered" % notify_importance) notify_method_cls = ExtensionRegistry.get_notification_method( m.group(1).lower()) if not notify_method_cls: raise RuleSyntaxError("Unsupported notification method '%s'" % m.group(1)) (res, msg) = notify_method_cls.test_valid(notify_method) if not res: raise RuleSyntaxError(msg) state.actions.append( 'notify', (notify_method, notify_from, notify_importance, notify_options, notify_message) # type: ignore )
def evaluate(self, message: Message, state: EvaluationState) -> None: state.check_required_extension('pipe', 'PIPE') pipe_dest = self.positional_args[0] pipe_dest = map(lambda s: expand_variables(s, state), pipe_dest) # type: ignore state.actions.append('pipe', pipe_dest) # type: ignore state.actions.cancel_implicit_keep()
def evaluate(self, message: Message, state: EvaluationState) -> None: state.check_required_extension('variables', 'VARIABLES') variable_modifier = self.tagged_args variable_name = self.positional_args[0][0] # type: ignore if (not re.match(r'^[A-Za-z_][A-Za-z0-9_]*$', variable_name)): raise RuleSyntaxError("Illegal variable name '%s' encountered" % variable_name) variable_value: Text = self.positional_args[1][0] # type: ignore variable_value = expand_variables(variable_value, state) if 'lower' in variable_modifier: variable_value = variable_value.lower() if 'upper' in variable_modifier: variable_value = variable_value.upper() if 'lowerfirst' in variable_modifier: variable_value = variable_value[:1].lower() + variable_value[1:] if 'upperfirst' in variable_modifier: variable_value = variable_value[:1].upper() + variable_value[1:] if 'quotewildcard' in variable_modifier: variable_value = variable_value.replace('*', '\\*') variable_value = variable_value.replace('?', '\\?') variable_value = variable_value.replace('\\', '\\\\') if 'quoteregex' in variable_modifier: variable_value = re.escape(variable_value) if 'encodeurl' in variable_modifier: variable_value = quote(variable_value, safe='-._~') if 'length' in variable_modifier: variable_value = "" + str(len(variable_value)) state.named_variables[variable_name] = variable_value
def evaluate_part(self, part_str: Text, state: EvaluationState) -> bool: for key in self.keylist: # type: ignore key = expand_variables(key, state) if string_compare(part_str, key, state, self.comparator, self.match_type): # type: ignore return True return False
def evaluate(self, message: Message, state: EvaluationState) -> None: state.check_required_extension('fileinto', 'FILEINTO') file_dest = self.positional_args[0] file_dest = list(map(lambda s: expand_variables(s, state), file_dest)) # type: ignore state.actions.append('fileinto', file_dest) state.actions.cancel_implicit_keep()
def evaluate(self, message: Message, state: EvaluationState) -> bool: headers = self.positional_args[0] if not isinstance(headers, list): raise ValueError("TestExists.headers must be a list") for header in headers: header = expand_variables(header, state) if header not in message: return False return True
def evaluate(self, message: Message, state: EvaluationState) -> None: state.check_required_extension('imap4flags', 'imapflags') flag_list = self.positional_args[0] flag_list = list(map(lambda s: expand_variables(s, state), flag_list)) # type: ignore state.actions.append('addflag', flag_list)
def evaluate(self, message: Message, state: EvaluationState) -> None: email_address = expand_variables(self.email_address, state) state.actions.append('redirect', email_address) state.actions.cancel_implicit_keep()