2 * Copyright (C) 2000-2002 Vsevolod Volkov <vvv@mutt.org.ua>
3 * Copyright (C) 2006-7 Rocco Rutte <pdmef@gmx.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "mutt_curses.h"
28 #include "mutt_crypt.h"
38 #define HC_FNAME "mutt" /* filename for hcache as POP lacks paths */
39 #define HC_FEXT "hcache" /* extension for hcache as POP lacks paths */
42 /* write line to file */
43 static int fetch_message (char *line, void *file)
45 FILE *f = (FILE *) file;
48 if (fputc ('\n', f) == EOF)
58 * -1 - conection lost,
59 * -2 - invalid command or execution error,
60 * -3 - error writing to tempfile
62 static int pop_read_header (POP_DATA *pop_data, HEADER *h)
67 char buf[LONG_STRING];
68 char tempfile[_POSIX_PATH_MAX];
70 mutt_mktemp (tempfile, sizeof (tempfile));
71 if (!(f = safe_fopen (tempfile, "w+")))
73 mutt_perror (tempfile);
77 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
78 ret = pop_query (pop_data, buf, sizeof (buf));
81 sscanf (buf, "+OK %d %ld", &index, &length);
83 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
84 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
86 if (pop_data->cmd_top == 2)
90 pop_data->cmd_top = 1;
92 dprint (1, (debugfile, "pop_read_header: set TOP capability\n"));
97 pop_data->cmd_top = 0;
99 dprint (1, (debugfile, "pop_read_header: unset TOP capability\n"));
100 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
101 _("Command TOP is not supported by server."));
111 h->env = mutt_read_rfc822_header (f, h, 0, 0);
112 h->content->length = length - h->content->offset + 1;
116 h->content->length--;
117 fgets (buf, sizeof (buf), f);
123 mutt_error ("%s", pop_data->err_msg);
128 mutt_error _("Can't write header to temporary file!");
139 static int fetch_uidl (char *line, void *data)
142 CONTEXT *ctx = (CONTEXT *)data;
143 POP_DATA *pop_data = (POP_DATA *)ctx->data;
145 sscanf (line, "%d %s", &index, line);
146 for (i = 0; i < ctx->msgcount; i++)
147 if (!mutt_strcmp (line, ctx->hdrs[i]->data))
150 if (i == ctx->msgcount)
152 dprint (1, (debugfile, "pop_fetch_headers: new header %d %s\n", index, line));
154 if (i >= ctx->hdrmax)
155 mx_alloc_memory(ctx);
158 ctx->hdrs[i] = mutt_new_header ();
159 ctx->hdrs[i]->data = safe_strdup (line);
161 else if (ctx->hdrs[i]->index != index - 1)
162 pop_data->clear_cache = 1;
164 ctx->hdrs[i]->refno = index;
165 ctx->hdrs[i]->index = index - 1;
170 static int msg_cache_check (const char *id, body_cache_t *bcache, void *data)
176 if (!(ctx = (CONTEXT *)data))
178 if (!(pop_data = (POP_DATA *)ctx->data))
182 /* keep hcache file if hcache == bcache */
183 if (strcmp (HC_FNAME "." HC_FEXT, id) == 0)
187 for (i = 0; i < ctx->msgcount; i++)
188 /* if the id we get is known for a header: done (i.e. keep in cache) */
189 if (ctx->hdrs[i]->data && mutt_strcmp (ctx->hdrs[i]->data, id) == 0)
192 /* message not found in context -> remove it from cache
193 * return the result of bcache, so we stop upon its first error
195 return mutt_bcache_del (bcache, id);
199 static int pop_hcache_namer (const char *path, char *dest, size_t destlen)
201 return snprintf (dest, destlen, "%s." HC_FEXT, path);
204 static header_cache_t *pop_hcache_open (POP_DATA *pop_data, const char *path)
209 if (!pop_data || !pop_data->conn)
210 return mutt_hcache_open (HeaderCache, path, NULL);
212 mutt_account_tourl (&pop_data->conn->account, &url);
214 url_ciss_tostring (&url, p, sizeof (p), U_PATH);
215 return mutt_hcache_open (HeaderCache, p, pop_hcache_namer);
223 * -1 - conection lost,
224 * -2 - invalid command or execution error,
225 * -3 - error writing to tempfile
227 static int pop_fetch_headers (CONTEXT *ctx)
229 int i, ret, old_count, new_count, deleted;
230 unsigned short hcached = 0, bcached;
231 POP_DATA *pop_data = (POP_DATA *)ctx->data;
235 header_cache_t *hc = NULL;
238 hc = pop_hcache_open (pop_data, ctx->path);
241 time (&pop_data->check_time);
242 pop_data->clear_cache = 0;
244 for (i = 0; i < ctx->msgcount; i++)
245 ctx->hdrs[i]->refno = -1;
247 old_count = ctx->msgcount;
248 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
249 new_count = ctx->msgcount;
250 ctx->msgcount = old_count;
252 if (pop_data->cmd_uidl == 2)
256 pop_data->cmd_uidl = 1;
258 dprint (1, (debugfile, "pop_fetch_headers: set UIDL capability\n"));
261 if (ret == -2 && pop_data->cmd_uidl == 2)
263 pop_data->cmd_uidl = 0;
265 dprint (1, (debugfile, "pop_fetch_headers: unset UIDL capability\n"));
266 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
267 _("Command UIDL is not supported by server."));
272 mutt_progress_init (&progress, _("Fetching message headers..."),
273 M_PROGRESS_MSG, ReadInc, new_count - old_count);
277 for (i = 0, deleted = 0; i < old_count; i++)
279 if (ctx->hdrs[i]->refno == -1)
281 ctx->hdrs[i]->deleted = 1;
287 mutt_error (_("%d messages have been lost. Try reopening the mailbox."),
292 for (i = old_count; i < new_count; i++)
295 mutt_progress_update (&progress, i + 1 - old_count, -1);
297 if ((data = mutt_hcache_fetch (hc, ctx->hdrs[i]->data, strlen)))
299 char *uidl = safe_strdup (ctx->hdrs[i]->data);
300 int refno = ctx->hdrs[i]->refno;
301 int index = ctx->hdrs[i]->index;
303 * - POP dynamically numbers headers and relies on h->refno
304 * to map messages; so restore header and overwrite restored
305 * refno with current refno, same for index
306 * - h->data needs to a separate pointer as it's driver-specific
307 * data freed separately elsewhere
308 * (the old h->data should point inside a malloc'd block from
309 * hcache so there shouldn't be a memleak here)
311 HEADER *h = mutt_hcache_restore ((unsigned char *) data, NULL);
312 mutt_free_header (&ctx->hdrs[i]);
314 ctx->hdrs[i]->refno = refno;
315 ctx->hdrs[i]->index = index;
316 ctx->hdrs[i]->data = uidl;
322 if ((ret = pop_read_header (pop_data, ctx->hdrs[i])) < 0)
327 mutt_hcache_store (hc, ctx->hdrs[i]->data, ctx->hdrs[i], 0, strlen);
334 * faked support for flags works like this:
335 * - if 'hcached' is 1, we have the message in our hcache:
336 * - if we also have a body: read
337 * - if we don't have a body: old
338 * (if $mark_old is set which is maybe wrong as
339 * $mark_old should be considered for syncing the
340 * folder and not when opening it XXX)
341 * - if 'hcached' is 0, we don't have the message in our hcache:
342 * - if we also have a body: read
343 * - if we don't have a body: new
345 bcached = mutt_bcache_exists (pop_data->bcache, ctx->hdrs[i]->data) == 0;
346 ctx->hdrs[i]->old = 0;
347 ctx->hdrs[i]->read = 0;
351 ctx->hdrs[i]->read = 1;
352 else if (option (OPTMARKOLD))
353 ctx->hdrs[i]->old = 1;
358 ctx->hdrs[i]->read = 1;
365 mx_update_context (ctx, i - old_count);
369 mutt_hcache_close (hc);
374 for (i = ctx->msgcount; i < new_count; i++)
375 mutt_free_header (&ctx->hdrs[i]);
379 /* after putting the result into our structures,
380 * clean up cache, i.e. wipe messages deleted outside
381 * the availability of our cache
383 if (option (OPTMESSAGECACHECLEAN))
384 mutt_bcache_list (pop_data->bcache, msg_cache_check, (void*)ctx);
387 return (new_count - old_count);
390 /* open POP mailbox - fetch only headers */
391 int pop_open_mailbox (CONTEXT *ctx)
394 char buf[LONG_STRING];
400 if (pop_parse_path (ctx->path, &acct))
402 mutt_error (_("%s is an invalid POP path"), ctx->path);
407 mutt_account_tourl (&acct, &url);
409 url_ciss_tostring (&url, buf, sizeof (buf), 0);
410 conn = mutt_conn_find (NULL, &acct);
415 ctx->path = safe_strdup (buf);
417 pop_data = safe_calloc (1, sizeof (POP_DATA));
418 pop_data->conn = conn;
419 ctx->data = pop_data;
420 ctx->mx_close = pop_close_mailbox;
422 if (pop_open_connection (pop_data) < 0)
425 conn->data = pop_data;
426 pop_data->bcache = mutt_bcache_open (&acct, NULL);
428 /* init (hard-coded) ACL rights */
429 memset (ctx->rights, 0, sizeof (ctx->rights));
430 mutt_bit_set (ctx->rights, M_ACL_SEEN);
431 mutt_bit_set (ctx->rights, M_ACL_DELETE);
433 /* flags are managed using header cache, so it only makes sense to
434 * enable them in that case */
435 mutt_bit_set (ctx->rights, M_ACL_WRITE);
440 if (pop_reconnect (ctx) < 0)
443 ctx->size = pop_data->size;
445 mutt_message _("Fetching list of messages...");
447 ret = pop_fetch_headers (ctx);
460 /* delete all cached messages */
461 static void pop_clear_cache (POP_DATA *pop_data)
465 if (!pop_data->clear_cache)
468 dprint (1, (debugfile, "pop_clear_cache: delete cached messages\n"));
470 for (i = 0; i < POP_CACHE_LEN; i++)
472 if (pop_data->cache[i].path)
474 unlink (pop_data->cache[i].path);
475 FREE (&pop_data->cache[i].path);
480 /* close POP mailbox */
481 int pop_close_mailbox (CONTEXT *ctx)
483 POP_DATA *pop_data = (POP_DATA *)ctx->data;
490 if (pop_data->status != POP_NONE)
491 mutt_socket_close (pop_data->conn);
493 pop_data->status = POP_NONE;
495 pop_data->clear_cache = 1;
496 pop_clear_cache (pop_data);
498 if (!pop_data->conn->data)
499 mutt_socket_free (pop_data->conn);
501 mutt_bcache_close (&pop_data->bcache);
506 /* fetch message from POP server */
507 int pop_fetch_message (MESSAGE* msg, CONTEXT* ctx, int msgno)
511 char buf[LONG_STRING];
512 char path[_POSIX_PATH_MAX];
513 progress_t progressbar;
514 POP_DATA *pop_data = (POP_DATA *)ctx->data;
516 HEADER *h = ctx->hdrs[msgno];
517 unsigned short bcache = 1;
519 /* see if we already have the message in body cache */
520 if ((msg->fp = mutt_bcache_get (pop_data->bcache, h->data)))
524 * see if we already have the message in our cache in
525 * case $message_cachedir is unset
527 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
531 if (cache->index == h->index)
533 /* yes, so just return a pointer to the message */
534 msg->fp = fopen (cache->path, "r");
538 mutt_perror (cache->path);
544 /* clear the previous entry */
545 unlink (cache->path);
552 if (pop_reconnect (ctx) < 0)
555 /* verify that massage index is correct */
558 mutt_error _("The message index is incorrect. Try reopening the mailbox.");
563 mutt_progress_init (&progressbar, _("Fetching message..."),
564 M_PROGRESS_SIZE, NetInc, h->content->length + h->content->offset - 1);
566 /* see if we can put in body cache; use our cache as fallback */
567 if (!(msg->fp = mutt_bcache_put (pop_data->bcache, h->data, 1)))
571 mutt_mktemp (path, sizeof (path));
572 if (!(msg->fp = safe_fopen (path, "w+")))
580 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
582 ret = pop_fetch_data (pop_data, buf, &progressbar, fetch_message, msg->fp);
586 safe_fclose (&msg->fp);
588 /* if RETR failed (e.g. connection closed), be sure to remove either
589 * the file in bcache or from POP's own cache since the next iteration
590 * of the loop will re-attempt to put() the message */
596 mutt_error ("%s", pop_data->err_msg);
603 mutt_error _("Can't write message to temporary file!");
609 /* Update the header information. Previously, we only downloaded a
610 * portion of the headers, those required for the main display.
613 mutt_bcache_commit (pop_data->bcache, h->data);
616 cache->index = h->index;
617 cache->path = safe_strdup (path);
621 mutt_free_envelope (&h->env);
622 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
625 fgets (buf, sizeof (buf), msg->fp);
626 while (!feof (msg->fp))
628 ctx->hdrs[msgno]->lines++;
629 fgets (buf, sizeof (buf), msg->fp);
632 h->content->length = ftello (msg->fp) - h->content->offset;
634 /* This needs to be done in case this is a multipart message */
636 h->security = crypt_query (h->content);
644 /* update POP mailbox - delete messages from server */
645 int pop_sync_mailbox (CONTEXT *ctx, int *index_hint)
648 char buf[LONG_STRING];
649 POP_DATA *pop_data = (POP_DATA *)ctx->data;
652 header_cache_t *hc = NULL;
655 pop_data->check_time = 0;
659 if (pop_reconnect (ctx) < 0)
662 mutt_progress_init (&progress, _("Marking messages deleted..."),
663 M_PROGRESS_MSG, WriteInc, ctx->deleted);
666 hc = pop_hcache_open (pop_data, ctx->path);
669 for (i = 0, j = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++)
671 if (ctx->hdrs[i]->deleted && ctx->hdrs[i]->refno != -1)
675 mutt_progress_update (&progress, j, -1);
676 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
677 if ((ret = pop_query (pop_data, buf, sizeof (buf))) == 0)
679 mutt_bcache_del (pop_data->bcache, ctx->hdrs[i]->data);
681 mutt_hcache_delete (hc, ctx->hdrs[i]->data, strlen);
687 if (ctx->hdrs[i]->changed)
689 mutt_hcache_store (hc, ctx->hdrs[i]->data, ctx->hdrs[i], 0, strlen);
696 mutt_hcache_close (hc);
701 strfcpy (buf, "QUIT\r\n", sizeof (buf));
702 ret = pop_query (pop_data, buf, sizeof (buf));
707 pop_data->clear_cache = 1;
708 pop_clear_cache (pop_data);
709 pop_data->status = POP_DISCONNECTED;
715 mutt_error ("%s", pop_data->err_msg);
722 /* Check for new messages and fetch headers */
723 int pop_check_mailbox (CONTEXT *ctx, int *index_hint)
726 POP_DATA *pop_data = (POP_DATA *)ctx->data;
728 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
733 mutt_socket_close (pop_data->conn);
735 if (pop_open_connection (pop_data) < 0)
738 ctx->size = pop_data->size;
740 mutt_message _("Checking for new messages...");
742 ret = pop_fetch_headers (ctx);
743 pop_clear_cache (pop_data);
754 /* Fetch messages and save them in $spoolfile */
755 void pop_fetch_mail (void)
757 char buffer[LONG_STRING];
758 char msgbuf[SHORT_STRING];
760 int i, delanswer, last = 0, msgs, bytes, rset = 0, ret;
769 mutt_error _("POP host is not defined.");
773 url = p = safe_calloc (strlen (PopHost) + 7, sizeof (char));
774 if (url_check_scheme (PopHost) == U_UNKNOWN)
776 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
777 p = strchr (url, '\0');
779 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
781 ret = pop_parse_path (url, &acct);
785 mutt_error (_("%s is an invalid POP path"), PopHost);
789 conn = mutt_conn_find (NULL, &acct);
793 pop_data = safe_calloc (1, sizeof (POP_DATA));
794 pop_data->conn = conn;
796 if (pop_open_connection (pop_data) < 0)
798 mutt_socket_free (pop_data->conn);
803 conn->data = pop_data;
805 mutt_message _("Checking for new messages...");
807 /* find out how many messages are in the mailbox. */
808 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
809 ret = pop_query (pop_data, buffer, sizeof (buffer));
814 mutt_error ("%s", pop_data->err_msg);
818 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
820 /* only get unread messages */
821 if (msgs > 0 && option (OPTPOPLAST))
823 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
824 ret = pop_query (pop_data, buffer, sizeof (buffer));
828 sscanf (buffer, "+OK %d", &last);
833 mutt_message _("No new mail in POP mailbox.");
837 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
840 delanswer = query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
842 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."), bytes);
843 mutt_message ("%s", msgbuf);
845 for (i = last + 1 ; i <= msgs ; i++)
847 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
851 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
852 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
856 if (ret == 0 && mx_commit_message (msg, &ctx) != 0)
862 mx_close_message (&msg);
865 if (ret == 0 && delanswer == M_YES)
867 /* delete the message on the server */
868 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
869 ret = pop_query (pop_data, buffer, sizeof (buffer));
874 mx_close_mailbox (&ctx, NULL);
879 mutt_error ("%s", pop_data->err_msg);
884 mutt_error _("Error while writing mailbox!");
888 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last, msgs - last);
891 mx_close_mailbox (&ctx, NULL);
895 /* make sure no messages get deleted */
896 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
897 if (pop_query (pop_data, buffer, sizeof (buffer)) == -1)
902 /* exit gracefully */
903 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
904 if (pop_query (pop_data, buffer, sizeof (buffer)) == -1)
906 mutt_socket_close (conn);
911 mutt_error _("Server closed connection!");
912 mutt_socket_close (conn);