imapfilter convenience wrapper

imapdomo.skel 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "-l list handlers and exit" \
  13. "-d turn on debugging mode" \
  14. -- \
  15. "imapdomo will try to read init.lua and handlers/ACTION.lua from its" \
  16. "configuration directory." \
  17. "" \
  18. "See imapfilter_config(5)) for guide and API reference. Few functions"\
  19. "are also available in .imapdomo/common.lua"
  20. }
  21. mkcmd() {
  22. #
  23. # Compose imapfilter command
  24. #
  25. echo -n "IMAPDOMO_ACTION=$Action"
  26. echo -n " IMAPDOMO_HEADERS=$HeaderDir"
  27. echo -n " IMAPFILTER_HOME=$CfgDir"
  28. echo -n " imapfilter"
  29. if $Debug
  30. then
  31. mkdir -p "$LogDir"
  32. echo -n " -d $LogDir/debug.log"
  33. echo -n " -v"
  34. fi
  35. echo -n " -c $IMAPDOMO_HOME/main.lua"
  36. }
  37. show_version() {
  38. echo '__MKIT_PROJ_NAME__ (__MKIT_PROJ_TAGLINE__) __MKIT_PROJ_VERSION__'
  39. exit 0
  40. }
  41. show_semversion() {
  42. echo '__MKIT_PROJ_VERSION__'
  43. exit 0
  44. }
  45. lshandlers() {
  46. #
  47. # List recognized handlers
  48. #
  49. find "$IMAPDOMO_HOME/handlers" -name "*.lua" -printf "%f\n" \
  50. | sed 's/.lua$//'
  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. -l) lshandlers; exit ;;
  69. -V|--version-semver) show_semversion ;;
  70. --version) show_version ;;
  71. -*) usage -w "unknown argument: '$1'" ;;
  72. *) break ;;
  73. esac done
  74. Action="$1"; shift
  75. lshandlers | grep -qw "$Action" \
  76. || dle "no handler for action: $Action.lua in $IMAPDOMO_HOME/handlers"
  77. cmd=$(mkcmd)
  78. debug -v cmd
  79. bash -n <<<"$cmd" || die
  80. if test -n "$CdTo";
  81. then
  82. cd "$CdTo" || die
  83. fi
  84. mkdir -p "$HeaderDir" || die
  85. eval "$cmd"
  86. }
  87. main "$@"