Uberspace U7 mail filter

Outdated This article is marked outdated. Also, I moved to mailbox.org.

With the upcoming uberspace 7 the guys at uberspace introduced a new environment with various changes in comparison to u6 (see u7 changelog).

One major aspect is the deprecation of dpsam and spamassassin (which I just managed to setup flawlessly :). However, with U7.1 spam is now handled globally via rspamd.

The following post will show a server-side mail filter that sorts spam, newsletters and service-emails into dedicated folders.

Once spam is detected, the header X-Rspamd-Bar, X-Rspamd-Report and X-Rspamd-Score are attached to the mail. X-Rspamd-Bar contains an spam-indicator based on - and +, e.g. the value +++ indicates quite certainly spam an e-mail flagged with -- might contain some useful contents.

Due to the huge amount of daily spam e-emails it's sensible to perform a server-side sorting via mailfilter as described in the old uberspace wiki.

A good starting point for a modulized mailfilter can be found in macfrogs blog, 2014.

When it comes to actually filter the e-mails, following rules came in handy:

# Here go the filter rules...
# Move all mails with a bar greater `+++` to .Junk
if ( /^X-Rspamd-Bar:\s\+\+\+[+]/:h ) {
    DESTDIR="$MAILDIR/.Junk" }
# Sort newsletters
elsif ( /^List-ID:.*/:h ) {
    DESTDIR="$MAILDIR/.newsletters" }
# Sort e-mails provided by services (e.g. twitter/tumblr).
elsif ( /^Delivered-To:.*-*@.*$/:h )
  {
    DESTDIR="$MAILDIR/.services"
 } 

A clutched version of the whole script (based on uberspacece and macfrog) looks like this and needs to be placed in the file ~/.mailfilter:

mailfilter.sh (Source)

# which maildir is to use?
# set default Maildir
MAILDIR="$HOME/Maildir"
# check if we're called from a .qmail-EXT instead of .qmail
import EXT
if ( $EXT )
{
  # does a vmailmgr user named $EXT exist?
  # if yes, deliver mail to his Maildir instead
  CHECKMAILDIR = `dumpvuser $EXT | grep '^Directory' | awk '{ print $2 }'`
  if ( $CHECKMAILDIR )
  {
    MAILDIR="$HOME/$CHECKMAILDIR"
  }
}
# move spam to .Junk folder
DESTDIR="$MAILDIR"
# Here go the filter rules...
if ( /^X-Rspamd-Bar:\s\+\+\+[+]/:h )
{
  DESTDIR="$MAILDIR/.Junk"
}
elsif ( /^List-ID:.*/:h )
{
DESTDIR="$MAILDIR/.newsletters"
}
elsif ( /^Delivered-To:.*-*@.*$/:h )
{
  DESTDIR="$MAILDIR/.services"
}
# Hierarchically create DESTDIR if it does not already exist.
HIER="$DESTDIR"
CONTINUE=1
while ( $CONTINUE && $HIER =~ /^(.*\/(\.[^.]+)*)(\.[^.]+)$/ )
{
  if ( $MATCH3 ne ".INBOX" )
  {
    `test -d "$MATCH"`
    if ( $RETURNCODE == 1 )
    {
      `maildirmake "$MATCH"`
    }
    else
    {
      CONTINUE=0
    }
  }
  HIER=$MATCH1
}
# Deliver mail. If SILENT_DESTDIR is set, mark all mail in
# DESTDIR as read.
if ( $SILENT_DESTDIR )
{
  # mark as read
  cc "$DESTDIR"
  `find "$DESTDIR/new/" -mindepth 1 -maxdepth 1 -type f -printf '%f\0' | xargs -0 -I {} mv "$DESTDIR/new/{}" "$DESTDIR/cur/{}:2,S"`
  exit
}
else
{
  to "$DESTDIR"
}

The stored mailfilter can be activated via account by editing the corresponding .qmail-* file and setting its content to |maildrop.

As always, more verbose configurations with various, differing mailfilters via user, are feasible.