Ajouter un commentaire

Share and options

Playing with Python, D-Bus and Pidgin

Since last week, I finally can use my own domain name as jabber domain Yay! Thanks to http://jabber.apinc.org public free server, I used some DNS SRV records, and with the agreement of server owners let them handle my domain.
Now, the boring phase begins, I have to tell all my contacts to use the new XMPP screen name instead of the old @gmail.com one I was using before. The fact is I have *a lot* of contacts, even some of them which I don't really talked to. But they are all people I know, I like, or I work with, so I can't afford to loose some of them.

So, what about a bit of automation?

Let's start with the XMPP client I use, Pidgin. What's nice with pidgin, it's libpurple, and its D-Bus interface.

When doing a quick overlook on the Pidgin D-Bus howto page, we see that the API is quite complete. So I decided to start a small python script. My first goal was to send a simple text message that says to my contacts "Hi, I'm changing of XMPP account, please update your contact list!".

The first step here was to listen at the "buddy-signed-on" signal, if the buddy is present within the old account's contact list, but not in the new one's, send him the message, this until he suddenly decides to add my new account to his contact list. Ok, it works, but something is missing here.
Because my script is meant to run when I'm nearby my screen (because Pidgin has to be turned on to emit D-Bus signals) I did some nice desktop notifications using libnotify via python pynotify package.

Doing some tests with a friend, he finally told me that the message was annoying and intrusive, and my script should run the add authorization request itself. Well, it's a great idea. The fact is while reading the Pidgin D-Bus howto, no function about that exists, not even function that allow to add new buddies to some account contact list. So I did some Pidgin source code and documentation reading, until I saw in some *enormous* C file that the full libpurple API is exposed through D-Bus. Whaw! I'd never expect this, now, using the libpurple API, I did a nice python script which is able to connect to Pidgin, use its events, do the contact lists diff between old and new accounts, and wait for my buddies to connect. At each buddy connection, it creates a new buddy cloning the old one, appends it in my Pidgin BList, in the same group as the old one was, adds him in my new account's server side contact list (which automatically send the buddy an authorization request on his client), then, when the contact accepts, removes it from the wait list and continues to listen for the new buddies!

Quite easy, despite the fact it could be much more efficient, by adding all my contacts from the old account to the new doing a one shot run! But, I don't want this, waiting for them to connect allow me to monitor what really happen in my script (yeah, I'm a g33k), and also let me see who in this big list does not use their account anymore.

That's all, source code attached below. It currently uses gtk/pygtk (systray icon), libnotify/pynotify (desktop notifications), dbus/gobject (Pidgin/libpurple interface) and some other nice stuff.

In order to use my script, you first have to create a 'pidgin' directory and put the __init__.py into, I'm too lazy to do a nice tarball.

Fichier attachéTaille
__init__.py7.37 KB
pidgin_account_migration.py8.29 KB