Maildirs, Timestamps, and Metadata Indexes

PostsMaildirs, Timestamps, and Metadata Indexes

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.

fixdate.py

UPDATE: Added fix so that Ctrl-C actually stops the script.  Thanks to Noah Kantrowitz for that one.

About Jayson Vantuyl

I live in San Francisco, California; have an awesome son, David; and make machines do (subjectively) interesting things. I'm generally an all around handy fellow.

2 comments

  • Reply Gilles LAMIRAL said: February 14, 2009 4:22 pm

    Hello,

    imapsync has good option:
    --syncinternaldates to deal with this :

    --syncinternaldates : sets the internal dates on host2 same as host1.
    Turned on by default.
    --idatefromheader : sets the internal dates on host2 same as the
    "Date:" headers.

    See also learn/adjust_time.pl

    Is your script free/open/gratis software?

  • Reply Jayson Vantuyl said: March 10, 2009 3:01 am

    Sure. I hereby release it under the Creative Commons Zero license: http://creativecommons.org/licenses/zero/1.0/

    In my particular case, I was moving from one Maildir-based server to another. It didn't require "syncing" anything, and getting passwords to all of my users would have been onerous.

    This solution was just specific for DoveCot. It looks like adjust_time.pl does the same thing--although it appears to use the touch command to do it, which is probably a bit more intensive.

    Thanks for the helpful links, though.

Leave a Comment

Your email address will not be published. Required fields are marked *