sync.c 8.5 KB

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