grml-exec-wrapper: add double quotes around $@
[grml-scripts.git] / usr_bin / osd_server.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2005 2006 Alexander Bernauer <alex@copton.net>
4 # Copyright (C) 2005 2006 Rico Schiekel <fire@donwgra.de>
5 # Copyright (C) 2005 2006 Ulrich Dangel <uli@spamt.net>
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation version 2
10 # of the License.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA # 02111-1307, USA.
20 #
21
22 import sys, getopt, logging
23 from socket import *
24 from select import select
25 try: 
26     import dbus
27 except ImportError:
28     print >>sys.stderr, 'Please install python-dbus'
29     raise SystemExit(1)
30
31
32 host='localhost'
33 port=1234
34 timeout=5
35
36 def syntax():
37     print "osd_server.py [options]"
38     print "   options:"
39     print "     -h --help       print this message"
40     print "     -H --host       host of the osd server (def: " + host + ")"
41     print "     -P --port       port of the osd server (def: " + str(port) + ")"
42     print "     -t --timeout    timeout in seconds (def: " + str(timeout) + ")"
43     print "     -l --log        log file ('-' logs to stdout)"
44
45 logfile_name = ''
46
47 try:
48     opts, args = getopt.getopt(sys.argv[1:], "hH:P:l:t:", ["help", "host=", "port=", "log=", 'timeout='])
49 except getopt.GetoptError:
50     syntax()
51     sys.exit(2)
52
53 for opt, arg in opts:
54     if opt in ("-h", "--help"):
55         syntax()
56         sys.exit(3)
57     elif opt in ("-H", "--host"):
58         host = arg
59     elif opt in ("-P", "--port"):
60         port = int(arg)
61     elif opt in ("-l", "--log"):
62         logfile_name = arg
63     elif opt in ("-p", "--timeout"):
64         timeout=int(arg)
65
66 l = socket(AF_INET, SOCK_STREAM)
67 l.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
68 l.bind((host, port))
69 l.listen(5)
70
71 logger = logging.getLogger('osd_server')
72 lformatter = logging.Formatter('%(asctime)s %(message)s')
73 if logfile_name not in ('', '-'):
74     lfh = logging.FileHandler(logfile_name)
75     lfh.setFormatter(lformatter)
76     logger.addHandler(lfh)
77 else:
78     lout = logging.StreamHandler(sys.stdout)
79     lout.setFormatter(lformatter)
80     logger.addHandler(lout)
81 logger.setLevel(logging.INFO)
82
83 logger.info("osd_server running on [%s] port [%d]" % (host, port))
84
85 bus = dbus.SessionBus()
86 devobj = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
87 notify = dbus.Interface(devobj, 'org.freedesktop.Notifications')
88
89 while 1:
90     (con, addr) = l.accept()
91     message = con.recv(60).strip()
92     con.close()
93
94     message = message.splitlines()
95     if message:
96         body = ' '.join([m for m in message[1:]])
97         notify.Notify('', 0, '', message[0], body, '', [], timeout*1000)
98         logger.info(message)
99