sync.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. static 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. {
  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. for (cur = mbox->msgs; cur; cur = cur->next)
  67. {
  68. tmp = find_msg (imap->msgs, cur->uid);
  69. if (!tmp)
  70. {
  71. printf ("warning, uid %d doesn't exist on server\n", cur->uid);
  72. if (flags & SYNC_DELETE)
  73. {
  74. cur->flags |= D_DELETED;
  75. cur->dead = 1;
  76. mbox->deleted++;
  77. }
  78. continue;
  79. }
  80. tmp->processed = 1;
  81. if (!(flags & SYNC_FAST))
  82. {
  83. /* check if local flags are different from server flags.
  84. * ignore \Recent and \Draft
  85. */
  86. if (cur->flags != (tmp->flags & ~(D_RECENT | D_DRAFT)))
  87. {
  88. /* set local flags that don't exist on the server */
  89. if (!(tmp->flags & D_DELETED) && (cur->flags & D_DELETED))
  90. imap->deleted++;
  91. imap_set_flags (imap, cur->uid, cur->flags & ~tmp->flags);
  92. /* update local flags */
  93. if((cur->flags & D_DELETED) == 0 && (tmp->flags & D_DELETED))
  94. mbox->deleted++;
  95. cur->flags |= (tmp->flags & ~(D_RECENT | D_DRAFT));
  96. cur->changed = 1;
  97. mbox->changed = 1;
  98. }
  99. }
  100. }
  101. fputs ("Fetching new messages", stdout);
  102. fflush (stdout);
  103. for (cur = imap->msgs; cur; cur = cur->next)
  104. {
  105. if (!cur->processed)
  106. {
  107. /* new message on server */
  108. if ((flags & SYNC_EXPUNGE) && (cur->flags & D_DELETED))
  109. {
  110. /* this message has been marked for deletion and
  111. * we are currently expunging a mailbox. don't
  112. * bother downloading this message
  113. */
  114. continue;
  115. }
  116. for (;;)
  117. {
  118. /* create new file */
  119. snprintf (path, sizeof (path), "%s/tmp/%s.%ld_%d.%d.UID%d",
  120. mbox->path, Hostname, time (0), MaildirCount++,
  121. getpid (), cur->uid);
  122. if (stat (path, &sb))
  123. {
  124. if (errno == ENOENT)
  125. break;
  126. }
  127. sleep (2);
  128. }
  129. if (cur->flags)
  130. {
  131. /* append flags */
  132. snprintf (path + strlen (path), sizeof (path) - strlen (path),
  133. ":2,%s%s%s%s",
  134. (cur->flags & D_FLAGGED) ? "F" : "",
  135. (cur->flags & D_ANSWERED) ? "R" : "",
  136. (cur->flags & D_SEEN) ? "S" : "",
  137. (cur->flags & D_DELETED) ? "T" : "");
  138. }
  139. /* give some visual feedback that something is happening */
  140. fputs (".", stdout);
  141. fflush (stdout);
  142. // printf("creating %s\n", path);
  143. fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600);
  144. if (fd < 0)
  145. {
  146. perror ("open");
  147. continue;
  148. }
  149. ret = imap_fetch_message (imap, cur->uid, fd);
  150. if (close (fd))
  151. perror ("close");
  152. else if (!ret)
  153. {
  154. p = strrchr (path, '/');
  155. snprintf (newpath, sizeof (newpath), "%s/%s%s", mbox->path,
  156. (cur->flags & D_SEEN) ? "cur" : "new", p);
  157. // printf ("moving %s to %s\n", path, newpath);
  158. /* its ok if this fails, the next time we sync the message
  159. * will get pulled down
  160. */
  161. if (link (path, newpath))
  162. perror ("link");
  163. }
  164. /* always remove the temp file */
  165. unlink (path);
  166. }
  167. }
  168. puts ("");
  169. return 0;
  170. }