Monday, December 23, 2013

Sorting contact groups in Pidgin

Pidgin is a great open source messenger application, and the only one I know which can handle thousands of contacts without choking itself to death. The only feature I miss about it is that you cannot easily sort contact groups by name. The company I work for has their employees arranged in over two hundred groups, and without some kind of automatic ordering it is really hard to find the right one. There is a 7 years old ticket with a request for adding alphabetic group sorting to Pidgin, and it still remains unresolved.

Fortunately, Pidgin stores the contact list (sometimes called roster) in an XML file blist.xml, and there are lots of libraries for different programming languages which help to manipulate XML structures. One of the most interesting scripts I have found was written in Python: it sorts the contact list alphabetically by name, and it's only 12 lines of code. I prepared an enhanced version, which allows you to create a list with predefined priorities, for example:
roster = { 'Family': 1, 'Friends': 2 }
In this case groups Family and Friends will be moved to the top of the list, with group Family as first, Friends as second, and the remaining groups sorted alphabetically in ascending order.

You can download the script here. Some important notes:

  • You have to run it in the same directory Pidgin keep the blist.xml in.
  • Also, turn off the messenger before you run the script, because it keeps the whole contact list in memory and will overwrite version stored on disk once you shut it down.
  • Don't forget to make a backup of the original blist.xml file in case something goes wrong.