main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. /* set defaults from the global configuration section */
  83. static void
  84. config_defaults (config_t * conf)
  85. {
  86. conf->user = global.user;
  87. conf->pass = global.pass;
  88. conf->port = global.port;
  89. conf->box = global.box;
  90. conf->host = global.host;
  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. #if HAVE_LIBSSL
  200. else if (!strncasecmp ("CertificateFile", buf, 15))
  201. {
  202. if (*cur)
  203. (*cur)->cert_file = strdup (p);
  204. else
  205. global.cert_file = strdup (p);
  206. }
  207. else if (!strncasecmp ("RequireSSL", buf, 10))
  208. {
  209. if (*cur)
  210. (*cur)->require_ssl = (strcasecmp (p, "yes") == 0);
  211. else
  212. global.require_ssl = (strcasecmp (p, "yes") == 0);
  213. }
  214. #endif
  215. else if (buf[0])
  216. printf ("%s:%d:unknown command:%s", path, line, buf);
  217. }
  218. fclose (fp);
  219. }
  220. static config_t *
  221. find_box (const char *s)
  222. {
  223. config_t *p = box;
  224. for (; p; p = p->next)
  225. if (!strcmp (s, p->path) || (p->alias && !strcmp (s, p->alias)))
  226. return p;
  227. return 0;
  228. }
  229. char *
  230. next_arg (char **s)
  231. {
  232. char *ret;
  233. if (!s)
  234. return 0;
  235. if (!*s)
  236. return 0;
  237. while (isspace ((unsigned char)**s))
  238. (*s)++;
  239. if (!**s)
  240. {
  241. *s = 0;
  242. return 0;
  243. }
  244. ret = *s;
  245. while (**s && !isspace ((unsigned char)**s))
  246. (*s)++;
  247. if (**s)
  248. *(*s)++ = 0;
  249. if (!**s)
  250. *s = 0;
  251. return ret;
  252. }
  253. int
  254. main (int argc, char **argv)
  255. {
  256. int i;
  257. config_t *box;
  258. mailbox_t *mail;
  259. imap_t *imap;
  260. int expunge = 0; /* by default, don't delete anything */
  261. int fast = 0;
  262. int delete = 0;
  263. char *config = 0;
  264. struct passwd *pw;
  265. pw = getpwuid (getuid ());
  266. /* defaults */
  267. memset (&global, 0, sizeof (global));
  268. global.port = 143;
  269. global.box = "INBOX";
  270. global.user = strdup (pw->pw_name);
  271. #if HAVE_LIBSSL
  272. /* this will probably annoy people, but its the best default just in
  273. * case people forget to turn it on
  274. */
  275. global.require_ssl = 1;
  276. #endif
  277. #if HAVE_GETOPT_LONG
  278. while ((i = getopt_long (argc, argv, "defhp:u:r:s:vV", Opts, NULL)) != -1)
  279. #else
  280. while ((i = getopt (argc, argv, "defhp:u:r:s:vV")) != -1)
  281. #endif
  282. {
  283. switch (i)
  284. {
  285. case 'c':
  286. config = optarg;
  287. break;
  288. case 'd':
  289. delete = 1;
  290. break;
  291. case 'e':
  292. expunge = 1;
  293. break;
  294. case 'f':
  295. fast = 1;
  296. break;
  297. case 'p':
  298. global.port = atoi (optarg);
  299. break;
  300. case 'r':
  301. global.box = optarg;
  302. break;
  303. case 's':
  304. global.host = optarg;
  305. break;
  306. case 'u':
  307. free (global.user);
  308. global.user = optarg;
  309. break;
  310. case 'V':
  311. Verbose = 1;
  312. break;
  313. case 'v':
  314. version ();
  315. default:
  316. usage ();
  317. }
  318. }
  319. if (!argv[optind])
  320. {
  321. puts ("No box specified");
  322. usage ();
  323. }
  324. gethostname (Hostname, sizeof (Hostname));
  325. load_config (config);
  326. box = find_box (argv[optind]);
  327. if (!box)
  328. {
  329. /* if enough info is given on the command line, don't worry if
  330. * the mailbox isn't defined.
  331. */
  332. if (!global.host)
  333. {
  334. puts ("No such mailbox");
  335. exit (1);
  336. }
  337. global.path = argv[optind];
  338. box = &global;
  339. }
  340. if (!box->pass)
  341. {
  342. char *pass = getpass ("Password:");
  343. if (pass)
  344. {
  345. puts ("Aborting, no password");
  346. exit (1);
  347. }
  348. box->pass = strdup (pass);
  349. }
  350. printf ("Reading %s\n", box->path);
  351. mail = maildir_open (box->path, fast);
  352. if (!mail)
  353. {
  354. puts ("Unable to load mailbox");
  355. exit (1);
  356. }
  357. imap = imap_open (box, fast);
  358. if (!imap)
  359. exit (1);
  360. puts ("Synchronizing");
  361. i = 0;
  362. i |= (fast) ? SYNC_FAST : 0;
  363. i |= (delete) ? SYNC_DELETE : 0;
  364. i |= (expunge) ? SYNC_EXPUNGE : 0;
  365. if (sync_mailbox (mail, imap, i))
  366. exit (1);
  367. if (!fast)
  368. {
  369. if (expunge && (imap->deleted || mail->deleted))
  370. {
  371. /* remove messages marked for deletion */
  372. printf ("Expunging %d messages from server\n", imap->deleted);
  373. if (imap_expunge (imap))
  374. exit (1);
  375. printf ("Expunging %d messages from local mailbox\n",
  376. mail->deleted);
  377. if (maildir_expunge (mail, 0))
  378. exit (1);
  379. }
  380. /* remove messages deleted from server. this can safely be an
  381. * `else' clause since dead messages are marked as deleted by
  382. * sync_mailbox.
  383. */
  384. else if (delete)
  385. maildir_expunge (mail, 1);
  386. /* write changed flags back to the mailbox */
  387. printf ("Committing changes to %s\n", mail->path);
  388. if (maildir_sync (mail))
  389. exit (1);
  390. }
  391. /* gracefully close connection to the IMAP server */
  392. imap_close (imap);
  393. exit (0);
  394. }