imapfilter convenience wrapper

imapdomo.skel 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. debug -v NoLock
  77. $NoLock && return 0
  78. date +"$$@%s" > "$IMAPDOMO_USER_CACHE/lock"
  79. debug -f "$IMAPDOMO_USER_CACHE/lock"
  80. }
  81. is_locked() {
  82. #
  83. # Does lockfile exist?
  84. #
  85. # True if lockfile exists. False if lockfile does not exist or locking
  86. # is turned off.
  87. #
  88. debug -v NoLock
  89. $NoLock && return 1
  90. debug -f "$IMAPDOMO_USER_CACHE/lock"
  91. test -e "$IMAPDOMO_USER_CACHE/lock"
  92. }
  93. unlock() {
  94. #
  95. # Remove the lockfile
  96. #
  97. debug -v NoLock
  98. $NoLock && return 0
  99. debug -f "$IMAPDOMO_USER_CACHE/lock"
  100. rm "$IMAPDOMO_USER_CACHE/lock"
  101. }
  102. handle() {
  103. #
  104. # Handle action $Action
  105. #
  106. local cmd # imapfilter command
  107. lshandlers | grep -qwe "$Action" || {
  108. warn "no handler for action: $Action.lua in $CfgDir/handlers"
  109. return 2
  110. }
  111. cmd=$(mkcmd)
  112. debug -v cmd
  113. bash -n <<<"$cmd" || {
  114. bug "bad syntax of cmd: '$cmd'"
  115. return 3
  116. }
  117. if test -n "$CdTo";
  118. then
  119. cd "$CdTo" || {
  120. warn "cannot chdir to: $CdTo"
  121. return 3
  122. }
  123. fi
  124. mkdir -p "$HeaderDir" || {
  125. warn "cannot create header directory: $HeaderDir"
  126. return 3
  127. }
  128. eval "$cmd"
  129. }
  130. main() {
  131. local Action # what to do
  132. local Debug # true if debugging
  133. local CfgDir # config directory
  134. local LogDir # directory to store logs
  135. local HeaderDir # directory to store headers by save_header()
  136. local CdTo # change dir to this before running imapfilter
  137. local Certs # certificate storage
  138. local es=0 # exit status of this function
  139. local NoLock # disable locks
  140. CfgDir="$IMAPDOMO_CFGDIR"
  141. LogDir="$IMAPDOMO_USER_CACHE/logs"
  142. HeaderDir="$IMAPDOMO_USER_CACHE/headers"
  143. Certs="$IMAPDOMO_CFGDIR/certificates"
  144. NoLock=false
  145. Debug=false
  146. #shellcheck disable=SC2034
  147. while true; do case $1 in
  148. -c) CdTo="$2"; shift 2 || usage -w "missing value to: $1" ;;
  149. -d) Debug=true; PRETTY_DEBUG=true; shift ;;
  150. -t) Certs="$2"; shift 2 || usage -w "missing value to: $1" ;;
  151. -l) lshandlers; exit ;;
  152. -N) NoLock=true; shift ;;
  153. -V|--version-semver) show_semversion ;;
  154. --version) show_version ;;
  155. -*) usage -w "unknown argument: '$1'" ;;
  156. *) break ;;
  157. esac done
  158. Action="$1"; shift
  159. test -n "$Action" || usage -w "no action specified"
  160. debug -v Action CfgDir LogDir HeaderDir Debug CdTo NoLock
  161. is_locked && return 1
  162. lock
  163. handle; es=$?
  164. unlock
  165. return $es
  166. }
  167. main "$@"