From time to time I get recruited to help someone export mail and contacts from one e-mail program and import the data into another. The fact that a civilian must recruit a geek to accomplish this seemingly mundane task speaks volumes about our industry's sad history of data lock-in.
Even for a geek, the solution can easily become a slide down a slippery slope. There's no shortage of converters floating around on the Net, but it's surprisingly hard to find one that will reliably and completely transform, say, WAB (Windows Address Book) to LDIF (LDAP Data Interchange Format) or CSV (comma-separated values).
Back in 2002, I discovered that Mozilla's mail program, now known as Thunderbird, could import mail and contacts from an Outlook PST file and then export the data as Mbox for mail and LDIF or CSV for contacts. My referral log tells me that, to this day, people continue to seek out and use that technique.
It came in handy again this week when a friend wanted to switch from Outlook Express to Gmail. Exporting her contacts to a CSV file, and then importing them into Gmail, turned out to be a snap. But when I declared victory, she sent me scrambling down the slippery slope with this innocent-sounding question: "What about my distribution lists?"
Uh-oh. It turns out that she uses 15 lists, some with just a few individual addresses and some with more than 100. Those lists didn't appear in the CSV file, or in the output of any other WAB converter I could find. Even my trusty Thunderbird trick only partly worked. Although Thunderbird can export lists to LDIF, it does only one at a time, so I had to create a file for each separate list. Grumble.
With half the battle won, how to inject those LDIF files into Gmail? There's no official Google-supported API, but I've gotten lots of mileage out of an unofficial one called libgmail. Good news: libgmail has added support for Gmail contacts since I last used it. Bad news: It only supports individual contacts, not lists.
The solution I cobbled together speaks volumes about the fundamental openness of Web applications. To find out how Gmail creates a distribution list, I logged in, created a list interactively using Gmail's form, and captured the resulting HTTP transaction using one of the handiest tools in my Web developer's kit, Firefox's LiveHTTPHeaders extension.
The next step was to replay that transaction outside of the browser. I rearranged its elements -- a URL, a chunk of HTTP POST data, and a set of HTTP headers including a cookie packed with crucial name/value pairs -- as a command-line invocation of another of the handiest tools in my kit: curl.
As proof of concept, I used Gmail's interface to delete the list I'd just made, then invoked the curl command to recreate it. When that worked, I wrote a simple script to interpolate names and addresses from the exported LDIF files into a series of curl commands, and invoke them one at a time. And that was that.
It was only a partial solution, of course. A fully automated version would tie into libgmail's authentication scheme, obviating the need to capture and replay an HTTP header. But the fact that it's possible to discover and exploit implicit APIs in this way is a testament to the power and flexibility of the Web's architectural style.