imapfilter convenience wrapper

imapdomo.skel 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. if test -n "$Certs"
  46. then
  47. echo -n " -t $Certs"
  48. fi
  49. }
  50. show_version() {
  51. echo '__MKIT_PROJ_NAME__ (__MKIT_PROJ_TAGLINE__) __MKIT_PROJ_VERSION__'
  52. exit 0
  53. }
  54. show_semversion() {
  55. echo '__MKIT_PROJ_VERSION__'
  56. exit 0
  57. }
  58. lshandlers() {
  59. #
  60. # List recognized handlers
  61. #
  62. find "$CfgDir/handlers" -name "*.lua" -printf "%f\n" \
  63. | sed 's/.lua$//'
  64. }
  65. main() {
  66. local Action # what to do
  67. local Debug # true if debugging
  68. local cmd # imapfilter command
  69. local CfgDir # config directory
  70. local LogDir # directory to store logs
  71. local HeaderDir # directory to store headers by save_header()
  72. local CdTo # change dir to this before running imapfilter
  73. local Certs # certificate storage
  74. CfgDir="$IMAPDOMO_CFGDIR"
  75. LogDir="$IMAPDOMO_USER_CACHE/logs"
  76. HeaderDir="$IMAPDOMO_USER_CACHE/headers"
  77. Certs="$IMAPDOMO_CFGDIR/certificates"
  78. Debug=false
  79. #shellcheck disable=SC2034
  80. while true; do case $1 in
  81. -c) CdTo="$2"; shift 2 || usage -w "missing value to: $1" ;;
  82. -d) Debug=true; PRETTY_DEBUG=true; shift ;;
  83. -t) Certs="$2"; shift 2 || usage -w "missing value to: $1" ;;
  84. -l) lshandlers; exit ;;
  85. -V|--version-semver) show_semversion ;;
  86. --version) show_version ;;
  87. -*) usage -w "unknown argument: '$1'" ;;
  88. *) break ;;
  89. esac done
  90. Action="$1"; shift
  91. test -n "$Action" || usage -w "no action specified"
  92. debug -v Action CfgDir LogDir HeaderDir Debug CdTo
  93. lshandlers | grep -qw "$Action" \
  94. || die "no handler for action: $Action.lua in $CfgDir/handlers"
  95. cmd=$(mkcmd)
  96. debug -v cmd
  97. bash -n <<<"$cmd" || bug "bad syntax of cmd: '$cmd'"
  98. if test -n "$CdTo";
  99. then
  100. cd "$CdTo" || die "cannot chdir to: $CdTo"
  101. fi
  102. mkdir -p "$HeaderDir" || die "cannot create header directory: $HeaderDir"
  103. eval "$cmd"
  104. }
  105. main "$@"