sync.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000-2 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. static int
  40. set_uid (DBM * db, const char *f, unsigned int uid)
  41. {
  42. char *s;
  43. datum key, val;
  44. key.dptr = (void *) f;
  45. s = strchr (f, ':');
  46. key.dsize = s ? (size_t) (s - key.dptr) : strlen (f);
  47. val.dptr = (void *) &uid;
  48. val.dsize = sizeof (uid);
  49. dbm_store (db, key, val, DBM_REPLACE);
  50. return 0;
  51. }
  52. int
  53. sync_mailbox (mailbox_t * mbox, imap_t * imap, int flags,
  54. unsigned int max_size, unsigned int max_msgs)
  55. {
  56. message_t *cur;
  57. message_t *tmp;
  58. char path[_POSIX_PATH_MAX];
  59. char newpath[_POSIX_PATH_MAX];
  60. char suffix[_POSIX_PATH_MAX];
  61. char *p;
  62. int fd;
  63. int ret;
  64. int fetched = 0;
  65. int upload = 0;
  66. unsigned int msg_count;
  67. if (mbox->uidseen)
  68. {
  69. if (mbox->uidvalidity != imap->uidvalidity)
  70. {
  71. /* if the UIDVALIDITY value has changed, it means all our
  72. * local UIDs are invalid, so we can't sync.
  73. */
  74. fputs ("ERROR: UIDVALIDITY changed on server (fatal)\n", stderr);
  75. return -1;
  76. }
  77. }
  78. else if (maildir_set_uidvalidity (mbox, imap->uidvalidity))
  79. {
  80. fputs ("ERROR: unable to store UIDVALIDITY\n", stderr);
  81. return -1;
  82. }
  83. if (mbox->maxuid == 0 || imap->maxuid > mbox->maxuid)
  84. {
  85. mbox->maxuid = imap->maxuid;
  86. if (maildir_update_maxuid (mbox))
  87. return -1;
  88. }
  89. /* if we are --fast mode, the mailbox wont have been loaded, so
  90. * this next step is skipped.
  91. */
  92. for (cur = mbox->msgs; cur; cur = cur->next)
  93. {
  94. tmp = find_msg (imap->msgs, cur->uid);
  95. if (!tmp)
  96. {
  97. /* if this message wasn't fetched from the server, attempt to
  98. * upload it
  99. */
  100. if (cur->uid == (unsigned int) -1)
  101. {
  102. struct stat sb;
  103. if ((cur->flags & D_DELETED) && (flags & SYNC_EXPUNGE))
  104. {
  105. /*
  106. * This message is marked as deleted and we are
  107. * expunging. Don't upload to the server.
  108. */
  109. continue;
  110. }
  111. if (!upload)
  112. info ("Uploading messages");
  113. infoc ('.');
  114. fflush (stdout);
  115. upload++;
  116. /* upload the message if its not too big */
  117. snprintf (path, sizeof (path), "%s/%s/%s", mbox->path,
  118. cur->new ? "new" : "cur", cur->file);
  119. if (stat (path, &sb))
  120. {
  121. perror (path);
  122. continue; /* not fatal */
  123. }
  124. if (imap->box->max_size > 0
  125. && sb.st_size > imap->box->max_size)
  126. {
  127. info ("Warning, local message is too large (%lu), skipping...\n",
  128. (unsigned long) sb.st_size);
  129. continue;
  130. }
  131. fd = open (path, O_RDONLY);
  132. if (fd == -1)
  133. {
  134. fprintf (stderr, "Error, unable to open %s: %s (errno %d)\n",
  135. path, strerror (errno), errno);
  136. continue;
  137. }
  138. cur->size = sb.st_size;
  139. cur->uid = imap_append_message (imap, fd, cur);
  140. /* if the server gave us back a uid, update the db */
  141. if (cur->uid != (unsigned int) -1) {
  142. set_uid (mbox->db, cur->file, cur->uid);
  143. if (!cur->uid)
  144. printf("warning: no uid for new messge %s\n", cur->file);
  145. }
  146. close (fd);
  147. }
  148. /*
  149. * message used to exist on server but no longer does (we know
  150. * this beacause it has a UID associated with it).
  151. */
  152. else if (flags & SYNC_DELETE)
  153. {
  154. cur->flags |= D_DELETED;
  155. cur->dead = 1;
  156. mbox->deleted++;
  157. }
  158. /* if the user doesn't want local msgs deleted when they don't
  159. * exist on the server, warn that such messages exist.
  160. */
  161. else
  162. info ("Warning, uid %u doesn't exist on server\n", cur->uid);
  163. continue;
  164. }
  165. tmp->processed = 1;
  166. /* if the message is deleted, and CopyDeletedTo is set, and we
  167. * are expunging, make a copy of the message now.
  168. */
  169. if (((cur->flags | tmp->flags) & D_DELETED) != 0 &&
  170. (flags & SYNC_EXPUNGE) && imap->box->copy_deleted_to)
  171. {
  172. if (imap_copy_message (imap, cur->uid,
  173. imap->box->copy_deleted_to))
  174. {
  175. fprintf (stderr,
  176. "ERROR: unable to copy deleted message to \"%s\"\n",
  177. imap->box->copy_deleted_to);
  178. return -1;
  179. }
  180. }
  181. /* check if local flags are different from server flags.
  182. * ignore \Recent and \Draft
  183. */
  184. if (cur->flags != (tmp->flags & ~(D_RECENT | D_DRAFT)))
  185. {
  186. /* set local flags that don't exist on the server */
  187. if (!(tmp->flags & D_DELETED) && (cur->flags & D_DELETED))
  188. imap->deleted++;
  189. imap_set_flags (imap, cur->uid, cur->flags & ~tmp->flags);
  190. /* update local flags */
  191. if ((cur->flags & D_DELETED) == 0 && (tmp->flags & D_DELETED))
  192. mbox->deleted++;
  193. cur->flags |= (tmp->flags & ~(D_RECENT | D_DRAFT));
  194. /* don't bother renaming the file if we are just going to
  195. * remove it later.
  196. */
  197. if ((cur->flags & D_DELETED) == 0 || (flags & SYNC_EXPUNGE) == 0)
  198. {
  199. /* generate old path */
  200. snprintf (path, sizeof (path), "%s/%s/%s",
  201. mbox->path, cur->new ? "new" : "cur", cur->file);
  202. /* truncate old flags (if present) */
  203. p = strchr (cur->file, ':');
  204. if (p)
  205. *p = 0;
  206. /* generate new path - always put this in the cur/ directory
  207. * because its no longer new
  208. */
  209. snprintf (newpath, sizeof (newpath), "%s/cur/%s:2,%s%s%s%s",
  210. mbox->path,
  211. cur->file, (cur->flags & D_FLAGGED) ? "F" : "",
  212. (cur->flags & D_ANSWERED) ? "R" : "",
  213. (cur->flags & D_SEEN) ? "S" : "",
  214. (cur->flags & D_DELETED) ? "T" : "");
  215. if (rename (path, newpath))
  216. {
  217. perror ("rename");
  218. return -1;
  219. }
  220. else
  221. {
  222. /* update the filename in the msg struct */
  223. p = strrchr (newpath, '/');
  224. free (cur->file);
  225. cur->file = strdup (p + 1);
  226. cur->new = 0; /* not any more */
  227. }
  228. }
  229. }
  230. }
  231. if (upload)
  232. info (" %d messages.\n", upload);
  233. info ("Fetching new messages");
  234. fflush (stdout);
  235. if (max_msgs == 0)
  236. max_msgs = UINT_MAX;
  237. else
  238. {
  239. /* expire messages in excess of the max-count for this mailbox.
  240. * flagged mails are considered sacrosant and not deleted.
  241. * we have already done the upload to the server, so messing with
  242. * the flags variable do not have remote side effects.
  243. */
  244. for (cur = imap->msgs, msg_count = 0;
  245. cur && msg_count < max_msgs; cur = cur->next, msg_count++)
  246. {
  247. tmp = find_msg (mbox->msgs, cur->uid);
  248. if (tmp)
  249. tmp->wanted = 1;
  250. }
  251. for (cur = mbox->msgs; cur; cur = cur->next)
  252. {
  253. if (!cur->wanted && !(cur->flags & D_FLAGGED))
  254. {
  255. cur->flags |= D_DELETED;
  256. cur->dead = 1;
  257. mbox->deleted++;
  258. }
  259. }
  260. }
  261. for (cur = imap->msgs, msg_count = 0;
  262. cur && msg_count < max_msgs; cur = cur->next, msg_count++)
  263. {
  264. if (!cur->processed)
  265. {
  266. /* new message on server */
  267. if ((flags & SYNC_EXPUNGE) && (cur->flags & D_DELETED))
  268. {
  269. /* this message has been marked for deletion and
  270. * we are currently expunging a mailbox. don't
  271. * bother downloading this message
  272. */
  273. continue;
  274. }
  275. if (max_size && cur->size > max_size)
  276. {
  277. info ("Warning, message skipped because it is too big (%u)\n",
  278. cur->size);
  279. continue;
  280. }
  281. /* construct the flags part of the file name. */
  282. *suffix = 0;
  283. if (cur->flags & ~D_RECENT)
  284. {
  285. snprintf (suffix, sizeof (suffix), ":2,%s%s%s%s",
  286. (cur->flags & D_FLAGGED) ? "F" : "",
  287. (cur->flags & D_ANSWERED) ? "R" : "",
  288. (cur->flags & D_SEEN) ? "S" : "",
  289. (cur->flags & D_DELETED) ? "T" : "");
  290. }
  291. for (;;)
  292. {
  293. /* create new file */
  294. snprintf (path, sizeof (path), "%s/tmp/%ld_%d.%d.%s%s",
  295. mbox->path, time (0), MaildirCount++, getpid (),
  296. Hostname, suffix);
  297. if ((fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600)) > 0)
  298. break;
  299. if (errno != EEXIST)
  300. {
  301. perror (path);
  302. break;
  303. }
  304. sleep (2);
  305. }
  306. if (fd < 0)
  307. continue;
  308. /* give some visual feedback that something is happening */
  309. infoc ('.');
  310. fflush (stdout);
  311. fetched++;
  312. ret = imap_fetch_message (imap, cur->uid, fd);
  313. if (fsync (fd))
  314. {
  315. perror ("fsync");
  316. close (fd);
  317. }
  318. else if (close (fd))
  319. perror ("close");
  320. else if (!ret)
  321. {
  322. p = strrchr (path, '/');
  323. snprintf (newpath, sizeof (newpath), "%s/%s%s", mbox->path,
  324. (cur->flags & ~D_RECENT) ? "cur" : "new", p);
  325. /* its ok if this fails, the next time we sync the message
  326. * will get pulled down
  327. */
  328. if (link (path, newpath))
  329. perror ("link");
  330. else
  331. {
  332. /* update the db with the UID mapping for this file */
  333. set_uid (mbox->db, p + 1, cur->uid);
  334. }
  335. }
  336. /* always remove the temp file */
  337. unlink (path);
  338. }
  339. }
  340. info (" %d messages\n", fetched);
  341. return 0;
  342. }