#set delete-oversized
set default-user "nicholas"
#set allow-multiple
set purge-after none
#set proxy "socks://localhost:1080/"
set timeout 60
#set parallel-accounts 3

# My fdm configuration base path.
$base = "%h/.fdm.d"

# Lock file path.
set lock-file "${base}/lock"

# This is pretty silly, but hey ;-).
#set maximum-size %(\df -kP /tmp|awk '/^\// { print int($4 / 6) }') kilobytes
set maximum-size 128m

# Verify SSL certificates.
set verify-certificates

# These seem to vaguely work okay for me.
set queue-high 5
set queue-low 3

# Use dotlock locking, since FreeBSD doesn't like anything else on NFS.
set lock-types dotlock

# The following strings are replaced in pipe commands and maildir/mbox paths:
#       %a: account name
#       %h: user's home directory
#       %n: user's uid
#       %t: action name if performing action
#       %u: name of user
#       %H: current hour (00-23)
#       %M: current minute (00-59)
#       %S: current second (00-59)
#       %d: current day of the month (00-31)
#       %m: current month (01-12)
#       %y: current year
#       %W: current day of the week (0-6, Sunday is 0)
#       %Y: current day of the year (000-365)
#       %Q: current quarter (1-4)

# Macro holding the path where the maildirs are.
$path = "%h/mail"

# My main inbox and junk mail directories.
action "inbox" maildir "${path}/inbox"
action "junk" maildir "${path}/junk"

# Note that I don't bother escaping literal periods (.) in regexps in this file.

# ------------------------------------------------------------------------------
# These two definitions are part of killfiling - the action appends the 
# message-id to my block list and the macro is used to check that no 
# message-id in the references or in-reply-to headers matches any in the file,
# if it does the mail is dropped later. This means I not only don't need to
# read mail from idiots, I also don't have to read many of (not every client
# adds the headers :-/) the hundred follow-ups telling them they are an idiot.
# Use with care: this can be slow.
#
# I only do this for the OpenBSD lists at the moment.
# ------------------------------------------------------------------------------
action "die" exec "echo '%[message_id]' >>${base}/ignore"
$is_dead = "echo '%1'|grep -Fqif ${base}/ignore"
# ------------------------------------------------------------------------------

# Mark a maildir entry as read. Stolen from Frank Terbeck's config.
action "mark-read" exec
	'mf="%[mail_file]"; mv "${mf}" "${mf%%/*}/../cur/${mf##*/}:2,S"'

# Account definitions. All passwords and most usernames are read from .netrc.
account "gmx" pop3 server "pop.gmx.net"
account "ntlworld" pop3	server "pop.ntlworld.com"
account "gmail.nicholas.marriott" pop3s server "pop.googlemail.com" port 995
	user "nicholas.marriott@gmail.com"
account "gmail.nicm123" pop3s server "pop.googlemail.com" port 995
	user "nicm123@gmail.com"
account "gmail.nicm321" pop3s server "pop.googlemail.com" port 995
	user "nicm321@gmail.com"
account "gmail.nicm123.spam" disabled imaps server "imap.googlemail.com"
	port 993 user "nicm123@gmail.com" folder "[Gmail]/Spam"
account "eutonian.nicm" imaps server "eutonian.ath.cx"
	user "nicm" no-verify

# The stdin account is disabled: it will be ignored unless explicitly requested
# using the -a switch on the command line.
account "stdin" disabled stdin

# RSS feed account for RATM, http://lfod.us/sw/ratm/.
account "rss" disabled stdin

# Catch local mail early.
match "^From:.*root@[a-z-]*.nicm.ath.cx" in headers and account "stdin"
	action "inbox"

match account "gmail.nicm123.spam" action maildir "${path}/spam"

# ------------------------------------------------------------------------------
# I like to only keep mail in my maildirs for 30 days, after which it is 
# archived to mboxes which are easier to grep, and which I rm -f periodically.
# This section defines the rules fdm needs to do the archiving: with these, I
# can have cron run "fdm -vaarchive f" every 10 days or so, and everything is
# gravy.
# ------------------------------------------------------------------------------
account "archive" disabled maildirs { 
	"${path}/freebsd-{current,hackers}"
	"${path}/gnu-gcc"
	"${path}/linux-kernel"
	"${path}/netbsd-tech-kern"
	"${path}/news-{agvp,cubom,cup}"
	"${path}/openbsd-{bugs,misc,ports,ports-changes,source-changes,tech}"
	"${path}/pf"
	"${path}/openssh-unix-dev"
	"${path}/rss"
	"${path}/secureshell"
}
action "archive" mbox "${path}/%[maildir]-archives/%[maildir]-%yq%Q" compress

