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.
22 #include "mutt_crypt.h"
27 /* write line to file */
28 static int fetch_message (char *line, void *file)
30 FILE *f = (FILE *) file;
33 if (fputc ('\n', f) == EOF)
43 * -1 - conection lost,
44 * -2 - invalid command or execution error,
45 * -3 - error writing to tempfile
47 static int pop_read_header (POP_DATA *pop_data, HEADER *h)
52 char buf[LONG_STRING];
53 char tempfile[_POSIX_PATH_MAX];
55 mutt_mktemp (tempfile);
56 if (!(f = safe_fopen (tempfile, "w+")))
58 mutt_perror (tempfile);
62 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
63 ret = pop_query (pop_data, buf, sizeof (buf));
66 sscanf (buf, "+OK %d %ld", &index, &length);
68 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
69 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
71 if (pop_data->cmd_top == 2)
75 pop_data->cmd_top = 1;
77 dprint (1, (debugfile, "pop_read_header: set TOP capability\n"));
82 pop_data->cmd_top = 0;
84 dprint (1, (debugfile, "pop_read_header: unset TOP capability\n"));
85 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
86 _("Command TOP is not supported by server."));
96 h->env = mutt_read_rfc822_header (f, h, 0, 0);
97 h->content->length = length - h->content->offset + 1;
101 h->content->length--;
102 fgets (buf, sizeof (buf), f);
108 mutt_error ("%s", pop_data->err_msg);
113 mutt_error _("Can't write header to temporary file!");
124 static int fetch_uidl (char *line, void *data)
127 CONTEXT *ctx = (CONTEXT *)data;
128 POP_DATA *pop_data = (POP_DATA *)ctx->data;
130 sscanf (line, "%d %s", &index, line);
131 for (i = 0; i < ctx->msgcount; i++)
132 if (!mutt_strcmp (line, ctx->hdrs[i]->data))
135 if (i == ctx->msgcount)
137 dprint (1, (debugfile, "pop_fetch_headers: new header %d %s\n", index, line));
139 if (i >= ctx->hdrmax)
140 mx_alloc_memory(ctx);
143 ctx->hdrs[i] = mutt_new_header ();
144 ctx->hdrs[i]->data = safe_strdup (line);
146 else if (ctx->hdrs[i]->index != index - 1)
147 pop_data->clear_cache = 1;
149 ctx->hdrs[i]->refno = index;
150 ctx->hdrs[i]->index = index - 1;
159 * -1 - conection lost,
160 * -2 - invalid command or execution error,
161 * -3 - error writing to tempfile
163 static int pop_fetch_headers (CONTEXT *ctx)
165 int i, ret, old_count, new_count;
166 POP_DATA *pop_data = (POP_DATA *)ctx->data;
168 time (&pop_data->check_time);
169 pop_data->clear_cache = 0;
171 for (i = 0; i < ctx->msgcount; i++)
172 ctx->hdrs[i]->refno = -1;
174 old_count = ctx->msgcount;
175 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
176 new_count = ctx->msgcount;
177 ctx->msgcount = old_count;
179 if (pop_data->cmd_uidl == 2)
183 pop_data->cmd_uidl = 1;
185 dprint (1, (debugfile, "pop_fetch_headers: set UIDL capability\n"));
188 if (ret == -2 && pop_data->cmd_uidl == 2)
190 pop_data->cmd_uidl = 0;
192 dprint (1, (debugfile, "pop_fetch_headers: unset UIDL capability\n"));
193 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
194 _("Command UIDL is not supported by server."));
200 for (i = 0; i < old_count; i++)
201 if (ctx->hdrs[i]->refno == -1)
202 ctx->hdrs[i]->deleted = 1;
204 for (i = old_count; i < new_count; i++)
206 mutt_message (_("Fetching message headers... [%d/%d]"),
207 i + 1 - old_count, new_count - old_count);
209 ret = pop_read_header (pop_data, ctx->hdrs[i]);
217 mx_update_context (ctx, i - old_count);
222 for (i = ctx->msgcount; i < new_count; i++)
223 mutt_free_header (&ctx->hdrs[i]);
228 return (new_count - old_count);
231 /* open POP mailbox - fetch only headers */
232 int pop_open_mailbox (CONTEXT *ctx)
235 char buf[LONG_STRING];
241 if (pop_parse_path (ctx->path, &acct))
243 mutt_error (_("%s is an invalid POP path"), ctx->path);
248 mutt_account_tourl (&acct, &url);
250 url_ciss_tostring (&url, buf, sizeof (buf), 0);
251 conn = mutt_conn_find (NULL, &acct);
256 ctx->path = safe_strdup (buf);
258 pop_data = safe_calloc (1, sizeof (POP_DATA));
259 pop_data->conn = conn;
260 ctx->data = pop_data;
262 if (pop_open_connection (pop_data) < 0)
265 conn->data = pop_data;
269 if (pop_reconnect (ctx) < 0)
272 ctx->size = pop_data->size;
274 mutt_message _("Fetching list of messages...");
276 ret = pop_fetch_headers (ctx);
289 /* delete all cached messages */
290 static void pop_clear_cache (POP_DATA *pop_data)
294 if (!pop_data->clear_cache)
297 dprint (1, (debugfile, "pop_clear_cache: delete cached messages\n"));
299 for (i = 0; i < POP_CACHE_LEN; i++)
301 if (pop_data->cache[i].path)
303 unlink (pop_data->cache[i].path);
304 FREE (&pop_data->cache[i].path);
309 /* close POP mailbox */
310 void pop_close_mailbox (CONTEXT *ctx)
312 POP_DATA *pop_data = (POP_DATA *)ctx->data;
319 if (pop_data->status != POP_NONE)
320 mutt_socket_close (pop_data->conn);
322 pop_data->status = POP_NONE;
324 pop_data->clear_cache = 1;
325 pop_clear_cache (pop_data);
327 if (!pop_data->conn->data)
328 mutt_socket_free (pop_data->conn);
333 /* fetch message from POP server */
334 int pop_fetch_message (MESSAGE* msg, CONTEXT* ctx, int msgno)
338 char buf[LONG_STRING];
339 char path[_POSIX_PATH_MAX];
340 char *m = _("Fetching message...");
341 POP_DATA *pop_data = (POP_DATA *)ctx->data;
343 HEADER *h = ctx->hdrs[msgno];
345 /* see if we already have the message in our cache */
346 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
350 if (cache->index == h->index)
352 /* yes, so just return a pointer to the message */
353 msg->fp = fopen (cache->path, "r");
357 mutt_perror (cache->path);
363 /* clear the previous entry */
364 unlink (cache->path);
371 if (pop_reconnect (ctx) < 0)
374 /* verify that massage index is correct */
377 mutt_error _("The message index is incorrect. Try reopening the mailbox.");
385 msg->fp = safe_fopen (path, "w+");
393 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
395 ret = pop_fetch_data (pop_data, buf, m, fetch_message, msg->fp);
399 safe_fclose (&msg->fp);
404 mutt_error ("%s", pop_data->err_msg);
411 mutt_error _("Can't write message to temporary file!");
417 /* Update the header information. Previously, we only downloaded a
418 * portion of the headers, those required for the main display.
420 cache->index = h->index;
421 cache->path = safe_strdup (path);
424 mutt_free_envelope (&h->env);
425 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
428 fgets (buf, sizeof (buf), msg->fp);
429 while (!feof (msg->fp))
431 ctx->hdrs[msgno]->lines++;
432 fgets (buf, sizeof (buf), msg->fp);
435 h->content->length = ftell (msg->fp) - h->content->offset;
437 /* This needs to be done in case this is a multipart message */
439 h->security = crypt_query (h->content);
447 /* update POP mailbox - delete messages from server */
448 int pop_sync_mailbox (CONTEXT *ctx, int *index_hint)
451 char buf[LONG_STRING];
452 POP_DATA *pop_data = (POP_DATA *)ctx->data;
454 pop_data->check_time = 0;
458 if (pop_reconnect (ctx) < 0)
461 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
463 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++)
465 if (ctx->hdrs[i]->deleted)
467 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
468 ret = pop_query (pop_data, buf, sizeof (buf));
474 strfcpy (buf, "QUIT\r\n", sizeof (buf));
475 ret = pop_query (pop_data, buf, sizeof (buf));
480 pop_data->clear_cache = 1;
481 pop_clear_cache (pop_data);
482 pop_data->status = POP_DISCONNECTED;
488 mutt_error ("%s", pop_data->err_msg);
495 /* Check for new messages and fetch headers */
496 int pop_check_mailbox (CONTEXT *ctx, int *index_hint)
499 POP_DATA *pop_data = (POP_DATA *)ctx->data;
501 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
506 mutt_socket_close (pop_data->conn);
508 if (pop_open_connection (pop_data) < 0)
511 ctx->size = pop_data->size;
513 mutt_message _("Checking for new messages...");
515 ret = pop_fetch_headers (ctx);
516 pop_clear_cache (pop_data);
527 /* Fetch messages and save them in $spoolfile */
528 void pop_fetch_mail (void)
530 char buffer[LONG_STRING];
531 char msgbuf[SHORT_STRING];
533 int i, delanswer, last = 0, msgs, bytes, rset = 0, ret;
542 mutt_error _("POP host is not defined.");
546 url = p = safe_calloc (strlen (PopHost) + 7, sizeof (char));
547 if (url_check_scheme (PopHost) == U_UNKNOWN)
549 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
550 p = strchr (url, '\0');
552 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
554 ret = pop_parse_path (url, &acct);
558 mutt_error (_("%s is an invalid POP path"), PopHost);
562 conn = mutt_conn_find (NULL, &acct);
566 pop_data = safe_calloc (1, sizeof (POP_DATA));
567 pop_data->conn = conn;
569 if (pop_open_connection (pop_data) < 0)
571 mutt_socket_free (pop_data->conn);
576 conn->data = pop_data;
578 mutt_message _("Checking for new messages...");
580 /* find out how many messages are in the mailbox. */
581 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
582 ret = pop_query (pop_data, buffer, sizeof (buffer));
587 mutt_error ("%s", pop_data->err_msg);
591 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
593 /* only get unread messages */
594 if (msgs > 0 && option (OPTPOPLAST))
596 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
597 ret = pop_query (pop_data, buffer, sizeof (buffer));
601 sscanf (buffer, "+OK %d", &last);
606 mutt_message _("No new mail in POP mailbox.");
610 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
613 delanswer = query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
615 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."), bytes);
616 mutt_message ("%s", msgbuf);
618 for (i = last + 1 ; i <= msgs ; i++)
620 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
624 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
625 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
629 if (ret == 0 && mx_commit_message (msg, &ctx) != 0)
635 mx_close_message (&msg);
638 if (ret == 0 && delanswer == M_YES)
640 /* delete the message on the server */
641 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
642 ret = pop_query (pop_data, buffer, sizeof (buffer));
647 mx_close_mailbox (&ctx, NULL);
652 mutt_error ("%s", pop_data->err_msg);
657 mutt_error _("Error while writing mailbox!");
661 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last, msgs - last);
664 mx_close_mailbox (&ctx, NULL);
668 /* make sure no messages get deleted */
669 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
670 if (pop_query (pop_data, buffer, sizeof (buffer)) == -1)
675 /* exit gracefully */
676 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
677 if (pop_query (pop_data, buffer, sizeof (buffer)) == -1)
679 mutt_socket_close (conn);
684 mutt_error _("Server closed connection!");
685 mutt_socket_close (conn);