main.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000-2 Michael R. Elkins <me@mutt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <sys/types.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <stdarg.h>
  24. #include <limits.h>
  25. #include <pwd.h>
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <dirent.h>
  31. #include "isync.h"
  32. #if HAVE_GETOPT_LONG
  33. #define _GNU_SOURCE
  34. #include <getopt.h>
  35. int Quiet;
  36. void
  37. info (const char *msg, ...)
  38. {
  39. va_list va;
  40. if (!Quiet)
  41. {
  42. va_start (va, msg);
  43. vprintf (msg, va);
  44. va_end (va);
  45. }
  46. }
  47. void
  48. infoc (char c)
  49. {
  50. if (!Quiet)
  51. putchar (c);
  52. }
  53. struct option Opts[] = {
  54. {"all", 0, NULL, 'a'},
  55. {"list", 0, NULL, 'l'},
  56. {"config", 1, NULL, 'c'},
  57. {"create", 0, NULL, 'C'},
  58. {"create-local", 0, NULL, 'L'},
  59. {"create-remote", 0, NULL, 'R'},
  60. {"delete", 0, NULL, 'd'},
  61. {"expunge", 0, NULL, 'e'},
  62. {"fast", 0, NULL, 'f'},
  63. {"help", 0, NULL, 'h'},
  64. {"remote", 1, NULL, 'r'},
  65. {"folder", 1, NULL, 'F'},
  66. {"maildir", 1, NULL, 'M'},
  67. {"one-to-one", 0, NULL, '1'},
  68. {"inbox", 1, NULL, 'I'},
  69. {"host", 1, NULL, 's'},
  70. {"port", 1, NULL, 'p'},
  71. {"quiet", 0, NULL, 'q'},
  72. {"user", 1, NULL, 'u'},
  73. {"version", 0, NULL, 'v'},
  74. {"verbose", 0, NULL, 'V'},
  75. {0, 0, 0, 0}
  76. };
  77. #endif
  78. config_t global;
  79. unsigned int Tag = 0;
  80. char Hostname[256];
  81. int Verbose = 0;
  82. static void
  83. version (void)
  84. {
  85. puts (PACKAGE " " VERSION);
  86. exit (0);
  87. }
  88. static void
  89. usage (int code)
  90. {
  91. fputs (
  92. PACKAGE " " VERSION " IMAP4 to maildir synchronizer\n"
  93. "Copyright (C) 2000-2 Michael R. Elkins <me@mutt.org>\n"
  94. "usage:\n"
  95. " " PACKAGE " [ flags ] mailbox [mailbox ...]\n"
  96. " " PACKAGE " [ flags ] -a\n"
  97. " " PACKAGE " [ flags ] -l\n"
  98. " -a, --all synchronize all defined mailboxes\n"
  99. " -l, --list list all defined mailboxes and exit\n"
  100. " -L, --create-local create local maildir mailbox if nonexistent\n"
  101. " -R, --create-remote create remote imap mailbox if nonexistent\n"
  102. " -C, --create create both local and remote mailboxes if nonexistent\n"
  103. " -d, --delete delete local msgs that don't exist on the server\n"
  104. " -e, --expunge expunge deleted messages from the server\n"
  105. " -f, --fast only fetch new messages\n"
  106. " -r, --remote BOX remote mailbox\n"
  107. " -F, --folder DIR remote IMAP folder containing mailboxes\n"
  108. " -M, --maildir DIR local directory containing mailboxes\n"
  109. " -1, --one-to-one map every IMAP <folder>/box to <maildir>/box\n"
  110. " -I, --inbox BOX map IMAP INBOX to <maildir>/BOX (exception to -1)\n"
  111. " -s, --host HOST IMAP server address\n"
  112. " -p, --port PORT server IMAP port\n"
  113. " -u, --user USER IMAP user name\n"
  114. " -c, --config CONFIG read an alternate config file (default: ~/.isyncrc)\n"
  115. " -V, --verbose verbose mode (display network traffic)\n"
  116. " -q, --quiet don't display progress info\n"
  117. " -v, --version display version\n"
  118. " -h, --help display this help message\n"
  119. "Compile time options:\n"
  120. #if HAVE_LIBSSL
  121. " +HAVE_LIBSSL\n"
  122. #else
  123. " -HAVE_LIBSSL\n"
  124. #endif
  125. , code ? stderr : stdout);
  126. exit (code);
  127. }
  128. char *
  129. next_arg (char **s)
  130. {
  131. char *ret;
  132. if (!s)
  133. return 0;
  134. if (!*s)
  135. return 0;
  136. while (isspace ((unsigned char) **s))
  137. (*s)++;
  138. if (!**s)
  139. {
  140. *s = 0;
  141. return 0;
  142. }
  143. if (**s == '"')
  144. {
  145. ++*s;
  146. ret = *s;
  147. *s = strchr (*s, '"');
  148. }
  149. else
  150. {
  151. ret = *s;
  152. while (**s && !isspace ((unsigned char) **s))
  153. (*s)++;
  154. }
  155. if (*s)
  156. {
  157. if (**s)
  158. *(*s)++ = 0;
  159. if (!**s)
  160. *s = 0;
  161. }
  162. return ret;
  163. }
  164. int
  165. main (int argc, char **argv)
  166. {
  167. int i;
  168. config_t *box = 0;
  169. mailbox_t *mail = 0;
  170. imap_t *imap = 0;
  171. int expunge = 0; /* by default, don't delete anything */
  172. int fast = 0;
  173. int delete = 0;
  174. char *config = 0;
  175. struct passwd *pw;
  176. int all = 0;
  177. int list = 0;
  178. int o2o = 0;
  179. int mbox_open_mode = 0;
  180. int imap_create = 0;
  181. pw = getpwuid (getuid ());
  182. /* defaults */
  183. memset (&global, 0, sizeof (global));
  184. /* XXX the precedence is borked:
  185. it's defaults < cmdline < file instead of defaults < file < cmdline */
  186. global.port = 143;
  187. global.box = "INBOX";
  188. global.folder = "";
  189. global.user = strdup (pw->pw_name);
  190. global.maildir = strdup (pw->pw_dir);
  191. global.use_namespace = 1;
  192. #if HAVE_LIBSSL
  193. /* this will probably annoy people, but its the best default just in
  194. * case people forget to turn it on
  195. */
  196. global.require_ssl = 1;
  197. global.use_tlsv1 = 1;
  198. #endif
  199. #define FLAGS "alCLRc:defhp:qu:r:F:M:1I:s:vV"
  200. #if HAVE_GETOPT_LONG
  201. while ((i = getopt_long (argc, argv, FLAGS, Opts, NULL)) != -1)
  202. #else
  203. while ((i = getopt (argc, argv, FLAGS)) != -1)
  204. #endif
  205. {
  206. switch (i)
  207. {
  208. case 'l':
  209. list = 1;
  210. /* plopp */
  211. case 'a':
  212. all = 1;
  213. break;
  214. case '1':
  215. o2o = 1;
  216. break;
  217. case 'C':
  218. mbox_open_mode |= OPEN_CREATE;
  219. imap_create = 1;
  220. break;
  221. case 'L':
  222. mbox_open_mode |= OPEN_CREATE;
  223. break;
  224. case 'R':
  225. imap_create = 1;
  226. break;
  227. case 'c':
  228. config = optarg;
  229. break;
  230. case 'd':
  231. delete = 1;
  232. break;
  233. case 'e':
  234. expunge = 1;
  235. break;
  236. case 'f':
  237. mbox_open_mode |= OPEN_FAST;
  238. fast = 1;
  239. break;
  240. case 'p':
  241. global.port = atoi (optarg);
  242. break;
  243. case 'q':
  244. Quiet = 1;
  245. Verbose = 0;
  246. break;
  247. case 'r':
  248. global.box = optarg;
  249. break;
  250. case 'F':
  251. global.folder = optarg;
  252. break;
  253. case 'M':
  254. free (global.maildir);
  255. global.maildir = strdup (optarg);
  256. break;
  257. case 'I':
  258. global.inbox = optarg;
  259. break;
  260. case 's':
  261. #if HAVE_LIBSSL
  262. if (!strncasecmp ("imaps:", optarg, 6))
  263. {
  264. global.use_imaps = 1;
  265. optarg += 6;
  266. }
  267. #endif
  268. global.host = optarg;
  269. break;
  270. case 'u':
  271. free (global.user);
  272. global.user = optarg;
  273. break;
  274. case 'V':
  275. Verbose = 1;
  276. break;
  277. case 'v':
  278. version ();
  279. case 'h':
  280. usage (0);
  281. default:
  282. usage (1);
  283. }
  284. }
  285. if (!argv[optind] && !all)
  286. {
  287. fprintf (stderr, "No mailbox specified");
  288. usage (1);
  289. }
  290. gethostname (Hostname, sizeof (Hostname));
  291. load_config (config, &o2o);
  292. if (all && o2o)
  293. {
  294. DIR *dir;
  295. struct dirent *de;
  296. if (global.inbox) {
  297. boxes = malloc (sizeof (config_t));
  298. memcpy (boxes, &global, sizeof (config_t));
  299. boxes->box = "INBOX";
  300. boxes->path = global.inbox;
  301. }
  302. if (!(dir = opendir (global.maildir))) {
  303. fprintf (stderr, "%s: %s\n", global.maildir, strerror(errno));
  304. return 1;
  305. }
  306. while ((de = readdir (dir))) {
  307. if (*de->d_name == '.')
  308. continue;
  309. if (global.inbox && !strcmp (global.inbox, de->d_name))
  310. continue;
  311. box = malloc (sizeof (config_t));
  312. memcpy (box, &global, sizeof (config_t));
  313. box->path = strdup (de->d_name);
  314. box->box = box->path;
  315. box->next = boxes;
  316. boxes = box;
  317. }
  318. closedir (dir);
  319. imap = imap_connect (&global);
  320. if (!imap)
  321. goto bork;
  322. if (imap_list (imap))
  323. goto bork;
  324. }
  325. if (list)
  326. {
  327. for (box = boxes; box; box = box->next)
  328. puts (box->path);
  329. exit (0);
  330. }
  331. for (box = boxes; (all && box) || (!all && argv[optind]); optind++)
  332. {
  333. if (!all)
  334. {
  335. if (o2o || NULL == (box = find_box (argv[optind])))
  336. {
  337. /* if enough info is given on the command line, don't worry if
  338. * the mailbox isn't defined.
  339. */
  340. if (!global.host)
  341. {
  342. fprintf (stderr, "%s: no such mailbox\n", argv[optind]);
  343. /* continue is ok here because we are not handling the
  344. * `all' case.
  345. */
  346. continue;
  347. }
  348. global.path = argv[optind];
  349. box = &global;
  350. if (o2o)
  351. global.box =
  352. (global.inbox && !strcmp (global.path, global.inbox)) ?
  353. "INBOX" : global.path;
  354. }
  355. }
  356. do {
  357. info ("Mailbox %s\n", box->path);
  358. mail = maildir_open (box->path, mbox_open_mode);
  359. if (!mail)
  360. {
  361. fprintf (stderr, "%s: unable to open mailbox\n", box->path);
  362. break;
  363. }
  364. imap = imap_open (box, fast ? mail->maxuid + 1 : 1, imap, imap_create);
  365. if (!imap)
  366. {
  367. fprintf (stderr, "%s: skipping mailbox due to IMAP error\n",
  368. box->path);
  369. break;
  370. }
  371. info ("Synchronizing\n");
  372. i = (delete || box->delete) ? SYNC_DELETE : 0;
  373. i |= (expunge || box->expunge) ? SYNC_EXPUNGE : 0;
  374. if (sync_mailbox (mail, imap, i, box->max_size, box->max_messages))
  375. {
  376. imap_close (imap); /* Just to be safe. Don't really know
  377. * what the problem was.
  378. */
  379. imap = NULL; /* context no longer valid */
  380. break;
  381. }
  382. if (!fast)
  383. {
  384. if ((expunge || box->expunge) &&
  385. (imap->deleted || mail->deleted))
  386. {
  387. /* remove messages marked for deletion */
  388. info ("Expunging %d messages from server\n", imap->deleted);
  389. if (imap_expunge (imap))
  390. {
  391. imap_close (imap);
  392. imap = NULL;
  393. break;
  394. }
  395. info ("Expunging %d messages from local mailbox\n",
  396. mail->deleted);
  397. if (maildir_expunge (mail, 0))
  398. break;
  399. }
  400. /* remove messages deleted from server. this can safely be an
  401. * `else' clause since dead messages are marked as deleted by
  402. * sync_mailbox.
  403. */
  404. else if (delete)
  405. maildir_expunge (mail, 1);
  406. }
  407. } while (0);
  408. /* we never sync the same mailbox twice, so close it now */
  409. if (mail)
  410. maildir_close (mail);
  411. /* the imap connection is not closed so we can keep the connection
  412. * open, and there is no IMAP command for un-SELECT-ing a mailbox.
  413. */
  414. if (all)
  415. box = box->next;
  416. }
  417. /* gracefully close connection to the IMAP server */
  418. imap_close (imap);
  419. bork:
  420. free_config ();
  421. #if DEBUG
  422. debug_cleanup ();
  423. #endif
  424. exit (0);
  425. }