sync.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000 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 "isync.h"
  29. static unsigned int MaildirCount = 0;
  30. message_t *
  31. find_msg (message_t * list, unsigned int uid)
  32. {
  33. for (; list; list = list->next)
  34. if (list->uid == uid)
  35. return list;
  36. return 0;
  37. }
  38. int
  39. sync_mailbox (mailbox_t * mbox, imap_t * imap, int flags, unsigned int max_size)
  40. {
  41. message_t *cur;
  42. message_t *tmp;
  43. char path[_POSIX_PATH_MAX];
  44. char newpath[_POSIX_PATH_MAX];
  45. char suffix[_POSIX_PATH_MAX];
  46. char *p;
  47. int fd;
  48. int ret;
  49. int fetched = 0;
  50. if (mbox->uidvalidity > 0)
  51. {
  52. if (mbox->uidvalidity != imap->uidvalidity)
  53. {
  54. /* if the UIDVALIDITY value has changed, it means all our
  55. * local UIDs are invalid, so we can't sync.
  56. */
  57. puts ("Error, UIDVALIDITY changed on server (fatal)");
  58. return -1;
  59. }
  60. }
  61. else if (maildir_set_uidvalidity (mbox, imap->uidvalidity))
  62. {
  63. puts ("Error, unable to store UIDVALIDITY");
  64. return -1;
  65. }
  66. if (mbox->maxuid == 0 || imap->maxuid > mbox->maxuid)
  67. {
  68. mbox->maxuid = imap->maxuid;
  69. mbox->maxuidchanged = 1;
  70. }
  71. /* if we are --fast mode, the mailbox wont have been loaded, so
  72. * this next step is skipped.
  73. */
  74. for (cur = mbox->msgs; cur; cur = cur->next)
  75. {
  76. tmp = find_msg (imap->msgs, cur->uid);
  77. if (!tmp)
  78. {
  79. printf ("Warning, uid %d doesn't exist on server\n", cur->uid);
  80. if (flags & SYNC_DELETE)
  81. {
  82. cur->flags |= D_DELETED;
  83. cur->dead = 1;
  84. mbox->deleted++;
  85. }
  86. continue;
  87. }
  88. tmp->processed = 1;
  89. /* check if local flags are different from server flags.
  90. * ignore \Recent and \Draft
  91. */
  92. if (cur->flags != (tmp->flags & ~(D_RECENT | D_DRAFT)))
  93. {
  94. /* set local flags that don't exist on the server */
  95. if (!(tmp->flags & D_DELETED) && (cur->flags & D_DELETED))
  96. imap->deleted++;
  97. imap_set_flags (imap, cur->uid, cur->flags & ~tmp->flags);
  98. /* update local flags */
  99. if((cur->flags & D_DELETED) == 0 && (tmp->flags & D_DELETED))
  100. mbox->deleted++;
  101. cur->flags |= (tmp->flags & ~(D_RECENT | D_DRAFT));
  102. cur->changed = 1;
  103. mbox->changed = 1;
  104. }
  105. }
  106. fputs ("Fetching new messages", stdout);
  107. fflush (stdout);
  108. for (cur = imap->msgs; cur; cur = cur->next)
  109. {
  110. if (!cur->processed)
  111. {
  112. /* new message on server */
  113. if ((flags & SYNC_EXPUNGE) && (cur->flags & D_DELETED))
  114. {
  115. /* this message has been marked for deletion and
  116. * we are currently expunging a mailbox. don't
  117. * bother downloading this message
  118. */
  119. continue;
  120. }
  121. if (max_size && cur->size > max_size)
  122. {
  123. printf ("Warning, message skipped because it is too big (%u)\n",
  124. cur->size);
  125. continue;
  126. }
  127. /* construct the flags part of the file name. */
  128. *suffix = 0;
  129. if (cur->flags)
  130. {
  131. snprintf (suffix, sizeof (suffix), ":2,%s%s%s%s",
  132. (cur->flags & D_FLAGGED) ? "F" : "",
  133. (cur->flags & D_ANSWERED) ? "R" : "",
  134. (cur->flags & D_SEEN) ? "S" : "",
  135. (cur->flags & D_DELETED) ? "T" : "");
  136. }
  137. for (;;)
  138. {
  139. /* create new file */
  140. snprintf (path, sizeof (path), "%s/tmp/%s.%ld_%d.%d.UID%d%s",
  141. mbox->path, Hostname, time (0), MaildirCount++,
  142. getpid (), cur->uid, suffix);
  143. if ((fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600)) > 0)
  144. break;
  145. if (errno != EEXIST)
  146. {
  147. perror ("open");
  148. break;
  149. }
  150. sleep (2);
  151. }
  152. if (fd < 0)
  153. continue;
  154. /* give some visual feedback that something is happening */
  155. fputs (".", stdout);
  156. fflush (stdout);
  157. fetched++;
  158. ret = imap_fetch_message (imap, cur->uid, fd);
  159. if (close (fd))
  160. perror ("close");
  161. else if (!ret)
  162. {
  163. p = strrchr (path, '/');
  164. snprintf (newpath, sizeof (newpath), "%s/%s%s", mbox->path,
  165. (cur->flags & D_SEEN) ? "cur" : "new", p);
  166. /* its ok if this fails, the next time we sync the message
  167. * will get pulled down
  168. */
  169. if (link (path, newpath))
  170. perror ("link");
  171. }
  172. /* always remove the temp file */
  173. unlink (path);
  174. }
  175. }
  176. printf (" %d messages\n", fetched);
  177. return 0;
  178. }