sync.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "isync.h"
  28. static unsigned int MaildirCount = 0;
  29. static message_t *
  30. find_msg (message_t * list, unsigned int uid)
  31. {
  32. for (; list; list = list->next)
  33. if (list->uid == uid)
  34. return list;
  35. return 0;
  36. }
  37. int
  38. sync_mailbox (mailbox_t * mbox, imap_t * imap, int flags)
  39. {
  40. message_t *cur;
  41. message_t *tmp;
  42. char path[_POSIX_PATH_MAX];
  43. char newpath[_POSIX_PATH_MAX];
  44. char *p;
  45. int fd;
  46. int ret;
  47. for (cur = mbox->msgs; cur; cur = cur->next)
  48. {
  49. tmp = find_msg (imap->msgs, cur->uid);
  50. if (!tmp)
  51. {
  52. printf ("warning, uid %d doesn't exist on server\n", cur->uid);
  53. if (flags & SYNC_DELETE)
  54. {
  55. cur->flags |= D_DELETED;
  56. cur->dead = 1;
  57. mbox->deleted++;
  58. }
  59. continue;
  60. }
  61. tmp->processed = 1;
  62. if (!(flags & SYNC_FAST))
  63. {
  64. /* check if local flags are different from server flags.
  65. * ignore \Recent and \Draft
  66. */
  67. if (cur->flags != (tmp->flags & ~(D_RECENT | D_DRAFT)))
  68. {
  69. /* set local flags that don't exist on the server */
  70. if (!(tmp->flags & D_DELETED) && (cur->flags & D_DELETED))
  71. imap->deleted++;
  72. imap_set_flags (imap, cur->uid, cur->flags & ~tmp->flags);
  73. /* update local flags */
  74. if((cur->flags & D_DELETED) == 0 && (tmp->flags & D_DELETED))
  75. mbox->deleted++;
  76. cur->flags |= (tmp->flags & ~(D_RECENT | D_DRAFT));
  77. cur->changed = 1;
  78. mbox->changed = 1;
  79. }
  80. }
  81. }
  82. fputs ("Fetching new messages", stdout);
  83. fflush (stdout);
  84. for (cur = imap->msgs; cur; cur = cur->next)
  85. {
  86. if (!cur->processed)
  87. {
  88. /* new message on server */
  89. if ((flags & SYNC_EXPUNGE) && (cur->flags & D_DELETED))
  90. {
  91. /* this message has been marked for deletion and
  92. * we are currently expunging a mailbox. don't
  93. * bother downloading this message
  94. */
  95. continue;
  96. }
  97. /* create new file */
  98. snprintf (path, sizeof (path), "%s/tmp/%s.%ld_%d.%d.UID%d",
  99. mbox->path, Hostname, time (0), MaildirCount++,
  100. getpid (), cur->uid);
  101. if (cur->flags)
  102. {
  103. /* append flags */
  104. snprintf (path + strlen (path), sizeof (path) - strlen (path),
  105. ":2,%s%s%s%s",
  106. (cur->flags & D_FLAGGED) ? "F" : "",
  107. (cur->flags & D_ANSWERED) ? "R" : "",
  108. (cur->flags & D_SEEN) ? "S" : "",
  109. (cur->flags & D_DELETED) ? "T" : "");
  110. }
  111. /* give some visual feedback that something is happening */
  112. fputs (".", stdout);
  113. fflush (stdout);
  114. // printf("creating %s\n", path);
  115. fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600);
  116. if (fd < 0)
  117. {
  118. perror ("open");
  119. continue;
  120. }
  121. ret = imap_fetch_message (imap, cur->uid, fd);
  122. close (fd);
  123. if (!ret)
  124. {
  125. p = strrchr (path, '/');
  126. snprintf (newpath, sizeof (newpath), "%s/%s%s", mbox->path,
  127. (cur->flags & D_SEEN) ? "cur" : "new", p);
  128. // printf ("moving %s to %s\n", path, newpath);
  129. if (rename (path, newpath))
  130. perror ("rename");
  131. }
  132. else
  133. unlink(path);
  134. }
  135. }
  136. puts ("");
  137. return 0;
  138. }