imapfilter convenience wrapper

imapdomo.skel 4.8KB

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