30-async-imap.dpatch 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #!/bin/sh -e
  2. ## 30-aysnc-imap.dpatch by Theodore Y. Ts'o <tytso@mit.edu>
  3. ##
  4. ## DP: Add the beginnings of asynchronous IMAP support. So far, we only
  5. ## DP: support asynchronous flag setting, since that's the easist.
  6. ## DP: Eventually we need to support asynchronous message fetches and
  7. ## DP: uploads.
  8. if [ $# -ne 1 ]; then
  9. echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
  10. exit 1
  11. fi
  12. [ -f debian/patches/00patch-opts ] && . debian/patches/00patch-opts
  13. patch_opts="${patch_opts:--f --no-backup-if-mismatch}"
  14. case "$1" in
  15. -patch) patch $patch_opts -p1 < $0;;
  16. -unpatch) patch $patch_opts -p1 -R < $0;;
  17. *)
  18. echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
  19. exit 1;;
  20. esac
  21. exit 0
  22. @DPATCH@
  23. diff -urNad /usr/projects/isync/SF-cvs/isync/src/imap.c isync/src/imap.c
  24. --- /usr/projects/isync/SF-cvs/isync/src/imap.c 2004-01-14 18:31:49.000000000 -0500
  25. +++ isync/src/imap.c 2004-01-14 18:39:18.000000000 -0500
  26. @@ -3,6 +3,7 @@
  27. * isync - IMAP4 to maildir mailbox synchronizer
  28. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  29. * Copyright (C) 2002-2003 Oswald Buddenhagen <ossi@users.sf.net>
  30. + * Copyright (C) 2004 Theodore Ts'o <tytso@alum.mit.edu>
  31. *
  32. * This program is free software; you can redistribute it and/or modify
  33. * it under the terms of the GNU General Public License as published by
  34. @@ -35,13 +36,33 @@
  35. #include <string.h>
  36. #include <ctype.h>
  37. #include <sys/socket.h>
  38. +#include <sys/ioctl.h>
  39. #include <netinet/in.h>
  40. +#include <netinet/tcp.h>
  41. #include <arpa/inet.h>
  42. #include <netdb.h>
  43. #if HAVE_LIBSSL
  44. # include <openssl/err.h>
  45. #endif
  46. +struct imap_cmd {
  47. + unsigned int tag;
  48. + char *cmd;
  49. + int flags;
  50. + int response;
  51. + struct imap_cmd *next;
  52. + int (*complete_fn) (imap_t *imap, struct imap_cmd * cmd);
  53. +};
  54. +
  55. +#define IMAP_FLAG_DONE 0x0001
  56. +
  57. +static struct imap_cmd *in_progress = NULL;
  58. +static int num_in_progress = 0;
  59. +int max_in_progress_high = 50;
  60. +int max_in_progress_low = 10;
  61. +
  62. +static struct imap_cmd *get_cmd_result(imap_t *imap);
  63. +
  64. const char *Flags[] = {
  65. "\\Seen",
  66. "\\Answered",
  67. @@ -199,6 +220,22 @@
  68. return write (sock->fd, buf, len);
  69. }
  70. +static int
  71. +socket_pending(Socket_t *sock)
  72. +{
  73. + int num = -1;
  74. +
  75. + if (ioctl(sock->fd, FIONREAD, &num) < 0)
  76. + return -1;
  77. + if (num > 0)
  78. + return num;
  79. +#if HAVE_LIBSSL
  80. + if (sock->use_ssl)
  81. + return SSL_pending (sock->ssl);
  82. +#endif
  83. + return 0;
  84. +}
  85. +
  86. static void
  87. socket_perror (const char *func, Socket_t *sock, int ret)
  88. {
  89. @@ -301,16 +338,20 @@
  90. }
  91. static int
  92. -parse_fetch (imap_t * imap, list_t * list)
  93. +parse_fetch (imap_t * imap, char *cmd)
  94. {
  95. - list_t *tmp;
  96. + list_t *tmp, *list;
  97. unsigned int uid = 0;
  98. unsigned int mask = 0;
  99. unsigned int size = 0;
  100. message_t *cur;
  101. - if (!is_list (list))
  102. + list = parse_list (cmd, 0);
  103. +
  104. + if (!is_list (list)) {
  105. + free_list(list);
  106. return -1;
  107. + }
  108. for (tmp = list->child; tmp; tmp = tmp->next)
  109. {
  110. @@ -325,6 +366,7 @@
  111. if (uid < imap->minuid)
  112. {
  113. /* already saw this message */
  114. + free_list(list);
  115. return 0;
  116. }
  117. else if (uid > imap->maxuid)
  118. @@ -387,6 +429,7 @@
  119. cur->flags = mask;
  120. cur->size = size;
  121. + free_list(list);
  122. return 0;
  123. }
  124. @@ -415,39 +458,114 @@
  125. }
  126. }
  127. -static int
  128. -imap_exec (imap_t * imap, const char *fmt, ...)
  129. +static struct imap_cmd *issue_imap_cmd(imap_t *imap,
  130. + const char *fmt, ...)
  131. {
  132. va_list ap;
  133. - char tmp[256];
  134. - char buf[256];
  135. - char *cmd;
  136. - char *arg;
  137. - char *arg1;
  138. - config_t *box;
  139. + char tmp[1024];
  140. + char buf[1024];
  141. + struct imap_cmd *cmd;
  142. int n;
  143. + cmd = malloc(sizeof(struct imap_cmd));
  144. + if (!cmd)
  145. + return NULL;
  146. +
  147. + cmd->tag = ++Tag;
  148. + cmd->flags = 0;
  149. + cmd->response = 0;
  150. + cmd->complete_fn = 0;
  151. +
  152. va_start (ap, fmt);
  153. vsnprintf (tmp, sizeof (tmp), fmt, ap);
  154. va_end (ap);
  155. - snprintf (buf, sizeof (buf), "%d %s\r\n", ++Tag, tmp);
  156. + cmd->cmd = malloc(strlen(tmp)+1);
  157. + if (cmd->cmd)
  158. + strcpy(cmd->cmd, tmp);
  159. +
  160. + snprintf (buf, sizeof (buf), "%d %s\r\n", cmd->tag, tmp);
  161. if (Verbose) {
  162. - printf (">>> %s", buf);
  163. + if (num_in_progress)
  164. + printf("(%d in progress) ", num_in_progress);
  165. + if (strncmp(tmp, "LOGIN", 5))
  166. + printf (">>> %s", buf);
  167. + else
  168. + printf (">>> LOGIN USERNAME PASSWORD\n");
  169. fflush (stdout);
  170. }
  171. n = socket_write (imap->sock, buf, strlen (buf));
  172. if (n <= 0)
  173. {
  174. socket_perror ("write", imap->sock, n);
  175. - return -1;
  176. + free(cmd);
  177. + return NULL;
  178. + }
  179. + cmd->next = in_progress;
  180. + in_progress = cmd;
  181. + num_in_progress++;
  182. + if ((num_in_progress > max_in_progress_high) ||
  183. + socket_pending(imap->sock)) {
  184. + while ((num_in_progress > max_in_progress_low) ||
  185. + socket_pending(imap->sock)) {
  186. + if (Verbose && socket_pending(imap->sock))
  187. + printf("(Socket input pending)\n");
  188. + get_cmd_result(imap);
  189. + }
  190. }
  191. + return cmd;
  192. +}
  193. +
  194. +static struct imap_cmd *find_imap_cmd(unsigned int tag)
  195. +{
  196. + struct imap_cmd *cmd, *prev;
  197. +
  198. + for (prev=NULL, cmd=in_progress; cmd; cmd = cmd->next) {
  199. + if (tag == cmd->tag) {
  200. + return cmd;
  201. + }
  202. + prev = cmd;
  203. + }
  204. + return NULL;
  205. +}
  206. +
  207. +static void dequeue_imap_cmd(unsigned int tag)
  208. +{
  209. + struct imap_cmd *cmd, *prev;
  210. +
  211. + for (prev=NULL, cmd=in_progress; cmd; cmd = cmd->next) {
  212. + if (tag != cmd->tag) {
  213. + prev = cmd;
  214. + continue;
  215. + }
  216. + if (prev)
  217. + prev->next = cmd->next;
  218. + else
  219. + in_progress = cmd->next;
  220. + cmd->next = 0;
  221. + if (cmd->cmd)
  222. + free(cmd->cmd);
  223. + cmd->cmd = 0;
  224. + free(cmd);
  225. + break;
  226. + }
  227. +}
  228. +
  229. +static struct imap_cmd *get_cmd_result(imap_t *imap)
  230. +{
  231. + char *cmd;
  232. + char *arg;
  233. + char *arg1;
  234. + config_t *box;
  235. + int n;
  236. + unsigned int tag;
  237. + struct imap_cmd *cmdp;
  238. for (;;)
  239. {
  240. next:
  241. if (buffer_gets (imap->buf, &cmd))
  242. - return -1;
  243. + return NULL;
  244. arg = next_arg (&cmd);
  245. if (*arg == '*')
  246. @@ -456,7 +574,7 @@
  247. if (!arg)
  248. {
  249. fprintf (stderr, "IMAP error: unable to parse untagged response\n");
  250. - return -1;
  251. + return NULL;
  252. }
  253. if (!strcmp ("NAMESPACE", arg))
  254. @@ -528,23 +646,14 @@
  255. imap->recent = atoi (arg);
  256. else if (!strcmp ("FETCH", arg1))
  257. {
  258. - list_t *list;
  259. -
  260. - list = parse_list (cmd, 0);
  261. -
  262. - if (parse_fetch (imap, list))
  263. - {
  264. - free_list (list);
  265. - return -1;
  266. - }
  267. -
  268. - free_list (list);
  269. + if (parse_fetch (imap, cmd))
  270. + return NULL;
  271. }
  272. }
  273. else
  274. {
  275. fprintf (stderr, "IMAP error: unable to parse untagged response\n");
  276. - return -1;
  277. + return NULL;
  278. }
  279. }
  280. #if HAVE_LIBSSL
  281. @@ -555,7 +664,7 @@
  282. if (!imap->cram)
  283. {
  284. fprintf (stderr, "IMAP error, not doing CRAM-MD5 authentication\n");
  285. - return -1;
  286. + return NULL;
  287. }
  288. resp = cram (cmd, imap->box->user, imap->box->pass);
  289. @@ -568,34 +677,88 @@
  290. if (n <= 0)
  291. {
  292. socket_perror ("write", imap->sock, n);
  293. - return -1;
  294. + return NULL;
  295. }
  296. n = socket_write (imap->sock, "\r\n", 2);
  297. if (n <= 0)
  298. {
  299. socket_perror ("write", imap->sock, n);
  300. - return -1;
  301. + return NULL;
  302. }
  303. imap->cram = 0;
  304. }
  305. #endif
  306. - else if ((size_t) atol (arg) != Tag)
  307. - {
  308. - fprintf (stderr, "IMAP error: wrong tag\n");
  309. - return -1;
  310. - }
  311. - else
  312. - {
  313. - arg = next_arg (&cmd);
  314. - parse_response_code (imap, cmd);
  315. - if (!strcmp ("OK", arg))
  316. - return 0;
  317. - return -1;
  318. + else {
  319. + tag = (unsigned int) atol (arg);
  320. + cmdp = find_imap_cmd(tag);
  321. + if (!cmdp) {
  322. + fprintf(stderr, "IMAP error: sent unknown tag: %u\n",
  323. + tag);
  324. + return NULL;
  325. + }
  326. + arg = next_arg (&cmd);
  327. + if (strncmp("OK", arg, 2)) {
  328. + fprintf(stderr, "tag %u returned error: %s\n",
  329. + tag, arg);
  330. + cmdp->response = -1;
  331. + }
  332. + parse_response_code (imap, cmd);
  333. + num_in_progress--;
  334. + cmdp->flags |= IMAP_FLAG_DONE;
  335. + if (Verbose)
  336. + printf("Tag %u completed with response %d\n",
  337. + cmdp->tag, cmdp->response);
  338. + return cmdp;
  339. }
  340. }
  341. /* not reached */
  342. }
  343. +static void flush_imap_cmds(imap_t *imap)
  344. +{
  345. + struct imap_cmd *cmdp;
  346. +
  347. + while (num_in_progress) {
  348. + if (in_progress && in_progress->flags & IMAP_FLAG_DONE) {
  349. + dequeue_imap_cmd(in_progress->tag);
  350. + continue;
  351. + }
  352. + cmdp = get_cmd_result(imap);
  353. + if (!cmdp)
  354. + printf("Error trying to flush pending imap cmds\n");
  355. + }
  356. +}
  357. +
  358. +static int
  359. +imap_exec (imap_t * imap, const char *fmt, ...)
  360. +{
  361. + va_list ap;
  362. + char tmp[1024];
  363. + struct imap_cmd *cmdp, *waitp;
  364. + int result;
  365. +
  366. + va_start (ap, fmt);
  367. + vsnprintf (tmp, sizeof (tmp), fmt, ap);
  368. + va_end (ap);
  369. +
  370. + cmdp = issue_imap_cmd(imap, "%s", tmp);
  371. + if (!cmdp)
  372. + return -1;
  373. +
  374. + if (cmdp->flags & IMAP_FLAG_DONE)
  375. + return cmdp->response;
  376. +
  377. + do {
  378. + waitp = get_cmd_result(imap);
  379. + } while (waitp->tag != cmdp->tag);
  380. +
  381. + result = cmdp->response;
  382. + dequeue_imap_cmd(cmdp->tag);
  383. +
  384. + return cmdp->response;
  385. +
  386. +}
  387. +
  388. #ifdef HAVE_LIBSSL
  389. static int
  390. start_tls (imap_t *imap, config_t * cfg)
  391. @@ -1039,6 +1202,7 @@
  392. size_t n;
  393. char buf[1024];
  394. + flush_imap_cmds(imap);
  395. send_server (imap->sock, "UID FETCH %d BODY.PEEK[]", uid);
  396. for (;;)
  397. @@ -1160,7 +1324,9 @@
  398. (buf[0] != 0) ? " " : "", Flags[i]);
  399. }
  400. - return imap_exec (imap, "UID STORE %d +FLAGS.SILENT (%s)", uid, buf);
  401. + if (issue_imap_cmd(imap, "UID STORE %d +FLAGS.SILENT (%s)", uid, buf))
  402. + return 0;
  403. + return -1;
  404. }
  405. int
  406. @@ -1249,6 +1415,7 @@
  407. strcat (flagstr,") ");
  408. }
  409. + flush_imap_cmds(imap);
  410. send_server (imap->sock, "APPEND %s%s %s{%d}",
  411. imap->prefix, imap->box->box, flagstr, len + extra);
  412. @@ -1341,6 +1508,7 @@
  413. }
  414. /* didn't receive an APPENDUID */
  415. + flush_imap_cmds(imap);
  416. send_server (imap->sock,
  417. "UID SEARCH HEADER X-TUID %08lx%05lx%04x",
  418. tv.tv_sec, tv.tv_usec, pid);