main.c 11 KB

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