main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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.use_namespace = 1;
  144. #if HAVE_LIBSSL
  145. /* this will probably annoy people, but its the best default just in
  146. * case people forget to turn it on
  147. */
  148. global.require_ssl = 1;
  149. global.use_sslv2 = 0;
  150. global.use_sslv3 = 0;
  151. global.use_tlsv1 = 1;
  152. #endif
  153. #define FLAGS "ac:defhp:qu:r:s:vV"
  154. #if HAVE_GETOPT_LONG
  155. while ((i = getopt_long (argc, argv, FLAGS, Opts, NULL)) != -1)
  156. #else
  157. while ((i = getopt (argc, argv, FLAGS)) != -1)
  158. #endif
  159. {
  160. switch (i)
  161. {
  162. case 'a':
  163. all = 1;
  164. break;
  165. case 'c':
  166. config = optarg;
  167. break;
  168. case 'd':
  169. delete = 1;
  170. break;
  171. case 'e':
  172. expunge = 1;
  173. break;
  174. case 'f':
  175. fast = 1;
  176. break;
  177. case 'p':
  178. global.port = atoi (optarg);
  179. break;
  180. case 'q':
  181. quiet = 1;
  182. Verbose = 0;
  183. break;
  184. case 'r':
  185. global.box = optarg;
  186. break;
  187. case 's':
  188. #if HAVE_LIBSSL
  189. if (!strncasecmp ("imaps:", optarg, 6))
  190. {
  191. global.use_imaps = 1;
  192. optarg += 6;
  193. }
  194. #endif
  195. global.host = optarg;
  196. break;
  197. case 'u':
  198. free (global.user);
  199. global.user = optarg;
  200. break;
  201. case 'V':
  202. Verbose = 1;
  203. break;
  204. case 'v':
  205. version ();
  206. default:
  207. usage ();
  208. }
  209. }
  210. if (!argv[optind] && !all)
  211. {
  212. puts ("No mailbox specified");
  213. usage ();
  214. }
  215. gethostname (Hostname, sizeof (Hostname));
  216. load_config (config);
  217. for (box = boxes; (all && box) || (!all && argv[optind]); optind++)
  218. {
  219. if (!all)
  220. {
  221. if (NULL == (box = find_box (argv[optind])))
  222. {
  223. /* if enough info is given on the command line, don't worry if
  224. * the mailbox isn't defined.
  225. */
  226. if (!global.host)
  227. {
  228. fprintf (stderr, "%s: no such mailbox\n", argv[optind]);
  229. continue;
  230. }
  231. global.path = argv[optind];
  232. box = &global;
  233. }
  234. }
  235. if (!box->pass)
  236. {
  237. /* if we don't have a global password set, prompt the user for
  238. * it now.
  239. */
  240. if (!global.pass)
  241. {
  242. global.pass = getpass ("Password:");
  243. if (!global.pass)
  244. {
  245. puts ("Aborting, no password");
  246. exit (1);
  247. }
  248. }
  249. box->pass = strdup (global.pass);
  250. }
  251. if (!quiet)
  252. printf ("Reading %s\n", box->path);
  253. mail = maildir_open (box->path, fast);
  254. if (!mail)
  255. {
  256. fprintf (stderr, "%s: unable to load mailbox\n", box->path);
  257. goto cleanup;
  258. }
  259. imap = imap_open (box, fast ? mail->maxuid + 1 : 1, imap);
  260. if (!imap)
  261. {
  262. fprintf (stderr, "%s: skipping mailbox due to IMAP error\n",
  263. box->path);
  264. goto cleanup;
  265. }
  266. if (!quiet)
  267. puts ("Synchronizing");
  268. i = (delete || box->delete) ? SYNC_DELETE : 0;
  269. i |= (expunge || box->expunge) ? SYNC_EXPUNGE : 0;
  270. if (sync_mailbox (mail, imap, i, box->max_size))
  271. exit (1);
  272. if (!fast)
  273. {
  274. if ((expunge || box->expunge) && (imap->deleted || mail->deleted))
  275. {
  276. /* remove messages marked for deletion */
  277. if (!quiet)
  278. printf ("Expunging %d messages from server\n",
  279. imap->deleted);
  280. if (imap_expunge (imap))
  281. exit (1);
  282. if (!quiet)
  283. printf ("Expunging %d messages from local mailbox\n",
  284. mail->deleted);
  285. if (maildir_expunge (mail, 0))
  286. exit (1);
  287. }
  288. /* remove messages deleted from server. this can safely be an
  289. * `else' clause since dead messages are marked as deleted by
  290. * sync_mailbox.
  291. */
  292. else if (delete)
  293. maildir_expunge (mail, 1);
  294. /* write changed flags back to the mailbox */
  295. if (mail->changed)
  296. {
  297. if (!quiet)
  298. printf ("Committing changes to %s\n", mail->path);
  299. if (maildir_sync (mail))
  300. exit (1);
  301. }
  302. }
  303. maildir_close (mail);
  304. cleanup:
  305. if (all)
  306. box = box->next;
  307. }
  308. /* gracefully close connection to the IMAP server */
  309. imap_close (imap);
  310. exit (0);
  311. }