Caching LDAP Queries in Mutt

You can use the lbdb tool to query an LDAP server within Mutt using the query_command setting. Setting up lbdb is out of scope for this article, although you can find plenty of documentation and examples online (here and here, for instance). One drawback to this approach, however, is that everytime you want to look up a recipient in Mutt you have to query your LDAP server, which can be quite slow.

A workaround to this problem is to “cache” the results of prior LDAP queries into a Mutt aliases file. That way you only have to query lbdb once, and future recipients can be found in the aliases file, which is significantly faster.

The following script does exactly this:

The script queries your LDAP server using lbdbq and returns those “raw” results to Mutt so that you can make your selection. However, it also reformats the output and saves it to an aliases file for Mutt to use later.

Save this script to ~/.lbdb/, make it executable, and use it in Mutt’s query_command setting:

set query_command = "~/.lbdb/ '%s'"

Now, everytime you make a query in Mutt the results will be saved to $HOME/.cache/mutt/aliases. Obviously, you need to tell Mutt to read from this alias file as well:

set alias_file = ~/.cache/mutt/aliases
source $alias_file

One caveat to this is that Mutt only reads the alias file on startup, so any changes to that file will not be detected unless you manually run :source $alias_file or restart Mutt.

Last modified by Greg Anders on