main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000 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 <stdlib.h>
  21. #include <unistd.h>
  22. #include <limits.h>
  23. #include <pwd.h>
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include "isync.h"
  29. #if HAVE_GETOPT_LONG
  30. #define _GNU_SOURCE
  31. #include <getopt.h>
  32. struct option Opts[] = {
  33. {"config", 1, NULL, 'c'},
  34. {"delete", 0, NULL, 'd'},
  35. {"expunge", 0, NULL, 'e'},
  36. {"fast", 0, NULL, 'f'},
  37. {"help", 0, NULL, 'h'},
  38. {"remote", 1, NULL, 'r'},
  39. {"host", 1, NULL, 's'},
  40. {"port", 1, NULL, 'p'},
  41. {"user", 1, NULL, 'u'},
  42. {"version", 0, NULL, 'v'},
  43. {"verbose", 0, NULL, 'V'},
  44. {0, 0, 0, 0}
  45. };
  46. #endif
  47. config_t global;
  48. unsigned int Tag = 0;
  49. static config_t *box = 0;
  50. char Hostname[256];
  51. int Verbose = 0;
  52. static void
  53. version (void)
  54. {
  55. printf ("%s %s\n", PACKAGE, VERSION);
  56. exit (0);
  57. }
  58. static void
  59. usage (void)
  60. {
  61. printf ("%s %s IMAP4 to maildir synchronizer\n", PACKAGE, VERSION);
  62. puts ("Copyright (C) 2000 Michael R. Elkins <me@mutt.org>");
  63. printf ("usage: %s [ flags ] mailbox [mailbox ...]\n", PACKAGE);
  64. puts
  65. (" -c, --config CONFIG read an alternate config file (default: ~/.isyncrc)");
  66. puts
  67. (" -d, --delete delete local msgs that don't exist on the server");
  68. puts
  69. (" -e, --expunge expunge deleted messages from the server");
  70. puts (" -f, --fast only fetch new messages");
  71. puts (" -h, --help display this help message");
  72. puts (" -p, --port PORT server IMAP port");
  73. puts (" -r, --remote BOX remote mailbox");
  74. puts (" -s, --host HOST IMAP server address");
  75. puts (" -u, --user USER IMAP user name");
  76. puts (" -v, --version display version");
  77. puts
  78. (" -V, --verbose verbose mode (display network traffic)");
  79. exit (0);
  80. }
  81. /* set defaults from the global configuration section */
  82. static void
  83. config_defaults (config_t * conf)
  84. {
  85. conf->user = global.user;
  86. conf->pass = global.pass;
  87. conf->port = global.port;
  88. conf->box = global.box;
  89. conf->host = global.host;
  90. conf->max_size = global.max_size;
  91. conf->use_namespace = global.use_namespace;
  92. #if HAVE_LIBSSL
  93. conf->require_ssl = global.require_ssl;
  94. conf->use_imaps = global.use_imaps;
  95. conf->cert_file = global.cert_file;
  96. conf->use_sslv2 = global.use_sslv2;
  97. conf->use_sslv3 = global.use_sslv3;
  98. conf->use_tlsv1 = global.use_tlsv1;
  99. #endif
  100. }
  101. static void
  102. load_config (char *where)
  103. {
  104. char path[_POSIX_PATH_MAX];
  105. char buf[1024];
  106. struct passwd *pw;
  107. config_t **cur = &box;
  108. char *p, *q;
  109. int line = 0;
  110. FILE *fp;
  111. if (!where)
  112. {
  113. pw = getpwuid (getuid ());
  114. snprintf (path, sizeof (path), "%s/.isyncrc", pw->pw_dir);
  115. where = path;
  116. }
  117. printf ("Reading %s\n", where);
  118. fp = fopen (where, "r");
  119. if (!fp)
  120. {
  121. if (errno != ENOENT)
  122. {
  123. perror ("fopen");
  124. return;
  125. }
  126. }
  127. while ((fgets (buf, sizeof (buf) - 1, fp)))
  128. {
  129. if (buf[0])
  130. buf[strlen (buf) - 1] = 0;
  131. p = buf;
  132. q = next_arg (&p);
  133. line++;
  134. if (!q || *q == '#')
  135. continue;
  136. if (!strncasecmp ("mailbox", q, 7))
  137. {
  138. if (*cur)
  139. cur = &(*cur)->next;
  140. *cur = calloc (1, sizeof (config_t));
  141. config_defaults (*cur);
  142. (*cur)->path = strdup (p);
  143. }
  144. else if (!strncasecmp ("host", q, 4))
  145. {
  146. #if HAVE_LIBSSL
  147. if (!strncasecmp ("imaps:", p, 6))
  148. {
  149. p += 6;
  150. if (*cur)
  151. {
  152. (*cur)->use_imaps = 1;
  153. (*cur)->port = 993;
  154. }
  155. else
  156. {
  157. global.use_imaps = 1;
  158. global.port = 993;
  159. }
  160. }
  161. #endif
  162. if (*cur)
  163. (*cur)->host = strdup (p);
  164. else
  165. global.host = strdup (p);
  166. }
  167. else if (!strncasecmp ("user", q, 4))
  168. {
  169. if (*cur)
  170. (*cur)->user = strdup (p);
  171. else
  172. global.user = strdup (p);
  173. }
  174. else if (!strncasecmp ("pass", q, 4))
  175. {
  176. if (*cur)
  177. (*cur)->pass = strdup (p);
  178. else
  179. global.pass = strdup (p);
  180. }
  181. else if (!strncasecmp ("port", q, 4))
  182. {
  183. if (*cur)
  184. (*cur)->port = atoi (p);
  185. else
  186. global.port = atoi (p);
  187. }
  188. else if (!strncasecmp ("box", q, 3))
  189. {
  190. if (*cur)
  191. (*cur)->box = strdup (p);
  192. else
  193. global.box = strdup (p);
  194. }
  195. else if (!strncasecmp ("alias", q, 5))
  196. {
  197. if (*cur)
  198. (*cur)->alias = strdup (p);
  199. }
  200. else if (!strncasecmp ("maxsize", q, 7))
  201. {
  202. if (*cur)
  203. (*cur)->max_size = atol (p);
  204. else
  205. global.max_size = atol (p);
  206. }
  207. else if (!strncasecmp ("UseNamespace", q, 12))
  208. {
  209. if (*cur)
  210. (*cur)->use_namespace = (strcasecmp (p, "yes") == 0);
  211. else
  212. global.use_namespace = (strcasecmp (p, "yes") == 0);
  213. }
  214. #if HAVE_LIBSSL
  215. else if (!strncasecmp ("CertificateFile", q, 15))
  216. {
  217. if (*cur)
  218. (*cur)->cert_file = strdup (p);
  219. else
  220. global.cert_file = strdup (p);
  221. }
  222. else if (!strncasecmp ("RequireSSL", q, 10))
  223. {
  224. if (*cur)
  225. (*cur)->require_ssl = (strcasecmp (p, "yes") == 0);
  226. else
  227. global.require_ssl = (strcasecmp (p, "yes") == 0);
  228. }
  229. else if (!strncasecmp ("UseSSLv2", q, 8))
  230. {
  231. if (*cur)
  232. (*cur)->use_sslv2 = (strcasecmp (p, "yes") == 0);
  233. else
  234. global.use_sslv2 = (strcasecmp (p, "yes") == 0);
  235. }
  236. else if (!strncasecmp ("UseSSLv3", q, 8))
  237. {
  238. if (*cur)
  239. (*cur)->use_sslv3 = (strcasecmp (p, "yes") == 0);
  240. else
  241. global.use_sslv3 = (strcasecmp (p, "yes") == 0);
  242. }
  243. else if (!strncasecmp ("UseTLSv1", q, 8))
  244. {
  245. if (*cur)
  246. (*cur)->use_tlsv1 = (strcasecmp (p, "yes") == 0);
  247. else
  248. global.use_tlsv1 = (strcasecmp (p, "yes") == 0);
  249. }
  250. else if (!strncasecmp ("RequireCRAM", q, 11))
  251. {
  252. if (*cur)
  253. (*cur)->require_cram = (strcasecmp (p, "yes") == 0);
  254. else
  255. global.require_cram = (strcasecmp (p, "yes") == 0);
  256. }
  257. #endif
  258. else if (buf[0])
  259. printf ("%s:%d:unknown command:%s", path, line, q);
  260. }
  261. fclose (fp);
  262. }
  263. static config_t *
  264. find_box (const char *s)
  265. {
  266. config_t *p = box;
  267. for (; p; p = p->next)
  268. if (!strcmp (s, p->path) || (p->alias && !strcmp (s, p->alias)))
  269. return p;
  270. return 0;
  271. }
  272. char *
  273. next_arg (char **s)
  274. {
  275. char *ret;
  276. if (!s)
  277. return 0;
  278. if (!*s)
  279. return 0;
  280. while (isspace ((unsigned char) **s))
  281. (*s)++;
  282. if (!**s)
  283. {
  284. *s = 0;
  285. return 0;
  286. }
  287. ret = *s;
  288. while (**s && !isspace ((unsigned char) **s))
  289. (*s)++;
  290. if (**s)
  291. *(*s)++ = 0;
  292. if (!**s)
  293. *s = 0;
  294. return ret;
  295. }
  296. int
  297. main (int argc, char **argv)
  298. {
  299. int i;
  300. config_t *box;
  301. mailbox_t *mail;
  302. imap_t *imap = 0;
  303. int expunge = 0; /* by default, don't delete anything */
  304. int fast = 0;
  305. int delete = 0;
  306. char *config = 0;
  307. struct passwd *pw;
  308. pw = getpwuid (getuid ());
  309. /* defaults */
  310. memset (&global, 0, sizeof (global));
  311. global.port = 143;
  312. global.box = "INBOX";
  313. global.user = strdup (pw->pw_name);
  314. global.max_size = 0;
  315. global.use_namespace = 1;
  316. #if HAVE_LIBSSL
  317. /* this will probably annoy people, but its the best default just in
  318. * case people forget to turn it on
  319. */
  320. global.require_ssl = 1;
  321. global.use_sslv2 = 1;
  322. global.use_sslv3 = 1;
  323. global.use_tlsv1 = 1;
  324. #endif
  325. #if HAVE_GETOPT_LONG
  326. while ((i = getopt_long (argc, argv, "defhp:u:r:s:vV", Opts, NULL)) != -1)
  327. #else
  328. while ((i = getopt (argc, argv, "defhp:u:r:s:vV")) != -1)
  329. #endif
  330. {
  331. switch (i)
  332. {
  333. case 'c':
  334. config = optarg;
  335. break;
  336. case 'd':
  337. delete = 1;
  338. break;
  339. case 'e':
  340. expunge = 1;
  341. break;
  342. case 'f':
  343. fast = 1;
  344. break;
  345. case 'p':
  346. global.port = atoi (optarg);
  347. break;
  348. case 'r':
  349. global.box = optarg;
  350. break;
  351. case 's':
  352. global.host = optarg;
  353. break;
  354. case 'u':
  355. free (global.user);
  356. global.user = optarg;
  357. break;
  358. case 'V':
  359. Verbose = 1;
  360. break;
  361. case 'v':
  362. version ();
  363. default:
  364. usage ();
  365. }
  366. }
  367. if (!argv[optind])
  368. {
  369. puts ("No box specified");
  370. usage ();
  371. }
  372. gethostname (Hostname, sizeof (Hostname));
  373. load_config (config);
  374. for (; argv[optind]; optind++)
  375. {
  376. box = find_box (argv[optind]);
  377. if (!box)
  378. {
  379. /* if enough info is given on the command line, don't worry if
  380. * the mailbox isn't defined.
  381. */
  382. if (!global.host)
  383. {
  384. puts ("No such mailbox");
  385. exit (1);
  386. }
  387. global.path = argv[optind];
  388. box = &global;
  389. }
  390. if (!box->pass)
  391. {
  392. char *pass = getpass ("Password:");
  393. if (!pass)
  394. {
  395. puts ("Aborting, no password");
  396. exit (1);
  397. }
  398. box->pass = strdup (pass);
  399. }
  400. printf ("Reading %s\n", box->path);
  401. mail = maildir_open (box->path, fast);
  402. if (!mail)
  403. {
  404. puts ("Unable to load mailbox");
  405. exit (1);
  406. }
  407. imap = imap_open (box, fast ? mail->maxuid + 1 : 1, imap);
  408. if (!imap)
  409. exit (1);
  410. puts ("Synchronizing");
  411. i = delete ? SYNC_DELETE : 0;
  412. i |= expunge ? SYNC_EXPUNGE : 0;
  413. if (sync_mailbox (mail, imap, i, box->max_size))
  414. exit (1);
  415. if (!fast)
  416. {
  417. if (expunge && (imap->deleted || mail->deleted))
  418. {
  419. /* remove messages marked for deletion */
  420. printf ("Expunging %d messages from server\n", imap->deleted);
  421. if (imap_expunge (imap))
  422. exit (1);
  423. printf ("Expunging %d messages from local mailbox\n",
  424. mail->deleted);
  425. if (maildir_expunge (mail, 0))
  426. exit (1);
  427. }
  428. /* remove messages deleted from server. this can safely be an
  429. * `else' clause since dead messages are marked as deleted by
  430. * sync_mailbox.
  431. */
  432. else if (delete)
  433. maildir_expunge (mail, 1);
  434. /* write changed flags back to the mailbox */
  435. printf ("Committing changes to %s\n", mail->path);
  436. if (maildir_sync (mail))
  437. exit (1);
  438. }
  439. maildir_close (mail);
  440. }
  441. /* gracefully close connection to the IMAP server */
  442. imap_close (imap);
  443. exit (0);
  444. }