imapfilter convenience wrapper

imapdomo.skel 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/bash
  2. . "$(shellfu-get path)" || exit 3
  3. shellfu import pretty
  4. IMAPDOMO_CFGDIR="__IMAPDOMO_CONFIG_USER__"
  5. IMAPDOMO_USER_CACHE="__IMAPDOMO_USER_CACHE__"
  6. IMAPDOMO_HOME="__IMAPDOMO_SHARE__"
  7. usage() {
  8. mkusage "[options] ACTION" \
  9. -o \
  10. "-c DIR change to DIR before doing anything" \
  11. "-d turn on debugging mode" \
  12. -- \
  13. "imapdomo will try to read .imapdomo/host/HOSTNAME/init.lua and" \
  14. ".imapdomo/host/HOSTNAME/handlers/ACTION.lua, where HOSTNAME is your" \
  15. "short hostname (eg. 'foo' in 'foo.example.com')." \
  16. "" \
  17. "four valid actions are understood; since you must write handler for" \
  18. "each action you want to use, the meanings below are purely a guide:" \
  19. "" \
  20. " newmail - check for and file new mail" \
  21. " rewind - re-add messages from FILTER_FAIL back to FILTER_QUEUE" \
  22. " cleanup - delete or archive old messages" \
  23. " migrate - move mail between mailboxes" \
  24. "" \
  25. "See imapfilter_config(5)) for guide and API reference. Few functions"\
  26. "are also available in .imapdomo/common.lua"
  27. }
  28. mkcmd() {
  29. #
  30. # Compose imapfilter command
  31. #
  32. echo -n "IMAPDOMO_ACTION=$Action"
  33. echo -n " IMAPDOMO_HEADERS=$HeaderDir"
  34. echo -n " IMAPFILTER_HOME=$CfgDir"
  35. echo -n " imapfilter"
  36. if $Debug
  37. then
  38. mkdir -p "$LogDir"
  39. echo -n " -d $LogDir/debug.log"
  40. echo -n " -v"
  41. fi
  42. echo -n " -c $IMAPDOMO_HOME/main.lua"
  43. }
  44. main() {
  45. local Action # what to do
  46. local Debug # true if debugging
  47. local cmd # imapfilter command
  48. local CfgDir # config directory
  49. local LogDir # directory to store logs
  50. local HeaderDir # directory to store headers by save_header()
  51. local CdTo # change dir to this before running imapfilter
  52. CfgDir="$IMAPDOMO_CFGDIR"
  53. LogDir="$IMAPDOMO_USER_CACHE/logs"
  54. HeaderDir="$IMAPDOMO_USER_CACHE/headers"
  55. Debug=false
  56. #shellcheck disable=SC2034
  57. while true; do case $1 in
  58. -c) CdTo="$2"; shift 2 || usage ;;
  59. -d) Debug=true; PRETTY_DEBUG=true; shift ;;
  60. -*) usage ;;
  61. *) break ;;
  62. esac done
  63. Action="$1"; shift
  64. grep -qw "$Action" <<< "newmail|rewind|cleanup|migrate" || usage
  65. cmd=$(mkcmd)
  66. debug -v cmd
  67. bash -n <<<"$cmd" || die
  68. if test -n "$CdTo";
  69. then
  70. cd "$CdTo" || die
  71. fi
  72. mkdir -p "$HeaderDir" || die
  73. eval "$cmd"
  74. }
  75. main "$@"