Ray Van Dolson's Blog

Pontifications from smoggy Southern California

Archive for May 2011

Cleaning up Gmail (Hosted Apps)

leave a comment »

A couple of years back, I migrated all of my email off of my Postfix/Dovecot mail server to Google Apps.  I was tired of fighting spam and dealing with the occasional outages to my server that would impact the family and friends whose email lived there.

I didn’t have a lot of accounts to migrate, and many of my users were POP3 users who downloaded everything — so there wasn’t a lot of email to move over.  What there was I did manually with an IMAP capable email client and just copied folders and their contents “en masse” over to GMail.

This worked pretty well except for the fact that I’d been using Mutt, and had all my sent mail copied to a series of folders embedded under a parent “Sent” folder with the year and month in the name.  So Sent/Sent-Mail-2001-05, etc.  There were a ton of these folders and in GMail they show up as Labels.  A very long list of labels. With no easy way to hide them all.

I dealt with a few manually, but GMail didn’t seem to have a way to do any sort of bulk actions within their UI, and recursively moving the contents of these folders into GMail’s Sent Mail folder from within Thunderbird wasn’t much easier.

Enter Python and imaplib.  The following hacky and one-off script ended up doing the job for me pretty nicely:

import re
import imaplib

LRP = re.compile(r'\((?P<flags>.*?)\) "(?P<delimiter>.*)" (?P<name>.*)')

def main():
    global LRP

    m = imaplib.IMAP4_SSL('imap.gmail.com', 993)
    m.login('username', 'password')

    status, mailboxes = m.list("/Sent", '*')

    for mb in mailboxes:
        flags, delimiter, mailbox_name = LRP.match(mb).groups()

        print mailbox_name
        s, d = m.select(mailbox_name)

        # If we got an OK and there are > 0 messages in the folder.
        if s == 'OK' and int(d[0]) > 0:
            print "  Trying to move %d messages." % int(d[0])
            typ, [response] = m.search(None, 'SEEN')
            if typ != 'OK':
                raise RunTimeError(response)
            msg_ids = ','.join(response.split(' '))
            m.copy(msg_ids, '[Gmail]/Sent Mail')
            m.store(msg_ids, '+FLAGS', '\\Deleted')
            m.expunge()
        else:
            if mailbox_name != '"Sent"':
                if int(d[0]) == 0:
                    print "  No messages, deleting %s" % mailbox_name
                    m.delete(mailbox_name)
            else:
                print "  Skipping Sent"

if __name__ == '__main__':
    main()

Now my GMail is much more neat and tidy. It would be nice if imaplib had a move() call…

Written by rayvd

May 30, 2011 at 8:30 am

Posted in Systems Administration, Technology

Tagged with ,

Welcome

with one comment

Once upon a time I had a pyblosxom blog.  It lived on a Linux box hosted via my DSL connection in the comfort of one of my spare bedrooms.  During my switch to Linode, I didn’t port the blog over, though I intended to.  I wanted first to get it packaged up nicely for Fedora, but realized in my ongoing quest for laziness, I didn’t really want to maintain the package (it needed a lot of work to prepare for packaging along with plugins to actually make it useful).  I had been using pyblosxom with hooks into MoinMoin to let me use its syntax, however as I’ve been using Mediawiki almost exclusively lately, the MoinMoin connection wasn’t as much of a draw and in fact was just confusing me having to remember a variant of yet another wiki syntax.

To make a boring story short, this new blog is the culmination of my laziness.  pyblosxom was great, and I’ll miss being able to edit via vi, but I have a feeling moving to WordPress will be better for my blogging existence going forward. 🙂

Written by rayvd

May 30, 2011 at 6:00 am

Posted in General