2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2005-2009 Alexander Bernauer <alex@copton.net>
4 # Copyright (C) 2005-2009 Rico Schiekel <fire@downgra.de>
5 # Copyright (C) 2005-2009 Ulrich Dangel <uli@spamt.net>
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
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.
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.
23 example of ~/.notifyd.conf:
24 ---------------------------
30 logfile = os.path.expanduser("~/.event.log")
31 osdparams = "-p bottom --color=red --delay=4 --age=4 " \
32 "--font=\-\*\-helvetica\-medium\-r\-\*\-\*\-34\-\*\-\*\-\*\-\*\-\*\-\*\-15 " \
33 "--offset=100 --shadow=0 --lines=5 --align=right --indent=100"
36 ("IRC:&bitlbee:bitlbee", [], False),
37 (".*shutdown.*", [command('sudo shutdown -h now %(msg)s')], False),
38 (".*", [libnotify], False),
52 default_hostname = 'localhost'
54 default_osd_params = osdparams = "-p bottom --color=red --delay=4 --age=4 " \
55 "--font=\-\*\-helvetica\-medium\-r\-\*\-\*\-34\-\*\-\*\-\*\-\*\-\*\-\*\-15 " \
56 "--offset=100 --shadow=0 --lines=5 --align=right --indent=100"
57 default_logfile = None
61 def play_wrapper(msg):
62 with open(os.devnull, 'w') as devnull:
63 subprocess.Popen(['/usr/bin/aplay', sound_file], stderr=devnull)
67 def command_wrapper(msg):
68 subprocess.call(command % dict(msg = msg))
69 return command_wrapper
72 osdcmd = "/usr/bin/osd_cat"
73 osdpipe = os.popen("%s %s" % (osdcmd, osdparams), 'w')
81 sys.stderr.write('Please install python-dbus\n')
84 bus = dbus.SessionBus()
85 notifyService = bus.get_object("org.freedesktop.Notifications", '/org/freedesktop/Notifications')
86 interface = dbus.Interface(notifyService, 'org.freedesktop.Notifications')
88 message, title = (':' + msg).split(':')[::-1][0:2]
90 title, message = message, title
91 interface.Notify('notify-server', 0, 'notification-message-im', title, message, [], {'x-canonical-append':'allowed'}, -1)
98 print "osd_server.py [options]"
100 print " -h --help print this message"
101 print " -H --host host of the osd server (def: " + default_hostname + ")"
102 print " -P --port port of the osd server (def: " + str(default_port) + ")"
103 print " -l --log log file ('-' logs to stdout)"
106 env = { 'play' : play,
109 'libnotify' : libnotify,
111 'host' : default_hostname,
112 'port' : default_port,
113 'logfile' : default_logfile,
118 (".*", [libnotify], False),
122 default_bind = (default_hostname, default_port)
125 execfile(os.path.expanduser('~/.notifyd.conf'), {}, env)
130 opts, args = getopt.getopt(sys.argv[1:], "hH:P:l:", ["help", "host=", "port=", "log="])
131 except getopt.GetoptError:
135 for opt, arg in opts:
136 if opt in ("-h", "--help"):
139 elif opt in ("-H", "--host"):
141 elif opt in ("-P", "--port"):
142 env['port'] = int(arg)
143 elif opt in ("-l", "--log"):
147 actions = env.get('actions', default_actions)
148 logfile_name = env.get('logfile')
149 logfile_format = env.get('logformat', '%(asctime)s %(message)s')
150 bind_address = (env['host'], env['port'])
151 osd_params = env.get('osdparams', default_osd_params)
154 logger = logging.getLogger('notify_server')
155 lformatter = logging.Formatter(logfile_format)
156 if logfile_name not in ('', '-'):
157 lfh = logging.FileHandler(logfile_name)
158 lfh.setFormatter(lformatter)
159 logger.addHandler(lfh)
161 lout = logging.StreamHandler(sys.stdout)
162 lout.setFormatter(lformatter)
163 logger.addHandler(lout)
164 logger.setLevel(logging.INFO)
168 l = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
169 l.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
174 return c in string.printable + "äöüßÄÖÜ" and c or '_'
178 (con, addr) = l.accept()
181 data = con.recv(50).strip()
184 log = ''.join(filter_char(c) for c in data)
186 for pattern, handlers, cont in actions:
187 if re.match(pattern, log):
188 for handler in handlers: