Add new notifyd as replacement for osd_server.py
authorUlrich Dangel <uli@spamt.net>
Thu, 22 Oct 2009 20:00:14 +0000 (22:00 +0200)
committerUlrich Dangel <uli@spamt.net>
Thu, 22 Oct 2009 21:50:35 +0000 (23:50 +0200)
Remove old osd_server.py

debian/overrides
usr_bin/notifyd.py [new file with mode: 0755]
usr_bin/osd_server.py [deleted file]

index a9ebd8b..fcd045e 100644 (file)
@@ -9,6 +9,7 @@ grml-scripts: script-with-language-extension usr/bin/hgrep.sh
 grml-scripts: script-with-language-extension usr/bin/irclog2html-2.1.pl
 grml-scripts: script-with-language-extension usr/bin/lesspipe.sh
 grml-scripts: script-with-language-extension usr/bin/osd_server.py
+grml-scripts: script-with-language-extension usr/bin/notifyd.py
 grml-scripts: script-with-language-extension usr/bin/sepdate.pl
 grml-scripts: script-with-language-extension usr/bin/sepdate.rb
 grml-scripts: script-with-language-extension usr/bin/sepdate.sh
diff --git a/usr_bin/notifyd.py b/usr_bin/notifyd.py
new file mode 100755 (executable)
index 0000000..cf94ee6
--- /dev/null
@@ -0,0 +1,189 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2005-2009 Alexander Bernauer <alex@copton.net>
+# Copyright (C) 2005-2009 Rico Schiekel <fire@downgra.de>
+# Copyright (C) 2005-2009 Ulrich Dangel <uli@spamt.net>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation version 2
+# of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA # 02111-1307, USA.
+
+
+"""
+example of ~/.notifid.conf:
+---------------------------
+
+import os
+
+host = "127.0.0.1"
+port = 8901
+logfile = os.path.expanduser("~/.event.log")
+osdparams = "-p bottom  --color=red --delay=4 --age=4 " \
+ "--font=\-\*\-helvetica\-medium\-r\-\*\-\*\-34\-\*\-\*\-\*\-\*\-\*\-\*\-15 " \
+     "--offset=100 --shadow=0 --lines=5 --align=right --indent=100"
+actions = (
+    (".*", [log], True),
+    ("IRC:&bitlbee:bitlbee", [], False),
+    (".*shutdown.*", [command('sudo shutdown -h now %(msg)s')], False),
+    (".*", [libnotify], False),
+)
+
+"""
+
+import os
+import sys
+import re
+import string
+import socket
+import logging
+import getopt
+
+default_hostname = 'localhost'
+default_port = 1234
+default_osd_params = osdparams = "-p bottom  --color=red --delay=4 --age=4 " \
+           "--font=\-\*\-helvetica\-medium\-r\-\*\-\*\-34\-\*\-\*\-\*\-\*\-\*\-\*\-15 " \
+            "--offset=100 --shadow=0 --lines=5 --align=right --indent=100"
+default_logfile = None
+
+
+def play(sound_file):
+    def play_wrapper(msg):
+        os.system('/usr/bin/aplay "%s" 2> /dev/null &' % sound_file)
+    return play_wrapper
+
+def execute(command):
+    def command_wrapper(msg):
+        os.system(command % dict(msg = msg))
+    return command_wrapper
+
+def osd(msg):
+    osdcmd = "/usr/bin/osd_cat"
+    osdpipe = os.popen("%s %s" % (osdcmd, osdparams), 'w')
+    osdpipe.write(msg)
+    osdpipe.close()
+
+def libnotify(msg):
+    try:
+        import dbus
+    except ImportError:
+        sys.stderr.write('Please install python-dbus\n')
+        raise SystemExit(1)
+
+    bus = dbus.SessionBus()
+    notifyService = bus.get_object("org.freedesktop.Notifications", '/org/freedesktop/Notifications')
+    interface = dbus.Interface(notifyService, 'org.freedesktop.Notifications')
+
+    message, title = (':' + msg).split(':')[::-1][0:2]
+    if not title:
+        title, message = message, title
+    interface.Notify('notify-server', 0, 'notification-message-im', title, message, [], {'x-canonical-append':'allowed'}, -1)
+
+def log(msg):
+    if logger:
+        logger.info(msg)
+
+def syntax():
+    print "osd_server.py [options]"
+    print "   options:"
+    print "     -h --help       print this message"
+    print "     -H --host       host of the osd server (def: " + default_hostname + ")"
+    print "     -P --port       port of the osd server (def: " + str(default_port) + ")"
+    print "     -l --log        log file ('-' logs to stdout)"
+
+
+env = { 'play' : play,
+        'execute' : execute,
+        'osd' : osd,
+        'libnotify' : libnotify,
+        'log' : log,
+        'host' : default_hostname,
+        'port' : default_port,
+        'logfile' : default_logfile,
+        }
+
+default_actions = (
+    (".*", [log], True),
+    (".*", [libnotify], False),
+)
+
+
+default_bind = (default_hostname, default_port)
+
+try:
+    execfile(os.path.expanduser('~/.notifyd.conf'), {}, env)
+except IOError:
+    pass
+
+try:
+    opts, args = getopt.getopt(sys.argv[1:], "hH:P:l:", ["help", "host=", "port=", "log="])
+except getopt.GetoptError:
+    syntax()
+    sys.exit(2)
+
+for opt, arg in opts:
+    if opt in ("-h", "--help"):
+        syntax()
+        sys.exit(3)
+    elif opt in ("-H", "--host"):
+        env['host'] = arg
+    elif opt in ("-P", "--port"):
+        env['port'] = int(arg)
+    elif opt in ("-l", "--log"):
+        env['logfile'] = arg
+
+
+actions = env.get('actions', default_actions)
+logfile_name = env.get('logfile')
+logfile_format = env.get('logformat', '%(asctime)s %(message)s')
+bind_address = (env['host'], env['port'])
+osd_params = env.get('osdparams', default_osd_params)
+
+if logfile_name:
+    logger = logging.getLogger('notify_server')
+    lformatter = logging.Formatter(logfile_format)
+    if logfile_name not in ('', '-'):
+        lfh = logging.FileHandler(logfile_name)
+        lfh.setFormatter(lformatter)
+        logger.addHandler(lfh)
+    else:
+        lout = logging.StreamHandler(sys.stdout)
+        lout.setFormatter(lformatter)
+        logger.addHandler(lout)
+    logger.setLevel(logging.INFO)
+else:
+    logger = None
+
+l = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+l.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+l.bind(bind_address)
+l.listen(5)
+
+def filter_char(c):
+    return c in string.printable + "äöüßÄÖÜ" and c or '_'
+
+while 1:
+    try:
+        (con, addr) = l.accept()
+    except:
+        continue
+    data = con.recv(50).strip()
+    con.close()
+
+    log = ''.join(filter_char(c) for c in data)
+
+    for pattern, handlers, cont in actions:
+        if re.match(pattern, log):
+            for handler in handlers:
+                handler(log)
+            if not cont:
+                break
diff --git a/usr_bin/osd_server.py b/usr_bin/osd_server.py
deleted file mode 100755 (executable)
index a35cacf..0000000
+++ /dev/null
@@ -1,99 +0,0 @@
-#!/usr/bin/python
-# -*- coding: utf-8 -*-
-# Copyright (C) 2005 2006 Alexander Bernauer <alex@copton.net>
-# Copyright (C) 2005 2006 Rico Schiekel <fire@donwgra.de>
-# Copyright (C) 2005 2006 Ulrich Dangel <uli@spamt.net>
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation version 2
-# of the License.
-# 
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-# 
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA # 02111-1307, USA.
-#
-
-import sys, getopt, logging
-from socket import *
-from select import select
-try: 
-    import dbus
-except ImportError:
-    print >>sys.stderr, 'Please install python-dbus'
-    raise SystemExit(1)
-
-
-host='localhost'
-port=1234
-timeout=5
-
-def syntax():
-    print "osd_server.py [options]"
-    print "   options:"
-    print "     -h --help       print this message"
-    print "     -H --host       host of the osd server (def: " + host + ")"
-    print "     -P --port       port of the osd server (def: " + str(port) + ")"
-    print "     -t --timeout    timeout in seconds (def: " + str(timeout) + ")"
-    print "     -l --log        log file ('-' logs to stdout)"
-
-logfile_name = ''
-
-try:
-    opts, args = getopt.getopt(sys.argv[1:], "hH:P:l:t:", ["help", "host=", "port=", "log=", 'timeout='])
-except getopt.GetoptError:
-    syntax()
-    sys.exit(2)
-
-for opt, arg in opts:
-    if opt in ("-h", "--help"):
-        syntax()
-        sys.exit(3)
-    elif opt in ("-H", "--host"):
-        host = arg
-    elif opt in ("-P", "--port"):
-        port = int(arg)
-    elif opt in ("-l", "--log"):
-        logfile_name = arg
-    elif opt in ("-p", "--timeout"):
-        timeout=int(arg)
-
-l = socket(AF_INET, SOCK_STREAM)
-l.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
-l.bind((host, port))
-l.listen(5)
-
-logger = logging.getLogger('osd_server')
-lformatter = logging.Formatter('%(asctime)s %(message)s')
-if logfile_name not in ('', '-'):
-    lfh = logging.FileHandler(logfile_name)
-    lfh.setFormatter(lformatter)
-    logger.addHandler(lfh)
-else:
-    lout = logging.StreamHandler(sys.stdout)
-    lout.setFormatter(lformatter)
-    logger.addHandler(lout)
-logger.setLevel(logging.INFO)
-
-logger.info("osd_server running on [%s] port [%d]" % (host, port))
-
-bus = dbus.SessionBus()
-devobj = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
-notify = dbus.Interface(devobj, 'org.freedesktop.Notifications')
-
-while 1:
-    (con, addr) = l.accept()
-    message = con.recv(60).strip()
-    con.close()
-
-    message = message.splitlines()
-    if message:
-        body = ' '.join([m for m in message[1:]])
-        notify.Notify('', 0, '', message[0], body, '', [], timeout*1000)
-        logger.info(message)
-