main.c 10 KB

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