main.c 8.2 KB

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