main.c 6.6 KB

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