simple_notify.py 1.49 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
4
import pipes
louiz’'s avatar
louiz’ committed
5 6 7 8 9

class Plugin(BasePlugin):
    def init(self):
        self.add_event_handler('private_msg', self.on_private_msg)
        self.add_event_handler('conversation_msg', self.on_conversation_msg)
10
        self.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 28 29 30 31
    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
        command = self.config.get('command', '').strip()
        if not command:
            self.core.information('No notification command was provided in the configuration file', 'Warning')
            return
32
        self.core.exec_command(command % {'body':pipes.quote(body), 'from':pipes.quote(fro)})
33 34 35
        after_command = self.config.get('after_command', '').strip()
        if not after_command:
            return
36
        delayed_event = DelayedEvent(self.config.get('delay', 1), self.core.exec_command, after_command % {'body':pipes.quote(body), 'from':pipes.quote(fro)})
37
        self.core.add_timed_event(delayed_event)
38
4