main.c 9.2 KB

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