Another rename
[darcs-mirror-pam-dbus.git] / handlers / pam-dbus-notify
1 #!/usr/bin/python
2
3 import pygtk
4 import gtk
5 import gobject
6
7 import dbus
8 import dbus.service
9 import dbus.glib
10
11 import pynotify
12
13 # list of Notifications around, kept to prevent the garbage collector to handle them.
14 ns = []
15
16 def notify_cb(n, action, pam_cb):
17     if pam_cb is not None:
18         if action == "permit":
19             pam_cb(True)
20         else:
21             pam_cb(False)
22         pam_cb = None
23     ns.remove(n)
24
25 # Initializite pynotify
26 if not pynotify.init("pam-dbus"):
27     sys.exit(1)
28
29 # dbus object to answer the request
30 class PamDbusAnswer(dbus.service.Object):
31      def __init__(self, bus_name, object_path="/de/nomeata/pam_dbus"):
32          dbus.service.Object.__init__(self, bus_name, object_path)
33  
34      @dbus.service.method(dbus_interface='de.nomeata.pam_dbus',
35                           in_signature='ssss', out_signature='b',
36                           async_callbacks=('ret_cb', 'error_cb'))
37      def MayLogin(self, service, user, rhost, ruser, ret_cb, error_cb):
38
39          n = pynotify.Notification("Login request","Someone " + 
40                             ("named %s " % ruser if ruser else "") +
41                             "tries to log in via %s " % service + 
42                             ("from %s " % rhost if rhost else "") + 
43                             "as user %s. " % user + 
44                             "Do you want to allow that?")
45
46          n.set_timeout(5*60*1000)
47          n.add_action("permit", "Permit login", notify_cb, ret_cb)
48          n.add_action("deny", "Deny login", notify_cb,ret_cb)
49          n.connect("closed",lambda n: notify_cb(n,"closed", ret_cb))
50
51          if not n.show():
52              print "Failed to send notification"
53              sys.exit(1)
54
55          ns.append(n)
56  
57 system_bus = dbus.SystemBus()
58 name = dbus.service.BusName("de.nomeata.pam_dbus", bus=system_bus)
59 object = PamDbusAnswer(name)
60
61 gtk.main()
62