Every once in a while, you have a problem that has an approachable solution, but you just don’t want to tackle. This weekend, I took a brief break from a more significant problem to attack one that I’ve had since February 20, 2008 at about 12:35pm.
You might ask, how do I know the date so precisely. Well, it’s been staring at me from my e-mail Inbox. Essentially, around that time I was getting ready to shut down my e-mail server I had run out of my cybercafe for a while. In doing so, I had to choose to move somewhere. I tried GMail. While GMail is a great webmail client, it’s IMAP implementation leaves something to be desired, so I quickly decided to move somewhere else. Being short on time and not having good luck with the three other people I checked out, I settled on running my own on a small VPS at a decent provider.
In order to get the mail back out of GMail, I used imapsync, a script that will dump an IMAP mailbox into a Maildir. At the same time, I switched to the Dovecot IMAP server. I had previously used it, but had never wanted to fight with Cyrus IMAP, which I had been using before GMail. I figured, what could possibly go wrong?
Well, what went wrong was fairly understandable. Dovecot decided that it would use the filesystem timestamp on the files instead of their Date header. This created the interesting situation where all of my imported mail showed the time that it was downloaded from GMail, but paradoxically showed a different Date header.
This has been more than a minor nuisance. Essentially, when I have wanted to read an old conversation thread, it’s all out of order. Messages downloaded in a strange order that didn’t exactly match the receive order, so threads were often a challenge to read. Additionally, any really old mail couldn’t be found by sorting by Date, so I was also challenged when finding messages that I can’t remember their exact content, but I can remember when they were received.
This was an entirely ignorable problem, with a reasonably tractable solution. I just never really wanted to tackle it.
While my parents were here for my birthday, Mom (who also hosts her e-mail with me) remembered that she was having a similar problem. Since I got stuck on a different problem, decided to tackle this.
If anybody ever has a problem where the IMAP server dates their e-mail by stat (i.e. mtime) instead of RFC822-style date, here’s a script for you. Should work in Python 2.5.
UPDATE: Added fix so that Ctrl-C actually stops the script. Thanks to Noah Kantrowitz for that one.