imap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /* isync - IMAP4 to maildir mailbox synchronizer
  2. * Copyright (C) 2000 Michael R. Elkins <me@mutt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <assert.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <sys/socket.h>
  26. #include <netinet/in.h>
  27. #include <arpa/inet.h>
  28. #include <netdb.h>
  29. #include "isync.h"
  30. const char *Flags[] = {
  31. "\\Seen",
  32. "\\Answered",
  33. "\\Deleted",
  34. "\\Flagged",
  35. "\\Recent",
  36. "\\Draft"
  37. };
  38. /* simple line buffering */
  39. static int
  40. buffer_gets (buffer_t * b, char **s)
  41. {
  42. int n;
  43. int start = b->offset;
  44. *s = b->buf + start;
  45. for (;;)
  46. {
  47. if (b->offset + 2 > b->bytes)
  48. {
  49. /* shift down used bytes */
  50. *s = b->buf;
  51. assert (start <= b->bytes);
  52. n = b->bytes - start;
  53. if (n)
  54. memmove (b->buf, b->buf + start, n);
  55. b->offset = n;
  56. start = 0;
  57. n = read (b->fd, b->buf + b->offset, sizeof (b->buf) - b->offset);
  58. if (n <= 0)
  59. {
  60. if (n == -1)
  61. perror ("read");
  62. else
  63. puts ("EOF");
  64. return -1;
  65. }
  66. b->bytes = b->offset + n;
  67. // printf ("buffer_gets:read %d bytes\n", n);
  68. }
  69. if (b->buf[b->offset] == '\r')
  70. {
  71. if (b->buf[b->offset + 1] == '\n')
  72. {
  73. b->buf[b->offset] = 0; /* terminate the string */
  74. b->offset += 2; /* next line */
  75. return 0;
  76. }
  77. }
  78. b->offset++;
  79. }
  80. /* not reached */
  81. }
  82. static int
  83. imap_exec (imap_t * imap, const char *fmt, ...)
  84. {
  85. va_list ap;
  86. char tmp[256];
  87. char buf[256];
  88. char *cmd;
  89. char *arg;
  90. char *arg1;
  91. message_t **cur = 0;
  92. message_t **rec = 0;
  93. va_start (ap, fmt);
  94. vsnprintf (tmp, sizeof (tmp), fmt, ap);
  95. va_end (ap);
  96. snprintf (buf, sizeof (buf), "%d %s\r\n", ++Tag, tmp);
  97. if (Verbose)
  98. fputs (buf, stdout);
  99. write (imap->fd, buf, strlen (buf));
  100. for (;;)
  101. {
  102. if (buffer_gets (imap->buf, &cmd))
  103. return -1;
  104. if (Verbose)
  105. puts (cmd);
  106. arg = next_arg (&cmd);
  107. if (*arg == '*')
  108. {
  109. arg = next_arg (&cmd);
  110. arg1 = next_arg (&cmd);
  111. if (arg1 && !strcmp ("EXISTS", arg1))
  112. imap->count = atoi (arg);
  113. else if (arg1 && !strcmp ("RECENT", arg1))
  114. imap->recent = atoi (arg);
  115. else if (!strcmp ("SEARCH", arg))
  116. {
  117. if (!rec)
  118. {
  119. rec = &imap->msgs;
  120. while (*rec)
  121. rec = &(*rec)->next;
  122. }
  123. while ((arg = next_arg (&cmd)))
  124. {
  125. *rec = calloc (1, sizeof (message_t));
  126. (*rec)->uid = atoi (arg);
  127. rec = &(*rec)->next;
  128. }
  129. }
  130. else if (arg1 && !strcmp ("FETCH", arg1))
  131. {
  132. if (!cur)
  133. {
  134. cur = &imap->msgs;
  135. while (*cur)
  136. cur = &(*cur)->next;
  137. }
  138. /* new message
  139. * * <N> FETCH (UID <uid> FLAGS (...))
  140. */
  141. arg = next_arg (&cmd); /* (UID */
  142. arg = next_arg (&cmd); /* <uid> */
  143. *cur = calloc (1, sizeof (message_t));
  144. (*cur)->uid = atoi (arg);
  145. arg = next_arg (&cmd); /* FLAGS */
  146. if (!arg || strcmp ("FLAGS", arg))
  147. {
  148. printf ("FETCH parse error: expected FLAGS at %s\n", arg);
  149. return -1;
  150. }
  151. /* if we need to parse additional info, we should keep
  152. * a copy of this `arg' pointer
  153. */
  154. cmd++;
  155. arg = strchr (cmd, ')');
  156. if (!arg)
  157. {
  158. puts ("FETCH parse error");
  159. return -1;
  160. }
  161. *arg = 0;
  162. /* parse message flags */
  163. while ((arg = next_arg (&cmd)))
  164. {
  165. if (!strcmp ("\\Seen", arg))
  166. (*cur)->flags |= D_SEEN;
  167. else if (!strcmp ("\\Flagged", arg))
  168. (*cur)->flags |= D_FLAGGED;
  169. else if (!strcmp ("\\Deleted", arg))
  170. (*cur)->flags |= D_DELETED;
  171. else if (!strcmp ("\\Answered", arg))
  172. (*cur)->flags |= D_ANSWERED;
  173. else if (!strcmp ("\\Draft", arg))
  174. (*cur)->flags |= D_DRAFT;
  175. else if (!strcmp ("\\Recent", arg))
  176. (*cur)->flags |= D_RECENT;
  177. else
  178. printf ("warning, unknown flag %s\n", arg);
  179. }
  180. cur = &(*cur)->next;
  181. }
  182. }
  183. else if ((size_t) atol (arg) != Tag)
  184. {
  185. puts ("wrong tag");
  186. return -1;
  187. }
  188. else
  189. {
  190. arg = next_arg (&cmd);
  191. if (!strcmp ("OK", arg))
  192. return 0;
  193. puts ("IMAP command failed");
  194. return -1;
  195. }
  196. }
  197. /* not reached */
  198. }
  199. static int
  200. fetch_recent_flags (imap_t * imap)
  201. {
  202. char buf[1024];
  203. message_t **cur = &imap->recent_msgs;
  204. message_t *tmp;
  205. unsigned int start = -1;
  206. unsigned int last = -1;
  207. int ret = 0;
  208. buf[0] = 0;
  209. while (*cur)
  210. {
  211. tmp = *cur;
  212. if (last == (unsigned int) -1)
  213. {
  214. /* init */
  215. start = tmp->uid;
  216. last = tmp->uid;
  217. }
  218. else if (tmp->uid == last + 1)
  219. last++;
  220. else
  221. {
  222. /* out of sequence */
  223. if (start == last)
  224. ret = imap_exec (imap, "UID FETCH %d (UID FLAGS)", start);
  225. else
  226. ret =
  227. imap_exec (imap, "UID FETCH %d:%d (UID FLAGS)", start,
  228. last);
  229. start = tmp->uid;
  230. last = tmp->uid;
  231. }
  232. free (tmp);
  233. *cur = (*cur)->next;
  234. }
  235. if (start != (unsigned int) -1)
  236. {
  237. if (start == last)
  238. ret = imap_exec (imap, "UID FETCH %d (UID FLAGS)", start);
  239. else
  240. ret =
  241. imap_exec (imap, "UID FETCH %d:%d (UID FLAGS)", start, last);
  242. }
  243. return ret;
  244. }
  245. imap_t *
  246. imap_open (config_t * box, int fast)
  247. {
  248. int ret;
  249. imap_t *imap;
  250. int s;
  251. struct sockaddr_in sin;
  252. struct hostent *he;
  253. /* open connection to IMAP server */
  254. memset (&sin, 0, sizeof (sin));
  255. sin.sin_port = htons (box->port);
  256. sin.sin_family = AF_INET;
  257. printf ("Resolving %s... ", box->host);
  258. fflush (stdout);
  259. he = gethostbyname (box->host);
  260. if (!he)
  261. {
  262. perror ("gethostbyname");
  263. return 0;
  264. }
  265. puts ("ok");
  266. sin.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
  267. s = socket (PF_INET, SOCK_STREAM, 0);
  268. printf ("Connecting to %s:%hu... ", inet_ntoa (sin.sin_addr),
  269. ntohs (sin.sin_port));
  270. fflush (stdout);
  271. if (connect (s, (struct sockaddr *) &sin, sizeof (sin)))
  272. {
  273. perror ("connect");
  274. exit (1);
  275. }
  276. puts ("ok");
  277. imap = calloc (1, sizeof (imap_t));
  278. imap->fd = s;
  279. //imap->state = imap_state_init;
  280. imap->buf = calloc (1, sizeof (buffer_t));
  281. imap->buf->fd = s;
  282. imap->box = box;
  283. puts ("Logging in...");
  284. ret = imap_exec (imap, "LOGIN %s %s", box->user, box->pass);
  285. if (!ret)
  286. {
  287. fputs ("Selecting mailbox... ", stdout);
  288. fflush (stdout);
  289. ret = imap_exec (imap, "SELECT %s", box->box);
  290. if (!ret)
  291. printf ("%d messages, %d recent\n", imap->count, imap->recent);
  292. }
  293. if (!ret)
  294. {
  295. if (fast)
  296. {
  297. if (imap->recent > 0)
  298. {
  299. puts ("Fetching info for recent messages");
  300. ret = imap_exec (imap, "UID SEARCH RECENT");
  301. if (!ret)
  302. ret = fetch_recent_flags (imap);
  303. }
  304. }
  305. else if (imap->count > 0)
  306. {
  307. puts ("Reading IMAP mailbox index");
  308. ret = imap_exec (imap, "FETCH 1:%d (UID FLAGS)", imap->count);
  309. }
  310. }
  311. if (ret)
  312. {
  313. imap_exec (imap, "LOGOUT");
  314. close (s);
  315. free (imap->buf);
  316. free (imap);
  317. imap = 0;
  318. }
  319. return imap;
  320. }
  321. void
  322. imap_close (imap_t * imap)
  323. {
  324. puts ("Closing IMAP connection");
  325. imap_exec (imap, "LOGOUT");
  326. }
  327. /* write a buffer stripping all \r bytes */
  328. static int
  329. write_strip (int fd, char *buf, size_t len)
  330. {
  331. size_t start = 0;
  332. size_t end = 0;
  333. while (start < len)
  334. {
  335. while (end < len && buf[end] != '\r')
  336. end++;
  337. write (fd, buf + start, end - start);
  338. end++;
  339. start = end;
  340. }
  341. return 0;
  342. }
  343. static void
  344. send_server (int fd, const char *fmt, ...)
  345. {
  346. char buf[128];
  347. char cmd[128];
  348. va_list ap;
  349. va_start (ap, fmt);
  350. vsnprintf (buf, sizeof (buf), fmt, ap);
  351. va_end (ap);
  352. snprintf (cmd, sizeof (cmd), "%d %s\r\n", ++Tag, buf);
  353. write (fd, cmd, strlen (cmd));
  354. if (Verbose)
  355. fputs (cmd, stdout);
  356. }
  357. int
  358. imap_fetch_message (imap_t * imap, unsigned int uid, int fd)
  359. {
  360. char *cmd;
  361. char *arg;
  362. size_t bytes;
  363. size_t n;
  364. char buf[1024];
  365. send_server (imap->fd, "UID FETCH %d RFC822.PEEK", uid);
  366. for (;;)
  367. {
  368. if (buffer_gets (imap->buf, &cmd))
  369. return -1;
  370. if (Verbose)
  371. puts (cmd);
  372. if (*cmd == '*')
  373. {
  374. /* need to figure out how long the message is
  375. * * <msgno> FETCH (RFC822 {<size>}
  376. */
  377. next_arg (&cmd); /* * */
  378. next_arg (&cmd); /* <msgno> */
  379. next_arg (&cmd); /* FETCH */
  380. next_arg (&cmd); /* (RFC822 */
  381. arg = next_arg (&cmd);
  382. if (*arg != '{')
  383. {
  384. puts ("parse error getting size");
  385. return -1;
  386. }
  387. bytes = strtol (arg + 1, 0, 10);
  388. // printf ("receiving %d byte message\n", bytes);
  389. /* dump whats left over in the input buffer */
  390. n = imap->buf->bytes - imap->buf->offset;
  391. if (n > bytes)
  392. {
  393. /* the entire message fit in the buffer */
  394. n = bytes;
  395. }
  396. /* ick. we have to strip out the \r\n line endings, so
  397. * i can't just dump the raw bytes to disk.
  398. */
  399. write_strip (fd, imap->buf->buf + imap->buf->offset, n);
  400. bytes -= n;
  401. // printf ("wrote %d buffered bytes\n", n);
  402. /* mark that we used part of the buffer */
  403. imap->buf->offset += n;
  404. /* now read the rest of the message */
  405. while (bytes > 0)
  406. {
  407. n = bytes;
  408. if (n > sizeof (buf))
  409. n = sizeof (buf);
  410. n = read (imap->fd, buf, n);
  411. if (n > 0)
  412. {
  413. // printf("imap_fetch_message:%d:read %d bytes\n", __LINE__, n);
  414. write_strip (fd, buf, n);
  415. bytes -= n;
  416. }
  417. else
  418. {
  419. if (n == (size_t) - 1)
  420. perror ("read");
  421. else
  422. puts ("EOF");
  423. return -1;
  424. }
  425. }
  426. // puts ("finished fetching msg");
  427. buffer_gets (imap->buf, &cmd);
  428. if (Verbose)
  429. puts (cmd); /* last part of line */
  430. }
  431. else
  432. {
  433. arg = next_arg (&cmd);
  434. if (!arg || (size_t) atoi (arg) != Tag)
  435. {
  436. puts ("wrong tag");
  437. return -1;
  438. }
  439. break;
  440. }
  441. }
  442. return 0;
  443. }
  444. /* add flags to existing flags */
  445. int
  446. imap_set_flags (imap_t * imap, unsigned int uid, unsigned int flags)
  447. {
  448. char buf[256];
  449. int i;
  450. buf[0] = 0;
  451. for (i = 0; i < D_MAX; i++)
  452. {
  453. if (flags & (1 << i))
  454. snprintf (buf + strlen (buf),
  455. sizeof (buf) - strlen (buf), "%s%s",
  456. (buf[0] != 0) ? " " : "", Flags[i]);
  457. }
  458. return imap_exec (imap, "UID STORE %d +FLAGS.SILENT (%s)", uid, buf);
  459. }
  460. int
  461. imap_expunge (imap_t * imap)
  462. {
  463. return imap_exec (imap, "EXPUNGE");
  464. }