sync.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. /* don't bother renaming the file if we are just going to
  186. * remove it later.
  187. */
  188. if ((cur->flags & D_DELETED) == 0 || (flags & SYNC_EXPUNGE) == 0)
  189. {
  190. /* generate old path */
  191. snprintf (path, sizeof (path), "%s/%s/%s",
  192. mbox->path, cur->new ? "new" : "cur", cur->file);
  193. /* truncate old flags (if present) */
  194. p = strchr (cur->file, ':');
  195. if (p)
  196. *p = 0;
  197. /* generate new path - always put this in the cur/ directory
  198. * because its no longer new
  199. */
  200. snprintf (newpath, sizeof (newpath), "%s/cur/%s:2,%s%s%s%s",
  201. mbox->path,
  202. cur->file, (cur->flags & D_FLAGGED) ? "F" : "",
  203. (cur->flags & D_ANSWERED) ? "R" : "",
  204. (cur->flags & D_SEEN) ? "S" : "",
  205. (cur->flags & D_DELETED) ? "T" : "");
  206. if (rename (path, newpath))
  207. {
  208. perror ("rename");
  209. return -1;
  210. }
  211. else
  212. {
  213. /* update the filename in the msg struct */
  214. p = strrchr (newpath, '/');
  215. free (cur->file);
  216. cur->file = strdup (p + 1);
  217. cur->new = 0; /* not any more */
  218. }
  219. }
  220. }
  221. }
  222. if (upload)
  223. fprintf (stdout, " %d messages.\n", upload);
  224. if ((flags & SYNC_QUIET) == 0)
  225. {
  226. fputs ("Fetching new messages", stdout);
  227. fflush (stdout);
  228. }
  229. if (max_msgs == 0)
  230. max_msgs = UINT_MAX;
  231. else
  232. {
  233. /* expire messages in excess of the max-count for this mailbox.
  234. * flagged mails are considered sacrosant and not deleted.
  235. * we have already done the upload to the server, so messing with
  236. * the flags variable do not have remote side effects.
  237. */
  238. for (cur = imap->msgs, msg_count = 0;
  239. cur && msg_count < max_msgs; cur = cur->next, msg_count++)
  240. {
  241. tmp = find_msg (mbox->msgs, cur->uid);
  242. if (tmp)
  243. tmp->wanted = 1;
  244. }
  245. for (cur = mbox->msgs; cur; cur = cur->next)
  246. {
  247. if (!cur->wanted && !(cur->flags & D_FLAGGED))
  248. {
  249. cur->flags |= D_DELETED;
  250. cur->dead = 1;
  251. mbox->deleted++;
  252. }
  253. }
  254. }
  255. for (cur = imap->msgs, msg_count = 0;
  256. cur && msg_count < max_msgs; cur = cur->next, msg_count++)
  257. {
  258. if (!cur->processed)
  259. {
  260. /* new message on server */
  261. if ((flags & SYNC_EXPUNGE) && (cur->flags & D_DELETED))
  262. {
  263. /* this message has been marked for deletion and
  264. * we are currently expunging a mailbox. don't
  265. * bother downloading this message
  266. */
  267. continue;
  268. }
  269. if (max_size && cur->size > max_size)
  270. {
  271. if ((flags & SYNC_QUIET) == 0)
  272. printf
  273. ("Warning, message skipped because it is too big (%u)\n",
  274. cur->size);
  275. continue;
  276. }
  277. /* construct the flags part of the file name. */
  278. *suffix = 0;
  279. if (cur->flags & ~D_RECENT)
  280. {
  281. snprintf (suffix, sizeof (suffix), ":2,%s%s%s%s",
  282. (cur->flags & D_FLAGGED) ? "F" : "",
  283. (cur->flags & D_ANSWERED) ? "R" : "",
  284. (cur->flags & D_SEEN) ? "S" : "",
  285. (cur->flags & D_DELETED) ? "T" : "");
  286. }
  287. for (;;)
  288. {
  289. /* create new file */
  290. snprintf (path, sizeof (path), "%s/tmp/%ld_%d.%d.%s%s",
  291. mbox->path, time (0), MaildirCount++, getpid (),
  292. Hostname, suffix);
  293. if ((fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600)) > 0)
  294. break;
  295. if (errno != EEXIST)
  296. {
  297. perror (path);
  298. break;
  299. }
  300. sleep (2);
  301. }
  302. if (fd < 0)
  303. continue;
  304. if ((flags & SYNC_QUIET) == 0)
  305. {
  306. /* give some visual feedback that something is happening */
  307. fputs (".", stdout);
  308. fflush (stdout);
  309. }
  310. fetched++;
  311. ret = imap_fetch_message (imap, cur->uid, fd);
  312. if (fsync (fd))
  313. {
  314. perror ("fsync");
  315. close (fd);
  316. }
  317. else if (close (fd))
  318. perror ("close");
  319. else if (!ret)
  320. {
  321. p = strrchr (path, '/');
  322. snprintf (newpath, sizeof (newpath), "%s/%s%s", mbox->path,
  323. (cur->flags & ~D_RECENT) ? "cur" : "new", p);
  324. /* its ok if this fails, the next time we sync the message
  325. * will get pulled down
  326. */
  327. if (link (path, newpath))
  328. perror ("link");
  329. else
  330. {
  331. /* update the db with the UID mapping for this file */
  332. set_uid (mbox->db, p + 1, cur->uid);
  333. }
  334. }
  335. /* always remove the temp file */
  336. unlink (path);
  337. }
  338. }
  339. if ((flags & SYNC_QUIET) == 0)
  340. printf (" %d messages\n", fetched);
  341. return 0;
  342. }