sync.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000-1 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 <errno.h>
  28. #include <sys/stat.h>
  29. #include "isync.h"
  30. static unsigned int MaildirCount = 0;
  31. 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. unsigned int max_size)
  42. {
  43. message_t *cur;
  44. message_t *tmp;
  45. char path[_POSIX_PATH_MAX];
  46. char newpath[_POSIX_PATH_MAX];
  47. char suffix[_POSIX_PATH_MAX];
  48. char *p;
  49. int fd;
  50. int ret;
  51. int fetched = 0;
  52. if (mbox->uidvalidity > 0)
  53. {
  54. if (mbox->uidvalidity != imap->uidvalidity)
  55. {
  56. /* if the UIDVALIDITY value has changed, it means all our
  57. * local UIDs are invalid, so we can't sync.
  58. */
  59. puts ("Error, UIDVALIDITY changed on server (fatal)");
  60. return -1;
  61. }
  62. }
  63. else if (maildir_set_uidvalidity (mbox, imap->uidvalidity))
  64. {
  65. puts ("Error, unable to store UIDVALIDITY");
  66. return -1;
  67. }
  68. if (mbox->maxuid == 0 || imap->maxuid > mbox->maxuid)
  69. {
  70. mbox->maxuid = imap->maxuid;
  71. mbox->maxuidchanged = 1;
  72. }
  73. /* if we are --fast mode, the mailbox wont have been loaded, so
  74. * this next step is skipped.
  75. */
  76. for (cur = mbox->msgs; cur; cur = cur->next)
  77. {
  78. tmp = find_msg (imap->msgs, cur->uid);
  79. if (!tmp)
  80. {
  81. /* if this message wasn't fetched from the server, attempt to
  82. * upload it
  83. */
  84. if (cur->uid == (unsigned int) -1)
  85. {
  86. struct stat sb;
  87. int fd;
  88. int uid;
  89. /* upload the message if its not too big */
  90. snprintf (path, sizeof (path), "%s/%s/%s", mbox->path,
  91. cur->new ? "new" : "cur", cur->file);
  92. if (stat (path, &sb))
  93. {
  94. printf ("Error, unable to stat %s: %s (errno %d)\n",
  95. path, strerror (errno), errno);
  96. continue; /* not fatal */
  97. }
  98. if (sb.st_size > imap->box->max_size)
  99. {
  100. printf
  101. ("Warning, local message is too large (%ld), skipping...\n",
  102. sb.st_size);
  103. continue;
  104. }
  105. fd = open (path, O_RDONLY);
  106. if (fd == -1)
  107. {
  108. printf ("Error, unable to open %s: %s (errno %d)\n",
  109. path, strerror (errno), errno);
  110. continue;
  111. }
  112. cur->size = sb.st_size;
  113. uid = imap_append_message (imap, fd, cur);
  114. close (fd);
  115. /* if the server gave us back a uid, rename the file so
  116. * we remember for next time
  117. */
  118. if (uid != -1)
  119. {
  120. char newpath[_POSIX_PATH_MAX];
  121. char *p;
  122. strfcpy (newpath, path, sizeof (newpath));
  123. /* kill :info field */
  124. p = strchr (newpath, ':');
  125. if (p)
  126. *p = 0;
  127. /* XXX not quite right, should really always put the
  128. * msg in "cur/", but i'm too tired right now.
  129. */
  130. snprintf (newpath + strlen (newpath),
  131. sizeof (newpath) - strlen (newpath),
  132. ",U=%d:2,%s%s%s%s", uid,
  133. (cur->flags & D_FLAGGED) ? "F" : "",
  134. (cur->flags & D_ANSWERED) ? "R" : "",
  135. (cur->flags & D_SEEN) ? "S" : "",
  136. (cur->flags & D_DELETED) ? "T" : "");
  137. if (rename (path, newpath))
  138. perror ("rename");
  139. }
  140. }
  141. else
  142. {
  143. printf ("Warning, uid %u doesn't exist on server\n",
  144. cur->uid);
  145. if (flags & SYNC_DELETE)
  146. {
  147. cur->flags |= D_DELETED;
  148. cur->dead = 1;
  149. mbox->deleted++;
  150. }
  151. }
  152. continue;
  153. }
  154. tmp->processed = 1;
  155. /* if the message is deleted, and CopyDeletedTo is set, and we
  156. * are expunging, make a copy of the message now.
  157. */
  158. if (((cur->flags | tmp->flags) & D_DELETED) != 0 &&
  159. (flags & SYNC_EXPUNGE) && imap->box->copy_deleted_to)
  160. {
  161. if (imap_copy_message (imap, cur->uid,
  162. imap->box->copy_deleted_to))
  163. {
  164. printf ("Error, unable to copy deleted message to \"%s\"\n",
  165. imap->box->copy_deleted_to);
  166. return -1;
  167. }
  168. }
  169. /* check if local flags are different from server flags.
  170. * ignore \Recent and \Draft
  171. */
  172. if (cur->flags != (tmp->flags & ~(D_RECENT | D_DRAFT)))
  173. {
  174. /* set local flags that don't exist on the server */
  175. if (!(tmp->flags & D_DELETED) && (cur->flags & D_DELETED))
  176. imap->deleted++;
  177. imap_set_flags (imap, cur->uid, cur->flags & ~tmp->flags);
  178. /* update local flags */
  179. if ((cur->flags & D_DELETED) == 0 && (tmp->flags & D_DELETED))
  180. mbox->deleted++;
  181. cur->flags |= (tmp->flags & ~(D_RECENT | D_DRAFT));
  182. cur->changed = 1;
  183. mbox->changed = 1;
  184. }
  185. }
  186. fputs ("Fetching new messages", stdout);
  187. fflush (stdout);
  188. for (cur = imap->msgs; cur; cur = cur->next)
  189. {
  190. if (!cur->processed)
  191. {
  192. /* new message on server */
  193. if ((flags & SYNC_EXPUNGE) && (cur->flags & D_DELETED))
  194. {
  195. /* this message has been marked for deletion and
  196. * we are currently expunging a mailbox. don't
  197. * bother downloading this message
  198. */
  199. continue;
  200. }
  201. if (max_size && cur->size > max_size)
  202. {
  203. printf
  204. ("Warning, message skipped because it is too big (%u)\n",
  205. cur->size);
  206. continue;
  207. }
  208. /* construct the flags part of the file name. */
  209. *suffix = 0;
  210. if (cur->flags)
  211. {
  212. snprintf (suffix, sizeof (suffix), ":2,%s%s%s%s",
  213. (cur->flags & D_FLAGGED) ? "F" : "",
  214. (cur->flags & D_ANSWERED) ? "R" : "",
  215. (cur->flags & D_SEEN) ? "S" : "",
  216. (cur->flags & D_DELETED) ? "T" : "");
  217. }
  218. for (;;)
  219. {
  220. /* create new file */
  221. snprintf (path, sizeof (path), "%s/tmp/%s.%ld_%d.%d,U=%d%s",
  222. mbox->path, Hostname, time (0), MaildirCount++,
  223. getpid (), cur->uid, suffix);
  224. if ((fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600)) > 0)
  225. break;
  226. if (errno != EEXIST)
  227. {
  228. perror ("open");
  229. break;
  230. }
  231. sleep (2);
  232. }
  233. if (fd < 0)
  234. continue;
  235. /* give some visual feedback that something is happening */
  236. fputs (".", stdout);
  237. fflush (stdout);
  238. fetched++;
  239. ret = imap_fetch_message (imap, cur->uid, fd);
  240. if (close (fd))
  241. perror ("close");
  242. else if (!ret)
  243. {
  244. p = strrchr (path, '/');
  245. snprintf (newpath, sizeof (newpath), "%s/%s%s", mbox->path,
  246. cur->flags ? "cur" : "new", p);
  247. /* its ok if this fails, the next time we sync the message
  248. * will get pulled down
  249. */
  250. if (link (path, newpath))
  251. perror ("link");
  252. }
  253. /* always remove the temp file */
  254. unlink (path);
  255. }
  256. }
  257. printf (" %d messages\n", fetched);
  258. return 0;
  259. }