main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* isync - IMAP4 to maildir mailbox synchronizer
  2. * Copyright (C) 2000 Michael R. Elkins <me@mutt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <limits.h>
  21. #include <pwd.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <termios.h>
  27. #include "isync.h"
  28. #if HAVE_GETOPT_LONG
  29. #define _GNU_SOURCE
  30. #include <getopt.h>
  31. struct option Opts[] = {
  32. {"config", 1, NULL, 'c'},
  33. {"delete", 0, NULL, 'd'},
  34. {"expunge", 0, NULL, 'e'},
  35. {"fast", 0, NULL, 'f'},
  36. {"help", 0, NULL, 'h'},
  37. {"remote", 1, NULL, 'r'},
  38. {"host", 1, NULL, 's'},
  39. {"port", 1, NULL, 'p'},
  40. {"user", 1, NULL, 'u'},
  41. {"version", 0, NULL, 'v'},
  42. {"verbose", 0, NULL, 'V'},
  43. {0, 0, 0, 0}
  44. };
  45. #endif
  46. config_t global;
  47. unsigned int Tag = 0;
  48. static config_t *box = 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\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. static char *
  81. enter_password (void)
  82. {
  83. struct termios t;
  84. char pass[32];
  85. tcgetattr (0, &t);
  86. t.c_lflag &= ~ECHO;
  87. tcsetattr (0, TCSANOW, &t);
  88. printf ("Password: ");
  89. fflush (stdout);
  90. pass[sizeof (pass) - 1] = 0;
  91. fgets (pass, sizeof (pass) - 1, stdin);
  92. if (pass[0])
  93. pass[strlen (pass) - 1] = 0; /* kill newline */
  94. t.c_lflag |= ECHO;
  95. tcsetattr (0, TCSANOW, &t);
  96. puts ("");
  97. return strdup (pass);
  98. }
  99. static void
  100. load_config (char *where)
  101. {
  102. char path[_POSIX_PATH_MAX];
  103. char buf[1024];
  104. struct passwd *pw;
  105. config_t **cur = &box;
  106. char *p;
  107. int line = 0;
  108. FILE *fp;
  109. if (!where)
  110. {
  111. pw = getpwuid (getuid ());
  112. snprintf (path, sizeof (path), "%s/.isyncrc", pw->pw_dir);
  113. where = path;
  114. }
  115. printf ("Reading %s\n", where);
  116. fp = fopen (where, "r");
  117. if (!fp)
  118. {
  119. if (errno != ENOENT)
  120. {
  121. perror ("fopen");
  122. return;
  123. }
  124. }
  125. while ((fgets (buf, sizeof (buf) - 1, fp)))
  126. {
  127. if (buf[0])
  128. buf[strlen (buf) - 1] = 0;
  129. line++;
  130. if (buf[0] == '#')
  131. continue;
  132. p = buf;
  133. while (*p && !isspace (*p))
  134. p++;
  135. while (isspace (*p))
  136. p++;
  137. if (!strncmp ("mailbox", buf, 7))
  138. {
  139. if (*cur)
  140. cur = &(*cur)->next;
  141. *cur = calloc (1, sizeof (config_t));
  142. (*cur)->path = strdup (p);
  143. }
  144. else if (!strncmp ("host", buf, 4))
  145. {
  146. if (*cur)
  147. (*cur)->host = strdup (p);
  148. else
  149. global.host = strdup (p);
  150. }
  151. else if (!strncmp ("user", buf, 4))
  152. {
  153. if (*cur)
  154. (*cur)->user = strdup (p);
  155. else
  156. global.user = strdup (p);
  157. }
  158. else if (!strncmp ("pass", buf, 4))
  159. {
  160. if (*cur)
  161. (*cur)->pass = strdup (p);
  162. else
  163. global.pass = strdup (p);
  164. }
  165. else if (!strncmp ("port", buf, 4))
  166. {
  167. if (*cur)
  168. (*cur)->port = atoi (p);
  169. else
  170. global.port = atoi (p);
  171. }
  172. else if (!strncmp ("box", buf, 3))
  173. {
  174. if (*cur)
  175. (*cur)->box = strdup (p);
  176. else
  177. global.box = strdup (p);
  178. }
  179. else if (!strncmp ("alias", buf, 5))
  180. {
  181. if (*cur)
  182. (*cur)->alias = strdup (p);
  183. }
  184. else if (buf[0])
  185. printf ("%s:%d:unknown command:%s", path, line, buf);
  186. }
  187. fclose (fp);
  188. }
  189. static config_t *
  190. find_box (const char *s)
  191. {
  192. config_t *p = box;
  193. for (; p; p = p->next)
  194. if (!strcmp (s, p->path) || (p->alias && !strcmp (s, p->alias)))
  195. return p;
  196. return 0;
  197. }
  198. char *
  199. next_arg (char **s)
  200. {
  201. char *ret;
  202. if (!s)
  203. return 0;
  204. if (!*s)
  205. return 0;
  206. while (isspace (**s))
  207. (*s)++;
  208. if (!**s)
  209. {
  210. *s = 0;
  211. return 0;
  212. }
  213. ret = *s;
  214. while (**s && !isspace (**s))
  215. (*s)++;
  216. if (**s)
  217. *(*s)++ = 0;
  218. if (!**s)
  219. *s = 0;
  220. return ret;
  221. }
  222. int
  223. main (int argc, char **argv)
  224. {
  225. int i;
  226. config_t *box;
  227. mailbox_t *mail;
  228. imap_t *imap;
  229. int expunge = 0; /* by default, don't delete anything */
  230. int fast = 0;
  231. int delete = 0;
  232. char *config = 0;
  233. struct passwd *pw;
  234. pw = getpwuid (getuid ());
  235. /* defaults */
  236. memset (&global, 0, sizeof (global));
  237. global.port = 143;
  238. global.box = "INBOX";
  239. global.user = strdup (pw->pw_name);
  240. #if HAVE_GETOPT_LONG
  241. while ((i = getopt_long (argc, argv, "defhp:u:r:s:vV", Opts, NULL)) != -1)
  242. #else
  243. while ((i = getopt (argc, argv, "defhp:u:r:s:vV")) != -1)
  244. #endif
  245. {
  246. switch (i)
  247. {
  248. case 'c':
  249. config = optarg;
  250. break;
  251. case 'd':
  252. delete = 1;
  253. break;
  254. case 'e':
  255. expunge = 1;
  256. break;
  257. case 'f':
  258. fast = 1;
  259. break;
  260. case 'p':
  261. global.port = atoi (optarg);
  262. break;
  263. case 'r':
  264. global.box = optarg;
  265. break;
  266. case 's':
  267. global.host = optarg;
  268. break;
  269. case 'u':
  270. free (global.user);
  271. global.user = optarg;
  272. break;
  273. case 'V':
  274. Verbose = 1;
  275. break;
  276. case 'v':
  277. version ();
  278. default:
  279. usage ();
  280. }
  281. }
  282. if (!argv[optind])
  283. {
  284. puts ("No box specified");
  285. usage ();
  286. }
  287. gethostname (Hostname, sizeof (Hostname));
  288. load_config (config);
  289. box = find_box (argv[optind]);
  290. if (!box)
  291. {
  292. /* if enough info is given on the command line, don't worry if
  293. * the mailbox isn't defined.
  294. */
  295. if (!global.host)
  296. {
  297. puts ("No such mailbox");
  298. exit (1);
  299. }
  300. global.path = argv[optind];
  301. box = &global;
  302. }
  303. /* fill in missing info with defaults */
  304. if (!box->pass)
  305. {
  306. if (!global.pass)
  307. {
  308. box->pass = enter_password ();
  309. if (!box->pass)
  310. {
  311. puts ("Aborting, no password");
  312. exit (1);
  313. }
  314. }
  315. else
  316. box->pass = global.pass;
  317. }
  318. if (!box->user)
  319. box->user = global.user;
  320. if (!box->port)
  321. box->port = global.port;
  322. if (!box->host)
  323. box->host = global.host;
  324. if (!box->box)
  325. box->box = global.box;
  326. printf ("Reading %s\n", box->path);
  327. mail = maildir_open (box->path, fast);
  328. if (!mail)
  329. {
  330. puts ("Unable to load mailbox");
  331. exit (1);
  332. }
  333. imap = imap_open (box, fast);
  334. if (!imap)
  335. exit (1);
  336. puts ("Synchronizing");
  337. i = 0;
  338. i |= (fast) ? SYNC_FAST : 0;
  339. i |= (delete) ? SYNC_DELETE : 0;
  340. if (sync_mailbox (mail, imap, i))
  341. exit (1);
  342. if (!fast)
  343. {
  344. if (expunge)
  345. {
  346. /* remove messages marked for deletion */
  347. puts ("Expunging messages");
  348. if (imap_expunge (imap))
  349. exit (1);
  350. if (maildir_expunge (mail, 0))
  351. exit (1);
  352. }
  353. /* remove messages deleted from server. this can safely be an
  354. * `else' clause since dead messages are marked as deleted by
  355. * sync_mailbox.
  356. */
  357. else if (delete)
  358. maildir_expunge (mail, 1);
  359. /* write changed flags back to the mailbox */
  360. printf ("Committing changes to %s\n", mail->path);
  361. if (maildir_sync (mail))
  362. exit (1);
  363. }
  364. /* gracefully close connection to the IMAP server */
  365. imap_close (imap);
  366. exit (0);
  367. }