sync.c 4.2 KB

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