sync.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000-2 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 <stdio.h>
  21. #include <limits.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <time.h>
  25. #include <fcntl.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <sys/stat.h>
  29. #include "isync.h"
  30. static unsigned int MaildirCount = 0;
  31. message_t *
  32. find_msg (message_t * list, unsigned int uid)
  33. {
  34. for (; list; list = list->next)
  35. if (list->uid == uid)
  36. return list;
  37. return 0;
  38. }
  39. static int
  40. set_uid (DBM * db, const char *f, unsigned int uid)
  41. {
  42. char *s;
  43. datum key, val;
  44. key.dptr = (void *) f;
  45. s = strchr (f, ':');
  46. key.dsize = s ? (size_t) (s - key.dptr) : strlen (f);
  47. val.dptr = (void *) &uid;
  48. val.dsize = sizeof (uid);
  49. dbm_store (db, key, val, DBM_REPLACE);
  50. return 0;
  51. }
  52. int
  53. sync_mailbox (mailbox_t * mbox, imap_t * imap, int flags,
  54. unsigned int max_size, unsigned int max_msgs)
  55. {
  56. message_t *cur;
  57. message_t *tmp;
  58. char path[_POSIX_PATH_MAX];
  59. char newpath[_POSIX_PATH_MAX];
  60. char suffix[_POSIX_PATH_MAX];
  61. char *p;
  62. int fd;
  63. int ret;
  64. int fetched = 0;
  65. int upload = 0;
  66. unsigned int msg_count;
  67. if (mbox->uidseen)
  68. {
  69. if (mbox->uidvalidity != imap->uidvalidity)
  70. {
  71. /* if the UIDVALIDITY value has changed, it means all our
  72. * local UIDs are invalid, so we can't sync.
  73. */
  74. fputs ("ERROR: UIDVALIDITY changed on server (fatal)\n", stderr);
  75. return -1;
  76. }
  77. }
  78. else if (maildir_set_uidvalidity (mbox, imap->uidvalidity))
  79. {
  80. fputs ("ERROR: unable to store UIDVALIDITY\n", stderr);
  81. return -1;
  82. }
  83. if (mbox->maxuid == 0 || imap->maxuid > mbox->maxuid)
  84. {
  85. mbox->maxuid = imap->maxuid;
  86. if (maildir_update_maxuid (mbox))
  87. return -1;
  88. }
  89. /* if we are --fast mode, the mailbox wont have been loaded, so
  90. * this next step is skipped.
  91. */
  92. for (cur = mbox->msgs; cur; cur = cur->next)
  93. {
  94. tmp = find_msg (imap->msgs, cur->uid);
  95. if (!tmp)
  96. {
  97. /* if this message wasn't fetched from the server, attempt to
  98. * upload it
  99. */
  100. if (cur->uid == (unsigned int) -1)
  101. {
  102. struct stat sb;
  103. if ((cur->flags & D_DELETED) && (flags & SYNC_EXPUNGE))
  104. {
  105. /*
  106. * This message is marked as deleted and we are
  107. * expunging. Don't upload to the server.
  108. */
  109. continue;
  110. }
  111. if ((flags & SYNC_QUIET) == 0)
  112. {
  113. if (!upload)
  114. fputs ("Uploading messages", stdout);
  115. fputc ('.', stdout);
  116. fflush (stdout);
  117. upload++;
  118. }
  119. /* upload the message if its not too big */
  120. snprintf (path, sizeof (path), "%s/%s/%s", mbox->path,
  121. cur->new ? "new" : "cur", cur->file);
  122. if (stat (path, &sb))
  123. {
  124. perror (path);
  125. continue; /* not fatal */
  126. }
  127. if (imap->box->max_size > 0
  128. && sb.st_size > imap->box->max_size)
  129. {
  130. if ((flags & SYNC_QUIET) == 0)
  131. printf
  132. ("Warning, local message is too large (%lu), skipping...\n",
  133. (unsigned long) sb.st_size);
  134. continue;
  135. }
  136. fd = open (path, O_RDONLY);
  137. if (fd == -1)
  138. {
  139. printf ("Error, unable to open %s: %s (errno %d)\n",
  140. path, strerror (errno), errno);
  141. continue;
  142. }
  143. cur->size = sb.st_size;
  144. cur->uid = imap_append_message (imap, fd, cur);
  145. /* if the server gave us back a uid, update the db */
  146. if (cur->uid != (unsigned int) -1)
  147. set_uid (mbox->db, cur->file, cur->uid);
  148. close (fd);
  149. }
  150. /*
  151. * message used to exist on server but no longer does (we know
  152. * this beacause it has a UID associated with it).
  153. */
  154. else if (flags & SYNC_DELETE)
  155. {
  156. cur->flags |= D_DELETED;
  157. cur->dead = 1;
  158. mbox->deleted++;
  159. }
  160. /* if the user doesn't want local msgs deleted when they don't
  161. * exist on the server, warn that such messages exist.
  162. */
  163. else if ((flags & SYNC_QUIET) == 0)
  164. printf ("Warning, uid %u doesn't exist on server\n",
  165. cur->uid);
  166. continue;
  167. }
  168. tmp->processed = 1;
  169. /* if the message is deleted, and CopyDeletedTo is set, and we
  170. * are expunging, make a copy of the message now.
  171. */
  172. if (((cur->flags | tmp->flags) & D_DELETED) != 0 &&
  173. (flags & SYNC_EXPUNGE) && imap->box->copy_deleted_to)
  174. {
  175. if (imap_copy_message (imap, cur->uid,
  176. imap->box->copy_deleted_to))
  177. {
  178. fprintf (stderr,
  179. "ERROR: unable to copy deleted message to \"%s\"\n",
  180. imap->box->copy_deleted_to);
  181. return -1;
  182. }
  183. }
  184. /* check if local flags are different from server flags.
  185. * ignore \Recent and \Draft
  186. */
  187. if (cur->flags != (tmp->flags & ~(D_RECENT | D_DRAFT)))
  188. {
  189. /* set local flags that don't exist on the server */
  190. if (!(tmp->flags & D_DELETED) && (cur->flags & D_DELETED))
  191. imap->deleted++;
  192. imap_set_flags (imap, cur->uid, cur->flags & ~tmp->flags);
  193. /* update local flags */
  194. if ((cur->flags & D_DELETED) == 0 && (tmp->flags & D_DELETED))
  195. mbox->deleted++;
  196. cur->flags |= (tmp->flags & ~(D_RECENT | D_DRAFT));
  197. /* don't bother renaming the file if we are just going to
  198. * remove it later.
  199. */
  200. if ((cur->flags & D_DELETED) == 0 || (flags & SYNC_EXPUNGE) == 0)
  201. {
  202. /* generate old path */
  203. snprintf (path, sizeof (path), "%s/%s/%s",
  204. mbox->path, cur->new ? "new" : "cur", cur->file);
  205. /* truncate old flags (if present) */
  206. p = strchr (cur->file, ':');
  207. if (p)
  208. *p = 0;
  209. /* generate new path - always put this in the cur/ directory
  210. * because its no longer new
  211. */
  212. snprintf (newpath, sizeof (newpath), "%s/cur/%s:2,%s%s%s%s",
  213. mbox->path,
  214. cur->file, (cur->flags & D_FLAGGED) ? "F" : "",
  215. (cur->flags & D_ANSWERED) ? "R" : "",
  216. (cur->flags & D_SEEN) ? "S" : "",
  217. (cur->flags & D_DELETED) ? "T" : "");
  218. if (rename (path, newpath))
  219. {
  220. perror ("rename");
  221. return -1;
  222. }
  223. else
  224. {
  225. /* update the filename in the msg struct */
  226. p = strrchr (newpath, '/');
  227. free (cur->file);
  228. cur->file = strdup (p + 1);
  229. cur->new = 0; /* not any more */
  230. }
  231. }
  232. }
  233. }
  234. if (upload)
  235. fprintf (stdout, " %d messages.\n", upload);
  236. if ((flags & SYNC_QUIET) == 0)
  237. {
  238. fputs ("Fetching new messages", stdout);
  239. fflush (stdout);
  240. }
  241. if (max_msgs == 0)
  242. max_msgs = UINT_MAX;
  243. else
  244. {
  245. /* expire messages in excess of the max-count for this mailbox.
  246. * flagged mails are considered sacrosant and not deleted.
  247. * we have already done the upload to the server, so messing with
  248. * the flags variable do not have remote side effects.
  249. */
  250. for (cur = imap->msgs, msg_count = 0;
  251. cur && msg_count < max_msgs; cur = cur->next, msg_count++)
  252. {
  253. tmp = find_msg (mbox->msgs, cur->uid);
  254. if (tmp)
  255. tmp->wanted = 1;
  256. }
  257. for (cur = mbox->msgs; cur; cur = cur->next)
  258. {
  259. if (!cur->wanted && !(cur->flags & D_FLAGGED))
  260. {
  261. cur->flags |= D_DELETED;
  262. cur->dead = 1;
  263. mbox->deleted++;
  264. }
  265. }
  266. }
  267. for (cur = imap->msgs, msg_count = 0;
  268. cur && msg_count < max_msgs; cur = cur->next, msg_count++)
  269. {
  270. if (!cur->processed)
  271. {
  272. /* new message on server */
  273. if ((flags & SYNC_EXPUNGE) && (cur->flags & D_DELETED))
  274. {
  275. /* this message has been marked for deletion and
  276. * we are currently expunging a mailbox. don't
  277. * bother downloading this message
  278. */
  279. continue;
  280. }
  281. if (max_size && cur->size > max_size)
  282. {
  283. if ((flags & SYNC_QUIET) == 0)
  284. printf
  285. ("Warning, message skipped because it is too big (%u)\n",
  286. cur->size);
  287. continue;
  288. }
  289. /* construct the flags part of the file name. */
  290. *suffix = 0;
  291. if (cur->flags & ~D_RECENT)
  292. {
  293. snprintf (suffix, sizeof (suffix), ":2,%s%s%s%s",
  294. (cur->flags & D_FLAGGED) ? "F" : "",
  295. (cur->flags & D_ANSWERED) ? "R" : "",
  296. (cur->flags & D_SEEN) ? "S" : "",
  297. (cur->flags & D_DELETED) ? "T" : "");
  298. }
  299. for (;;)
  300. {
  301. /* create new file */
  302. snprintf (path, sizeof (path), "%s/tmp/%ld_%d.%d.%s%s",
  303. mbox->path, time (0), MaildirCount++, getpid (),
  304. Hostname, suffix);
  305. if ((fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600)) > 0)
  306. break;
  307. if (errno != EEXIST)
  308. {
  309. perror (path);
  310. break;
  311. }
  312. sleep (2);
  313. }
  314. if (fd < 0)
  315. continue;
  316. if ((flags & SYNC_QUIET) == 0)
  317. {
  318. /* give some visual feedback that something is happening */
  319. fputs (".", stdout);
  320. fflush (stdout);
  321. }
  322. fetched++;
  323. ret = imap_fetch_message (imap, cur->uid, fd);
  324. if (fsync (fd))
  325. {
  326. perror ("fsync");
  327. close (fd);
  328. }
  329. else if (close (fd))
  330. perror ("close");
  331. else if (!ret)
  332. {
  333. p = strrchr (path, '/');
  334. snprintf (newpath, sizeof (newpath), "%s/%s%s", mbox->path,
  335. (cur->flags & ~D_RECENT) ? "cur" : "new", p);
  336. /* its ok if this fails, the next time we sync the message
  337. * will get pulled down
  338. */
  339. if (link (path, newpath))
  340. perror ("link");
  341. else
  342. {
  343. /* update the db with the UID mapping for this file */
  344. set_uid (mbox->db, p + 1, cur->uid);
  345. }
  346. }
  347. /* always remove the temp file */
  348. unlink (path);
  349. }
  350. }
  351. if ((flags & SYNC_QUIET) == 0)
  352. printf (" %d messages\n", fetched);
  353. return 0;
  354. }