imapfilter convenience wrapper

imapdomo.skel 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. usage() {
  7. mkusage "[options] ACTION" \
  8. -o \
  9. "-c DIR change to DIR before doing anything" \
  10. "-d turn on debugging mode" \
  11. -- \
  12. "imapdomo will try to read .imapdomo/host/HOSTNAME/init.lua and" \
  13. ".imapdomo/host/HOSTNAME/handlers/ACTION.lua, where HOSTNAME is your" \
  14. "short hostname (eg. 'foo' in 'foo.example.com')." \
  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 $CfgDir/main.lua"
  42. }
  43. main() {
  44. local Action # what to do
  45. local Debug # true if debugging
  46. local cmd # imapfilter command
  47. local CfgDir # config directory
  48. local LogDir # directory to store logs
  49. local HeaderDir # directory to store headers by save_header()
  50. local CdTo # change dir to this before running imapfilter
  51. CfgDir="$IMAPDOMO_CFGDIR"
  52. LogDir="$IMAPDOMO_USER_CACHE/logs"
  53. HeaderDir="$IMAPDOMO_USER_CACHE/headers"
  54. Debug=false
  55. #shellcheck disable=SC2034
  56. while true; do case $1 in
  57. -c) CdTo="$2"; shift 2 || usage ;;
  58. -d) Debug=true; PRETTY_DEBUG=true; shift ;;
  59. -*) usage ;;
  60. *) break ;;
  61. esac done
  62. Action="$1"; shift
  63. grep -qw "$Action" <<< "newmail|rewind|cleanup|migrate" || usage
  64. cmd=$(mkcmd)
  65. debug -v cmd
  66. bash -n <<<"$cmd" || die
  67. if test -n "$CdTo";
  68. then
  69. cd "$CdTo" || die
  70. fi
  71. mkdir -p "$HeaderDir" || die
  72. eval "$cmd"
  73. }
  74. main "$@"