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 pop_query_status pop_read_header (POP_DATA * pop_data, HEADER * h)
49 char buf[LONG_STRING];
50 char tempfile[_POSIX_PATH_MAX];
52 mutt_mktemp (tempfile);
53 if (!(f = safe_fopen (tempfile, "w+"))) {
54 mutt_perror (tempfile);
55 return PFD_FUNCT_ERROR;
58 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
59 ret = pop_query (pop_data, buf, sizeof (buf));
61 sscanf (buf, "+OK %d %ld", &index, &length);
63 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
64 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
66 if (pop_data->cmd_top == CMD_UNKNOWN) {
68 pop_data->cmd_top = CMD_AVAILABLE;
70 dprint (1, (debugfile, "pop_read_header: set TOP capability\n"));
74 pop_data->cmd_top = CMD_NOT_AVAILABLE;
76 dprint (1, (debugfile, "pop_read_header: unset TOP capability\n"));
77 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
78 _("Command TOP is not supported by server."));
87 h->env = mutt_read_rfc822_header (f, h, 0, 0);
88 h->content->length = length - h->content->offset + 1;
92 fgets (buf, sizeof (buf), f);
98 mutt_error ("%s", pop_data->err_msg);
101 case PFD_FUNCT_ERROR:
103 mutt_error _("Can't write header to temporary file!");
115 static int fetch_uidl (char *line, void *data)
118 CONTEXT *ctx = (CONTEXT *) data;
119 POP_DATA *pop_data = (POP_DATA *) ctx->data;
121 sscanf (line, "%d %s", &index, line);
122 for (i = 0; i < ctx->msgcount; i++)
123 if (!mutt_strcmp (line, ctx->hdrs[i]->data))
126 if (i == ctx->msgcount) {
128 (debugfile, "pop_fetch_headers: new header %d %s\n", index,
131 if (i >= ctx->hdrmax)
132 mx_alloc_memory (ctx);
135 ctx->hdrs[i] = mutt_new_header ();
136 ctx->hdrs[i]->data = safe_strdup (line);
138 else if (ctx->hdrs[i]->index != index - 1)
139 pop_data->clear_cache = 1;
141 ctx->hdrs[i]->refno = index;
142 ctx->hdrs[i]->index = index - 1;
151 * -1 - conection lost,
152 * -2 - invalid command or execution error,
153 * -3 - error writing to tempfile
155 static int pop_fetch_headers (CONTEXT * ctx)
157 int i, old_count, new_count;
158 pop_query_status ret;
159 POP_DATA *pop_data = (POP_DATA *) ctx->data;
161 time (&pop_data->check_time);
162 pop_data->clear_cache = 0;
164 for (i = 0; i < ctx->msgcount; i++)
165 ctx->hdrs[i]->refno = -1;
167 old_count = ctx->msgcount;
168 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
169 new_count = ctx->msgcount;
170 ctx->msgcount = old_count;
172 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
174 pop_data->cmd_uidl = CMD_AVAILABLE;
176 dprint (1, (debugfile, "pop_fetch_headers: set UIDL capability\n"));
179 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
180 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
182 dprint (1, (debugfile, "pop_fetch_headers: unset UIDL capability\n"));
183 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
184 _("Command UIDL is not supported by server."));
189 for (i = 0; i < old_count; i++)
190 if (ctx->hdrs[i]->refno == -1)
191 ctx->hdrs[i]->deleted = 1;
193 for (i = old_count; i < new_count; i++) {
194 mutt_message (_("Fetching message headers... [%d/%d]"),
195 i + 1 - old_count, new_count - old_count);
197 ret = pop_read_header (pop_data, ctx->hdrs[i]);
205 mx_update_context (ctx, i - old_count);
209 for (i = ctx->msgcount; i < new_count; i++)
210 mutt_free_header (&ctx->hdrs[i]);
215 return (new_count - old_count);
218 /* open POP mailbox - fetch only headers */
219 int pop_open_mailbox (CONTEXT * ctx)
222 char buf[LONG_STRING];
228 if (pop_parse_path (ctx->path, &acct)) {
229 mutt_error (_("%s is an invalid POP path"), ctx->path);
234 mutt_account_tourl (&acct, &url);
236 url_ciss_tostring (&url, buf, sizeof (buf), 0);
237 conn = mutt_conn_find (NULL, &acct);
242 ctx->path = safe_strdup (buf);
244 pop_data = safe_calloc (1, sizeof (POP_DATA));
245 pop_data->conn = conn;
246 ctx->data = pop_data;
248 if (pop_open_connection (pop_data) < 0)
251 conn->data = pop_data;
254 if (pop_reconnect (ctx) != PQ_OK)
257 ctx->size = pop_data->size;
259 mutt_message _("Fetching list of messages...");
261 ret = pop_fetch_headers (ctx);
273 /* delete all cached messages */
274 static void pop_clear_cache (POP_DATA * pop_data)
278 if (!pop_data->clear_cache)
281 dprint (1, (debugfile, "pop_clear_cache: delete cached messages\n"));
283 for (i = 0; i < POP_CACHE_LEN; i++) {
284 if (pop_data->cache[i].path) {
285 unlink (pop_data->cache[i].path);
286 FREE (&pop_data->cache[i].path);
291 /* close POP mailbox */
292 void pop_close_mailbox (CONTEXT * ctx)
294 POP_DATA *pop_data = (POP_DATA *) ctx->data;
301 if (pop_data->status != POP_NONE)
302 mutt_socket_close (pop_data->conn);
304 pop_data->status = POP_NONE;
306 pop_data->clear_cache = 1;
307 pop_clear_cache (pop_data);
309 if (!pop_data->conn->data)
310 mutt_socket_free (pop_data->conn);
315 /* fetch message from POP server */
316 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
320 char buf[LONG_STRING];
321 char path[_POSIX_PATH_MAX];
322 char *m = _("Fetching message...");
323 POP_DATA *pop_data = (POP_DATA *) ctx->data;
325 HEADER *h = ctx->hdrs[msgno];
327 /* see if we already have the message in our cache */
328 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
331 if (cache->index == h->index) {
332 /* yes, so just return a pointer to the message */
333 msg->fp = fopen (cache->path, "r");
337 mutt_perror (cache->path);
342 /* clear the previous entry */
343 unlink (cache->path);
349 if (pop_reconnect (ctx) != PQ_OK)
352 /* verify that massage index is correct */
355 _("The message index is incorrect. Try reopening the mailbox.");
363 msg->fp = safe_fopen (path, "w+");
370 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
372 ret = pop_fetch_data (pop_data, buf, m, fetch_message, msg->fp);
376 safe_fclose (&msg->fp);
380 mutt_error ("%s", pop_data->err_msg);
385 if (ret == PFD_FUNCT_ERROR) {
386 mutt_error _("Can't write message to temporary file!");
393 /* Update the header information. Previously, we only downloaded a
394 * portion of the headers, those required for the main display.
396 cache->index = h->index;
397 cache->path = safe_strdup (path);
400 mutt_free_envelope (&h->env);
401 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
404 fgets (buf, sizeof (buf), msg->fp);
405 while (!feof (msg->fp)) {
406 ctx->hdrs[msgno]->lines++;
407 fgets (buf, sizeof (buf), msg->fp);
410 h->content->length = ftell (msg->fp) - h->content->offset;
412 /* This needs to be done in case this is a multipart message */
414 h->security = crypt_query (h->content);
422 /* update POP mailbox - delete messages from server */
423 pop_query_status pop_sync_mailbox (CONTEXT * ctx, int *index_hint)
426 pop_query_status ret;
427 char buf[LONG_STRING];
428 POP_DATA *pop_data = (POP_DATA *) ctx->data;
430 pop_data->check_time = 0;
433 if (pop_reconnect (ctx) != PQ_OK)
434 return PQ_NOT_CONNECTED;
436 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
438 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
439 if (ctx->hdrs[i]->deleted) {
440 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
441 ret = pop_query (pop_data, buf, sizeof (buf));
446 strfcpy (buf, "QUIT\r\n", sizeof (buf));
447 ret = pop_query (pop_data, buf, sizeof (buf));
451 pop_data->clear_cache = 1;
452 pop_clear_cache (pop_data);
453 pop_data->status = POP_DISCONNECTED;
458 mutt_error ("%s", pop_data->err_msg);
460 return PQ_NOT_CONNECTED;
465 /* Check for new messages and fetch headers */
466 int pop_check_mailbox (CONTEXT * ctx, int *index_hint)
469 POP_DATA *pop_data = (POP_DATA *) ctx->data;
471 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
476 mutt_socket_close (pop_data->conn);
478 if (pop_open_connection (pop_data) < 0)
481 ctx->size = pop_data->size;
483 mutt_message _("Checking for new messages...");
485 ret = pop_fetch_headers (ctx);
486 pop_clear_cache (pop_data);
497 /* Fetch messages and save them in $spoolfile */
498 void pop_fetch_mail (void)
500 char buffer[LONG_STRING];
501 char msgbuf[SHORT_STRING];
503 int i, delanswer, last = 0, msgs, bytes, rset = 0;
504 pop_query_status ret;
512 mutt_error _("POP host is not defined.");
517 url = p = safe_calloc (strlen (PopHost) + 7, sizeof (char));
518 if (url_check_scheme (PopHost) == U_UNKNOWN) {
519 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
520 p = strchr (url, '\0');
522 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
524 ret = pop_parse_path (url, &acct);
527 mutt_error (_("%s is an invalid POP path"), PopHost);
531 conn = mutt_conn_find (NULL, &acct);
535 pop_data = safe_calloc (1, sizeof (POP_DATA));
536 pop_data->conn = conn;
538 if (pop_open_connection (pop_data) < 0) {
539 mutt_socket_free (pop_data->conn);
544 conn->data = pop_data;
546 mutt_message _("Checking for new messages...");
548 /* find out how many messages are in the mailbox. */
549 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
550 ret = pop_query (pop_data, buffer, sizeof (buffer));
551 if (ret == PQ_NOT_CONNECTED)
554 mutt_error ("%s", pop_data->err_msg);
558 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
560 /* only get unread messages */
561 if (msgs > 0 && option (OPTPOPLAST)) {
562 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
563 ret = pop_query (pop_data, buffer, sizeof (buffer));
564 if (ret == PQ_NOT_CONNECTED)
567 sscanf (buffer, "+OK %d", &last);
571 mutt_message _("No new mail in POP mailbox.");
576 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
580 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
582 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
584 mutt_message ("%s", msgbuf);
586 for (i = last + 1; i <= msgs; i++) {
587 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
590 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
591 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
592 if (ret == PFD_FUNCT_ERROR)
595 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
597 ret = PFD_FUNCT_ERROR;
600 mx_close_message (&msg);
603 if (ret == PQ_OK && delanswer == M_YES) {
604 /* delete the message on the server */
605 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
606 ret = pop_query (pop_data, buffer, sizeof (buffer));
609 if (ret == PQ_NOT_CONNECTED) {
610 mx_close_mailbox (&ctx, NULL);
614 mutt_error ("%s", pop_data->err_msg);
617 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
618 mutt_error _("Error while writing mailbox!");
623 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
627 mx_close_mailbox (&ctx, NULL);
630 /* make sure no messages get deleted */
631 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
632 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
637 /* exit gracefully */
638 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
639 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
641 mutt_socket_close (conn);
646 mutt_error _("Server closed connection!");
647 mutt_socket_close (conn);