imapfilter convenience wrapper

imapdomo.skel 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/bash
  2. #shellcheck disable=SC1090
  3. . "$(sfpath)" || exit 3
  4. shellfu import pretty
  5. IMAPDOMO_CFGDIR="__IMAPDOMO_CONFIG_USER__"
  6. IMAPDOMO_USER_CACHE="__IMAPDOMO_USER_CACHE__"
  7. IMAPDOMO_HOME="__IMAPDOMO_SHARE__"
  8. usage() {
  9. mkusage "$@" "[options] ACTION" \
  10. -o \
  11. "-c DIR change to DIR before doing anything" \
  12. "-d turn on debugging mode" \
  13. -- \
  14. "imapdomo will try to read init.lua and handlers/ACTION.lua from its" \
  15. "configuration directory." \
  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. show_version() {
  45. echo '__MKIT_PROJ_NAME__ (__MKIT_PROJ_TAGLINE__) __MKIT_PROJ_VERSION__'
  46. exit 0
  47. }
  48. show_semversion() {
  49. echo '__MKIT_PROJ_VERSION__'
  50. exit 0
  51. }
  52. main() {
  53. local Action # what to do
  54. local Debug # true if debugging
  55. local cmd # imapfilter command
  56. local CfgDir # config directory
  57. local LogDir # directory to store logs
  58. local HeaderDir # directory to store headers by save_header()
  59. local CdTo # change dir to this before running imapfilter
  60. CfgDir="$IMAPDOMO_CFGDIR"
  61. LogDir="$IMAPDOMO_USER_CACHE/logs"
  62. HeaderDir="$IMAPDOMO_USER_CACHE/headers"
  63. Debug=false
  64. #shellcheck disable=SC2034
  65. while true; do case $1 in
  66. -c) CdTo="$2"; shift 2 || usage -w "missing value to: $1" ;;
  67. -d) Debug=true; PRETTY_DEBUG=true; shift ;;
  68. -V|--version-semver) show_semversion ;;
  69. --version) show_version ;;
  70. -*) usage -w "unknown argument: '$1'" ;;
  71. *) break ;;
  72. esac done
  73. Action="$1"; shift
  74. grep -qw "$Action" <<< "newmail|rewind|cleanup|migrate" \
  75. || usage -w "invalid action: $Action"
  76. cmd=$(mkcmd)
  77. debug -v cmd
  78. bash -n <<<"$cmd" || die
  79. if test -n "$CdTo";
  80. then
  81. cd "$CdTo" || die
  82. fi
  83. mkdir -p "$HeaderDir" || die
  84. eval "$cmd"
  85. }
  86. main "$@"