#!/bin/sh

. /usr/share/alterator/build/backend3.sh

SPAMD_CONF="/var/lib/spamd/.spamassassin/user_prefs"
FILTER_CONF="/etc/sa-content-filter/system"

_()
{
LANG=${in_language%%;*}.utf8 gettext "alterator-spamassassin" "$1"
}

#service postfix start >&2 ||:
#service spamd start >&2 ||:

shell_add_or_subst()
{
	local name="$1" && shift
	local value="$1" && shift
	local file="$1" && shift

	[ -f "$file" ] || touch "$file"
	if grep -qs "^$name" "$file"; then
		/bin/sed -r -i "s,^$name.*,$name$value," -- "$file"
		return 0
	fi
	printf %s\\n "$name$value" >> "$file"
}

read_required_score()
{
    sed -nr '/^required_score/ {s,^required_score[[:space:]]([0-9]+).*,\1,;p}' "$SPAMD_CONF"
}

write_required_score()
{
    sed -r "s,^required_score[[:space:]]+.*,required_score $1," -i "$SPAMD_CONF"
    service spamd reload >&2
}

read_use_awl()
{
    grep -qs "^use_auto_whitelist[[:space:]]1" "$SPAMD_CONF" && echo "#t" || echo "#f"
}

write_use_awl()
{
    local AWL=0
    [ "$1" = "#t" ] && AWL=1
    sed -r "s,^use_auto_whitelist[[:space:]]+.*,use_auto_whitelist $AWL," -i "$SPAMD_CONF"
    service spamd reload >&2
}

list_scores()
{
    sed -nr '/^score/ {s,^score[[:space:]]+([[:alnum:]_]+)[[:space:]]+(.*)$,("\1" score "\2"),;p}' "$SPAMD_CONF"
}

list_rules()
{
    grep -hr "^score" /usr/share/spamassassin | \
	sed -r 's,score[[:space:]]+([^[:space:]]+).*$,("\1"),g' | sort
}

read_rule()
{
    local RULE=$1
    [ -z "$RULE" -o "$RULE" = "add" ] && \
    	RULE=$(grep -hr "^score" /usr/share/spamassassin | \
		sed -r 's,score[[:space:]]+([^[:space:]]+).*$,\1,g' | sort | head -n1)
    echo -n "(\"$RULE\" description \""
    grep -hr "^describe[[:space:]]*$RULE[[:space:]]" /usr/share/spamassassin| \
	sed -r "s,^describe[[:space:]]+$RULE[[:space:]]+,,g"
    echo -n '" score "'
    grep -hr "^score[[:space:]]*$RULE[[:space:]]" /usr/share/spamassassin| \
	sed -r "s,^score[[:space:]]+$RULE[[:space:]]+,,g"
    echo '")'
}

read_filter_mode()
{
    sed -nr '/^FILTER_MODE=/ {s,^FILTER_MODE=([^[:space:]]+).*,\1,;p}' "$FILTER_CONF"
}

write_filter_mode()
{
    sed -r "s,^FILTER_MODE=.*,FILTER_MODE=$1," -i "$FILTER_CONF"
}

on_message()
{
	case "$in_action" in
		constraints)
			echo '('
			if [ "$in__objects" == "/" ] ; then
			    printf 'smtp_mode (label "%s")' "`_ "SMTP transport status"`"
			    printf 'filter_mode (label "%s")' "`_ "Spam filter mode"`"
			    printf 'required_score (label "%s" match ("[0-9]+" "%s"))' \
				"`_ "Required score"`" \
				"`_ "should be number"`"
			    printf 'use_awl (default #f label "%s")' "`_ "Use automatic sender whitelist"`"
			fi
			printf 'name (label "%s")' "`_ "Rule"`"
			printf 'score (label "%s")' "`_ "Score"`"
			echo ')'
			;;
		list)
			echo '('
			case "$in__objects" in
			smtp_modes)
			    printf '("local" label "%s")' "`_ "Off"`"
			    printf '("server" label "%s")' "`_ "On, without content filter"`"
			    printf '("filter" label "%s")' "`_ "On, with content filter"`"
			    ;;
			filter_modes)
			    printf '("pass" label "%s")' "`_ "Mark only"`"
			    printf '("drop" label"%s")' "`_ "Drop mail"`"
			    ;;
			scores)
			    list_scores
			    ;;
			*)
			    list_rules
			    ;;
			esac
			echo ')'
			;;
		read)
			echo '('
			if [ "$in__objects" = "/" ]; then
			printf ' smtp_mode "%s"' "$(control postfix)"
			printf ' filter_mode "%s"' "$(read_filter_mode)"
			printf ' required_score "%s"' "$(read_required_score)"
			printf ' use_awl %s' "$(read_use_awl)"
			else
				read_rule "${in__objects#*/}"
			fi
			echo ')'
			;;
		write)
			[ -n "$in_smtp_mode" ] && control postfix "$in_smtp_mode" >&2
			[ -n "$in_required_score" ] && write_required_score "$in_required_score"
			[ -n "$in_filter_mode" ] && write_filter_mode "$in_filter_mode"
			[ -n "$in_use_awl" ] && write_use_awl "$in_use_awl"
			if [ "${in__objects%%/*}" = "add" ]; then
				shell_add_or_subst "score $in_name" " $in_score" "$SPAMD_CONF"
			fi
			echo '()'
			;;
		delete)
			if [ "${in__objects%%/*}" = "scores" ]; then
				sed "/^score ${in__objects#*/}/ {d}" -i "$SPAMD_CONF"
			fi
			echo '()'
			;;
		*)
			echo '#f'
			;;
	esac
}


message_loop

