python + dbus FTW!!!

was just thinking it’d be nice to be able to push a button to copy my current activity from Hamster to my work XMPP status in Empathy.  couldn’t find any such options or plugins for either app.  but with dbus (and dfeet) and python, how hard can it be?  turns out, this hard:

#!/usr/bin/python

# get status from hamster, write it to empathy

myjid = "firstname.surname@example.com/" # my XMPP node and domain - any XMPP resource

from dbus import SessionBus

def busAsPath(bus):
  return "/" + bus.replace(".", "/")

def jidToDbus(jid):
  return jid.replace(".", "_2e").replace("@", "_40").replace("/", "_2f")

ogh = "org.gnome.Hamster"
mybus = "org.freedesktop.Telepathy.Connection.gabble.jabber." + jidToDbus(myjid)
sb = SessionBus()

fact = sb.get_object(ogh, busAsPath(ogh)).GetCurrentFact()['name']
for bus in sb.list_names():
  if bus.startswith(mybus):
    sb.get_object(bus, busAsPath(bus)).SetPresence("available", fact,
    dbus_interface = "org.freedesktop.Telepathy.Connection.Interface.SimplePresence")

stuck that in a file, made a custom launcher for it on the gnome panel and put that next to hamster.  lovely.

this only sets the status on that one account. if you’ve got multiple accounts which can have their presence set then empathy won’t change what it displays in its status selector until they all match. easily done by removing the account-specific code and applying the SetPresence to every bus which startswith(“org.freedesktop.Telepathy.Connection.”) – then you need a try/pass to ignore any exceptions generated by attempting to SetPresence on accounts which don’t have one. 🙂

Leave a comment