#!/bin/bash . "$(shellfu-get path)" || exit 3 shellfu import pretty IMAPDOMO_CFGDIR="__IMAPDOMO_CONFIG_USER__" IMAPDOMO_USER_CACHE="__IMAPDOMO_USER_CACHE__" usage() { mkusage "[options] ACTION" \ -o \ "-c DIR change to DIR before doing anything" \ "-d turn on debugging mode" \ -- \ "imapdomo will try to read .imapdomo/host/HOSTNAME/init.lua and" \ ".imapdomo/host/HOSTNAME/handlers/ACTION.lua, where HOSTNAME is your" \ "short hostname (eg. 'foo' in 'foo.example.com')." \ "" \ "four valid actions are understood; since you must write handler for" \ "each action you want to use, the meanings below are purely a guide:" \ "" \ " newmail - check for and file new mail" \ " rewind - re-add messages from FILTER_FAIL back to FILTER_QUEUE" \ " cleanup - delete or archive old messages" \ " migrate - move mail between mailboxes" \ "" \ "See imapfilter_config(5)) for guide and API reference. Few functions"\ "are also available in .imapdomo/common.lua" } mkcmd() { # # Compose imapfilter command # echo -n "IMAPDOMO_ACTION=$Action" echo -n " IMAPDOMO_HEADERS=$HeaderDir" echo -n " IMAPFILTER_HOME=$CfgDir" echo -n " imapfilter" if $Debug then mkdir -p "$LogDir" echo -n " -d $LogDir/debug.log" echo -n " -v" fi echo -n " -c $CfgDir/main.lua" } main() { local Action # what to do local Debug # true if debugging local cmd # imapfilter command local CfgDir # config directory local LogDir # directory to store logs local HeaderDir # directory to store headers by save_header() local CdTo # change dir to this before running imapfilter CfgDir="$IMAPDOMO_CFGDIR" LogDir="$IMAPDOMO_USER_CACHE/logs" HeaderDir="$IMAPDOMO_USER_CACHE/headers" Debug=false #shellcheck disable=SC2034 while true; do case $1 in -c) CdTo="$2"; shift 2 || usage ;; -d) Debug=true; PRETTY_DEBUG=true; shift ;; -*) usage ;; *) break ;; esac done Action="$1"; shift grep -qw "$Action" <<< "newmail|rewind|cleanup|migrate" || usage cmd=$(mkcmd) debug -v cmd bash -n <<<"$cmd" || die if test -n "$CdTo"; then cd "$CdTo" || die fi mkdir -p "$HeaderDir" || die eval "$cmd" } main "$@"