sync.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <sys/stat.h>
  28. #include <errno.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, unsigned int max_size)
  41. {
  42. message_t *cur;
  43. message_t *tmp;
  44. char path[_POSIX_PATH_MAX];
  45. char newpath[_POSIX_PATH_MAX];
  46. char *p;
  47. int fd;
  48. int ret;
  49. struct stat sb;
  50. if (mbox->uidvalidity != (unsigned int) -1)
  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 == (unsigned int) -1 || 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. for (;;)
  128. {
  129. /* create new file */
  130. snprintf (path, sizeof (path), "%s/tmp/%s.%ld_%d.%d.UID%d",
  131. mbox->path, Hostname, time (0), MaildirCount++,
  132. getpid (), cur->uid);
  133. if (stat (path, &sb))
  134. {
  135. if (errno == ENOENT)
  136. break;
  137. }
  138. sleep (2);
  139. }
  140. if (cur->flags)
  141. {
  142. /* append flags */
  143. snprintf (path + strlen (path), sizeof (path) - strlen (path),
  144. ":2,%s%s%s%s",
  145. (cur->flags & D_FLAGGED) ? "F" : "",
  146. (cur->flags & D_ANSWERED) ? "R" : "",
  147. (cur->flags & D_SEEN) ? "S" : "",
  148. (cur->flags & D_DELETED) ? "T" : "");
  149. }
  150. /* give some visual feedback that something is happening */
  151. fputs (".", stdout);
  152. fflush (stdout);
  153. // printf("creating %s\n", path);
  154. fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600);
  155. if (fd < 0)
  156. {
  157. perror ("open");
  158. continue;
  159. }
  160. ret = imap_fetch_message (imap, cur->uid, fd);
  161. if (close (fd))
  162. perror ("close");
  163. else if (!ret)
  164. {
  165. p = strrchr (path, '/');
  166. snprintf (newpath, sizeof (newpath), "%s/%s%s", mbox->path,
  167. (cur->flags & D_SEEN) ? "cur" : "new", p);
  168. // printf ("moving %s to %s\n", path, newpath);
  169. /* its ok if this fails, the next time we sync the message
  170. * will get pulled down
  171. */
  172. if (link (path, newpath))
  173. perror ("link");
  174. }
  175. /* always remove the temp file */
  176. unlink (path);
  177. }
  178. }
  179. puts ("");
  180. return 0;
  181. }