imapfilter convenience wrapper

imapdomo.skel 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash
  2. . "$(sfpath)" || 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 init.lua and handlers/ACTION.lua from its" \
  14. "configuration directory." \
  15. "" \
  16. "four valid actions are understood; since you must write handler for" \
  17. "each action you want to use, the meanings below are purely a guide:" \
  18. "" \
  19. " newmail - check for and file new mail" \
  20. " rewind - re-add messages from FILTER_FAIL back to FILTER_QUEUE" \
  21. " cleanup - delete or archive old messages" \
  22. " migrate - move mail between mailboxes" \
  23. "" \
  24. "See imapfilter_config(5)) for guide and API reference. Few functions"\
  25. "are also available in .imapdomo/common.lua"
  26. }
  27. mkcmd() {
  28. #
  29. # Compose imapfilter command
  30. #
  31. echo -n "IMAPDOMO_ACTION=$Action"
  32. echo -n " IMAPDOMO_HEADERS=$HeaderDir"
  33. echo -n " IMAPFILTER_HOME=$CfgDir"
  34. echo -n " imapfilter"
  35. if $Debug
  36. then
  37. mkdir -p "$LogDir"
  38. echo -n " -d $LogDir/debug.log"
  39. echo -n " -v"
  40. fi
  41. echo -n " -c $IMAPDOMO_HOME/main.lua"
  42. }
  43. show_version() {
  44. echo '__MKIT_PROJ_NAME__ (__MKIT_PROJ_TAGLINE__) __MKIT_PROJ_VERSION__'
  45. exit 0
  46. }
  47. show_semversion() {
  48. echo '__MKIT_PROJ_VERSION__'
  49. exit 0
  50. }
  51. main() {
  52. local Action # what to do
  53. local Debug # true if debugging
  54. local cmd # imapfilter command
  55. local CfgDir # config directory
  56. local LogDir # directory to store logs
  57. local HeaderDir # directory to store headers by save_header()
  58. local CdTo # change dir to this before running imapfilter
  59. CfgDir="$IMAPDOMO_CFGDIR"
  60. LogDir="$IMAPDOMO_USER_CACHE/logs"
  61. HeaderDir="$IMAPDOMO_USER_CACHE/headers"
  62. Debug=false
  63. #shellcheck disable=SC2034
  64. while true; do case $1 in
  65. -c) CdTo="$2"; shift 2 || usage ;;
  66. -d) Debug=true; PRETTY_DEBUG=true; shift ;;
  67. -V|--version-semver) show_semversion ;;
  68. --version) show_version ;;
  69. -*) usage ;;
  70. *) break ;;
  71. esac done
  72. Action="$1"; shift
  73. grep -qw "$Action" <<< "newmail|rewind|cleanup|migrate" || usage
  74. cmd=$(mkcmd)
  75. debug -v cmd
  76. bash -n <<<"$cmd" || die
  77. if test -n "$CdTo";
  78. then
  79. cd "$CdTo" || die
  80. fi
  81. mkdir -p "$HeaderDir" || die
  82. eval "$cmd"
  83. }
  84. main "$@"