main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 <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. {"all", 0, NULL, 'a'},
  34. {"config", 1, NULL, 'c'},
  35. {"create", 0, NULL, 'C'},
  36. {"delete", 0, NULL, 'd'},
  37. {"expunge", 0, NULL, 'e'},
  38. {"fast", 0, NULL, 'f'},
  39. {"help", 0, NULL, 'h'},
  40. {"remote", 1, NULL, 'r'},
  41. {"host", 1, NULL, 's'},
  42. {"port", 1, NULL, 'p'},
  43. {"quiet", 0, NULL, 'q'},
  44. {"user", 1, NULL, 'u'},
  45. {"version", 0, NULL, 'v'},
  46. {"verbose", 0, NULL, 'V'},
  47. {0, 0, 0, 0}
  48. };
  49. #endif
  50. config_t global;
  51. unsigned int Tag = 0;
  52. char Hostname[256];
  53. int Verbose = 0;
  54. static void
  55. version (void)
  56. {
  57. printf ("%s %s\n", PACKAGE, VERSION);
  58. exit (0);
  59. }
  60. static void
  61. usage (void)
  62. {
  63. printf ("%s %s IMAP4 to maildir synchronizer\n", PACKAGE, VERSION);
  64. puts ("Copyright (C) 2000-2 Michael R. Elkins <me@mutt.org>");
  65. printf ("usage: %s [ flags ] mailbox [mailbox ...]\n", PACKAGE);
  66. puts (" -a, --all Synchronize all defined mailboxes");
  67. puts (" -c, --config CONFIG read an alternate config file (default: ~/.isyncrc)");
  68. puts (" -C, --create create local maildir mailbox if nonexistent");
  69. puts (" -d, --delete delete local msgs that don't exist on the server");
  70. puts (" -e, --expunge expunge deleted messages from the server");
  71. puts (" -f, --fast only fetch new messages");
  72. puts (" -h, --help display this help message");
  73. puts (" -p, --port PORT server IMAP port");
  74. puts (" -r, --remote BOX remote mailbox");
  75. puts (" -s, --host HOST IMAP server address");
  76. puts (" -u, --user USER IMAP user name");
  77. puts (" -v, --version display version");
  78. puts (" -V, --verbose verbose mode (display network traffic)");
  79. puts ("Compile time options:");
  80. #if HAVE_LIBSSL
  81. puts (" +HAVE_LIBSSL");
  82. #else
  83. puts (" -HAVE_LIBSSL");
  84. #endif
  85. exit (0);
  86. }
  87. char *
  88. next_arg (char **s)
  89. {
  90. char *ret;
  91. if (!s)
  92. return 0;
  93. if (!*s)
  94. return 0;
  95. while (isspace ((unsigned char) **s))
  96. (*s)++;
  97. if (!**s)
  98. {
  99. *s = 0;
  100. return 0;
  101. }
  102. if (**s == '"')
  103. {
  104. ++*s;
  105. ret = *s;
  106. *s = strchr (*s, '"');
  107. }
  108. else
  109. {
  110. ret = *s;
  111. while (**s && !isspace ((unsigned char) **s))
  112. (*s)++;
  113. }
  114. if (*s)
  115. {
  116. if (**s)
  117. *(*s)++ = 0;
  118. if (!**s)
  119. *s = 0;
  120. }
  121. return ret;
  122. }
  123. int
  124. main (int argc, char **argv)
  125. {
  126. int i;
  127. config_t *box = 0;
  128. mailbox_t *mail = 0;
  129. imap_t *imap = 0;
  130. int expunge = 0; /* by default, don't delete anything */
  131. int fast = 0;
  132. int delete = 0;
  133. char *config = 0;
  134. struct passwd *pw;
  135. int quiet = 0;
  136. int all = 0;
  137. int create = 0;
  138. pw = getpwuid (getuid ());
  139. /* defaults */
  140. memset (&global, 0, sizeof (global));
  141. global.port = 143;
  142. global.box = "INBOX";
  143. global.user = strdup (pw->pw_name);
  144. global.maildir = strdup (pw->pw_dir);
  145. global.max_size = 0;
  146. global.max_messages = 0;
  147. global.use_namespace = 1;
  148. #if HAVE_LIBSSL
  149. /* this will probably annoy people, but its the best default just in
  150. * case people forget to turn it on
  151. */
  152. global.require_ssl = 1;
  153. global.use_sslv2 = 0;
  154. global.use_sslv3 = 0;
  155. global.use_tlsv1 = 1;
  156. #endif
  157. #define FLAGS "aCc:defhp:qu:r:s:vV"
  158. #if HAVE_GETOPT_LONG
  159. while ((i = getopt_long (argc, argv, FLAGS, Opts, NULL)) != -1)
  160. #else
  161. while ((i = getopt (argc, argv, FLAGS)) != -1)
  162. #endif
  163. {
  164. switch (i)
  165. {
  166. case 'a':
  167. all = 1;
  168. break;
  169. case 'C':
  170. create = 1;
  171. break;
  172. case 'c':
  173. config = optarg;
  174. break;
  175. case 'd':
  176. delete = 1;
  177. break;
  178. case 'e':
  179. expunge = 1;
  180. break;
  181. case 'f':
  182. fast = 1;
  183. break;
  184. case 'p':
  185. global.port = atoi (optarg);
  186. break;
  187. case 'q':
  188. quiet = 1;
  189. Verbose = 0;
  190. break;
  191. case 'r':
  192. global.box = optarg;
  193. break;
  194. case 's':
  195. #if HAVE_LIBSSL
  196. if (!strncasecmp ("imaps:", optarg, 6))
  197. {
  198. global.use_imaps = 1;
  199. optarg += 6;
  200. }
  201. #endif
  202. global.host = optarg;
  203. break;
  204. case 'u':
  205. free (global.user);
  206. global.user = optarg;
  207. break;
  208. case 'V':
  209. Verbose = 1;
  210. break;
  211. case 'v':
  212. version ();
  213. default:
  214. usage ();
  215. }
  216. }
  217. if (!argv[optind] && !all)
  218. {
  219. puts ("No mailbox specified");
  220. usage ();
  221. }
  222. gethostname (Hostname, sizeof (Hostname));
  223. load_config (config);
  224. for (box = boxes; (all && box) || (!all && argv[optind]); optind++)
  225. {
  226. if (!all)
  227. {
  228. if (NULL == (box = find_box (argv[optind])))
  229. {
  230. /* if enough info is given on the command line, don't worry if
  231. * the mailbox isn't defined.
  232. */
  233. if (!global.host)
  234. {
  235. fprintf (stderr, "%s: no such mailbox\n", argv[optind]);
  236. /* continue is ok here because we are not handling the
  237. * `all' case.
  238. */
  239. continue;
  240. }
  241. global.path = argv[optind];
  242. box = &global;
  243. }
  244. }
  245. do {
  246. if (!box->pass)
  247. {
  248. /* if we don't have a global password set, prompt the user for
  249. * it now.
  250. */
  251. if (!global.pass)
  252. {
  253. global.pass = getpass ("Password:");
  254. if (!global.pass)
  255. {
  256. fprintf (stderr, "Skipping %s, no password", box->path);
  257. break;
  258. }
  259. }
  260. box->pass = strdup (global.pass);
  261. }
  262. if (!quiet)
  263. printf ("Reading %s\n", box->path);
  264. i = 0;
  265. if (fast)
  266. i |= OPEN_FAST;
  267. if (create)
  268. i |= OPEN_CREATE;
  269. mail = maildir_open (box->path, i);
  270. if (!mail)
  271. {
  272. fprintf (stderr, "%s: unable to open mailbox\n", box->path);
  273. break;
  274. }
  275. imap = imap_open (box, fast ? mail->maxuid + 1 : 1, imap, 0);
  276. if (!imap)
  277. {
  278. fprintf (stderr, "%s: skipping mailbox due to IMAP error\n",
  279. box->path);
  280. break;
  281. }
  282. if (!quiet)
  283. puts ("Synchronizing");
  284. i = 0;
  285. if (quiet)
  286. i |= SYNC_QUIET;
  287. i |= (delete || box->delete) ? SYNC_DELETE : 0;
  288. i |= (expunge || box->expunge) ? SYNC_EXPUNGE : 0;
  289. if (sync_mailbox (mail, imap, i, box->max_size, box->max_messages))
  290. {
  291. imap_close (imap); /* Just to be safe. Don't really know
  292. * what the problem was.
  293. */
  294. break;
  295. }
  296. if (!fast)
  297. {
  298. if ((expunge || box->expunge) &&
  299. (imap->deleted || mail->deleted))
  300. {
  301. /* remove messages marked for deletion */
  302. if (!quiet)
  303. printf ("Expunging %d messages from server\n",
  304. imap->deleted);
  305. if (imap_expunge (imap))
  306. {
  307. imap_close (imap);
  308. imap = NULL;
  309. break;
  310. }
  311. if (!quiet)
  312. printf ("Expunging %d messages from local mailbox\n",
  313. mail->deleted);
  314. if (maildir_expunge (mail, 0))
  315. break;
  316. }
  317. /* remove messages deleted from server. this can safely be an
  318. * `else' clause since dead messages are marked as deleted by
  319. * sync_mailbox.
  320. */
  321. else if (delete)
  322. maildir_expunge (mail, 1);
  323. }
  324. } while (0);
  325. /* we never sync the same mailbox twice, so close it now */
  326. if (mail)
  327. maildir_close (mail);
  328. /* the imap connection is not closed so we can keep the connection
  329. * open, and there is no IMAP command for un-SELECT-ing a mailbox.
  330. */
  331. if (all)
  332. box = box->next;
  333. }
  334. /* gracefully close connection to the IMAP server */
  335. imap_close (imap);
  336. free_config ();
  337. #if DEBUG
  338. debug_cleanup ();
  339. #endif
  340. exit (0);
  341. }