sync.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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->uidvalidity > 0)
  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 ((flags & SYNC_QUIET) == 0)
  104. {
  105. if (!upload)
  106. fputs ("Uploading messages", stdout);
  107. fputc ('.', stdout);
  108. fflush (stdout);
  109. upload++;
  110. }
  111. /* upload the message if its not too big */
  112. snprintf (path, sizeof (path), "%s/%s/%s", mbox->path,
  113. cur->new ? "new" : "cur", cur->file);
  114. if (stat (path, &sb))
  115. {
  116. perror (path);
  117. continue; /* not fatal */
  118. }
  119. if (imap->box->max_size > 0
  120. && sb.st_size > imap->box->max_size)
  121. {
  122. if ((flags & SYNC_QUIET) == 0)
  123. printf
  124. ("Warning, local message is too large (%lu), skipping...\n",
  125. (unsigned long) sb.st_size);
  126. continue;
  127. }
  128. fd = open (path, O_RDONLY);
  129. if (fd == -1)
  130. {
  131. printf ("Error, unable to open %s: %s (errno %d)\n",
  132. path, strerror (errno), errno);
  133. continue;
  134. }
  135. cur->size = sb.st_size;
  136. cur->uid = imap_append_message (imap, fd, cur);
  137. /* if the server gave us back a uid, update the db */
  138. if (cur->uid != (unsigned int) -1)
  139. set_uid (mbox->db, cur->file, cur->uid);
  140. close (fd);
  141. }
  142. else if (flags & SYNC_DELETE)
  143. {
  144. cur->flags |= D_DELETED;
  145. cur->dead = 1;
  146. mbox->deleted++;
  147. }
  148. /* if the user doesn't want local msgs deleted when they don't
  149. * exist on the server, warn that such messages exist.
  150. */
  151. else if ((flags & SYNC_QUIET) == 0)
  152. printf ("Warning, uid %u doesn't exist on server\n",
  153. cur->uid);
  154. continue;
  155. }
  156. tmp->processed = 1;
  157. /* if the message is deleted, and CopyDeletedTo is set, and we
  158. * are expunging, make a copy of the message now.
  159. */
  160. if (((cur->flags | tmp->flags) & D_DELETED) != 0 &&
  161. (flags & SYNC_EXPUNGE) && imap->box->copy_deleted_to)
  162. {
  163. if (imap_copy_message (imap, cur->uid,
  164. imap->box->copy_deleted_to))
  165. {
  166. fprintf (stderr,
  167. "ERROR: unable to copy deleted message to \"%s\"\n",
  168. imap->box->copy_deleted_to);
  169. return -1;
  170. }
  171. }
  172. /* check if local flags are different from server flags.
  173. * ignore \Recent and \Draft
  174. */
  175. if (cur->flags != (tmp->flags & ~(D_RECENT | D_DRAFT)))
  176. {
  177. /* set local flags that don't exist on the server */
  178. if (!(tmp->flags & D_DELETED) && (cur->flags & D_DELETED))
  179. imap->deleted++;
  180. imap_set_flags (imap, cur->uid, cur->flags & ~tmp->flags);
  181. /* update local flags */
  182. if ((cur->flags & D_DELETED) == 0 && (tmp->flags & D_DELETED))
  183. mbox->deleted++;
  184. cur->flags |= (tmp->flags & ~(D_RECENT | D_DRAFT));
  185. /* generate old path */
  186. snprintf (path, sizeof (path), "%s/%s/%s",
  187. mbox->path, cur->new ? "new" : "cur", cur->file);
  188. /* truncate old flags (if present) */
  189. p = strchr (cur->file, ':');
  190. if (p)
  191. *p = 0;
  192. /* generate new path - always put this in the cur/ directory
  193. * because its no longer new
  194. */
  195. snprintf (newpath, sizeof (newpath), "%s/cur/%s:2,%s%s%s%s",
  196. mbox->path,
  197. cur->file, (cur->flags & D_FLAGGED) ? "F" : "",
  198. (cur->flags & D_ANSWERED) ? "R" : "",
  199. (cur->flags & D_SEEN) ? "S" : "",
  200. (cur->flags & D_DELETED) ? "T" : "");
  201. if (rename (path, newpath))
  202. {
  203. perror ("rename");
  204. return -1;
  205. }
  206. else
  207. {
  208. /* update the filename in the msg struct */
  209. p = strrchr (newpath, '/');
  210. free (cur->file);
  211. cur->file = strdup (p + 1);
  212. }
  213. }
  214. }
  215. if (upload)
  216. fprintf (stdout, " %d messages.\n", upload);
  217. if ((flags & SYNC_QUIET) == 0)
  218. {
  219. fputs ("Fetching new messages", stdout);
  220. fflush (stdout);
  221. }
  222. if (max_msgs == 0)
  223. max_msgs = UINT_MAX;
  224. else
  225. {
  226. /* expire messages in excess of the max-count for this mailbox.
  227. * flagged mails are considered sacrosant and not deleted.
  228. * we have already done the upload to the server, so messing with
  229. * the flags variable do not have remote side effects.
  230. */
  231. for (cur = imap->msgs, msg_count = 0;
  232. cur && msg_count < max_msgs; cur = cur->next, msg_count++)
  233. {
  234. tmp = find_msg (mbox->msgs, cur->uid);
  235. if (tmp)
  236. tmp->wanted = 1;
  237. }
  238. for (cur = mbox->msgs; cur; cur = cur->next)
  239. {
  240. if (!cur->wanted && !(cur->flags & D_FLAGGED))
  241. {
  242. cur->flags |= D_DELETED;
  243. cur->dead = 1;
  244. mbox->deleted++;
  245. }
  246. }
  247. }
  248. for (cur = imap->msgs, msg_count = 0;
  249. cur && msg_count < max_msgs; cur = cur->next, msg_count++)
  250. {
  251. if (!cur->processed)
  252. {
  253. /* new message on server */
  254. if ((flags & SYNC_EXPUNGE) && (cur->flags & D_DELETED))
  255. {
  256. /* this message has been marked for deletion and
  257. * we are currently expunging a mailbox. don't
  258. * bother downloading this message
  259. */
  260. continue;
  261. }
  262. if (max_size && cur->size > max_size)
  263. {
  264. if ((flags & SYNC_QUIET) == 0)
  265. printf
  266. ("Warning, message skipped because it is too big (%u)\n",
  267. cur->size);
  268. continue;
  269. }
  270. /* construct the flags part of the file name. */
  271. *suffix = 0;
  272. if (cur->flags & ~D_RECENT)
  273. {
  274. snprintf (suffix, sizeof (suffix), ":2,%s%s%s%s",
  275. (cur->flags & D_FLAGGED) ? "F" : "",
  276. (cur->flags & D_ANSWERED) ? "R" : "",
  277. (cur->flags & D_SEEN) ? "S" : "",
  278. (cur->flags & D_DELETED) ? "T" : "");
  279. }
  280. for (;;)
  281. {
  282. /* create new file */
  283. snprintf (path, sizeof (path), "%s/tmp/%ld_%d.%d.%s%s",
  284. mbox->path, time (0), MaildirCount++, getpid (),
  285. Hostname, suffix);
  286. if ((fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600)) > 0)
  287. break;
  288. if (errno != EEXIST)
  289. {
  290. perror (path);
  291. break;
  292. }
  293. sleep (2);
  294. }
  295. if (fd < 0)
  296. continue;
  297. if ((flags & SYNC_QUIET) == 0)
  298. {
  299. /* give some visual feedback that something is happening */
  300. fputs (".", stdout);
  301. fflush (stdout);
  302. }
  303. fetched++;
  304. ret = imap_fetch_message (imap, cur->uid, fd);
  305. if (fsync (fd))
  306. {
  307. perror ("fsync");
  308. close (fd);
  309. }
  310. else if (close (fd))
  311. perror ("close");
  312. else if (!ret)
  313. {
  314. p = strrchr (path, '/');
  315. snprintf (newpath, sizeof (newpath), "%s/%s%s", mbox->path,
  316. (cur->flags & ~D_RECENT) ? "cur" : "new", p);
  317. /* its ok if this fails, the next time we sync the message
  318. * will get pulled down
  319. */
  320. if (link (path, newpath))
  321. perror ("link");
  322. else
  323. {
  324. /* update the db with the UID mapping for this file */
  325. set_uid (mbox->db, p + 1, cur->uid);
  326. }
  327. }
  328. /* always remove the temp file */
  329. unlink (path);
  330. }
  331. }
  332. if ((flags & SYNC_QUIET) == 0)
  333. printf (" %d messages\n", fetched);
  334. return 0;
  335. }