match account "archive" {
	# Archive mail with invalid dates or older than 30 days.
	match age invalid or age > 30 days action "archive"

	# Don't let any other mail get to the normal rules.
	match all action keep
}
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Newsgroups. These are disabled so I can run them using a "fdm -anews f" from
# a seperate cronjob less often than normal mail accounts.
# ------------------------------------------------------------------------------
action "news-alt.games.vga-planets" maildir "${path}/news-agvp"
action "news-comp.unix.bsd.openbsd.misc" maildir "${path}/news-cubom"
action "news-comp.unix.programmer" maildir "${path}/news-cup"

account "news" disabled nntp
	server "news.ntlworld.com"
	groups {
		"alt.games.vga-planets"
		"comp.unix.bsd.openbsd.misc"
		"comp.unix.programmer"
	}
	cache "${base}/news"

match account "news" {
	match "\\<HTML\\>" in body action "junk"

	# comp.unix.programmer pointless argument usual suspects.
	match "^From:.*rweikusat@mssgmbh.com" in headers action drop
	match "^From:.*davids@webmaster.com" in headers action drop

	# Real idiots.
	match "^From:.*MI5-?Victim@mi5.gov.uk" in headers action drop
	match "^Organization:.*www.altopia.com" in headers action drop
	match "^From:.*robin_carey5@yahoo.co.uk" in headers action drop

	# Match based on the group name.
	match all action "news-%[group]"
}
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# RSS.
# ------------------------------------------------------------------------------
match account "rss" {
	$crap = "football|sport|prince harry|horse|jockey|royal family|" +
		"britney spears|cricket|tennis|snooker|motorsport|rugby|" +
		"princess diana|heather mills|paul mccartney|jamie oliver|" +
		"music award|candle-lit|in memory|concert|celtic|the queen|" +
		"richard and judy|bbc.co.uk.*/entertainment|madeleine mccann|" +
		"richard branson|commonwealth games|olympic games|jill dando|" +
		"vigil|day of mourning|war dead|amy winehouse|prince charles|" +
		"motoring costs|remembrance day"
	match "(${crap})" action drop

	# Never worth reading...
	match "^From:.*Overheard In New York" in headers and
		"^Subject:.*Wednesday.*One-Liner" in headers action drop

	match "^X-Item-Link: (.*)" action rewrite "cat; " +
		"echo; echo '<br><br><p><a href=\"%1\">%1</a></p>'" continue
	match all action maildir "${path}/rss"
}
# ------------------------------------------------------------------------------

# Catch duplicate messages using a message-id cache.
$db = "${base}/duplicates"
$key = "%[message_id]"
cache $db expire 1 week

match not string $key to "" {
	# Filter messages already in the cache.	
	match in-cache $db key $key action maildir "${path}/duplicates"

	# Add message-ids to the cache.
	match all action to-cache $db key $key continue
}

# Copy invalid dates to a special action so I can inspect them.
match age invalid action maildir "${path}/invalid" continue

# Copy HTML mail to special action to test attachment parsing.
match attachment any-type "*/html" action maildir "${path}/html" continue

# Test rewriting through cat.
match all action rewrite "cat" continue

# Test mbox delivery.
match size > 1K and size < 4K action mbox "${path}/mbox" compress continue

# Drop HTML mail to gmail accounts.
match "\\<HTML\\>" in body and account "gmail*" action "junk"

# Junk (eejits).
match ".*YAHOO.*BOOTER.*" in body action "junk"

# Junk (spam).
match "^From:.*@*.chase.com" in headers action "junk"
match "^From:.*@*.chaseonline.com" in headers action "junk"
match "^From:.*@citi-bank.com" in headers action "junk"
match "^From:.*@emaillabs.com" in headers action "junk"
match "^From:.*baypos@gmail.com" in headers action "junk"
match "^From:.*E-Greeting" in headers action "junk"
match "^From:.*@postcard.org" in headers action "junk"
match "^From:.*@mail.itp.net" in headers action "junk"

# Junk (ISP garbage).
match "^From:.*@newsletter.ntlworld.com" in headers action "junk"
match "^From:.*@newsletter.virginmedia.com" in headers action "junk"
match "^From:.*mailings@(gmx.net|gmx-gmbh.de)" in headers and account "gmx"
	action drop

# ------------------------------------------------------------------------------
# Test add-header.
# ------------------------------------------------------------------------------
action "add-headers" {
	add-header "Lines"
		value "%[lines]; body=%[body_lines]; header=%[header_lines]"
}
match all action "add-headers" continue

