View Single Post
Posts: 82 | Thanked: 214 times | Joined on Jan 2010 @ Cape town
#1
I remember seeing a post about this recently, but I can't seem to find it... Anyhow:

Having set up a SIP account for an internal SIP server on my N900, it was quite irritating to have to constantly disable it after leaving WiFi to stop the messages about not being able to connect. So, I wrote a quick script which allows mapping of IM accounts to connections.

I've hardly tested it, but it works for me, and it's a kludgey but good enough hack until this feature (hopefully) gets integrated.

The script:
Code:
#!/usr/bin/env python
import conic
import dbus
import gobject
import dbus.glib

accounts = {
"account": ["connection1", "connection2"],

}

loop = gobject.MainLoop()
bus = dbus.SessionBus()


def state(account, new):
    accountd = bus.get_object('org.freedesktop.Telepathy.AccountManager', account)
    iface = dbus.Interface(accountd, 'org.freedesktop.DBus.Properties')
    iface.Set("org.freedesktop.Telepathy.Account", "Enabled", new)

def connection_cb(connection, event):
    if event.get_status() == conic.STATUS_CONNECTED:
        iap_id = event.get_iap_id()
        for account in accounts:
            state(account, iap_id in accounts[account])

connection = conic.Connection()
connection.connect("connection-event", connection_cb)
connection.set_property("automatic-connection-events", True)

loop.run()
Configuration is simple, change the dictionary "accounts" with whatever details you want.

To list your accounts, use the command
Code:
/usr/bin/dbus-send --type=method_call --print-reply --dest=org.freedesktop.Telepathy.AccountManager /org/freedesktop/Telepathy/AccountManager org.freedesktop.DBus.Properties.Get string:org.freedesktop.Telepathy.AccountManager string:ValidAccounts
And for the connections, either use the name of the connection (for GPRS / 3G) or the unique identifier for WLAN. To get the identifier, run the following bit of Python:

Code:
import conic

iaps = conic.Connection().get_all_iaps()
for iap in iaps:
    print "%s - %s" % (iap.get_name(), iap.get_id())
For example, for me, I'd use the following to have my GMail account only enabled on WiFi:

Code:
accounts = {
"/org/freedesktop/Telepathy/Account/gabble/jabber/florenzi_40gmail_2ecom0": ["ffcd0203-51ae-4bde-9622-d0256937b11b"],
}
And then fire it up in the Terminal as a normal user. You need python-dbus and python-conic installed.

Hope this helps some people out, if people could also test it and report back that would be great. It shouldn't really take too long to write a simple GUI and package this up properly. Also, other things, like only on WiFi / only on GPRS should be quite easy to implement.
 

The Following 6 Users Say Thank You to cb22 For This Useful Post: