2 * Copyright (C) 2000-2002 Vsevolod Volkov <vvv@mutt.org.ua>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 #include "mutt_crypt.h"
31 /* write line to file */
32 static int fetch_message (char *line, void *file)
34 FILE *f = (FILE *) file;
37 if (fputc ('\n', f) == EOF)
47 * -1 - conection lost,
48 * -2 - invalid command or execution error,
49 * -3 - error writing to tempfile
51 static int pop_read_header (POP_DATA *pop_data, HEADER *h)
56 char buf[LONG_STRING];
57 char tempfile[_POSIX_PATH_MAX];
59 mutt_mktemp (tempfile);
60 if (!(f = safe_fopen (tempfile, "w+")))
62 mutt_perror (tempfile);
66 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
67 ret = pop_query (pop_data, buf, sizeof (buf));
70 sscanf (buf, "+OK %d %ld", &index, &length);
72 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
73 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
75 if (pop_data->cmd_top == 2)
79 pop_data->cmd_top = 1;
81 dprint (1, (debugfile, "pop_read_header: set TOP capability\n"));
86 pop_data->cmd_top = 0;
88 dprint (1, (debugfile, "pop_read_header: unset TOP capability\n"));
89 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
90 _("Command TOP is not supported by server."));
100 h->env = mutt_read_rfc822_header (f, h, 0, 0);
101 h->content->length = length - h->content->offset + 1;
105 h->content->length--;
106 fgets (buf, sizeof (buf), f);
112 mutt_error ("%s", pop_data->err_msg);
117 mutt_error _("Can't write header to temporary file!");
128 static int fetch_uidl (char *line, void *data)
131 CONTEXT *ctx = (CONTEXT *)data;
132 POP_DATA *pop_data = (POP_DATA *)ctx->data;
134 sscanf (line, "%d %s", &index, line);
135 for (i = 0; i < ctx->msgcount; i++)
136 if (!mutt_strcmp (line, ctx->hdrs[i]->data))
139 if (i == ctx->msgcount)
141 dprint (1, (debugfile, "pop_fetch_headers: new header %d %s\n", index, line));
143 if (i >= ctx->hdrmax)
144 mx_alloc_memory(ctx);
147 ctx->hdrs[i] = mutt_new_header ();
148 ctx->hdrs[i]->data = safe_strdup (line);
150 else if (ctx->hdrs[i]->index != index - 1)
151 pop_data->clear_cache = 1;
153 ctx->hdrs[i]->refno = index;
154 ctx->hdrs[i]->index = index - 1;
163 * -1 - conection lost,
164 * -2 - invalid command or execution error,
165 * -3 - error writing to tempfile
167 static int pop_fetch_headers (CONTEXT *ctx)
169 int i, ret, old_count, new_count;
170 POP_DATA *pop_data = (POP_DATA *)ctx->data;
172 time (&pop_data->check_time);
173 pop_data->clear_cache = 0;
175 for (i = 0; i < ctx->msgcount; i++)
176 ctx->hdrs[i]->refno = -1;
178 old_count = ctx->msgcount;
179 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
180 new_count = ctx->msgcount;
181 ctx->msgcount = old_count;
183 if (pop_data->cmd_uidl == 2)
187 pop_data->cmd_uidl = 1;
189 dprint (1, (debugfile, "pop_fetch_headers: set UIDL capability\n"));
192 if (ret == -2 && pop_data->cmd_uidl == 2)
194 pop_data->cmd_uidl = 0;
196 dprint (1, (debugfile, "pop_fetch_headers: unset UIDL capability\n"));
197 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
198 _("Command UIDL is not supported by server."));
204 for (i = 0; i < old_count; i++)
205 if (ctx->hdrs[i]->refno == -1)
206 ctx->hdrs[i]->deleted = 1;
208 for (i = old_count; i < new_count; i++)
210 mutt_message (_("Fetching message headers... [%d/%d]"),
211 i + 1 - old_count, new_count - old_count);
213 ret = pop_read_header (pop_data, ctx->hdrs[i]);
221 mx_update_context (ctx, i - old_count);
226 for (i = ctx->msgcount; i < new_count; i++)
227 mutt_free_header (&ctx->hdrs[i]);
232 return (new_count - old_count);
235 /* open POP mailbox - fetch only headers */
236 int pop_open_mailbox (CONTEXT *ctx)
239 char buf[LONG_STRING];
245 if (pop_parse_path (ctx->path, &acct))
247 mutt_error (_("%s is an invalid POP path"), ctx->path);
252 mutt_account_tourl (&acct, &url);
254 url_ciss_tostring (&url, buf, sizeof (buf), 0);
255 conn = mutt_conn_find (NULL, &acct);
260 ctx->path = safe_strdup (buf);
262 pop_data = safe_calloc (1, sizeof (POP_DATA));
263 pop_data->conn = conn;
264 ctx->data = pop_data;
266 if (pop_open_connection (pop_data) < 0)
269 conn->data = pop_data;
273 if (pop_reconnect (ctx) < 0)
276 ctx->size = pop_data->size;
278 mutt_message _("Fetching list of messages...");
280 ret = pop_fetch_headers (ctx);
293 /* delete all cached messages */
294 static void pop_clear_cache (POP_DATA *pop_data)
298 if (!pop_data->clear_cache)
301 dprint (1, (debugfile, "pop_clear_cache: delete cached messages\n"));
303 for (i = 0; i < POP_CACHE_LEN; i++)
305 if (pop_data->cache[i].path)
307 unlink (pop_data->cache[i].path);
308 FREE (&pop_data->cache[i].path);
313 /* close POP mailbox */
314 void pop_close_mailbox (CONTEXT *ctx)
316 POP_DATA *pop_data = (POP_DATA *)ctx->data;
323 if (pop_data->status != POP_NONE)
324 mutt_socket_close (pop_data->conn);
326 pop_data->status = POP_NONE;
328 pop_data->clear_cache = 1;
329 pop_clear_cache (pop_data);
331 if (!pop_data->conn->data)
332 mutt_socket_free (pop_data->conn);
337 /* fetch message from POP server */
338 int pop_fetch_message (MESSAGE* msg, CONTEXT* ctx, int msgno)
342 char buf[LONG_STRING];
343 char path[_POSIX_PATH_MAX];
344 char *m = _("Fetching message...");
345 POP_DATA *pop_data = (POP_DATA *)ctx->data;
347 HEADER *h = ctx->hdrs[msgno];
349 /* see if we already have the message in our cache */
350 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
354 if (cache->index == h->index)
356 /* yes, so just return a pointer to the message */
357 msg->fp = fopen (cache->path, "r");
361 mutt_perror (cache->path);
367 /* clear the previous entry */
368 unlink (cache->path);
375 if (pop_reconnect (ctx) < 0)
378 /* verify that massage index is correct */
381 mutt_error _("The message index is incorrect. Try reopening the mailbox.");
389 msg->fp = safe_fopen (path, "w+");
397 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
399 ret = pop_fetch_data (pop_data, buf, m, fetch_message, msg->fp);
403 safe_fclose (&msg->fp);
408 mutt_error ("%s", pop_data->err_msg);
415 mutt_error _("Can't write message to temporary file!");
421 /* Update the header information. Previously, we only downloaded a
422 * portion of the headers, those required for the main display.
424 cache->index = h->index;
425 cache->path = safe_strdup (path);
428 mutt_free_envelope (&h->env);
429 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
432 fgets (buf, sizeof (buf), msg->fp);
433 while (!feof (msg->fp))
435 ctx->hdrs[msgno]->lines++;
436 fgets (buf, sizeof (buf), msg->fp);
439 h->content->length = ftell (msg->fp) - h->content->offset;
441 /* This needs to be done in case this is a multipart message */
443 h->security = crypt_query (h->content);
451 /* update POP mailbox - delete messages from server */
452 int pop_sync_mailbox (CONTEXT *ctx, int *index_hint)
455 char buf[LONG_STRING];
456 POP_DATA *pop_data = (POP_DATA *)ctx->data;
458 pop_data->check_time = 0;
462 if (pop_reconnect (ctx) < 0)
465 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
467 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++)
469 if (ctx->hdrs[i]->deleted)
471 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
472 ret = pop_query (pop_data, buf, sizeof (buf));
478 strfcpy (buf, "QUIT\r\n", sizeof (buf));
479 ret = pop_query (pop_data, buf, sizeof (buf));
484 pop_data->clear_cache = 1;
485 pop_clear_cache (pop_data);
486 pop_data->status = POP_DISCONNECTED;
492 mutt_error ("%s", pop_data->err_msg);
499 /* Check for new messages and fetch headers */
500 int pop_check_mailbox (CONTEXT *ctx, int *index_hint)
503 POP_DATA *pop_data = (POP_DATA *)ctx->data;
505 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
510 mutt_socket_close (pop_data->conn);
512 if (pop_open_connection (pop_data) < 0)
515 ctx->size = pop_data->size;
517 mutt_message _("Checking for new messages...");
519 ret = pop_fetch_headers (ctx);
520 pop_clear_cache (pop_data);
531 /* Fetch messages and save them in $spoolfile */
532 void pop_fetch_mail (void)
534 char buffer[LONG_STRING];
535 char msgbuf[SHORT_STRING];
537 int i, delanswer, last = 0, msgs, bytes, rset = 0, ret;
546 mutt_error _("POP host is not defined.");
550 url = p = safe_calloc (strlen (PopHost) + 7, sizeof (char));
551 if (url_check_scheme (PopHost) == U_UNKNOWN)
553 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
554 p = strchr (url, '\0');
556 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
558 ret = pop_parse_path (url, &acct);
562 mutt_error (_("%s is an invalid POP path"), PopHost);
566 conn = mutt_conn_find (NULL, &acct);
570 pop_data = safe_calloc (1, sizeof (POP_DATA));
571 pop_data->conn = conn;
573 if (pop_open_connection (pop_data) < 0)
575 mutt_socket_free (pop_data->conn);
580 conn->data = pop_data;
582 mutt_message _("Checking for new messages...");
584 /* find out how many messages are in the mailbox. */
585 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
586 ret = pop_query (pop_data, buffer, sizeof (buffer));
591 mutt_error ("%s", pop_data->err_msg);
595 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
597 /* only get unread messages */
598 if (msgs > 0 && option (OPTPOPLAST))
600 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
601 ret = pop_query (pop_data, buffer, sizeof (buffer));
605 sscanf (buffer, "+OK %d", &last);
610 mutt_message _("No new mail in POP mailbox.");
614 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
617 delanswer = query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
619 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."), bytes);
620 mutt_message ("%s", msgbuf);
622 for (i = last + 1 ; i <= msgs ; i++)
624 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
628 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
629 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
633 if (ret == 0 && mx_commit_message (msg, &ctx) != 0)
639 mx_close_message (&msg);
642 if (ret == 0 && delanswer == M_YES)
644 /* delete the message on the server */
645 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
646 ret = pop_query (pop_data, buffer, sizeof (buffer));
651 mx_close_mailbox (&ctx, NULL);
656 mutt_error ("%s", pop_data->err_msg);
661 mutt_error _("Error while writing mailbox!");
665 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last, msgs - last);
668 mx_close_mailbox (&ctx, NULL);
672 /* make sure no messages get deleted */
673 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
674 if (pop_query (pop_data, buffer, sizeof (buffer)) == -1)
679 /* exit gracefully */
680 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
681 if (pop_query (pop_data, buffer, sizeof (buffer)) == -1)
683 mutt_socket_close (conn);
688 mutt_error _("Server closed connection!");
689 mutt_socket_close (conn);