summaryrefslogtreecommitdiff
path: root/localsyncmail
blob: 009e0eb980266d1d0bfd06fa4d474fac18a4e199 (plain)
  1. #!/bin/sh
  2. set -e
  3. PRG=`basename $0`
  4. TEMP=`getopt -s sh -o ah:fk -l all,host:,force,kill -n "$PRG" -- "$@"`
  5. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  6. eval set -- "$TEMP"
  7. all="no"
  8. host="localhost"
  9. force="no"
  10. kill="no"
  11. while true ; do
  12. case "$1" in
  13. -a|--all) all="yes" ; shift ;;
  14. -h|--host) host="$2" ; shift 2 ;;
  15. -f|--force) force="yes" ; shift ;;
  16. -k|--kill) kill="yes" ; shift ;;
  17. --) shift ; break ;;
  18. *) echo "Internal error!" ; exit 1 ;;
  19. esac
  20. done
  21. USER="`whoami`"
  22. if [ "$all" = yes ]; then
  23. # users=$(find /home -mindepth 2 -maxdepth 2 -type f -name .offlineimaprc -printf '%h\n' | sed 's,.*/,,')
  24. users=$(members mailmirrors)
  25. else
  26. users="${@:-$USER}"
  27. fi
  28. #for user in $(find /home -mindepth 2 -maxdepth 2 -type f -name .offlineimaprc -printf '%h\n' | sed 's,.*/,,'); do
  29. for user in $users; do
  30. # x-terminal-emulator -e sh -c "echo \"while true; do pkill -u $user offlineimap || offlineimap -u Curses.Blinkenlights; done\" | ssh $user@localhost sh -s" &
  31. if [ "$host" = localhost ] && [ "$user" = "$USER" ]; then
  32. ssh=""
  33. else
  34. ssh="ssh $user@$host"
  35. fi
  36. if [ "$kill" = yes ]; then
  37. $ssh killall -wq -u $user offlineimap
  38. exit $?
  39. fi
  40. if $ssh pgrep -u $user offlineimap; then
  41. if [ "$force" = yes ]; then
  42. $ssh killall -wq -u $user offlineimap || true
  43. else
  44. x-terminal-emulator -e sh -c "echo \"Error: offlineimap process(es) already running. Use --force to kill before use.\"; echo \"Press enter to quit this terminal\"; read" &
  45. fi
  46. fi
  47. if [ -n "$ssh" ]; then
  48. # SSH expands shell command twice: protect semicolons by double-quoting
  49. x-terminal-emulator -e $ssh -t sh -c "\"while true; do offlineimap -u Curses.Blinkenlights; done; read\"" &
  50. else
  51. x-terminal-emulator -e sh -c "while true; do offlineimap -u Curses.Blinkenlights; done; read" &
  52. fi
  53. done
  54. exit 0
  55. unbreakshell() {
  56. echo "$@" | perl -pe 's/(\S*[\\\|";'\'']\S*)/'\''$1'\''/g'
  57. # echo "$@" | perl -pe 's/(\S+)/'\''$1'\''/g'
  58. # echo "$@" | perl -pe 's/'\''/'\'\\\''/g; s/(\S+)/'\''$1'\''/g'
  59. }
  60. execute() {
  61. TEMP=`getopt -s sh -o xtf --long fork,loop -n "$PRG" -- "$@"`
  62. eval set -- "$TEMP"
  63. fork=""
  64. loop=""
  65. x11=""
  66. term=""
  67. force=""
  68. while true ; do
  69. case "$1" in
  70. --fork) fork="1" ; shift ;;
  71. --loop) loop="1" ; shift ;;
  72. -x) x11="-x" ; shift ;;
  73. -t) term="-t" ; shift ;;
  74. -f) force="-f" ; shift ;;
  75. --) shift ; break ;;
  76. *) echo >&2 "Internal error in execute subroutine!" ; exit 1 ;;
  77. esac
  78. done
  79. user=$1; shift
  80. host=$1; shift
  81. shellbreaks=0
  82. if [ -n "$loop" ]; then
  83. # TEMP="sh -c \"while true; do $(unbreakshell "$@"); done\""
  84. # eval set -- "$TEMP"
  85. eval set -- "sh -c \"while true; do" "$@" "; done\""
  86. fi
  87. # if [ -n "$fork" ]; then
  88. # command="sh -c \"eval $@ \& \""
  89. # eval set -- `unbreakshell $command`
  90. # fi
  91. ssh=""
  92. if [ "$host" != localhost ] || [ "$user" != "$USER" ]; then
  93. ssh="1"
  94. fi
  95. sshopts="-e none"
  96. xterm=""
  97. if [ "$host" != localhost ]; then
  98. sshopts="$sshopts -C"
  99. fi
  100. if [ -n "$x11" ]; then
  101. if [ -n "$term" ]; then
  102. xterm="1"
  103. sshopts="$sshopts -T"
  104. else
  105. sshopts="$sshopts -X"
  106. fi
  107. fi
  108. # while [ "$shellbreaks" -gt 0 ]; do
  109. # eval set -- "`unbreakshell "$@"`"
  110. # shellbreaks=`expr $shellbreaks - 1`
  111. # done
  112. if [ -n "$ssh" ]; then
  113. TEMP="ssh $sshopts \"$user@$host\" -- $(unbreakshell "$@")"
  114. eval set -- "$TEMP"
  115. fi
  116. if [ -n "$x11" ] && [ -n "$term" ]; then
  117. TEMP="x-terminal-emulator -e sh -c \"$(unbreakshell "$@")\""
  118. eval set -- "$TEMP"
  119. fi
  120. if [ -n "$fork" ]; then
  121. $@ &
  122. else
  123. $@
  124. fi
  125. }
  126. findusers() {
  127. user=$1; shift
  128. host=$1; shift
  129. execute $force "$user" "$host" -- find /home -mindepth 2 -maxdepth 2 -type f -name .offlineimaprc -printf '%h\n' | sed 's,.*/,,'
  130. }
  131. dosync() {
  132. TEMP=`getopt -s sh -o xtf -n "$PRG" -- "$@"`
  133. eval set -- "$TEMP"
  134. x11=""
  135. term=""
  136. force=""
  137. while true ; do
  138. case "$1" in
  139. -x) x11="-x" ; shift ;;
  140. -t) term="-t" ; shift ;;
  141. -f) force="-f" ; shift ;;
  142. --) shift ; break ;;
  143. *) echo >&2 "Internal error in dosync subroutine!" ; exit 1 ;;
  144. esac
  145. done
  146. user=$1; shift
  147. host=$1; shift
  148. killfork=""
  149. if [ "$1" = KILL ]; then
  150. killfork="--fork"
  151. fi
  152. execute $x11 $term $force $killfork "$user" "$host" -- killall -u $user -wq -v -r "sh offlineimap" || [ $? -lt 2 ]
  153. if [ "$1" != KILL ]; then
  154. execute $x11 $term $force --loop --fork "$user" "$host" -- offlineimap "$@"
  155. # execute $x11 $term $force --loop "$user" "$host" -- offlineimap "$@"
  156. fi
  157. # execute "$user" "$host" pkill -u $user offlineimap || [ $? -lt 2 ]
  158. # execute "$user" "$host" pkill -u $user offlineimap || echo $?
  159. # execute "$user" "$host" sh -c \"pkill -u $user offlineimap \|\| echo \$?\"
  160. # execute "$user" "$host" sh -c \"pkill -u $user offlineimap\" || [ $? -lt 2 ]
  161. # while true; do execute "$user" "$host" offlineimap "$@"; done
  162. # while true; do execute "$user" "$host" sh -c \"pkill -u $user offlineimap \|\|offlineimap "$@"\"; done &
  163. }
  164. case "$user" in
  165. ALL)
  166. if [ -z "$x11" ]; then
  167. echo >&2 "Error: checking multiple accounts requires the --x11 option."
  168. exit 1
  169. fi
  170. realusers="`findusers "$USER" "$host"`"
  171. for realuser in $realusers; do
  172. dosync $x11 $term $force "$realuser" "$host" -- "$@"
  173. done
  174. ;;
  175. *)
  176. dosync $x11 $term $force "$user" "$host" -- "$@"
  177. ;;
  178. esac
  179. exit 0