Friday, April 20, 2007

pidgin view buddies by email

In pidgin (formerly gaim), there is no way by default an option to view the contacts by email.
How To do just that:

  1. quit pidgin
  2. browse to ~/.purple
  3. delete blist.xml
  4. restart pidgin and enable/connect to all accounts
  5. quit pidgin again
  6. cd ~/.purple
  7. run script here
  8. redirect standard output to a file X, delete blist.xml and rename the file X to blist.xml
  9. restart pidgin.




import sgmllib
import sys

class parser(sgmllib.SGMLParser):
def __init__(self, verbose=0):
sgmllib.SGMLParser.__init__(self, verbose)
self.thedata = ''

self.insideelement = 0
self.insidename = 0

def start_blist(self, attributes):
self.insideelement = 1
text = ""
for attr, value in attributes:
text = text + " %s='%s'" % (attr,value)
sys.stdout.write("<%s%s>" % ("blist", text))

def end_blist(self):
self.insideelement = 0
sys.stdout.write("</blist>")

def start_name(self, attributes):
self.insidename = 1
text = ""

for attr, value in attributes:
text = text + " %s='%s'" % (attr,value)
sys.stdout.write("<%s%s>" % ("name", text))

def end_name(self):
self.insidename = 0
sys.stdout.write("</name>")
sys.stdout.write("\n\t\t\t\t\t<alias>"+self.thedata+"</alias>")

def unknown_starttag(self, tag, attributes):
text = ""

for attr, value in attributes:
text = text + " %s='%s'" % (attr, value)
sys.stdout.write("<%s%s>" % (tag, text))

def unknown_endtag(self,tag):
sys.stdout.write("</%s>" % tag)

def handle_data(self, data):
if self.insideelement == 1:
if self.insidename == 1:
self.thedata = data
sys.stdout.write("%s" % data)

def returndata(self):
return self.thedata


sys.stdout.write("<?xml version='1.0' encoding='UTF-8' ?>")
pars = parser()
fileobj = open('blist.xml')
strdata = fileobj.read()
fileobj.close()
pars.feed(strdata)



3 comments:

Inash said...

Hi Vishah. This seems like a very convenient hack. But I'm using Gaim 2.0.0beta6 and I was thinking that it should work. But instead, I got the error below:

Error Reading blist.xml: An error was encountered reading your buddy list. They have not been loaded, and the old file has been renamed to ~/.gaim/blist.xml~.

Any idea? The modified blist.xml file seems to comply with what is created by Gaim by default. I've compared by running a diff. The difference seems to be that the script removes the alias tag from each of blist entries, only keeping the name tag.

Any idea?

HaLku said...

if there were aliases set for any buddies, the new file will have two aliases for that particular contact, thats why initially deleting blist.xml and restarting gaim to create a new list. We have to generate the new file from the newly created blist.xml file while gaim is closed, since it keeps on altering the file.

HaLku said...

there is a problem with the script in that it won't recognize self-closing tags.

This happens when all/some of the accounts are not enabled to create the blist.xml file, from which the new file is generated.Enabling all the accounts will remedy this problem.

I didn't notice this before, but sgmllib module doesn't recognize self closing tags, so the only solution it seems to be there, for this problem is to use HTMLParser