#!/bin/sh

addtag() { notmuch tag +"$1" -- "tag:new and ($2) and not tag:$1"; }
archive() { notmuch tag -new -unread -- "$@" and tag:new; }
spam() { notmuch tag -new -unread +spam -- "$@" and '(' tag:new or tag:unread or not tag:spam ')'; }

# Tag mails by their origin (top-level maildir)
for maildir in /data/mail/*; do
	origin="$(basename "$maildir")"
	addtag "$origin" "folder:/$origin/"
done

# Some aliases
addtag zeus "to:felixvdj@zeus.ugent.be"
addtag dodona "to:dodona@ugent.be"
addtag unipept "to:unipept@ugent.be"

# These mails tend to be numerous and do not need additional tagging
archive tag:dodona subject:"Mysql2::Error: Table 'dodona.active_storage_blobs' doesn't exist"
archive tag:dodona subject:'A request took'
archive tag:dodona subject:'Errno::ENOENT'
archive tag:unipept subject:'Mysql2::Error::ConnectionError'
archive tag:unipept subject:'invalid value for Integer'
archive tag:unipept subject:'NoMethodError'

# More complex tagging
for mid in $(notmuch search --output=messages tag:new); do

	tag=false

	# +-tags become actual tags
	for tag in $(notmuch show --format=raw "$mid" | sed -n '/^\tfor/s/.*<[^>]*+\([^@]*\)@.*>.*/\1/p' | sort | uniq); do
		addtag "$tag" "$mid"
		tag=true
	done

	# messages from contacts get a contact tag
	for sender in $(notmuch address --output=sender --output=address "$mid"); do
		if khard list "$sender" > /dev/null 2>&1; then
			addtag contact "$mid"
			tag=true
		fi
	done

	# mails to posteo must have a tag
	if ! "$tag"; then
		addtag killed "$mid and tag:posteo"
	fi

	# list ids
	for list in $(notmuch show --format=raw "$mid" | sed -n '/^List-Id:/,/^\w/p' | sed '$d' | tr '\n' ' ' | sed 's/[^<]*<// ; s/\..*//'); do
		addtag "lists" "$mid"
		addtag "lists/$list" "$mid"
	done
done

# Undelay threads with new messages
for delaytag in $(notmuch search --output=tags -- tag:delay and thread:{tag:new} | grep 'delay/'); do
	notmuch tag -delay -"$delaytag" +flagged -- tag:"$delaytag" and thread:{tag:new}
done

# Github notifications
addtag github "from:notifications@github.com"

# Uninteresting
archive tag:dodona subject:'Onderwijsinstelling aangemaakt voor .*'
archive tag:dodona from:'logcheck system account'
archive tag:dodona from:'KlasCement'
archive tag:dodona subject:'Een gebruiker kon niet inloggen'
archive tag:dodona subject:'Invalid or unknown LTI provider'
archive tag:dodona subject:faraday::connectionfailed
archive tag:dodona subject:'status was changed to internal error' 'course_id: 317' # C testcursus 2020-2021
archive tag:dodona subject:'status was changed to internal error' 'course_id: 335' # R basic 2020-2021
archive tag:dodona subject:'status was changed to internal error' 'course_id: 339' # Data Mining 2020-2021
archive tag:dodona subject:'status was changed to internal error' 'course_id: 346' # Programming Basics 2020-2021
archive tag:dodona subject:'status was changed to internal error' 'course_id: 349' # Programmingin Python 2020-2021
archive tag:dodona subject:'status was changed to internal error' 'course_id: 374' # Statistiek 2020-2021
archive tag:dodona subject:'status was changed to internal error' 'course_id: 375' # Statistiek: introductie tot R 2020-2021
archive tag:dodona subject:'status was changed to internal error' 'course_id: 376' # Statistiek: Data Exploratie .. 2020-2021
archive tag:dodona subject:'status was changed to internal error' 'course_id: 380' # C# 2020-2021
archive tag:dodona subject:'status was changed to internal error' 'course_id: 384' # E620500A 2020-2021
archive tag:dodona subject:'status was changed to internal error' 'user_id: 3' # Peter
archive tag:github thread:{from:dependabot} \
	-subject:Security \
	-to:mention@noreply.github.com  \
	-to:assign@noreply.github.com \
	-to:review_requested@noreply.github.com \
	-to:security_alert@noreply.github.com
archive tag:ugent subject:'Belnet FileSender: Ontvangstbevestiging'

# Muted threads stay muted for a month after arrival
archive thread:{tag:muted}
notmuch tag -muted -- tag:muted and date:'..30_days'

# Bogofilter
for mid in $(notmuch search --output=messages tag:new); do
	case "$(notmuch show --format=raw "$mid" | bogofilter -eut)" in
	H*) true ;;
	S*) spam "$mid" ; echo "$mid" >> ~/spamtest;;
	U*) true ;;
	esac
done

# Inbox remaining not-spam
addtag flagged "not tag:spam"
notmuch tag -new -- tag:new

# Show threads after their delay
today="$(date +'%s')"
for delaytag in $(notmuch search --output=tags tag:delay | grep 'delay/'); do
	until="$(date -d "${delaytag#*/}" +'%s')"
	if [ "$today" -gt "$until"  ]; then
		notmuch tag -delay -"$delaytag" +flagged -- tag:"$delaytag"
	fi
done