Building IMAPFilter on Ubuntu 14.04

IMAPFilter is an amazing IMAP filtering system written in lua. It allows you to pragmatically filter email and get some pretty amazing results. It also means in situations where you mightn't have access to a service like sieve (looking at you Gmail!) you can still get all the same functionality, not to mention the portability!

The version that ships with Ubuntu 14.04 (trusty) is 2.5.2 which was released on the 1st of March 2012, so its pretty damn old. There has been some major changes to IMAPFilter in the past 4 years, such as automatically validating certificates that match CA's available on your host OS. This is pretty important when trying to filter emails with a service like Gmail who regularly serve two different certificates with the same CN. After you've accepted the first certificate, if IMAPFilter has to reconnect and receives a different certificate you're prompted with this annoying message:

Server certificate subject: /C=US/ST=California/L=Mountain View/O=Google Inc/CN=imap.gmail.com
Server certificate issuer: /C=US/O=Google Inc/CN=Google Internet Authority G2
Server key fingerprint: 51:EB:F7:3E:85:40:DA:67:CA:BC:39:E8:63:1B:F8:6C
ATTENTION: SSL/TLS certificate fingerprint mismatch.
Proceed with the connection (y/n)?

Now, imagine that happening at 3am when you're asleep and you aren't filtering email for the next 5 hours. On Gmail especially it can take hours to catch up and start filtering email again.

Luckily the feature above was released in version 2.5.7 (again, released some time ago).

IMAPFilter 2.5.7 - 16 Nov 2014

  • Support for automatic validation of SSL connections, using the operating system's installed trusted CA certificates (usually from authorities that are shipped with web browsers).

So, the simple thing to do is to build our own version of IMAPFilter!

Building imapfilter

First things first, we need to install some required packages for the build process. You can uninstall these after.

$ apt-get install libpcre3-dev liblua5.2-dev libssl-dev lua5.2 make

Download the release you want to install, in my case I wanted the latest version (since there wasn't any reason not to, and which at the time of writing was 2.6.7) and extract it to somewhere temporary for the build process::

$ mkdir -p /tmp/imapfilter_build
$ cd /tmp/imapfilter_build
$ wget https://github.com/lefcha/imapfilter/archive/v2.6.7.zip
$ unzip v2.6.7.zip
$ cd imapfilter-2.6.7

Ubuntu specific switches

For some reason on Ubuntu we need to pass the make commands with an include for lua:

INCDIRS=-I/usr/include/lua5.2 LIBLUA=-llua5.2

Other wise you get presented with this error:

$ make
cd src && make all
make[1]: Entering directory `/tmp/imapfilter_build/imapfilter-2.6.7/src'
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"'     -c -o auth.o auth.c
In file included from auth.c:7:0:
imapfilter.h:9:17: fatal error: lua.h: No such file or directory
 #include <lua.h>
                 ^
compilation terminated.
make[1]: *** [auth.o] Error 1
make[1]: Leaving directory `/tmp/imapfilter_build/imapfilter-2.6.7/src'
make: *** [all] Error 2

Now run make to compile it:

$ make INCDIRS=-I/usr/include/lua5.2 LIBLUA=-llua5.2
cd src && make all
make[1]: Entering directory `/tmp/imapfilter_build/imapfilter-2.6.7/src'
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o auth.o auth.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o buffer.o buffer.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o cert.o cert.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o core.o core.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o file.o file.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o imapfilter.o imapfilter.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o list.o list.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o log.o log.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o lua.o lua.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o memory.o memory.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o misc.o misc.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o namespace.o namespace.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o pcre.o pcre.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o regexp.o regexp.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o request.o request.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o response.o response.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o session.o session.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o signal.o signal.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o socket.o socket.c
cc -Wall -Wextra -O -DCONFIG_SHAREDIR='"/usr/local/share/imapfilter"' -I/usr/include/lua5.2 -c -o system.o system.c
cc -o imapfilter   auth.o buffer.o cert.o core.o file.o imapfilter.o list.o log.o lua.o memory.o misc.o namespace.o pcre.o regexp.o request.o response.o session.o signal.o socket.o system.o -lm -ldl -llua5.2 -lpcre -lssl -lcrypto
make[1]: Leaving directory `/tmp/imapfilter_build/imapfilter-2.6.7/src'

And finally make install to actually compile and install the package:

$ make install INCDIRS=-I/usr/include/lua5.2 LIBLUA=-llua5.2
cd src && make install
make[1]: Entering directory `/tmp/imapfilter_build/imapfilter-2.6.7/src'
mkdir -p /usr/local/bin && \
	cp -f imapfilter /usr/local/bin && \
	chmod 0755 /usr/local/bin/imapfilter
mkdir -p /usr/local/share/imapfilter && \
	cp -f common.lua set.lua regex.lua account.lua mailbox.lua message.lua options.lua auxiliary.lua /usr/local/share/imapfilter && \
	chmod 0644 /usr/local/share/imapfilter/common.lua set.lua regex.lua account.lua mailbox.lua message.lua options.lua auxiliary.lua
mkdir -p /usr/local/man/man1 && \
	cp -f ../doc/imapfilter.1 /usr/local/man/man1 && \
	chmod 0644 /usr/local/man/man1/imapfilter.1
mkdir -p /usr/local/man/man5 && \
	cp -f ../doc/imapfilter_config.5 /usr/local/man/man5 && \
	chmod 0644 /usr/local/man/man5/imapfilter_config.5
make[1]: Leaving directory `/tmp/imapfilter_build/imapfilter-2.6.7/src'

And voliĆ ! IMAPFilter should now be installed in /usr/local/bin with the latest version available.

James Loh

I'm a Sysadmin for a web solutions company deploying clouds across the globe. I learn new things every day.

Australia @itsjloh jloh jloh