imapfilter convenience wrapper

imapdomo.skel 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. warn "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. handle() {
  66. #
  67. # Handle action $Action
  68. #
  69. local cmd # imapfilter command
  70. lshandlers | grep -qw "$Action" || {
  71. warn "no handler for action: $Action.lua in $CfgDir/handlers"
  72. return 2
  73. }
  74. cmd=$(mkcmd)
  75. debug -v cmd
  76. bash -n <<<"$cmd" || {
  77. bug "bad syntax of cmd: '$cmd'"
  78. return 3
  79. }
  80. if test -n "$CdTo";
  81. then
  82. cd "$CdTo" || {
  83. warn "cannot chdir to: $CdTo"
  84. return 3
  85. }
  86. fi
  87. mkdir -p "$HeaderDir" || {
  88. warn "cannot create header directory: $HeaderDir"
  89. return 3
  90. }
  91. eval "$cmd"
  92. }
  93. main() {
  94. local Action # what to do
  95. local Debug # true if debugging
  96. local CfgDir # config directory
  97. local LogDir # directory to store logs
  98. local HeaderDir # directory to store headers by save_header()
  99. local CdTo # change dir to this before running imapfilter
  100. local Certs # certificate storage
  101. local es=0 # exit status of this function
  102. CfgDir="$IMAPDOMO_CFGDIR"
  103. LogDir="$IMAPDOMO_USER_CACHE/logs"
  104. HeaderDir="$IMAPDOMO_USER_CACHE/headers"
  105. Certs="$IMAPDOMO_CFGDIR/certificates"
  106. Debug=false
  107. #shellcheck disable=SC2034
  108. while true; do case $1 in
  109. -c) CdTo="$2"; shift 2 || usage -w "missing value to: $1" ;;
  110. -d) Debug=true; PRETTY_DEBUG=true; shift ;;
  111. -t) Certs="$2"; shift 2 || usage -w "missing value to: $1" ;;
  112. -l) lshandlers; exit ;;
  113. -V|--version-semver) show_semversion ;;
  114. --version) show_version ;;
  115. -*) usage -w "unknown argument: '$1'" ;;
  116. *) break ;;
  117. esac done
  118. Action="$1"; shift
  119. test -n "$Action" || usage -w "no action specified"
  120. debug -v Action CfgDir LogDir HeaderDir Debug CdTo
  121. handle "$Action"; es=$?
  122. return $es
  123. }
  124. main "$@"