main.c 6.3 KB

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