2 * Copyright notice from original mutt:
3 * Copyright (C) 2000-2002 Vsevolod Volkov <vvv@mutt.org.ua>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
17 #include "mutt_crypt.h"
22 /* write line to file */
23 static int fetch_message (char *line, void *file)
25 FILE *f = (FILE *) file;
28 if (fputc ('\n', f) == EOF)
38 * -1 - conection lost,
39 * -2 - invalid command or execution error,
40 * -3 - error writing to tempfile
42 static int pop_read_header (POP_DATA * pop_data, HEADER * h)
48 char buf[LONG_STRING];
49 char tempfile[_POSIX_PATH_MAX];
51 mutt_mktemp (tempfile);
52 if (!(f = safe_fopen (tempfile, "w+"))) {
53 mutt_perror (tempfile);
57 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
58 ret = pop_query (pop_data, buf, sizeof (buf));
60 sscanf (buf, "+OK %d %ld", &index, &length);
62 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
63 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
65 if (pop_data->cmd_top == 2) {
67 pop_data->cmd_top = 1;
69 dprint (1, (debugfile, "pop_read_header: set TOP capability\n"));
73 pop_data->cmd_top = 0;
75 dprint (1, (debugfile, "pop_read_header: unset TOP capability\n"));
76 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
77 _("Command TOP is not supported by server."));
86 h->env = mutt_read_rfc822_header (f, h, 0, 0);
87 h->content->length = length - h->content->offset + 1;
91 fgets (buf, sizeof (buf), f);
97 mutt_error ("%s", pop_data->err_msg);
100 case PFD_FUNCT_ERROR:
102 mutt_error _("Can't write header to temporary file!");
114 static int fetch_uidl (char *line, void *data)
117 CONTEXT *ctx = (CONTEXT *) data;
118 POP_DATA *pop_data = (POP_DATA *) ctx->data;
120 sscanf (line, "%d %s", &index, line);
121 for (i = 0; i < ctx->msgcount; i++)
122 if (!mutt_strcmp (line, ctx->hdrs[i]->data))
125 if (i == ctx->msgcount) {
127 (debugfile, "pop_fetch_headers: new header %d %s\n", index,
130 if (i >= ctx->hdrmax)
131 mx_alloc_memory (ctx);
134 ctx->hdrs[i] = mutt_new_header ();
135 ctx->hdrs[i]->data = safe_strdup (line);
137 else if (ctx->hdrs[i]->index != index - 1)
138 pop_data->clear_cache = 1;
140 ctx->hdrs[i]->refno = index;
141 ctx->hdrs[i]->index = index - 1;
150 * -1 - conection lost,
151 * -2 - invalid command or execution error,
152 * -3 - error writing to tempfile
154 static int pop_fetch_headers (CONTEXT * ctx)
156 int i, old_count, new_count;
157 pop_query_status ret;
158 POP_DATA *pop_data = (POP_DATA *) ctx->data;
160 time (&pop_data->check_time);
161 pop_data->clear_cache = 0;
163 for (i = 0; i < ctx->msgcount; i++)
164 ctx->hdrs[i]->refno = -1;
166 old_count = ctx->msgcount;
167 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
168 new_count = ctx->msgcount;
169 ctx->msgcount = old_count;
171 if (pop_data->cmd_uidl == 2) {
173 pop_data->cmd_uidl = 1;
175 dprint (1, (debugfile, "pop_fetch_headers: set UIDL capability\n"));
178 if (ret == PQ_ERR && pop_data->cmd_uidl == 2) {
179 pop_data->cmd_uidl = 0;
181 dprint (1, (debugfile, "pop_fetch_headers: unset UIDL capability\n"));
182 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
183 _("Command UIDL is not supported by server."));
188 for (i = 0; i < old_count; i++)
189 if (ctx->hdrs[i]->refno == -1)
190 ctx->hdrs[i]->deleted = 1;
192 for (i = old_count; i < new_count; i++) {
193 mutt_message (_("Fetching message headers... [%d/%d]"),
194 i + 1 - old_count, new_count - old_count);
196 ret = pop_read_header (pop_data, ctx->hdrs[i]);
204 mx_update_context (ctx, i - old_count);
208 for (i = ctx->msgcount; i < new_count; i++)
209 mutt_free_header (&ctx->hdrs[i]);
214 return (new_count - old_count);
217 /* open POP mailbox - fetch only headers */
218 int pop_open_mailbox (CONTEXT * ctx)
221 char buf[LONG_STRING];
227 if (pop_parse_path (ctx->path, &acct)) {
228 mutt_error (_("%s is an invalid POP path"), ctx->path);
233 mutt_account_tourl (&acct, &url);
235 url_ciss_tostring (&url, buf, sizeof (buf), 0);
236 conn = mutt_conn_find (NULL, &acct);
241 ctx->path = safe_strdup (buf);
243 pop_data = safe_calloc (1, sizeof (POP_DATA));
244 pop_data->conn = conn;
245 ctx->data = pop_data;
247 if (pop_open_connection (pop_data) < 0)
250 conn->data = pop_data;
253 if (pop_reconnect (ctx) != PQ_OK)
256 ctx->size = pop_data->size;
258 mutt_message _("Fetching list of messages...");
260 ret = pop_fetch_headers (ctx);
272 /* delete all cached messages */
273 static void pop_clear_cache (POP_DATA * pop_data)
277 if (!pop_data->clear_cache)
280 dprint (1, (debugfile, "pop_clear_cache: delete cached messages\n"));
282 for (i = 0; i < POP_CACHE_LEN; i++) {
283 if (pop_data->cache[i].path) {
284 unlink (pop_data->cache[i].path);
285 FREE (&pop_data->cache[i].path);
290 /* close POP mailbox */
291 void pop_close_mailbox (CONTEXT * ctx)
293 POP_DATA *pop_data = (POP_DATA *) ctx->data;
300 if (pop_data->status != POP_NONE)
301 mutt_socket_close (pop_data->conn);
303 pop_data->status = POP_NONE;
305 pop_data->clear_cache = 1;
306 pop_clear_cache (pop_data);
308 if (!pop_data->conn->data)
309 mutt_socket_free (pop_data->conn);
314 /* fetch message from POP server */
315 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
319 char buf[LONG_STRING];
320 char path[_POSIX_PATH_MAX];
321 char *m = _("Fetching message...");
322 POP_DATA *pop_data = (POP_DATA *) ctx->data;
324 HEADER *h = ctx->hdrs[msgno];
326 /* see if we already have the message in our cache */
327 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
330 if (cache->index == h->index) {
331 /* yes, so just return a pointer to the message */
332 msg->fp = fopen (cache->path, "r");
336 mutt_perror (cache->path);
341 /* clear the previous entry */
342 unlink (cache->path);
348 if (pop_reconnect (ctx) != PQ_OK)
351 /* verify that massage index is correct */
354 _("The message index is incorrect. Try reopening the mailbox.");
362 msg->fp = safe_fopen (path, "w+");
369 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
371 ret = pop_fetch_data (pop_data, buf, m, fetch_message, msg->fp);
375 safe_fclose (&msg->fp);
379 mutt_error ("%s", pop_data->err_msg);
384 if (ret == PFD_FUNCT_ERROR) {
385 mutt_error _("Can't write message to temporary file!");
392 /* Update the header information. Previously, we only downloaded a
393 * portion of the headers, those required for the main display.
395 cache->index = h->index;
396 cache->path = safe_strdup (path);
399 mutt_free_envelope (&h->env);
400 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
403 fgets (buf, sizeof (buf), msg->fp);
404 while (!feof (msg->fp)) {
405 ctx->hdrs[msgno]->lines++;
406 fgets (buf, sizeof (buf), msg->fp);
409 h->content->length = ftell (msg->fp) - h->content->offset;
411 /* This needs to be done in case this is a multipart message */
413 h->security = crypt_query (h->content);
421 /* update POP mailbox - delete messages from server */
422 pop_query_status pop_sync_mailbox (CONTEXT * ctx, int *index_hint)
425 pop_query_status ret;
426 char buf[LONG_STRING];
427 POP_DATA *pop_data = (POP_DATA *) ctx->data;
429 pop_data->check_time = 0;
432 if (pop_reconnect (ctx) != PQ_OK)
433 return PQ_NOT_CONNECTED;
435 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
437 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
438 if (ctx->hdrs[i]->deleted) {
439 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
440 ret = pop_query (pop_data, buf, sizeof (buf));
445 strfcpy (buf, "QUIT\r\n", sizeof (buf));
446 ret = pop_query (pop_data, buf, sizeof (buf));
450 pop_data->clear_cache = 1;
451 pop_clear_cache (pop_data);
452 pop_data->status = POP_DISCONNECTED;
457 mutt_error ("%s", pop_data->err_msg);
459 return PQ_NOT_CONNECTED;
464 /* Check for new messages and fetch headers */
465 int pop_check_mailbox (CONTEXT * ctx, int *index_hint)
468 POP_DATA *pop_data = (POP_DATA *) ctx->data;
470 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
475 mutt_socket_close (pop_data->conn);
477 if (pop_open_connection (pop_data) < 0)
480 ctx->size = pop_data->size;
482 mutt_message _("Checking for new messages...");
484 ret = pop_fetch_headers (ctx);
485 pop_clear_cache (pop_data);
496 /* Fetch messages and save them in $spoolfile */
497 void pop_fetch_mail (void)
499 char buffer[LONG_STRING];
500 char msgbuf[SHORT_STRING];
502 int i, delanswer, last = 0, msgs, bytes, rset = 0;
503 pop_query_status ret;
511 mutt_error _("POP host is not defined.");
516 url = p = safe_calloc (strlen (PopHost) + 7, sizeof (char));
517 if (url_check_scheme (PopHost) == U_UNKNOWN) {
518 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
519 p = strchr (url, '\0');
521 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
523 ret = pop_parse_path (url, &acct);
526 mutt_error (_("%s is an invalid POP path"), PopHost);
530 conn = mutt_conn_find (NULL, &acct);
534 pop_data = safe_calloc (1, sizeof (POP_DATA));
535 pop_data->conn = conn;
537 if (pop_open_connection (pop_data) < 0) {
538 mutt_socket_free (pop_data->conn);
543 conn->data = pop_data;
545 mutt_message _("Checking for new messages...");
547 /* find out how many messages are in the mailbox. */
548 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
549 ret = pop_query (pop_data, buffer, sizeof (buffer));
550 if (ret == PQ_NOT_CONNECTED)
553 mutt_error ("%s", pop_data->err_msg);
557 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
559 /* only get unread messages */
560 if (msgs > 0 && option (OPTPOPLAST)) {
561 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
562 ret = pop_query (pop_data, buffer, sizeof (buffer));
563 if (ret == PQ_NOT_CONNECTED)
566 sscanf (buffer, "+OK %d", &last);
570 mutt_message _("No new mail in POP mailbox.");
575 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
579 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
581 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
583 mutt_message ("%s", msgbuf);
585 for (i = last + 1; i <= msgs; i++) {
586 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
589 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
590 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
591 if (ret == PFD_FUNCT_ERROR)
594 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
596 ret = PFD_FUNCT_ERROR;
599 mx_close_message (&msg);
602 if (ret == PQ_OK && delanswer == M_YES) {
603 /* delete the message on the server */
604 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
605 ret = pop_query (pop_data, buffer, sizeof (buffer));
608 if (ret == PQ_NOT_CONNECTED) {
609 mx_close_mailbox (&ctx, NULL);
613 mutt_error ("%s", pop_data->err_msg);
616 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
617 mutt_error _("Error while writing mailbox!");
622 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
626 mx_close_mailbox (&ctx, NULL);
629 /* make sure no messages get deleted */
630 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
631 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
636 /* exit gracefully */
637 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
638 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
640 mutt_socket_close (conn);
645 mutt_error _("Server closed connection!");
646 mutt_socket_close (conn);