main.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. static config_t *box = 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\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. /* set defaults from the global configuration section */
  82. static void
  83. config_defaults (config_t * conf)
  84. {
  85. conf->user = global.user;
  86. conf->pass = global.pass;
  87. conf->port = global.port;
  88. conf->box = global.box;
  89. conf->host = global.host;
  90. conf->max_size = global.max_size;
  91. #if HAVE_LIBSSL
  92. conf->require_ssl = global.require_ssl;
  93. conf->use_imaps = global.use_imaps;
  94. conf->cert_file = global.cert_file;
  95. #endif
  96. }
  97. static void
  98. load_config (char *where)
  99. {
  100. char path[_POSIX_PATH_MAX];
  101. char buf[1024];
  102. struct passwd *pw;
  103. config_t **cur = &box;
  104. char *p;
  105. int line = 0;
  106. FILE *fp;
  107. if (!where)
  108. {
  109. pw = getpwuid (getuid ());
  110. snprintf (path, sizeof (path), "%s/.isyncrc", pw->pw_dir);
  111. where = path;
  112. }
  113. printf ("Reading %s\n", where);
  114. fp = fopen (where, "r");
  115. if (!fp)
  116. {
  117. if (errno != ENOENT)
  118. {
  119. perror ("fopen");
  120. return;
  121. }
  122. }
  123. while ((fgets (buf, sizeof (buf) - 1, fp)))
  124. {
  125. if (buf[0])
  126. buf[strlen (buf) - 1] = 0;
  127. line++;
  128. if (buf[0] == '#')
  129. continue;
  130. p = buf;
  131. while (*p && !isspace ((unsigned char)*p))
  132. p++;
  133. while (isspace ((unsigned char)*p))
  134. p++;
  135. if (!strncasecmp ("mailbox", buf, 7))
  136. {
  137. if (*cur)
  138. cur = &(*cur)->next;
  139. *cur = calloc (1, sizeof (config_t));
  140. config_defaults (*cur);
  141. (*cur)->path = strdup (p);
  142. }
  143. else if (!strncasecmp ("host", buf, 4))
  144. {
  145. #if HAVE_LIBSSL
  146. if (!strncasecmp ("imaps:", p, 6))
  147. {
  148. p += 6;
  149. if (*cur)
  150. {
  151. (*cur)->use_imaps = 1;
  152. (*cur)->port = 993;
  153. }
  154. else
  155. {
  156. global.use_imaps = 1;
  157. global.port = 993;
  158. }
  159. }
  160. #endif
  161. if (*cur)
  162. (*cur)->host = strdup (p);
  163. else
  164. global.host = strdup (p);
  165. }
  166. else if (!strncasecmp ("user", buf, 4))
  167. {
  168. if (*cur)
  169. (*cur)->user = strdup (p);
  170. else
  171. global.user = strdup (p);
  172. }
  173. else if (!strncasecmp ("pass", buf, 4))
  174. {
  175. if (*cur)
  176. (*cur)->pass = strdup (p);
  177. else
  178. global.pass = strdup (p);
  179. }
  180. else if (!strncasecmp ("port", buf, 4))
  181. {
  182. if (*cur)
  183. (*cur)->port = atoi (p);
  184. else
  185. global.port = atoi (p);
  186. }
  187. else if (!strncasecmp ("box", buf, 3))
  188. {
  189. if (*cur)
  190. (*cur)->box = strdup (p);
  191. else
  192. global.box = strdup (p);
  193. }
  194. else if (!strncasecmp ("alias", buf, 5))
  195. {
  196. if (*cur)
  197. (*cur)->alias = strdup (p);
  198. }
  199. else if (!strncasecmp ("maxsize", buf, 7))
  200. {
  201. if (*cur)
  202. (*cur)->max_size = atol (p);
  203. else
  204. global.max_size = atol (p);
  205. }
  206. #if HAVE_LIBSSL
  207. else if (!strncasecmp ("CertificateFile", buf, 15))
  208. {
  209. if (*cur)
  210. (*cur)->cert_file = strdup (p);
  211. else
  212. global.cert_file = strdup (p);
  213. }
  214. else if (!strncasecmp ("RequireSSL", buf, 10))
  215. {
  216. if (*cur)
  217. (*cur)->require_ssl = (strcasecmp (p, "yes") == 0);
  218. else
  219. global.require_ssl = (strcasecmp (p, "yes") == 0);
  220. }
  221. #endif
  222. else if (buf[0])
  223. printf ("%s:%d:unknown command:%s", path, line, buf);
  224. }
  225. fclose (fp);
  226. }
  227. static config_t *
  228. find_box (const char *s)
  229. {
  230. config_t *p = box;
  231. for (; p; p = p->next)
  232. if (!strcmp (s, p->path) || (p->alias && !strcmp (s, p->alias)))
  233. return p;
  234. return 0;
  235. }
  236. char *
  237. next_arg (char **s)
  238. {
  239. char *ret;
  240. if (!s)
  241. return 0;
  242. if (!*s)
  243. return 0;
  244. while (isspace ((unsigned char)**s))
  245. (*s)++;
  246. if (!**s)
  247. {
  248. *s = 0;
  249. return 0;
  250. }
  251. ret = *s;
  252. while (**s && !isspace ((unsigned char)**s))
  253. (*s)++;
  254. if (**s)
  255. *(*s)++ = 0;
  256. if (!**s)
  257. *s = 0;
  258. return ret;
  259. }
  260. int
  261. main (int argc, char **argv)
  262. {
  263. int i;
  264. config_t *box;
  265. mailbox_t *mail;
  266. imap_t *imap;
  267. int expunge = 0; /* by default, don't delete anything */
  268. int fast = 0;
  269. int delete = 0;
  270. char *config = 0;
  271. struct passwd *pw;
  272. pw = getpwuid (getuid ());
  273. /* defaults */
  274. memset (&global, 0, sizeof (global));
  275. global.port = 143;
  276. global.box = "INBOX";
  277. global.user = strdup (pw->pw_name);
  278. global.max_size = 0;
  279. #if HAVE_LIBSSL
  280. /* this will probably annoy people, but its the best default just in
  281. * case people forget to turn it on
  282. */
  283. global.require_ssl = 1;
  284. #endif
  285. #if HAVE_GETOPT_LONG
  286. while ((i = getopt_long (argc, argv, "defhp:u:r:s:vV", Opts, NULL)) != -1)
  287. #else
  288. while ((i = getopt (argc, argv, "defhp:u:r:s:vV")) != -1)
  289. #endif
  290. {
  291. switch (i)
  292. {
  293. case 'c':
  294. config = optarg;
  295. break;
  296. case 'd':
  297. delete = 1;
  298. break;
  299. case 'e':
  300. expunge = 1;
  301. break;
  302. case 'f':
  303. fast = 1;
  304. break;
  305. case 'p':
  306. global.port = atoi (optarg);
  307. break;
  308. case 'r':
  309. global.box = optarg;
  310. break;
  311. case 's':
  312. global.host = optarg;
  313. break;
  314. case 'u':
  315. free (global.user);
  316. global.user = optarg;
  317. break;
  318. case 'V':
  319. Verbose = 1;
  320. break;
  321. case 'v':
  322. version ();
  323. default:
  324. usage ();
  325. }
  326. }
  327. if (!argv[optind])
  328. {
  329. puts ("No box specified");
  330. usage ();
  331. }
  332. gethostname (Hostname, sizeof (Hostname));
  333. load_config (config);
  334. box = find_box (argv[optind]);
  335. if (!box)
  336. {
  337. /* if enough info is given on the command line, don't worry if
  338. * the mailbox isn't defined.
  339. */
  340. if (!global.host)
  341. {
  342. puts ("No such mailbox");
  343. exit (1);
  344. }
  345. global.path = argv[optind];
  346. box = &global;
  347. }
  348. if (!box->pass)
  349. {
  350. char *pass = getpass ("Password:");
  351. if (!pass)
  352. {
  353. puts ("Aborting, no password");
  354. exit (1);
  355. }
  356. box->pass = strdup (pass);
  357. }
  358. printf ("Reading %s\n", box->path);
  359. mail = maildir_open (box->path, fast);
  360. if (!mail)
  361. {
  362. puts ("Unable to load mailbox");
  363. exit (1);
  364. }
  365. imap = imap_open (box, fast ? mail->maxuid + 1 : 1);
  366. if (!imap)
  367. exit (1);
  368. puts ("Synchronizing");
  369. i = delete ? SYNC_DELETE : 0;
  370. i |= expunge ? SYNC_EXPUNGE : 0;
  371. if (sync_mailbox (mail, imap, i, box->max_size))
  372. exit (1);
  373. if (!fast)
  374. {
  375. if (expunge && (imap->deleted || mail->deleted))
  376. {
  377. /* remove messages marked for deletion */
  378. printf ("Expunging %d messages from server\n", imap->deleted);
  379. if (imap_expunge (imap))
  380. exit (1);
  381. printf ("Expunging %d messages from local mailbox\n",
  382. mail->deleted);
  383. if (maildir_expunge (mail, 0))
  384. exit (1);
  385. }
  386. /* remove messages deleted from server. this can safely be an
  387. * `else' clause since dead messages are marked as deleted by
  388. * sync_mailbox.
  389. */
  390. else if (delete)
  391. maildir_expunge (mail, 1);
  392. /* write changed flags back to the mailbox */
  393. printf ("Committing changes to %s\n", mail->path);
  394. if (maildir_sync (mail))
  395. exit (1);
  396. }
  397. /* gracefully close connection to the IMAP server */
  398. imap_close (imap);
  399. exit (0);
  400. }