Rescuing contact details from accounts you can't export them from.

My Mum's work recently switched email systems at work and helpfully didn't move any of her contacts across to the new system. She tried to get me to do it for her manually but boredom and drudgery are evil so that was out. Instead I hunted around for any kind of export function that might let me get access to the contacts but, due to the poor system her work decided to use, there was no way to get any contacts out. In the end I resorted to composing an email to every contact, moving the addresses from the 'To' box to the message body and sending the email to myself with the idea that I could work with the list later.

Earlier today I decided to try and convert the list into vCard format. It would only be the most basic of information but it would be enough for Mum to send emails again which would keep her happy. The list was in the format:

Firstname Lastname <email@example.com>, Firstname2 and Firstname3 Lastname2
<email2@example.org>, ...

In short, it was a set of comma separated values with line breaks spread throughout and angle brackets around email addresses. Some entries had interesting names to work with ('Dad at work' for example) and others didn't have a name so just had the email address put in as the name (example@example.com <example@example.com>).

I knocked up a simple solution in Python using the vObject package for the vCard stuff. It kept me entertained for the afternoon and I have attached the resulting script for anyone who wants to use it for anything. (Disclaimer: It might wreck everything and/or anything. Don't come crying to me when it does, it isn't my fault. IANAL.) The code is fairly unreadable in parts, that is semi-deliberate (I decided to optimise the main function to get it down to the smallest number of statements, no doubt it can be done in fewer but I think I did pretty well at it).

Since I used a fair bit of functional programming stuff in it I am quite tempted to rewrite it in Scheme at some point but that will have to wait a while as I have university exams coming up shortly and have wasted too much time already.