|
| 1 | +import re |
| 2 | +import sifter.grammar |
| 3 | +import sifter.validators |
| 4 | +import sifter.grammar.notificationmethod |
| 5 | +from sifter.grammar.command import Command |
| 6 | +from sifter.validators.stringlist import StringList |
| 7 | +from sifter.validators.tag import Tag |
| 8 | + |
| 9 | + |
| 10 | +# RFC 5435 |
| 11 | +class CommandNotify(Command): |
| 12 | + |
| 13 | + RULE_IDENTIFIER = 'NOTIFY' |
| 14 | + TAGGED_ARGS = { |
| 15 | + 'from': Tag('FROM', (StringList(1),)), |
| 16 | + 'importance': Tag('IMPORTANCE', (StringList(1),)), |
| 17 | + 'options': Tag('OPTIONS', (StringList(),)), |
| 18 | + 'message': Tag('MESSAGE', (StringList(1),)), |
| 19 | + } |
| 20 | + POSITIONAL_ARGS = [ |
| 21 | + StringList(length=1) |
| 22 | + ] |
| 23 | + |
| 24 | + def __init__(self, arguments=None, tests=None, block=None): |
| 25 | + super(CommandNotify, self).__init__(arguments, tests, block) |
| 26 | + |
| 27 | + self.notify_from = self.notify_importance = self.notify_message = None |
| 28 | + self.notify_options = [] |
| 29 | + if 'from' in self.tagged_args: |
| 30 | + self.notify_from = self.tagged_args['from'][1][0] |
| 31 | + if 'importance' in self.tagged_args: |
| 32 | + self.notify_importance = self.tagged_args['importance'][1][0] |
| 33 | + if 'options' in self.tagged_args: |
| 34 | + self.notify_options = self.tagged_args['options'][1] |
| 35 | + if 'message' in self.tagged_args: |
| 36 | + self.notify_message = self.tagged_args['message'][1][0] |
| 37 | + self.notify_method = self.positional_args[0][0] |
| 38 | + |
| 39 | + def evaluate(self, message, state): |
| 40 | + state.check_required_extension('enotify', 'NOTIFY') |
| 41 | + notify_from = sifter.grammar.string.expand_variables(self.notify_from, state) |
| 42 | + notify_importance = sifter.grammar.string.expand_variables(self.notify_importance, state) |
| 43 | + notify_options = map(lambda s: sifter.grammar.string.expand_variables(s, state), self.notify_options) |
| 44 | + notify_message = sifter.grammar.string.expand_variables(self.notify_message, state) |
| 45 | + notify_method = sifter.grammar.string.expand_variables(self.notify_method, state) |
| 46 | + |
| 47 | + m = re.match('^([A-Za-z][A-Za-z0-9.+-]*):', notify_method) |
| 48 | + if not m: |
| 49 | + raise sifter.grammar.RuleSyntaxError("Notification method must be an URI, e.g. 'mailto:email@example.com'") |
| 50 | + if notify_importance and notify_importance not in ["1", "2", "3"]: |
| 51 | + raise sifter.grammar.RuleSyntaxError("Illegal notify importance '%s' encountered" % self.notify_importance) |
| 52 | + notify_method_cls = sifter.grammar.notificationmethod.get_cls(m.group(1).lower()) |
| 53 | + if not notify_method_cls: |
| 54 | + raise sifter.grammar.RuleSyntaxError("Unsupported notification method '%s'" % m.group(1)) |
| 55 | + (res, msg) = notify_method_cls.test_valid(notify_method) |
| 56 | + if not res: |
| 57 | + raise sifter.grammar.RuleSyntaxError(msg) |
| 58 | + |
| 59 | + state.actions.append('notify', (notify_method, notify_from, notify_importance, notify_options, notify_message)) |
0 commit comments