main.c 7.1 KB

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