Andreas Schuldei asked how to hook into gnome-power-manager.

I’m assuming you want to do some user hooks, too; the solutions using the low level hooks in /etc won’t help you then. Using a small user daemon that listens for DBus events is much nicer (though you won’t be able to have the suspend process wait for you to finish…) - because every user can setup his own stuff.

It would be cool to extend the following to a more general solution. I could imagine a combination of a dbus-monitor (logging incoming signals) and a general event handler. You could then either type in the signal you want to hook or choose one of the logged signals, setup some properties (e.g. the SSID you just connected to) and setup reactions (start VPN script etc.).

I havn’t hooked gnome power manager yet, but I’ve been doing this for NetworkManager. Here’s a python script:

#!/usr/bin/python
import dbus, dbus.glib
 
def device_now_active(path, ssid=None):
  pass
 
def device_no_longer_active(path):
  pass
 
sysbus = dbus.SystemBus()
nm_obj = sysbus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
nm_if = dbus.Interface(nm_obj, 'org.freedesktop.NetworkManager')
 
nm_if.connect_to_signal('DeviceNowActive', device_now_active)
nm_if.connect_to_signal('DeviceNoLongerActive', device_no_longer_active)
 
mainloop = gobject.MainLoop()
mainloop.run()

To hook to gnome-power-manager, use dbus-monitor to find out the appropriate signals to connect to.

I’ve been using this to start a VPN client when connecting to some wireless networks, and to have gaim automatically logon when network is available:

sesbus = dbus.SessionBus()
gaim_obj = sesbus.get_object("net.sf.gaim.GaimService", "/net/sf/gaim/GaimObject")
gaim = dbus.Interface(gaim_obj, "net.sf.gaim.GaimInterface")
 
# then use
gaim.GaimConnectionsDisconnectAll()
# or
accounts = gaim.GaimAccountsGetAllActive()
for account in accounts:
  gaim.GaimAccountConnect(account)

Dynamic languages like python can be very sweet to use.