2012-05-05 16:26:54 +00:00
|
|
|
from plugin import BasePlugin
|
2012-01-10 15:32:29 +00:00
|
|
|
from xhtml import clean_text, get_body_from_message_stanza
|
2012-07-02 23:59:25 +00:00
|
|
|
from timed_events import DelayedEvent
|
2012-10-22 15:14:21 +00:00
|
|
|
import shlex
|
2012-01-10 15:32:29 +00:00
|
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
def init(self):
|
2013-03-08 21:53:35 +00:00
|
|
|
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)
|
2012-01-10 15:32:29 +00:00
|
|
|
|
|
|
|
def on_private_msg(self, message, tab):
|
|
|
|
fro = message['from']
|
|
|
|
self.do_notify(message, fro)
|
|
|
|
|
2012-07-02 23:59:25 +00:00
|
|
|
def on_highlight(self, message, tab):
|
|
|
|
fro = message['from'].resource
|
|
|
|
self.do_notify(message, fro)
|
|
|
|
|
2012-01-10 15:32:29 +00:00
|
|
|
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
|
2012-10-22 15:14:21 +00:00
|
|
|
command_str = self.config.get('command', '').strip()
|
|
|
|
if not command_str:
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.information('No notification command was provided in the configuration file', 'Warning')
|
2012-01-10 15:32:29 +00:00
|
|
|
return
|
2012-10-22 15:14:21 +00:00
|
|
|
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:
|
2012-07-02 23:59:25 +00:00
|
|
|
return
|
2012-10-22 15:14:21 +00:00
|
|
|
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)
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.add_timed_event(delayed_event)
|