sync.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* isync - IMAP4 to maildir mailbox synchronizer
  2. * Copyright (C) 2000 Michael R. Elkins <me@mutt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <stdio.h>
  19. #include <limits.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <time.h>
  23. #include <fcntl.h>
  24. #include <string.h>
  25. #include "isync.h"
  26. static unsigned int MaildirCount = 0;
  27. static message_t *
  28. find_msg (message_t * list, unsigned int uid)
  29. {
  30. for (; list; list = list->next)
  31. if (list->uid == uid)
  32. return list;
  33. return 0;
  34. }
  35. int
  36. sync_mailbox (mailbox_t * mbox, imap_t * imap, int flags)
  37. {
  38. message_t *cur;
  39. message_t *tmp;
  40. char path[_POSIX_PATH_MAX];
  41. char newpath[_POSIX_PATH_MAX];
  42. char *p;
  43. int fd;
  44. for (cur = mbox->msgs; cur; cur = cur->next)
  45. {
  46. tmp = find_msg (imap->msgs, cur->uid);
  47. if (!tmp)
  48. {
  49. printf ("warning, uid %d doesn't exist on server\n", cur->uid);
  50. if (flags & SYNC_DELETE)
  51. {
  52. cur->flags |= D_DELETED;
  53. cur->dead = 1;
  54. }
  55. continue;
  56. }
  57. tmp->processed = 1;
  58. if (!(flags & SYNC_FAST))
  59. {
  60. /* check if local flags are different from server flags.
  61. * ignore \Recent and \Draft
  62. */
  63. if (cur->flags != (tmp->flags & ~(D_RECENT | D_DRAFT)))
  64. {
  65. /* set local flags that don't exist on the server */
  66. imap_set_flags (imap, cur->uid, cur->flags & ~tmp->flags);
  67. /* update local flags */
  68. cur->flags |= (tmp->flags & ~(D_RECENT | D_DRAFT));
  69. cur->changed = 1;
  70. mbox->changed = 1;
  71. }
  72. }
  73. }
  74. fputs ("Fetching new messages", stdout);
  75. fflush (stdout);
  76. for (cur = imap->msgs; cur; cur = cur->next)
  77. {
  78. if (!cur->processed)
  79. {
  80. /* new message on server */
  81. fputs (".", stdout);
  82. fflush (stdout);
  83. /* create new file */
  84. snprintf (path, sizeof (path), "%s/tmp/%s.%ld_%d.%d.UID%d",
  85. mbox->path, Hostname, time (0), MaildirCount++,
  86. getpid (), cur->uid);
  87. if (cur->flags)
  88. {
  89. /* append flags */
  90. snprintf (path + strlen (path), sizeof (path) - strlen (path),
  91. ":2,%s%s%s%s",
  92. (cur->flags & D_FLAGGED) ? "F" : "",
  93. (cur->flags & D_ANSWERED) ? "R" : "",
  94. (cur->flags & D_SEEN) ? "S" : "",
  95. (cur->flags & D_DELETED) ? "T" : "");
  96. }
  97. // printf("creating %s\n", path);
  98. fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600);
  99. if (fd < 0)
  100. {
  101. perror ("open");
  102. continue;
  103. }
  104. imap_fetch_message (imap, cur->uid, fd);
  105. close (fd);
  106. p = strrchr (path, '/');
  107. snprintf (newpath, sizeof (newpath), "%s/%s%s", mbox->path,
  108. (cur->flags & D_SEEN) ? "cur" : "new", p);
  109. // printf ("moving %s to %s\n", path, newpath);
  110. if (rename (path, newpath))
  111. perror ("rename");
  112. }
  113. }
  114. puts ("");
  115. return 0;
  116. }