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+"))) {
61 mutt_perror (tempfile);
65 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
66 ret = pop_query (pop_data, buf, sizeof (buf));
68 sscanf (buf, "+OK %d %ld", &index, &length);
70 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
71 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
73 if (pop_data->cmd_top == 2) {
75 pop_data->cmd_top = 1;
77 dprint (1, (debugfile, "pop_read_header: set TOP capability\n"));
81 pop_data->cmd_top = 0;
83 dprint (1, (debugfile, "pop_read_header: unset TOP capability\n"));
84 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
85 _("Command TOP is not supported by server."));
94 h->env = mutt_read_rfc822_header (f, h, 0, 0);
95 h->content->length = length - h->content->offset + 1;
99 fgets (buf, sizeof (buf), f);
105 mutt_error ("%s", pop_data->err_msg);
110 mutt_error _("Can't write header to temporary file!");
122 static int fetch_uidl (char *line, void *data)
125 CONTEXT *ctx = (CONTEXT *) data;
126 POP_DATA *pop_data = (POP_DATA *) ctx->data;
128 sscanf (line, "%d %s", &index, line);
129 for (i = 0; i < ctx->msgcount; i++)
130 if (!mutt_strcmp (line, ctx->hdrs[i]->data))
133 if (i == ctx->msgcount) {
135 (debugfile, "pop_fetch_headers: new header %d %s\n", index,
138 if (i >= ctx->hdrmax)
139 mx_alloc_memory (ctx);
142 ctx->hdrs[i] = mutt_new_header ();
143 ctx->hdrs[i]->data = safe_strdup (line);
145 else if (ctx->hdrs[i]->index != index - 1)
146 pop_data->clear_cache = 1;
148 ctx->hdrs[i]->refno = index;
149 ctx->hdrs[i]->index = index - 1;
158 * -1 - conection lost,
159 * -2 - invalid command or execution error,
160 * -3 - error writing to tempfile
162 static int pop_fetch_headers (CONTEXT * ctx)
164 int i, ret, old_count, new_count;
165 POP_DATA *pop_data = (POP_DATA *) ctx->data;
167 time (&pop_data->check_time);
168 pop_data->clear_cache = 0;
170 for (i = 0; i < ctx->msgcount; i++)
171 ctx->hdrs[i]->refno = -1;
173 old_count = ctx->msgcount;
174 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
175 new_count = ctx->msgcount;
176 ctx->msgcount = old_count;
178 if (pop_data->cmd_uidl == 2) {
180 pop_data->cmd_uidl = 1;
182 dprint (1, (debugfile, "pop_fetch_headers: set UIDL capability\n"));
185 if (ret == -2 && pop_data->cmd_uidl == 2) {
186 pop_data->cmd_uidl = 0;
188 dprint (1, (debugfile, "pop_fetch_headers: unset UIDL capability\n"));
189 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
190 _("Command UIDL is not supported by server."));
195 for (i = 0; i < old_count; i++)
196 if (ctx->hdrs[i]->refno == -1)
197 ctx->hdrs[i]->deleted = 1;
199 for (i = old_count; i < new_count; i++) {
200 mutt_message (_("Fetching message headers... [%d/%d]"),
201 i + 1 - old_count, new_count - old_count);
203 ret = pop_read_header (pop_data, ctx->hdrs[i]);
211 mx_update_context (ctx, i - old_count);
215 for (i = ctx->msgcount; i < new_count; i++)
216 mutt_free_header (&ctx->hdrs[i]);
221 return (new_count - old_count);
224 /* open POP mailbox - fetch only headers */
225 int pop_open_mailbox (CONTEXT * ctx)
228 char buf[LONG_STRING];
234 if (pop_parse_path (ctx->path, &acct)) {
235 mutt_error (_("%s is an invalid POP path"), ctx->path);
240 mutt_account_tourl (&acct, &url);
242 url_ciss_tostring (&url, buf, sizeof (buf), 0);
243 conn = mutt_conn_find (NULL, &acct);
248 ctx->path = safe_strdup (buf);
250 pop_data = safe_calloc (1, sizeof (POP_DATA));
251 pop_data->conn = conn;
252 ctx->data = pop_data;
254 if (pop_open_connection (pop_data) < 0)
257 conn->data = pop_data;
260 if (pop_reconnect (ctx) < 0)
263 ctx->size = pop_data->size;
265 mutt_message _("Fetching list of messages...");
267 ret = pop_fetch_headers (ctx);
279 /* delete all cached messages */
280 static void pop_clear_cache (POP_DATA * pop_data)
284 if (!pop_data->clear_cache)
287 dprint (1, (debugfile, "pop_clear_cache: delete cached messages\n"));
289 for (i = 0; i < POP_CACHE_LEN; i++) {
290 if (pop_data->cache[i].path) {
291 unlink (pop_data->cache[i].path);
292 FREE (&pop_data->cache[i].path);
297 /* close POP mailbox */
298 void pop_close_mailbox (CONTEXT * ctx)
300 POP_DATA *pop_data = (POP_DATA *) ctx->data;
307 if (pop_data->status != POP_NONE)
308 mutt_socket_close (pop_data->conn);
310 pop_data->status = POP_NONE;
312 pop_data->clear_cache = 1;
313 pop_clear_cache (pop_data);
315 if (!pop_data->conn->data)
316 mutt_socket_free (pop_data->conn);
321 /* fetch message from POP server */
322 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
326 char buf[LONG_STRING];
327 char path[_POSIX_PATH_MAX];
328 char *m = _("Fetching message...");
329 POP_DATA *pop_data = (POP_DATA *) ctx->data;
331 HEADER *h = ctx->hdrs[msgno];
333 /* see if we already have the message in our cache */
334 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
337 if (cache->index == h->index) {
338 /* yes, so just return a pointer to the message */
339 msg->fp = fopen (cache->path, "r");
343 mutt_perror (cache->path);
348 /* clear the previous entry */
349 unlink (cache->path);
355 if (pop_reconnect (ctx) < 0)
358 /* verify that massage index is correct */
361 _("The message index is incorrect. Try reopening the mailbox.");
369 msg->fp = safe_fopen (path, "w+");
376 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
378 ret = pop_fetch_data (pop_data, buf, m, fetch_message, msg->fp);
382 safe_fclose (&msg->fp);
386 mutt_error ("%s", pop_data->err_msg);
392 mutt_error _("Can't write message to temporary file!");
399 /* Update the header information. Previously, we only downloaded a
400 * portion of the headers, those required for the main display.
402 cache->index = h->index;
403 cache->path = safe_strdup (path);
406 mutt_free_envelope (&h->env);
407 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
410 fgets (buf, sizeof (buf), msg->fp);
411 while (!feof (msg->fp)) {
412 ctx->hdrs[msgno]->lines++;
413 fgets (buf, sizeof (buf), msg->fp);
416 h->content->length = ftell (msg->fp) - h->content->offset;
418 /* This needs to be done in case this is a multipart message */
420 h->security = crypt_query (h->content);
428 /* update POP mailbox - delete messages from server */
429 int pop_sync_mailbox (CONTEXT * ctx, int *index_hint)
432 char buf[LONG_STRING];
433 POP_DATA *pop_data = (POP_DATA *) ctx->data;
435 pop_data->check_time = 0;
438 if (pop_reconnect (ctx) < 0)
441 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
443 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
444 if (ctx->hdrs[i]->deleted) {
445 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
446 ret = pop_query (pop_data, buf, sizeof (buf));
451 strfcpy (buf, "QUIT\r\n", sizeof (buf));
452 ret = pop_query (pop_data, buf, sizeof (buf));
456 pop_data->clear_cache = 1;
457 pop_clear_cache (pop_data);
458 pop_data->status = POP_DISCONNECTED;
463 mutt_error ("%s", pop_data->err_msg);
470 /* Check for new messages and fetch headers */
471 int pop_check_mailbox (CONTEXT * ctx, int *index_hint)
474 POP_DATA *pop_data = (POP_DATA *) ctx->data;
476 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
481 mutt_socket_close (pop_data->conn);
483 if (pop_open_connection (pop_data) < 0)
486 ctx->size = pop_data->size;
488 mutt_message _("Checking for new messages...");
490 ret = pop_fetch_headers (ctx);
491 pop_clear_cache (pop_data);
502 /* Fetch messages and save them in $spoolfile */
503 void pop_fetch_mail (void)
505 char buffer[LONG_STRING];
506 char msgbuf[SHORT_STRING];
508 int i, delanswer, last = 0, msgs, bytes, rset = 0, ret;
516 mutt_error _("POP host is not defined.");
521 url = p = safe_calloc (strlen (PopHost) + 7, sizeof (char));
522 if (url_check_scheme (PopHost) == U_UNKNOWN) {
523 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
524 p = strchr (url, '\0');
526 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
528 ret = pop_parse_path (url, &acct);
531 mutt_error (_("%s is an invalid POP path"), PopHost);
535 conn = mutt_conn_find (NULL, &acct);
539 pop_data = safe_calloc (1, sizeof (POP_DATA));
540 pop_data->conn = conn;
542 if (pop_open_connection (pop_data) < 0) {
543 mutt_socket_free (pop_data->conn);
548 conn->data = pop_data;
550 mutt_message _("Checking for new messages...");
552 /* find out how many messages are in the mailbox. */
553 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
554 ret = pop_query (pop_data, buffer, sizeof (buffer));
558 mutt_error ("%s", pop_data->err_msg);
562 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
564 /* only get unread messages */
565 if (msgs > 0 && option (OPTPOPLAST)) {
566 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
567 ret = pop_query (pop_data, buffer, sizeof (buffer));
571 sscanf (buffer, "+OK %d", &last);
575 mutt_message _("No new mail in POP mailbox.");
580 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
584 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
586 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
588 mutt_message ("%s", msgbuf);
590 for (i = last + 1; i <= msgs; i++) {
591 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
594 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
595 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
599 if (ret == 0 && mx_commit_message (msg, &ctx) != 0) {
604 mx_close_message (&msg);
607 if (ret == 0 && delanswer == M_YES) {
608 /* delete the message on the server */
609 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
610 ret = pop_query (pop_data, buffer, sizeof (buffer));
614 mx_close_mailbox (&ctx, NULL);
618 mutt_error ("%s", pop_data->err_msg);
622 mutt_error _("Error while writing mailbox!");
627 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
631 mx_close_mailbox (&ctx, NULL);
634 /* make sure no messages get deleted */
635 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
636 if (pop_query (pop_data, buffer, sizeof (buffer)) == -1)
641 /* exit gracefully */
642 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
643 if (pop_query (pop_data, buffer, sizeof (buffer)) == -1)
645 mutt_socket_close (conn);
650 mutt_error _("Server closed connection!");
651 mutt_socket_close (conn);