imapfilter convenience wrapper

imapdomo.skel 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "-N turn off locking" \
  15. -- \
  16. "imapdomo will try to read init.lua and handlers/ACTION.lua from its" \
  17. "configuration directory." \
  18. "" \
  19. "See imapfilter_config(5)) for guide and API reference. Few functions"\
  20. "are also available in .imapdomo/common.lua"
  21. }
  22. bug() {
  23. #
  24. # Die because of bug
  25. #
  26. local msg=$1
  27. local self
  28. self=$(basename "$0")
  29. warn "bug in $self (__MKIT_PROJ_VERSION__): $msg"
  30. }
  31. mkcmd() {
  32. #
  33. # Compose imapfilter command
  34. #
  35. echo -n "IMAPDOMO_ACTION=$Action"
  36. echo -n " IMAPDOMO_HEADERS=$HeaderDir"
  37. echo -n " IMAPFILTER_HOME=$CfgDir"
  38. echo -n " imapfilter"
  39. if $Debug
  40. then
  41. mkdir -p "$LogDir"
  42. echo -n " -d $LogDir/debug.log"
  43. echo -n " -v"
  44. fi
  45. echo -n " -c $IMAPDOMO_HOME/main.lua"
  46. if test -n "$Certs"
  47. then
  48. echo -n " -t $Certs"
  49. fi
  50. }
  51. show_version() {
  52. echo '__MKIT_PROJ_NAME__ (__MKIT_PROJ_TAGLINE__) __MKIT_PROJ_VERSION__'
  53. exit 0
  54. }
  55. show_semversion() {
  56. echo '__MKIT_PROJ_VERSION__'
  57. exit 0
  58. }
  59. lshandlers() {
  60. #
  61. # List recognized handlers
  62. #
  63. find "$CfgDir/handlers" -name "*.lua" -printf "%f\n" \
  64. | sed 's/.lua$//'
  65. }
  66. lock() {
  67. #
  68. # Put the lockfile
  69. #
  70. $NoLock && return 0
  71. date +"$$@%s" > "$IMAPDOMO_USER_CACHE/lock"
  72. }
  73. is_locked() {
  74. #
  75. # Does lockfile exist?
  76. #
  77. # True if lockfile exists. False if lockfile does not exist or locking
  78. # is turned off.
  79. #
  80. $NoLock && return 1
  81. test -e "$IMAPDOMO_USER_CACHE/lock"
  82. }
  83. unlock() {
  84. #
  85. # Remove the lockfile
  86. #
  87. $NoLock && return 0
  88. rm "$IMAPDOMO_USER_CACHE/lock"
  89. }
  90. handle() {
  91. #
  92. # Handle action $Action
  93. #
  94. local cmd # imapfilter command
  95. lshandlers | grep -qw "$Action" || {
  96. warn "no handler for action: $Action.lua in $CfgDir/handlers"
  97. return 2
  98. }
  99. cmd=$(mkcmd)
  100. debug -v cmd
  101. bash -n <<<"$cmd" || {
  102. bug "bad syntax of cmd: '$cmd'"
  103. return 3
  104. }
  105. if test -n "$CdTo";
  106. then
  107. cd "$CdTo" || {
  108. warn "cannot chdir to: $CdTo"
  109. return 3
  110. }
  111. fi
  112. mkdir -p "$HeaderDir" || {
  113. warn "cannot create header directory: $HeaderDir"
  114. return 3
  115. }
  116. eval "$cmd"
  117. }
  118. main() {
  119. local Action # what to do
  120. local Debug # true if debugging
  121. local CfgDir # config directory
  122. local LogDir # directory to store logs
  123. local HeaderDir # directory to store headers by save_header()
  124. local CdTo # change dir to this before running imapfilter
  125. local Certs # certificate storage
  126. local es=0 # exit status of this function
  127. local NoLock # disable locks
  128. CfgDir="$IMAPDOMO_CFGDIR"
  129. LogDir="$IMAPDOMO_USER_CACHE/logs"
  130. HeaderDir="$IMAPDOMO_USER_CACHE/headers"
  131. Certs="$IMAPDOMO_CFGDIR/certificates"
  132. NoLock=false
  133. Debug=false
  134. #shellcheck disable=SC2034
  135. while true; do case $1 in
  136. -c) CdTo="$2"; shift 2 || usage -w "missing value to: $1" ;;
  137. -d) Debug=true; PRETTY_DEBUG=true; shift ;;
  138. -t) Certs="$2"; shift 2 || usage -w "missing value to: $1" ;;
  139. -l) lshandlers; exit ;;
  140. -N) NoLock=true; shift ;;
  141. -V|--version-semver) show_semversion ;;
  142. --version) show_version ;;
  143. -*) usage -w "unknown argument: '$1'" ;;
  144. *) break ;;
  145. esac done
  146. Action="$1"; shift
  147. test -n "$Action" || usage -w "no action specified"
  148. debug -v Action CfgDir LogDir HeaderDir Debug CdTo NoLock
  149. is_locked && return 1
  150. lock
  151. handle "$Action"; es=$?
  152. unlock
  153. return $es
  154. }
  155. main "$@"