simple_notify.py 1.63 KB
Newer Older
1
from plugin import BasePlugin
louiz’'s avatar
louiz’ committed
2
from xhtml import clean_text, get_body_from_message_stanza
3
from timed_events import DelayedEvent
louiz’'s avatar
louiz’ committed
4
import shlex
louiz’'s avatar
louiz’ committed
5 6 7

class Plugin(BasePlugin):
    def init(self):
8 9 10
        self.api.add_event_handler('private_msg', self.on_private_msg)
        self.api.add_event_handler('conversation_msg', self.on_conversation_msg)
        self.api.add_event_handler('highlight', self.on_highlight)
louiz’'s avatar
louiz’ committed
11 12 13 14 15

    def on_private_msg(self, message, tab):
        fro = message['from']
        self.do_notify(message, fro)

16 17 18 19
    def on_highlight(self, message, tab):
        fro = message['from'].resource
        self.do_notify(message, fro)

louiz’'s avatar
louiz’ committed
20 21 22 23 24 25 26 27
    def on_conversation_msg(self, message, tab):
        fro = message['from'].bare
        self.do_notify(message, fro)

    def do_notify(self, message, fro):
        body = clean_text(get_body_from_message_stanza(message))
        if not body:
            return
louiz’'s avatar
louiz’ committed
28 29
        command_str = self.config.get('command', '').strip()
        if not command_str:
30
            self.api.information('No notification command was provided in the configuration file', 'Warning')
louiz’'s avatar
louiz’ committed
31
            return
louiz’'s avatar
louiz’ committed
32 33 34 35
        command = [arg % {'body': body.replace('\n', ' '), 'from': fro} for arg in shlex.split(command_str)]
        self.core.exec_command(command)
        after_command_str = self.config.get('after_command', '').strip()
        if not after_command_str:
36
            return
louiz’'s avatar
louiz’ committed
37 38
        after_command = [arg % {'body': body.replace('\n', ' '), 'from': fro} for arg in shlex.split(after_command_str)]
        delayed_event = DelayedEvent(self.config.get('delay', 1), self.core.exec_command, after_command)
39
        self.api.add_timed_event(delayed_event)