sync.c 3.8 KB

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