imapsync_csv_wrapper 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. #!/bin/sh
  2. # $Id: imapsync_csv_wrapper,v 1.13 2021/11/01 13:24:47 gilles Exp gilles $
  3. exec 3>&1
  4. debug=true
  5. # comment the following to have debug data
  6. debug=false
  7. PATH=$PATH:/home/www/apache24/cgi-bin/
  8. echo3() {
  9. echo "$@" >&3
  10. }
  11. echodebug()
  12. {
  13. $debug && echo3 "$@"
  14. }
  15. run_test() {
  16. tests_count=`expr 1 + $tests_count`
  17. # do not run anything between the two following instructions
  18. "$@"; run_test_status=$?
  19. # now you can run something since $? is saved
  20. if test x"$run_test_status" = x"0"; then
  21. echo "ok $tests_count $@"
  22. else
  23. echo "not ok $tests_count $@"
  24. tests_failed_count=`expr 1 + $tests_failed_count`
  25. tests_failed_list="$tests_failed_list $tests_count"
  26. fi
  27. return $run_test_status
  28. }
  29. run_tests() {
  30. tests_count=0
  31. tests_failed_count=0
  32. tests_failed_list=
  33. for t in "$@"; do
  34. echo "### running $t"
  35. "$t"
  36. done
  37. echo
  38. echo "#### ALL tests done"
  39. if test 0 -eq $tests_failed_count; then
  40. echo "ALL $tests_count TESTS SUCCESSFUL"
  41. return 0
  42. else
  43. # At least one failed
  44. echo "FAILED $tests_failed_count/$tests_count TESTS: $tests_failed_list"
  45. return 1
  46. fi
  47. }
  48. uri_unescape()
  49. {
  50. #echo "$@" | perl -MURI::Escape -e 'print uri_unescape(<>)'
  51. echo "$1" | perl -pe 's/\%(\w\w)/chr hex $1/ge'
  52. }
  53. tests_uri_unescape()
  54. {
  55. run_test test '' = ''
  56. run_test test "" = ""
  57. run_test test '1' = '1'
  58. run_test test 'A B' = 'A B'
  59. run_test test "'A B' = 'A B'"
  60. run_test test '' = "`uri_unescape`"
  61. run_test test "" = "`uri_unescape`"
  62. run_test test 'A' = `uri_unescape A`
  63. run_test test 'The cat' = "`uri_unescape The%20cat`"
  64. run_test test "\r" = "\r"
  65. run_test test '"\r" = "`uri_unescape %0D`"'
  66. run_test test "\n" = "\n"
  67. nl=`uri_unescape %0A`
  68. nl="\n"
  69. run_test test "_\n_" = "_${nl}_"
  70. run_test test '!"#$%&'\''()' = "`uri_unescape %21%22%23%24%25%26%27%28%29`"
  71. run_test test '*+,/:;=?@[]' = "`uri_unescape %2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D`"
  72. run_test test '"_a\n\n\nz_" = "_`uri_unescape a%0A%0A%0Az`_"'
  73. run_test test '"_`uri_unescape a%0A%0A%0Az`_" = "_`uri_unescape a%0A%0A%0Az`_"'
  74. run_test test 'A B' = 'A B'
  75. uri_unescape 'a%0Az' > /tmp/$$_zzz1.txt
  76. /bin/echo "a
  77. z" > /tmp/$$_zzz2.txt
  78. run_test diff /tmp/$$_zzz1.txt /tmp/$$_zzz2.txt
  79. rm /tmp/$$_zzz1.txt /tmp/$$_zzz2.txt
  80. z3=`uri_unescape a%0A%0A%0Az`
  81. z4="a\n\n\nz"
  82. run_test test '"$z3" = "$z4"'
  83. run_test test '"\n" = "`uri_unescape %0A`"'
  84. #run_test test '\n' = "`uri_unescape %0A`"
  85. run_test test '"a\r\nz" = "a\r\nz"'
  86. run_test test '"_a\r\nz_" = "`uri_unescape _a%0D%0Az_`"'
  87. run_test test 'A' = `uri_unescape A`
  88. }
  89. value_of_key()
  90. {
  91. key="$1"
  92. test 1 -le "$#" && shift
  93. keyval="$1"
  94. #echo "key:$key"
  95. #echo "keyval:$keyval"
  96. #set -x
  97. echo "$keyval" | perl -ne "s/^.*$key"'=([^&]+).*$/$1/ and print'
  98. #set +x
  99. }
  100. tests_value_of_key()
  101. {
  102. run_test test "" = "`value_of_key`"
  103. run_test test "" = "`value_of_key badkey`"
  104. run_test test "" = "`value_of_key badkey mykey=myvalue`"
  105. run_test test "myvalue" = "`value_of_key mykey mykey=myvalue`"
  106. run_test test "myvalue" = `value_of_key mykey "mykey=myvalue"`
  107. run_test test "myvalue" = `value_of_key mykey "mykey=myvalue&other=rrr"`
  108. run_test test "myvalue" = `value_of_key mykey "otra=zzz&mykey=myvalue"`
  109. run_test test "myvalue" = `value_of_key mykey "otra=zzz&mykey=myvalue&other=rrr"`
  110. }
  111. remove_blanks_and_comments()
  112. {
  113. /bin/echo -n ''
  114. echo "$1" | perl -ne '!/^ *#.*$|^[\r ]*$/ && print'
  115. }
  116. tests_remove_blanks_and_comments()
  117. {
  118. #set -x
  119. run_test test "" = "`remove_blanks_and_comments`"
  120. run_test test "a" = "`remove_blanks_and_comments a`"
  121. myval="
  122. "
  123. run_test test "" = "`remove_blanks_and_comments $myval`"
  124. myval="a
  125. "
  126. run_test test "a" = "`remove_blanks_and_comments $myval`"
  127. myval="
  128. a
  129. "
  130. run_test test "a" = "`remove_blanks_and_comments $myval`"
  131. myval="
  132. a
  133. "
  134. run_test test "a" = "`remove_blanks_and_comments $myval`"
  135. myval="a
  136. b"
  137. run_test test '"$myval" = `remove_blanks_and_comments $myval`'
  138. myval="# caca
  139. a
  140. "
  141. run_test test '"a" = "`remove_blanks_and_comments $myval`"'
  142. myval="# caca
  143. a
  144. b
  145. # ooo
  146. c
  147. "
  148. myval_filtered="a
  149. b
  150. c
  151. "
  152. run_test test '"$myval_filtered" = "`remove_blanks_and_comments $myval`"'
  153. run_test remove_blanks_and_comments
  154. run_test remove_blanks_and_comments ' '
  155. run_test remove_blanks_and_comments ' # blabla '
  156. run_test test "" = "`remove_blanks_and_comments ' ' `"
  157. run_test test "" = "`remove_blanks_and_comments ' # blabla ' `"
  158. }
  159. is_blank_or_comment()
  160. {
  161. test "" = "`remove_blanks_and_comments $1 `"
  162. }
  163. tests_is_blank_or_comment()
  164. {
  165. run_test is_blank_or_comment
  166. run_test is_blank_or_comment ''
  167. run_test is_blank_or_comment ' '
  168. run_test is_blank_or_comment '# blabla 1 '
  169. run_test is_blank_or_comment ' # blabla 2 '
  170. is_blank_or_comment 'blabla 1 ' ; run_test test 1 = $?
  171. is_blank_or_comment ' blabla 2 ' ; run_test test 1 = $?
  172. is_blank_or_comment ' blabla 3 # blibli ' ; run_test test 1 = $?
  173. }
  174. logfailures()
  175. {
  176. run_on_each_line_failures="$run_on_each_line_failures
  177. $@"
  178. }
  179. tests_logfailures()
  180. {
  181. #reset
  182. run_on_each_line_failures=
  183. # add Blabla
  184. run_test logfailures Blabla
  185. run_test test "
  186. Blabla" = "$run_on_each_line_failures"
  187. # add Kiki
  188. run_test logfailures Kiki
  189. run_test test "
  190. Blabla
  191. Kiki" = "$run_on_each_line_failures"
  192. #reset again
  193. run_on_each_line_failures=
  194. }
  195. run_on_each_line()
  196. {
  197. echodebug '### Entering run_on_each_line ###'
  198. test -f abort_$$.txt && { echo abort demanded so not doing normal stuff ; return ; }
  199. /bin/echo -n ''
  200. test -z "$1" && { echo3 'First parameter is empty. Leaving run_on_each_line().' ; return ; }
  201. csv_data_multilines="$1"
  202. echodebug "csv_data_multilines=[$csv_data_multilines]"
  203. test 1 -le "$#" && shift
  204. command=${1:-echo}
  205. echodebug command="$command" "$@"
  206. test 1 -le "$#" && shift
  207. line_number=0
  208. csv_number=0
  209. failures_number=0
  210. while IFS=';' read h1 u1 p1 h2 u2 p2 extra fake; do
  211. test -f abort_$$.txt && { echo abort demanded so not doing normal stuff ; return ; }
  212. line_number=`expr 1 + $line_number`
  213. if is_blank_or_comment "$h1$u1$p1$h2$u2$p2$extra" ; then
  214. echo3 "Ignoring line " $line_number "$h1$u1$p1$h2$u2$p2$extra"
  215. else
  216. csv_number=`expr 1 + $csv_number`
  217. echo3 "Processing line " $line_number csv run $csv_number "[$h1] [$u1] [xxx] [$h2] [$u2] [xxx] [$extra] $$"
  218. $command "$@" --host1 "$h1" --user1 "$u1" --password1 "$p1" --host2 "$h2" --user2 "$u2" --password2 "$p2" $extra
  219. command_status=$?
  220. test "0" != "$command_status" && failures_number=`expr 1 + $failures_number`
  221. echodebug command_status=$command_status failures_number=$failures_number
  222. test "0" != "$command_status" && logfailures "line $line_number csv run $csv_number returned with status $command_status ($h1;$u1;$p1;$h2;$u2;$p2;$extra;)"
  223. fi
  224. done << EOF
  225. $csv_data_multilines
  226. EOF
  227. echodebug Leaving run_on_each_line, failures_number=$failures_number
  228. test "0" != "$failures_number" && echo "$run_on_each_line_failures"
  229. test "0" != "$failures_number" && return 1
  230. return 0
  231. }
  232. tests_run_on_each_line()
  233. {
  234. # 1-4
  235. run_test test '' = "`run_on_each_line`"
  236. run_test test '' = "`run_on_each_line ''`"
  237. run_test test '' = "`run_on_each_line '' echo imapsync`"
  238. run_test test '' = "`run_on_each_line '' echo imapsync blabla blibli`"
  239. # 5-7 blank data
  240. run_test test '' = "`run_on_each_line ' '`"
  241. run_test test '' = "`run_on_each_line ' ' `"
  242. run_test test '' = "`run_on_each_line ' ' echo imapsync blabla blibli`"
  243. # 8-10 comment data
  244. run_test test '' = "`run_on_each_line '# '`"
  245. run_test test '' = "`run_on_each_line ' # '`"
  246. run_test test '' = "`run_on_each_line '# ' echo imapsync blabla blibli`"
  247. # 11-13 empty line
  248. myval='
  249. '
  250. myret=`run_on_each_line "$myval"`
  251. run_test test "" = "$myret"
  252. myret=`run_on_each_line "$myval"`
  253. run_test test "" = "$myret"
  254. myret=`run_on_each_line "$myval" echo imapsync blabla blibli`
  255. run_test test "" = "$myret"
  256. # 14-16 blanks or comments
  257. myval='
  258. # blabla 1
  259. # blabla 2
  260. '
  261. myret=`run_on_each_line "$myval"`
  262. run_test test "" = "$myret"
  263. myret=`run_on_each_line "$myval" `
  264. run_test test "" = "$myret"
  265. myret=`run_on_each_line "$myval" echo imapsync blabla blibli`
  266. run_test test "" = "$myret"
  267. # 17-18
  268. # csv data no blank no comment
  269. lines='a1;b1;c1;d1;e1;f1
  270. a2;b2;c2;d2;e2;f2
  271. a3;b3;c3;d3;e3;f3'
  272. # no command (so echo)
  273. lines_out='--host1 a1 --user1 b1 --password1 c1 --host2 d1 --user2 e1 --password2 f1
  274. --host1 a2 --user1 b2 --password1 c2 --host2 d2 --user2 e2 --password2 f2
  275. --host1 a3 --user1 b3 --password1 c3 --host2 d3 --user2 e3 --password2 f3'
  276. run_test test "$lines_out" = "`run_on_each_line "$lines"`"
  277. # command = echo imapsync
  278. lines_out='imapsync --host1 a1 --user1 b1 --password1 c1 --host2 d1 --user2 e1 --password2 f1
  279. imapsync --host1 a2 --user1 b2 --password1 c2 --host2 d2 --user2 e2 --password2 f2
  280. imapsync --host1 a3 --user1 b3 --password1 c3 --host2 d3 --user2 e3 --password2 f3'
  281. run_test test "$lines_out" = "`run_on_each_line "$lines" echo imapsync`"
  282. # 19-20
  283. # csv data + blanks + comments
  284. lines='a1;b1;c1;d1;e1;f1
  285. # blabla 2
  286. a2;b2;c2;d2;e2;f2
  287. # bloblo 3
  288. a3;b3;c3;d3;e3;f3
  289. # end'
  290. # no command (so echo)
  291. lines_out='--host1 a1 --user1 b1 --password1 c1 --host2 d1 --user2 e1 --password2 f1
  292. --host1 a2 --user1 b2 --password1 c2 --host2 d2 --user2 e2 --password2 f2
  293. --host1 a3 --user1 b3 --password1 c3 --host2 d3 --user2 e3 --password2 f3'
  294. run_test test "$lines_out" = "`run_on_each_line "$lines"`"
  295. # command = echo imapsync
  296. lines_out='imapsync --host1 a1 --user1 b1 --password1 c1 --host2 d1 --user2 e1 --password2 f1
  297. imapsync --host1 a2 --user1 b2 --password1 c2 --host2 d2 --user2 e2 --password2 f2
  298. imapsync --host1 a3 --user1 b3 --password1 c3 --host2 d3 --user2 e3 --password2 f3'
  299. run_test test "$lines_out" = "`run_on_each_line "$lines" echo imapsync`"
  300. # 21-22
  301. # csv data 3/3;/6/6;/7/7;/8/8; parameters + blanks + comments
  302. # runs 1 2 3 4 5 6 7 8
  303. lines='a1;b1;c1
  304. # blabla 2
  305. a2;b2;c2;
  306. # bloblo 3
  307. a3;b3;c3;d3;e3;f3
  308. a4;b4;c4;d4;e4;f4;
  309. a5;b5;c5;d5;e5;f5;--extra blurp
  310. a6;b6;c6;d6;e6;f6;--extra blurp;
  311. a7;b7;c7;d7;e7;f7;--extra blurp; ignored
  312. a8;b8;c8;d8;e8;f8;--extra blurp; ignored;
  313. # end'
  314. # no command (so echo)
  315. lines_out='--host1 a1 --user1 b1 --password1 c1 --host2 --user2 --password2
  316. --host1 a2 --user1 b2 --password1 c2 --host2 --user2 --password2
  317. --host1 a3 --user1 b3 --password1 c3 --host2 d3 --user2 e3 --password2 f3
  318. --host1 a4 --user1 b4 --password1 c4 --host2 d4 --user2 e4 --password2 f4
  319. --host1 a5 --user1 b5 --password1 c5 --host2 d5 --user2 e5 --password2 f5 --extra blurp
  320. --host1 a6 --user1 b6 --password1 c6 --host2 d6 --user2 e6 --password2 f6 --extra blurp
  321. --host1 a7 --user1 b7 --password1 c7 --host2 d7 --user2 e7 --password2 f7 --extra blurp
  322. --host1 a8 --user1 b8 --password1 c8 --host2 d8 --user2 e8 --password2 f8 --extra blurp'
  323. run_test test "$lines_out" = "`run_on_each_line "$lines"`"
  324. # command = echo imapsync
  325. lines_out='imapsync --host1 a1 --user1 b1 --password1 c1 --host2 --user2 --password2
  326. imapsync --host1 a2 --user1 b2 --password1 c2 --host2 --user2 --password2
  327. imapsync --host1 a3 --user1 b3 --password1 c3 --host2 d3 --user2 e3 --password2 f3
  328. imapsync --host1 a4 --user1 b4 --password1 c4 --host2 d4 --user2 e4 --password2 f4
  329. imapsync --host1 a5 --user1 b5 --password1 c5 --host2 d5 --user2 e5 --password2 f5 --extra blurp
  330. imapsync --host1 a6 --user1 b6 --password1 c6 --host2 d6 --user2 e6 --password2 f6 --extra blurp
  331. imapsync --host1 a7 --user1 b7 --password1 c7 --host2 d7 --user2 e7 --password2 f7 --extra blurp
  332. imapsync --host1 a8 --user1 b8 --password1 c8 --host2 d8 --user2 e8 --password2 f8 --extra blurp'
  333. run_test test "$lines_out" = "`run_on_each_line "$lines" echo imapsync`"
  334. # 23-2x
  335. # "" input
  336. lines='a1;b1;c1;d1;e1;f1;--caca "KK"'
  337. # no command (so echo)
  338. lines_out='--host1 a1 --user1 b1 --password1 c1 --host2 d1 --user2 e1 --password2 f1 --caca "KK"'
  339. run_test test "$lines_out" = "`run_on_each_line "$lines"`"
  340. # command = echo imapsync
  341. lines_out='imapsync --host1 a1 --user1 b1 --password1 c1 --host2 d1 --user2 e1 --password2 f1 --caca "KK"'
  342. run_test test "$lines_out" = "`run_on_each_line "$lines" echo imapsync`"
  343. }
  344. remove_double_quotes()
  345. {
  346. echo "$1" | tr -d '"'
  347. }
  348. tests_remove_double_quotes()
  349. {
  350. echo 'Entering tests_remove_double_quotes()'
  351. run_test test -z `remove_double_quotes`
  352. run_test test 'abc' = `remove_double_quotes 'abc'`
  353. run_test test -z `remove_double_quotes '"'`
  354. run_test test -z `remove_double_quotes '""'`
  355. run_test test 'abc' = `remove_double_quotes '"ab"c'`
  356. echo 'Leaving tests_remove_double_quotes()'
  357. }
  358. hashsync()
  359. {
  360. #set -x
  361. mystring=`remove_double_quotes ${1:-''}`
  362. mykey=${2:-''}
  363. # impressive! is not it? quoting shit!
  364. perl -MDigest::HMAC_SHA1 -e 'print Digest::HMAC_SHA1::hmac_sha1_hex( "'"$mystring"'", "'"$mykey"'" )'
  365. #set +x
  366. }
  367. tests_hashsync()
  368. {
  369. echo 'Entering tests_hashsync()'
  370. run_test test 'fbdb1d1b18aa6c08324b7d64b71fb76370690e1d' = `hashsync`
  371. run_test test 'fbdb1d1b18aa6c08324b7d64b71fb76370690e1d' = `hashsync ''`
  372. run_test test 'fbdb1d1b18aa6c08324b7d64b71fb76370690e1d' = `hashsync '' ''`
  373. run_test test 'e86a28a3611c1e7bbaf8057cd00ae122781a11fe' = `hashsync 'zzz' ''`
  374. run_test test '6a7b451ac99eab1531ad8e6cd544b32420c552ac' = `hashsync 'zzz' 'A'`
  375. run_test test 'fbdb1d1b18aa6c08324b7d64b71fb76370690e1d' = `hashsync '""'`
  376. run_test test 'e86a28a3611c1e7bbaf8057cd00ae122781a11fe' = `hashsync '"zzz"'`
  377. echo 'Leaving tests_hashsync()'
  378. }
  379. tests_caca()
  380. {
  381. run_test test 'z' = ''
  382. run_test test '1' = '1'
  383. }
  384. change_working_directory_if_under_cgi()
  385. {
  386. test -n "$SERVER_SOFTWARE" &&
  387. {
  388. pwd
  389. #env
  390. ! test -d "$1" && {
  391. echo creating directory "$1"
  392. mkdir -p "$1"
  393. }
  394. echo changing current directory to "$1"
  395. cd "$1"
  396. pwd
  397. }
  398. }
  399. write_my_pid()
  400. {
  401. echo Writing my pid $$ in imapsync_csv_wrapper.pid
  402. echo $$ > imapsync_csv_wrapper.pid
  403. }
  404. kill_pid_in_files()
  405. {
  406. echo Will kill -TERM pid found in "$@"
  407. for pidfile in "$@"; do
  408. # FIXME add test -f $pidfile at least
  409. if test -f $pidfile; then
  410. pidlist="$pidlist `head -1 $pidfile`"
  411. test -f "$pidfile" && echo Doing kill -TERM `head -1 "$pidfile"` "$pidfile"
  412. test -f "$pidfile" && kill -TERM `head -1 "$pidfile"`
  413. else
  414. echo "No $pidfile here"
  415. fi
  416. done
  417. }
  418. abort_other_process()
  419. {
  420. echodebug 'Entering abort_other_process()'
  421. $debug && ls -ltr
  422. # No active imapsync_csv_wrapper => nothing to abort
  423. if ! test -f imapsync_csv_wrapper.pid ; then
  424. echo No imapsync_csv_wrapper.pid here. Nothing to abort.
  425. echodebug 'Leaving abort_other_process()'
  426. return 0
  427. else
  428. # a message to the active imapsync_csv_wrapper
  429. main_pid_to_abort=`head -1 imapsync_csv_wrapper.pid`
  430. touch abort_${main_pid_to_abort}.txt
  431. echodebug 'sleep 3' && sleep 3
  432. kill_pid_in_files imapsync.pid
  433. echodebug 'sleep 3 again then nother try to kill' && sleep 3
  434. kill_pid_in_files imapsync.pid
  435. ls -ltr
  436. echodebug 'Leaving abort_other_process()'
  437. return 0
  438. fi
  439. }
  440. quit()
  441. {
  442. echo "Leaving. I am $0 pid $$"
  443. for pidfile in "$@"; do
  444. test -f "$pidfile" && echo rm "$pidfile" && rm "$pidfile"
  445. done
  446. }
  447. tests()
  448. {
  449. :
  450. # All tests
  451. run_tests \
  452. tests_uri_unescape \
  453. tests_value_of_key \
  454. tests_remove_blanks_and_comments \
  455. tests_run_on_each_line \
  456. tests_hashsync \
  457. tests_is_blank_or_comment \
  458. tests_logfailures \
  459. tests_remove_double_quotes \
  460. }
  461. header()
  462. {
  463. # HTTP header, may be empty
  464. echo
  465. }
  466. getcsvdata()
  467. {
  468. read imapsync_csv_wrapper_from_ui
  469. echodebug '######################################################################'
  470. echodebug Here are the UI parameters, raw:
  471. echodebug "var:"
  472. echodebug "$imapsync_csv_wrapper_from_ui"
  473. echodebug;echodebug
  474. csv_data=`value_of_key csv_data "$imapsync_csv_wrapper_from_ui"`
  475. echodebug '######################################################################'
  476. echodebug Here csv_data:
  477. echodebug "$csv_data"
  478. csv_data_unescaped=`uri_unescape "$csv_data"`
  479. echodebug '######################################################################'
  480. echodebug Here csv_data, unescaped:
  481. echodebug "$csv_data_unescaped"
  482. csv_data_pure=`remove_blanks_and_comments "$csv_data_unescaped"`
  483. echodebug '######################################################################'
  484. echodebug Here csv_data no blanks nor comments:
  485. echodebug "$csv_data_pure"
  486. }
  487. tests_all_verbose_if_failure()
  488. {
  489. # Run tests silent but if failure then rerun them verbose.
  490. # return 0 if all tests passed
  491. # return 1 if some tests failed
  492. if ! tests > /dev/null 3>&1 ; then
  493. tests
  494. return 1
  495. fi
  496. return 0
  497. }
  498. abort_other_if_asked_to()
  499. {
  500. echodebug Looking for abort
  501. abort=`value_of_key abort "$imapsync_csv_wrapper_from_ui"`
  502. echodebug abort=$abort
  503. if test 'on' = "$abort" ; then
  504. abort_other_process
  505. echo "Abort done. Leaving"
  506. return 0
  507. else
  508. echodebug 'Not asked to abort other process'
  509. echodebug 'Leaving abort_other_if_asked_to()'
  510. return 1
  511. fi
  512. }
  513. is_another_running()
  514. {
  515. # I should check that the pid is actually a running one
  516. # but that's ok for now
  517. if test -f imapsync_csv_wrapper.pid; then
  518. echo Looks like another imapsync_csv_wrapper is running. Leaving
  519. return 0
  520. else
  521. echodebug Looks like I am alone. Good. Let us go on.
  522. return 1
  523. fi
  524. }
  525. signal_handling()
  526. {
  527. # 2=INT 3=QUIT 15=TERM
  528. trap "quit imapsync_csv_wrapper.pid abort_$$.txt" 15 2 3 0
  529. $debug && trap
  530. return 0
  531. }
  532. banner_round()
  533. {
  534. banner_line='
  535. ################################################################################'
  536. banner_title=${1:-$banner_line}
  537. echo3 "$banner_line$banner_title$banner_line"
  538. }
  539. justlogin()
  540. {
  541. banner_round '
  542. ######################## just login check round ################################'
  543. date
  544. test -f abort_$$.txt && { echo Abort demanded so not doing normal stuff ; return ; }
  545. echo Now run imapsync --justlogin to check all credentials are ok
  546. if run_on_each_line "$csv_data_unescaped" \
  547. imapsync --no-modulesversion --justlogin --tmpdir .
  548. then
  549. echo success of the just login check round
  550. return 0
  551. else
  552. echo failure of the just login check round
  553. return 1
  554. fi
  555. }
  556. justfoldersizes()
  557. {
  558. banner_round '
  559. ######################## just folders sizes counting round #####################'
  560. date
  561. test -f abort_$$.txt && { echo Abort demanded so not doing normal stuff ; return ; }
  562. echo Now run imapsync --justfoldersizes to show the volume to be synced
  563. run_on_each_line "$csv_data_unescaped" \
  564. imapsync --no-modulesversion --justfoldersizes --tmpdir . \
  565. || return 1
  566. }
  567. sync_all()
  568. {
  569. banner_round '
  570. ######################## now sync them all round ###############################'
  571. date
  572. test -f abort_$$.txt && { echo Abort demanded so not doing normal stuff ; return ; }
  573. echo Now run imapsync on the data given
  574. run_on_each_line "$csv_data_unescaped" \
  575. imapsync --no-modulesversion --tmpdir . \
  576. || return 1
  577. }
  578. tests_initialisation()
  579. {
  580. #### Variables initialisation
  581. tests_count=0
  582. tests_failed_count=0
  583. tests_failed_list=
  584. }
  585. main()
  586. {
  587. header
  588. # All stderr to stdin, yeah I am like that
  589. exec 2>&1
  590. # Some tests, uncomment which one you work with and also the return one
  591. #run_tests tests_is_blank_or_comment
  592. #run_tests tests_run_on_each_line
  593. #run_tests tests_remove_blanks_and_comments
  594. #tests_hashsync
  595. #run_tests tests_logfailures
  596. #tests_remove_double_quotes
  597. #return
  598. tests_all_verbose_if_failure || return 1
  599. echodebug my pid is $$
  600. #return
  601. getcsvdata
  602. hashsync=`hashsync "$csv_data_pure"`
  603. test -z $hashsync && return 1
  604. change_working_directory_if_under_cgi /var/tmp/imapsync_cgi/$hashsync/
  605. abort_other_if_asked_to && return 0
  606. is_another_running && return 0
  607. write_my_pid
  608. signal_handling
  609. SERVER_SOFTWARE_BAK="$SERVER_SOFTWARE"
  610. unset SERVER_SOFTWARE
  611. # Now the real stuff
  612. #justlogin || return 1
  613. #justfoldersizes
  614. sync_all || return 1
  615. $debug && ls -ltr
  616. quit imapsync_csv_wrapper.pid abort_$$.txt
  617. echo $0 ended at the end
  618. }
  619. tests_initialisation
  620. main
  621. return 0