# ------------------------------------------------------------------------------
# OpenBSD. All my openbsd maildirs are named openbsd-<something>, where
# <something> is the same as the sender the mail comes from, so I can shorthand
# this by using a subexpression. I save the regexp subexpression as a tag
# called list, then the final action delivers to a maildir called
# "openbsd-%[list]". I do need a special-case rule for gnats, which should go
# into openbsd-bugs (as well as mails to bugs@); and announce. which I want in
# my inbox.
# ------------------------------------------------------------------------------
match "^Sender:.*owner-([a-z-]*)@openbsd.org" in headers {
	# Kill/filter actions.
	action "openbsd-filtered" maildir "${path}/openbsd-filtered" 

	# Special-case announce.
	match string "%1" to "announce" action "inbox"

	# Tag the mail and fix gnats@.
	match all action tag "list" value "%1" continue
	match string "%[list]" to "gnats"
		action tag "list" value "bugs" continue

	# misc@ is way too high-traffic for a list some of which I actually
	# like to read, so aggressively filter it.
	$ignore = "ipsec|spamd|skype|ipv6|microsoft|linux|faster|gnu|ldap|" +
		"carp|pfsync|vpn|bgp|ospf|l2tp|ripd|printer|printing|" + 
		"opinion|pre-?orders?|ppp|kerberos|afs|\\[OT\\]|off-topic|" +
		"thanks|isos?|vmware|postfix|sasl|greytrap|isakmpd|wireless|" +
		"google|firefox|ipcomp|dovecot|apache|youtube|openoffice|php|" +
		"forum|clamav|wep|vlan|squid|wpa|atheros|communism|asterisk|" +
		"snmp"
	$unless = "panic"
	match string "%[list]" to "misc" and
		"^Subject:.*(${ignore})" in headers and
		not "^Subject:.*(${unless})" in headers 
		actions { "openbsd-filtered" "mark-read" }

	# Drop any mail which is replying to a message-id on the ignore list.
	match "^References:[ \t]*(.*)" in headers and
		exec ${is_dead} returns (0, )
		actions { "openbsd-filtered" "mark-read" }
	match "^In-Reply-To:[ \t]*([^()]*)" in headers and
		exec ${is_dead} returns (0, )
		actions { "openbsd-filtered" "mark-read" }

	# Drop eejits/trolls and add the message-ids to the ignore list.
	match "^From:.*rms@1407.org" in headers
		actions { "die" "openbsd-filtered" }
	match "^From:.*clock@twibright.com" in headers
		actions { "die" "openbsd-filtered" }
	match "^From:.*dfeustel@mindspring.com" in headers
		actions { "die" "openbsd-filtered" }
	match "^From:.*peter_philipp@freenet.de" in headers
		or "^From:.*philipp.peter@freenet.de" in headers
		actions { "die" "openbsd-filtered" }
	match "^From:.*suck@my-balls.com" in headers
		actions { "die" "openbsd-filtered" }
	match "^From:.*chefren@pi.net" in headers
		actions { "die" "openbsd-filtered" }
	match "^From:.*demuel@thephinix.org" in headers
		actions { "die" "openbsd-filtered" }
	match "^From:.*lvl@omnitec.net" in headers
		actions { "die" "openbsd-filtered" }
	match "^From:.*timo.schoeler@riscworks.net" in headers
		actions { "die" "openbsd-filtered" }

 	# Deliver to openbsd-%[list].
	match all action maildir "${path}/openbsd-%[list]"
}

# pf mailing list.
match "^Sender:.*owner-pf@benzedrine.cx" in headers
	action maildir "${path}/pf"
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# FreeBSD.
# ------------------------------------------------------------------------------
match "^Sender:.*owner-freebsd-([a-z-]*)@freebsd.org" in headers {
	match string "%1" to "announce" action "inbox"
	match all action maildir "${path}/freebsd-%1"
}
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# NetBSD.
# ------------------------------------------------------------------------------
match "^Sender:[ \t]*([a-z-]*)-owner@netbsd.org" in headers
	action maildir "${path}/netbsd-%1"
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# GNU.
# ------------------------------------------------------------------------------
match "^Sender:[ \t]*([a-z]*)-owner@[a-z]*.gnu.org" in headers
	action maildir "${path}/gnu-%1"
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# SSH-related lists.
# ------------------------------------------------------------------------------
match "^List-Post:.*secureshell@securityfocus.com" in headers
	action maildir "${path}/secureshell"
match "^X-BeenThere:.*openssh-unix-dev@mindrot.org" in headers
	action maildir "${path}/openssh-unix-dev"
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Linux kernel.
# ------------------------------------------------------------------------------
match "^Sender:.*linux-kernel-owner@vger.kernel.org" in headers
	action maildir "${path}/linux-kernel"
# ------------------------------------------------------------------------------

# Default action.
match all action "inbox"
