imapfilter convenience wrapper

imapdomo.skel 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. bug() {
  22. #
  23. # Die because of bug
  24. #
  25. local msg=$1
  26. local self
  27. self=$(basename "$0")
  28. die "bug in $self (__MKIT_PROJ_VERSION__): $msg"
  29. }
  30. mkcmd() {
  31. #
  32. # Compose imapfilter command
  33. #
  34. echo -n "IMAPDOMO_ACTION=$Action"
  35. echo -n " IMAPDOMO_HEADERS=$HeaderDir"
  36. echo -n " IMAPFILTER_HOME=$CfgDir"
  37. echo -n " imapfilter"
  38. if $Debug
  39. then
  40. mkdir -p "$LogDir"
  41. echo -n " -d $LogDir/debug.log"
  42. echo -n " -v"
  43. fi
  44. echo -n " -c $IMAPDOMO_HOME/main.lua"
  45. }
  46. show_version() {
  47. echo '__MKIT_PROJ_NAME__ (__MKIT_PROJ_TAGLINE__) __MKIT_PROJ_VERSION__'
  48. exit 0
  49. }
  50. show_semversion() {
  51. echo '__MKIT_PROJ_VERSION__'
  52. exit 0
  53. }
  54. lshandlers() {
  55. #
  56. # List recognized handlers
  57. #
  58. find "$CfgDir/handlers" -name "*.lua" -printf "%f\n" \
  59. | sed 's/.lua$//'
  60. }
  61. main() {
  62. local Action # what to do
  63. local Debug # true if debugging
  64. local cmd # imapfilter command
  65. local CfgDir # config directory
  66. local LogDir # directory to store logs
  67. local HeaderDir # directory to store headers by save_header()
  68. local CdTo # change dir to this before running imapfilter
  69. CfgDir="$IMAPDOMO_CFGDIR"
  70. LogDir="$IMAPDOMO_USER_CACHE/logs"
  71. HeaderDir="$IMAPDOMO_USER_CACHE/headers"
  72. Debug=false
  73. #shellcheck disable=SC2034
  74. while true; do case $1 in
  75. -c) CdTo="$2"; shift 2 || usage -w "missing value to: $1" ;;
  76. -d) Debug=true; PRETTY_DEBUG=true; shift ;;
  77. -l) lshandlers; exit ;;
  78. -V|--version-semver) show_semversion ;;
  79. --version) show_version ;;
  80. -*) usage -w "unknown argument: '$1'" ;;
  81. *) break ;;
  82. esac done
  83. Action="$1"; shift
  84. test -n "$Action" || usage -w "no action specified"
  85. debug -v Action CfgDir LogDir HeaderDir Debug CdTo
  86. lshandlers | grep -qw "$Action" \
  87. || die "no handler for action: $Action.lua in $IMAPDOMO_HOME/handlers"
  88. cmd=$(mkcmd)
  89. debug -v cmd
  90. bash -n <<<"$cmd" || bug "bad syntax of cmd: '$cmd'"
  91. if test -n "$CdTo";
  92. then
  93. cd "$CdTo" || die "cannot chdir to: $CdTo"
  94. fi
  95. mkdir -p "$HeaderDir" || die "cannot create header directory: $HeaderDir"
  96. eval "$cmd"
  97. }
  98. main "$@"