GajimDBus: monitor_dbus.2.py

File monitor_dbus.2.py, 1.5 kB (added by bluegraydragon@…, 2 years ago)

This python script will monitor dbus and do various useful things on gajim events

Line 
1#!/usr/bin/python
2
3# This will monitor dbus and do various useful things on gajim events.
4#
5# bluegraydragon@gmail.com
6
7import gobject, os
8import dbus
9if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
10    import dbus.glib
11
12OBJ_PATH = '/org/gajim/dbus/RemoteObject'
13INTERFACE = 'org.gajim.dbus.RemoteInterface'
14SERVICE = 'org.gajim.dbus'
15
16def check_mail(details):
17    print '%s has %s new emails' %(details[1][0], details[1][1])
18    os.system('/usr/bin/fetchmail')     # fetchmail daemon mode works well here
19
20def print_contact(details):
21    print '%s is online' %details[1][0]
22    if details[1][0].startswith('mygirlfriend'):
23        os.system('alsaplayer -l 0.2 -i text "oursong.mp3"')     # do something special for someone special ;)
24
25def init(service, arg1, arg2):
26    if service == 'org.gajim.dbus':
27        bus = dbus.SessionBus()
28        try:
29            bus.add_signal_receiver(check_mail, 'NewGmail', INTERFACE, SERVICE, OBJ_PATH)
30            bus.add_signal_receiver(print_contact, 'ContactPresence', INTERFACE, SERVICE, OBJ_PATH)
31            print 'added gajim signal receivers'
32        except:
33            print 'oops, no gajim'
34
35
36def main():
37    bus = dbus.SessionBus()
38    bus.add_signal_receiver(init, 'NameOwnerChanged',
39            'org.freedesktop.DBus',
40            'org.freedesktop.DBus',
41            '/org/freedesktop/DBus',
42            arg0='org.gajim.dbus')   # monitor changes in gajim dbus signal
43
44    init('org.gajim.dbus', '', '')
45   
46    print 'listening to dbus...'
47    mainloop = gobject.MainLoop()
48    mainloop.run()
49               
50if __name__ == '__main__':
51    import sys
52    try:
53        main()
54    except KeyboardInterrupt:
55        sys.exit(0)