sync.c 9.6 KB

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