main.c 7.4 KB

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