sync.c 7.7 KB

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