main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000-1 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-1 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;
  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;
  237. }
  238. global.path = argv[optind];
  239. box = &global;
  240. }
  241. }
  242. if (!box->pass)
  243. {
  244. /* if we don't have a global password set, prompt the user for
  245. * it now.
  246. */
  247. if (!global.pass)
  248. {
  249. global.pass = getpass ("Password:");
  250. if (!global.pass)
  251. {
  252. puts ("Aborting, no password");
  253. exit (1);
  254. }
  255. }
  256. box->pass = strdup (global.pass);
  257. }
  258. if (!quiet)
  259. printf ("Reading %s\n", box->path);
  260. i = 0;
  261. if (fast)
  262. i |= OPEN_FAST;
  263. if (create)
  264. i |= OPEN_CREATE;
  265. mail = maildir_open (box->path, i);
  266. if (!mail)
  267. {
  268. fprintf (stderr, "ERROR: unable to load mailbox %s\n", box->path);
  269. goto cleanup;
  270. }
  271. imap = imap_open (box, fast ? mail->maxuid + 1 : 1, imap);
  272. if (!imap)
  273. {
  274. fprintf (stderr, "%s: skipping mailbox due to IMAP error\n",
  275. box->path);
  276. goto cleanup;
  277. }
  278. if (!quiet)
  279. puts ("Synchronizing");
  280. i = 0;
  281. if (quiet)
  282. i |= SYNC_QUIET;
  283. i |= (delete || box->delete) ? SYNC_DELETE : 0;
  284. i |= (expunge || box->expunge) ? SYNC_EXPUNGE : 0;
  285. if (sync_mailbox (mail, imap, i, box->max_size, box->max_messages))
  286. exit (1);
  287. if (!fast)
  288. {
  289. if ((expunge || box->expunge) && (imap->deleted || mail->deleted))
  290. {
  291. /* remove messages marked for deletion */
  292. if (!quiet)
  293. printf ("Expunging %d messages from server\n",
  294. imap->deleted);
  295. if (imap_expunge (imap))
  296. exit (1);
  297. if (!quiet)
  298. printf ("Expunging %d messages from local mailbox\n",
  299. mail->deleted);
  300. if (maildir_expunge (mail, 0))
  301. exit (1);
  302. }
  303. /* remove messages deleted from server. this can safely be an
  304. * `else' clause since dead messages are marked as deleted by
  305. * sync_mailbox.
  306. */
  307. else if (delete)
  308. maildir_expunge (mail, 1);
  309. }
  310. /* write changed flags back to the mailbox */
  311. if (!quiet)
  312. printf ("Committing changes to %s\n", mail->path);
  313. if (maildir_close (mail))
  314. exit (1);
  315. cleanup:
  316. if (all)
  317. box = box->next;
  318. }
  319. /* gracefully close connection to the IMAP server */
  320. imap_close (imap);
  321. free_config ();
  322. #if DEBUG
  323. debug_cleanup ();
  324. #endif
  325. exit (0);
  326. }