Greg’s Adventures

Dovecot Virtuals are Spiffy

·4 mins

One of my goals with the new server was to learn as much as possible. In that light, I decided I would use as many new (to me) bits of server software as I could. I will do a write up soon on the whole setup, but for now I want to talk briefly about Dovecot’s virtual plugin. (Dovecot is the IMAP server I selected for my setup.)

As a gmail convert, I’ve been trying to find ways to replicate the bits of the “gmail experience” that I liked. One of my first priorities was filter based, server-side labels. I used labels at gmail in a way that is probably similar to most people where the labels can be broken down into two types: labels as folders, fed via a filter rule (for mail lists and such) and labels as “flags” that I applied manually.

Replicating the filter/folder analogy server-side with IMAP would traditionally involve sieve and a folder, but having to set up an extra folder for each “label” bugged me. What if I wanted something to show up two places? My first attempt at a better way was to use Postbox’s smart folders. These allow you to set up a virtual folder that runs a search (either server side or on your local indexes) and populates the folder based on that search. This met half of my pair of requirements, but was not server-side, so I couldn’t use the folders on my phone without replicating them there, which is not DRY.

It turns out that the Dovecot developers thought of this, and have an elegant solution via the “virtual” mailbox plugin. At first glance, you might think this is meant to facilitate mailboxes for users without unix accounts on the machine, as that is often what this terminology means in the context of mail servers, but in this case it isn’t. What it is instead is easily configured, server-side smart folders!

Setting them up takes only a few minutes (the documentation) is well written) and I only ran into one gotcha.

But before the gotcha got me, I needed to find where to add the configuration on my Ubuntu 12.04 server. The dovecot configuration is broken into multiple files in the /etc/dovecot/conf.d/ folder which looks like this on my machine:

root@tonks:/etc/dovecot# ls conf.d/
10-auth.conf      10-master.conf  20-lmtp.conf    auth-deny.conf.ext        auth-system.conf.ext
10-director.conf  10-ssl.conf     90-acl.conf     auth-master.conf.ext      auth-vpopmail.conf.ext
10-logging.conf   15-lda.conf     90-plugin.conf  auth-passwdfile.conf.ext
10-mail.conf      20-imap.conf    90-quota.conf   auth-static.conf.ext

This makes it a little hard to guess where a configuration item ought to go, but grep helped out, and I found the mail_plugins and namespace options in 10-mail.conf. So, following the documentation, I added the virtual plugin to the mail_plugin line, and a namespace stanza for the virtual boxes, like this:

namespace {
  prefix = virtual/
  separator = /
  location = virtual:~/mail/virtual

A quick reload of dovecot, and things went kaboom. The error was a bit mysterious:

Jun  5 08:51:22 tonks dovecot: imap(greg): Error: user greg: Initialization failed: namespace configuration error: inbox=yes namespace missing

I added a namespace. Why did that make one go missing? A quick Google search gave me the answer, which also happened to be spelled out clearly in the comments (I didn’t read) inside the config file:

# REMEMBER: If you add any namespaces, the default namespace must be added
# explicitly, ie. mail_location does nothing unless you have a namespace
# without a location setting. Default namespace is simply done by having a
# namespace with empty prefix.

Ahah! So I added an explicit “root” namespace, following the sample:

namespace {
  prefix =
  type = private
  inbox = yes
  separator = /
  subscriptions = yes

After reloading again, all was well. It worked!

Adding virtual mailboxes is extremely easy, and is clearly spelled out in the documentation on dovecot’s wiki, but it boils down to creating a folder under the mapped location specified in your virtual namespace that contains a single file named “dovecot-virtual”. I won’t repeat the docs here, as they are clear enough and have several examples. I set up a folder for one of my mailing lists, and found it populates extremely quickly even on my large mailstore, and is available across all my clients. Hooray!

Next I need to find a way to do the other kind of label, that I manage myself. I’ll write something up once I figure that out. Have fun!