imapfilter convenience wrapper

imapdomo.skel 5.2KB

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