main.c 11 KB

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