123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717 |
- #!/bin/bash
-
-
-
-
-
-
- shellfu() { :; }
- shellfu-get() { echo /dev/null; }
-
-
-
-
-
- #!/bin/bash
-
-
- EXIT_OK=0
- EXIT_NO=1
- EXIT_USAGE=2
- EXIT_ERROR=3
- EXIT_PANIC=4
-
-
- exit_ok() {
-
-
-
- exit $EXIT_OK
- }
-
-
- exit_no() {
-
-
-
- exit $EXIT_NO
- }
-
-
- exit_usage() {
-
-
-
- exit $EXIT_USAGE
- }
-
-
- exit_error() {
-
-
-
- exit $EXIT_ERROR
- }
-
-
- exit_panic() {
-
-
-
- exit $EXIT_PANIC
- }
-
-
-
-
-
-
-
-
-
- #!/bin/bash
-
- shellfu import exit
-
-
-
-
- PRETTY_DEBUG=${PRETTY_DEBUG:-false}
-
-
-
-
-
-
-
-
-
-
-
-
-
- PRETTY_DEBUG_EXCLUDE=${PRETTY_DEBUG_EXCLUDE:-}
-
-
-
-
- PRETTY_VERBOSE=${PRETTY_VERBOSE:-false}
-
-
-
-
-
-
-
-
-
- PRETTY=${PRETTY:-plain}
-
-
-
-
- PRETTY_USAGE=${PRETTY_USAGE:-}
-
-
- __shellfu_pretty__init() {
-
-
-
- if shellfu try_import "_pretty_${PRETTY}";
- then
- shellfu import "_pretty_${PRETTY}"
- return 0
- else
- warn "falling back to _pretty_plain"
- PRETTY=plain
- shellfu import "_pretty_${PRETTY}"
- fi
- }
-
-
-
-
-
-
- debug() {
-
-
-
-
-
-
-
-
- $PRETTY_DEBUG || return 0
- _pretty__echo "$@"
- }
-
-
- debug_pipe() {
-
-
-
- while IFS= read -r line;
- do
- debug "|$1: '$line'"
- echos "$line"
- done
- }
-
-
- die() {
-
-
-
- _pretty__echo -t
- _pretty__echo "$@"
- exit_error
- }
-
-
- echos() {
-
-
-
-
-
-
-
-
-
- printf -- '%s\n' "$@"
- }
-
-
- mkusage() {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- local es=$EXIT_USAGE
- local doexit=true
- while true; do case "$1" in
- -e) es="$2"; shift 2 || return 2 ;;
- -E) doexit=false; shift ;;
- -k) es=$EXIT_OK; shift ;;
- --) shift; break ;;
- *) break ;;
- esac done
- _pretty__echo -u "$@";
- $doexit && exit "$es"
- return "$es"
- }
-
-
- mkhelp() {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- local es=$EXIT_OK
- local doexit=true
- while true; do case "$1" in
- -e) es="$2"; shift 2 || return 2 ;;
- -E) doexit=false; shift ;;
- --) shift; break ;;
- *) break ;;
- esac done
- _pretty__echo "$@"
- $doexit && exit "$es"
- return "$es"
- }
-
-
- think() {
-
-
-
-
-
-
- $PRETTY_VERBOSE || return 0
- _pretty__echo "$@"
- }
-
-
- warn() {
-
-
-
-
-
-
- _pretty__echo "$@"
- }
-
-
-
-
-
-
-
- _pretty__cat() {
-
-
-
-
-
-
- while IFS= read -r line;
- do echos "$line"; done
- }
-
-
- _pretty__get_caller() {
-
-
-
- local fname
- local nidx="${#FUNCNAME[@]}"
- for fname in "${FUNCNAME[@]}";
- do
- (( nidx-- ))
- _pretty__is_internal && continue
- _pretty__is_frontend && continue
- test "$fname" = "usage" && continue
- echos "$nidx" "$fname"
- return
- done
- }
-
- _pretty__get_frontend() {
-
-
-
- local fname
- for fname in "${FUNCNAME[@]}";
- do
- _pretty__is_internal && continue
- _pretty__is_frontend && echos "$fname" && return 0
- echo "do not call _pretty_* directly: $fname" >&2
- return "$EXIT_USAGE"
- done
- }
-
- _pretty__is_excluded() {
-
-
-
-
-
-
- local listed
- local name
- local qualified
- name="$caller"
- case "$name" in
- __*__*) qualified=true ;;
- __*) qualified=false ;;
- *__*) qualified=true ;;
- *) qualified=false ;;
- esac
- if $qualified;
- then
-
- name=${name#_}
- name=${name#_}
- name=${name%__*}
- fi
- for listed in ${PRETTY_DEBUG_EXCLUDE//,/ };
- do
- test "$name" = "$listed" && return 0
- done
- return 1
- }
-
- _pretty__is_frontend() {
-
-
-
- case "$fname" in
- debug) return 0 ;;
- debug_pipe) return 0 ;;
- die) return 0 ;;
- mkhelp) return 0 ;;
- think) return 0 ;;
- mkusage) return 0 ;;
- warn) return 0 ;;
- esac
- return 1
- }
-
- _pretty__is_internal() {
-
-
-
- case "$fname" in
- _pretty__*) return 0 ;;
- *) return 1 ;;
- esac
- }
-
- _pretty__echo() {
-
-
-
-
-
-
-
-
-
-
-
-
-
- local frontend
- local caller
-
-
-
- local caller_nidx
- local caller_is_main
- local provider
- frontend="$(_pretty__get_frontend)" || exit_usage
- read caller_nidx caller <<<$(_pretty__get_caller)
- test "$frontend" = debug && _pretty__is_excluded "$caller" && return 0
- case $caller_nidx:$caller in
- 0:*) caller_is_main=true; caller="${0##*/}" ;;
- 1:main) caller_is_main=true; caller="${0##*/}" ;;
- *:usage) frontend=mkusage ;;
- *) caller_is_main=false ;;
- esac
- while true; do case $1 in
- -c|--cmd) provider=cmd; shift; break ;;
- -f|--files) provider=files; shift; break ;;
- -l|--lines) provider=lines; shift; break ;;
- -t|--trace) provider=trace; shift; break ;;
- -u|--usage) provider=usage; shift; break ;;
- -v|--vars) provider=vars; shift; break ;;
- *) provider=args; break ;;
- esac done
- _pretty__echo_$provider "$@" \
- | _pretty__$frontend >&2
- }
-
-
- _pretty__echo_args() {
-
-
-
- echos "$*"
- }
-
-
- _pretty__echo_cmd() {
-
-
-
- local c=\$
- test "$(id -u)" -eq 0 && c="#"
- echo "$c $*"
- "$@"
- echo "^ exit status: $?"
- }
-
-
- _pretty__echo_files() {
-
-
-
- local fp
- for fp in "$@";
- do
- if test "$fp" = "-";
- then
- cat
- elif test -s "$fp" || test "$fp" = "/dev/stdin";
- then
- echo "-- $fp --"
- cat "$fp"
- fi
- done
- }
-
-
- _pretty__echo_lines() {
-
-
-
- local l;
- for l in "$@"; do _pretty__echo_args "$l"; done
- }
-
-
- _pretty__echo_trace() {
-
-
-
- $PRETTY_DEBUG || return 0
- local depth
- echo "== trace =="
- for depth in $(seq 0 ${#FUNCNAME});
- do
- caller "$depth" || break
- done \
- | tail -n +3 \
- | sed -e '
- s/^\([^ ]\+\) \([^ ]\+\) \(.*\)/\3:\1:\2()/
- # ^line^, ^func^, ^file^
- 1 s/^/ -> /g
- 2,$ s/^/ /
- ' \
- | tac
- }
-
-
- _pretty__echo_help() {
- local oldverbose="$PRETTY_VERBOSE"
- think -l "$@"
- PRETTY_VERBOSE=$oldverbose
- }
-
- _pretty__echo_usage() {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- local self
- local mode=usage
- local esc=1
- local arg
- case "$PRETTY_USAGE" in
- subcommand) self="${0##*/}"; self="${self/-/ }" ;;
- *) self="$caller" ;;
- esac
- case $# in
- 0) return 0 ;;
- 1) echo "usage: $self $1"; return 0 ;;
- esac
- echo usage:
- for arg in "$@";
- do
- case $esc:$arg in
- 0:--) shift; mode=plain; esc=1; echo ;;
- 0:-c) shift; mode=indent; esc=1; echo; echo commands: ;;
- 0:-i) shift; mode=indent; esc=1; echo ;;
- 0:-o) shift; mode=indent; esc=1; echo; echo options: ;;
- 0:-u) shift; mode=usage; esc=1 ;;
- *) esc=0
- case $mode in
- usage) echo " $self $arg" ;;
- indent) echo " $arg" ;;
- plain) echos "$arg" ;;
- esac
- ;;
- esac
- done
- }
-
-
- _pretty__echo_vars() {
-
-
-
- local varname
- local declare_str
- for varname in "$@";
- do
- if ! _pretty__is_word "$varname";
- then
- warn "unsafe value skipped: $varname";
- continue
- fi
- if declare_str=$(declare -p "$varname" 2>/dev/null);
- then
- _pretty__echo "${declare_str#declare ?? }"
- else
- _pretty__echo "$varname #Unset"
- fi
- done
- }
-
-
- _pretty__is_word() {
-
-
-
- local tainted="$1"
- local clean=$(tr -c -d '_[:alnum:]' <<< "$tainted")
- test "$tainted" = "$clean"
- }
-
-
-
-
-
-
-
-
-
- #!/bin/bash
-
-
- _pretty__debug() {
- local decor="()"
- $caller_is_main && decor=
- while IFS= read -r line;
- do echo "debug:$caller$decor: $line"; done
- }
-
- _pretty__die() {
- while IFS= read -r line;
- do echo "$line"; done
- }
-
- _pretty__mkhelp() {
- while IFS= read -r line;
- do echo "$line"; done
- }
-
- _pretty__mkusage() {
- while IFS= read -r line;
- do echo "$line"; done
- }
-
- _pretty__think() {
- while IFS= read -r line;
- do echo "$line"; done
- }
-
- _pretty__warn() {
- while IFS= read -r line;
- do echo "$line"; done
- }
-
-
-
-
-
- #!/bin/bash
-
- . "$(shellfu-get path)" || exit 3
-
- shellfu import pretty
-
- usage() {
- mkusage "newmail" \
- "rewind" \
- "cleanup" \
- -- "newmail - check for and file new mail" \
- "rewind - re-add messages from FILTER_FAIL back" \
- " to FILTER_QUEUE" \
- "cleanup - delete or archive old messages"
- }
-
- mkcmd() {
-
-
-
- echo -n "IMAPDOMO_ACTION=$Action"
- 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
- local Debug
- local cmd
- local CfgDir
- local LogDir
- local CdTo
- CfgDir="$HOME/.imapdomo"
- LogDir="$HOME/.cache/imapdomo/logs"
- Debug=false
- 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" || usage
- cmd=$(mkcmd)
- debug -v cmd
- bash -n <<<"$cmd" || die
- if test -n "$CdTo";
- then
- cd "$CdTo" || die
- fi
- eval "$cmd"
- }
-
- main "$@"
-
-
|