sync.c 9.0 KB

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