Initial release
authorMikael Berthe <mikael@lilotux.net>
Sat, 21 Jun 2008 15:50:16 +0200
changeset 0 bb4902252888
child 1 000989fcd3d3
Initial release
mcevent.cfg
mcevent.py
sound.wav
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcevent.cfg	Sat Jun 21 15:50:16 2008 +0200
@@ -0,0 +1,21 @@
+[Notifications]
+notify: 0
+voice: 0
+sound: 1
+short_nick: 1
+
+[Contacts]
+#jid@domain.org: name
+
+# Alerts:
+# 0 = no alert
+# 1 = normal alert
+# 2 = persistent alert
+[Alerts]
+#jid@domain.org: 1
+
+[Voicemap]
+#jid@domain.org: VoiceName
+
+[Blacklist]
+#jid@domain.org: 0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcevent.py	Sat Jun 21 15:50:16 2008 +0200
@@ -0,0 +1,216 @@
+#! /usr/bin/env python
+# -*- coding: iso-8859-15 -*-
+#
+#  Copyright (C) 2007 Adam Wolk "Mulander" <netprobe@gmail.com>
+#  Copyright (C) 2007, 2008 Mikael Berthe "McKael" <mikael@lilotux.net>
+#
+# This script is provided under the terms of the GNU General Public License,
+# see the file COPYING in the root mcabber source directory.
+#
+
+import sys
+import pynotify
+
+CONFFILE = "mcevent.cfg"
+
+# Default option values
+opt = {
+        'use_notify': 0,
+        'use_voice':  0,
+        'use_sound':  1,
+        'short_nick': 1,
+}
+
+NOTIFY_TIMEOUT=4000
+
+contact_map = { }
+# Nickname used for the notification
+
+online_alerts = { }
+# 0: disabled  1: normal notification  2: permanent notify box
+
+voicemap = { }
+# Specify the name pronounced by espeak
+
+blacklist = { }
+# No notification for these JIDs
+
+CMD_MSG_IN="/usr/bin/play -V0 -v3 sound.wav"
+CMD_ESPEAK="/usr/bin/espeak"
+
+def init_notify():
+    import locale
+    global encoding
+    global NOTIFY_LOADED
+    pynotify.init('mcnotify')
+    encoding = (locale.getdefaultlocale())[1]
+    NOTIFY_LOADED = True
+
+def read_conf_from_file():
+    import ConfigParser
+
+    config = ConfigParser.ConfigParser()
+    config.read(CONFFILE)
+
+    contact_map.clear()
+    online_alerts.clear()
+    voicemap.clear()
+    blacklist.clear()
+
+    if config.has_option("Notifications", "notify"):
+        opt['use_notify'] = int(config.get("Notifications", "notify"))
+    if config.has_option("Notifications", "voice"):
+        opt['use_voice']  = int(config.get("Notifications", "voice"))
+    if config.has_option("Notifications", "sound"):
+        opt['use_sound']  = int(config.get("Notifications", "sound"))
+    if config.has_option("Notifications", "short_nick"):
+        opt['short_nick'] = int(config.get("Notifications", "short_nick"))
+
+    for id in config.options("Contacts"):
+        contact_map[id] = config.get("Contacts", id)
+
+    for id in config.options("Alerts"):
+        online_alerts[id] = int(config.get("Alerts", id))
+
+    for id in config.options("Voicemap"):
+        voicemap[id] = config.get("Voicemap", id)
+
+    for id in config.options("Blacklist"):
+        blacklist[id] = int(config.get("Blacklist", id))
+
+    if opt['use_notify'] and not NOTIFY_LOADED:
+        init_notify()
+
+def say(buddy, text):
+    import subprocess
+    p = subprocess.Popen(CMD_ESPEAK, stdin=subprocess.PIPE, close_fds=True)
+    child_stdin = p.stdin
+    child_stdin.write(buddy + " " + text)
+    child_stdin.close()
+
+def notify(buddy, msg, timeout):
+    msgbox = pynotify.Notification(unicode(buddy, encoding),
+                                   unicode(msg, encoding))
+    msgbox.set_timeout(timeout)
+    msgbox.set_urgency(pynotify.URGENCY_LOW)
+    msgbox.show()
+
+def get_nick(jid):
+    if jid in contact_map:
+        buddy = contact_map[jid]
+    else:
+        buddy = jid
+
+    if opt['short_nick'] and '@' in buddy:
+        buddy = buddy[0:buddy.index('@')]
+    return buddy
+
+def is_blacklisted(jid):
+    if jid in blacklist and blacklist[jid]:
+        return True
+    return False
+
+def process_line(args):
+    argn = len(args)
+
+    if argn < 2:
+        print "Ignoring invalid event line."
+        return
+    elif argn == 2:
+        event,arg1               = args[0:2]
+        arg2                     = None
+        filename                 = None
+    elif argn == 3:
+        event,arg1,arg2          = args[0:3]
+        filename                 = None
+    else:
+        # For now we simply ignore the file name
+        event,arg1,arg2          = args[0:3]
+        filename                 = None
+
+    if event == 'MSG' and arg1 == 'IN':
+        import os
+
+        jid = arg2
+        buddy = get_nick(jid)
+        msg = 'sent you a message.'
+
+        if not is_blacklisted(jid):
+            textmsg = None
+
+            if filename and os.path.exists(filename):
+                f   = file(filename)
+                textmsg = f.read()
+
+            if opt['use_notify']:
+                if not textmsg:
+                    textmsg = msg
+                notify(buddy, textmsg, NOTIFY_TIMEOUT)
+
+            if opt['use_sound']:
+                os.system(CMD_MSG_IN + '> /dev/null 2>&1')
+
+            if opt['use_voice'] and not is_blacklisted(jid):
+                if jid in voicemap:
+                    buddy = voicemap[jid]
+                say(buddy, msg)
+
+        if filename and os.path.exists(filename):
+            os.remove(filename)
+
+    elif event == 'STATUS':
+        jid = arg2
+        if arg1 == 'O' and jid in online_alerts:
+            import os
+
+            type = online_alerts[jid]
+            if type > 0:
+                if opt['use_sound']:
+                    os.system(CMD_MSG_IN + '> /dev/null 2>&1')
+
+                buddy = get_nick(jid)
+
+                if opt['use_notify']:
+                    if type == 1:
+                        timeout = NOTIFY_TIMEOUT
+                    else:
+                        timeout = pynotify.EXPIRES_NEVER
+                    notify(buddy, "is online", timeout)
+
+                    if filename and os.path.exists(filename):
+                        os.remove(filename)
+
+                if opt['use_voice']:
+                    if jid in voicemap:
+                        buddy = voicemap[jid]
+                    say(buddy, "is online now.")
+
+    elif event == 'UNREAD':
+        # arg1 is the number of unread buffers
+        fileHandle = open('/tmp/event.unread', 'w')
+        fileHandle.write(arg1)
+        fileHandle.close()
+
+
+##### MAIN #####
+
+NOTIFY_LOADED = False
+
+# read stdin line by line
+while 1:
+    try:
+        line = sys.stdin.readline()
+    except KeyboardInterrupt:
+        print "\nInterrupted!"
+        exit(0)
+    if not line:
+        break
+
+    read_conf_from_file()
+    lineargs = line.split()
+    process_line(lineargs)
+
+if NOTIFY_LOADED:
+    pynotify.uninit()
+
+# vim:set et sts=4 sw=4 fileencoding=iso-8859-15:
Binary file sound.wav has changed