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"
18 #include "mutt_curses.h"
23 #include "lib/debug.h"
28 /* write line to file */
29 static int fetch_message (char *line, void *file)
31 FILE *f = (FILE *) file;
34 if (fputc ('\n', f) == EOF)
44 * -1 - conection lost,
45 * -2 - invalid command or execution error,
46 * -3 - error writing to tempfile
48 static pop_query_status pop_read_header (POP_DATA * pop_data, HEADER * h)
54 char buf[LONG_STRING];
55 char tempfile[_POSIX_PATH_MAX];
57 mutt_mktemp (tempfile);
58 if (!(f = safe_fopen (tempfile, "w+"))) {
59 mutt_perror (tempfile);
60 return PFD_FUNCT_ERROR;
63 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
64 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 == CMD_UNKNOWN) {
73 pop_data->cmd_top = CMD_AVAILABLE;
75 debug_print (1, ("set TOP capability\n"));
79 pop_data->cmd_top = CMD_NOT_AVAILABLE;
81 debug_print (1, ("unset TOP capability\n"));
82 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
83 _("Command TOP is not supported by server."));
92 h->env = mutt_read_rfc822_header (f, h, 0, 0);
93 h->content->length = length - h->content->offset + 1;
97 fgets (buf, sizeof (buf), f);
103 mutt_error ("%s", pop_data->err_msg);
106 case PFD_FUNCT_ERROR:
108 mutt_error _("Can't write header to temporary file!");
112 case PQ_NOT_CONNECTED:
114 mutt_error _("Can't fetch header: Not connected!");
125 static int fetch_uidl (char *line, void *data)
128 CONTEXT *ctx = (CONTEXT *) data;
129 POP_DATA *pop_data = (POP_DATA *) ctx->data;
131 sscanf (line, "%d %s", &index, line);
132 for (i = 0; i < ctx->msgcount; i++)
133 if (!str_cmp (line, ctx->hdrs[i]->data))
136 if (i == ctx->msgcount) {
137 debug_print (1, ("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 = str_dup (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, old_count, new_count;
166 pop_query_status ret;
167 POP_DATA *pop_data = (POP_DATA *) ctx->data;
169 time (&pop_data->check_time);
170 pop_data->clear_cache = 0;
172 for (i = 0; i < ctx->msgcount; i++)
173 ctx->hdrs[i]->refno = -1;
175 old_count = ctx->msgcount;
176 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
177 new_count = ctx->msgcount;
178 ctx->msgcount = old_count;
180 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
182 pop_data->cmd_uidl = CMD_AVAILABLE;
184 debug_print (1, ("set UIDL capability\n"));
187 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
188 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
190 debug_print (1, ("unset UIDL capability\n"));
191 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
192 _("Command UIDL is not supported by server."));
197 for (i = 0; i < old_count; i++)
198 if (ctx->hdrs[i]->refno == -1)
199 ctx->hdrs[i]->deleted = 1;
201 for (i = old_count; i < new_count; i++) {
202 mutt_message (_("Fetching message headers... [%d/%d]"),
203 i + 1 - old_count, new_count - old_count);
205 ret = pop_read_header (pop_data, ctx->hdrs[i]);
213 mx_update_context (ctx, i - old_count);
217 for (i = ctx->msgcount; i < new_count; i++)
218 mutt_free_header (&ctx->hdrs[i]);
223 return (new_count - old_count);
226 /* open POP mailbox - fetch only headers */
227 int pop_open_mailbox (CONTEXT * ctx)
230 char buf[LONG_STRING];
236 if (pop_parse_path (ctx->path, &acct)) {
237 mutt_error (_("%s is an invalid POP path"), ctx->path);
242 mutt_account_tourl (&acct, &url);
244 url_ciss_tostring (&url, buf, sizeof (buf), 0);
245 conn = mutt_conn_find (NULL, &acct);
249 mem_free (&ctx->path);
250 ctx->path = str_dup (buf);
252 pop_data = mem_calloc (1, sizeof (POP_DATA));
253 pop_data->conn = conn;
254 ctx->data = pop_data;
256 if (pop_open_connection (pop_data) < 0)
259 conn->data = pop_data;
262 if (pop_reconnect (ctx) != PQ_OK)
265 ctx->size = pop_data->size;
267 mutt_message _("Fetching list of messages...");
269 ret = pop_fetch_headers (ctx);
281 /* delete all cached messages */
282 static void pop_clear_cache (POP_DATA * pop_data)
286 if (!pop_data->clear_cache)
289 debug_print (1, ("delete cached messages\n"));
291 for (i = 0; i < POP_CACHE_LEN; i++) {
292 if (pop_data->cache[i].path) {
293 unlink (pop_data->cache[i].path);
294 mem_free (&pop_data->cache[i].path);
299 /* close POP mailbox */
300 void pop_close_mailbox (CONTEXT * ctx)
302 POP_DATA *pop_data = (POP_DATA *) ctx->data;
309 if (pop_data->status != POP_NONE)
310 mutt_socket_close (pop_data->conn);
312 pop_data->status = POP_NONE;
314 pop_data->clear_cache = 1;
315 pop_clear_cache (pop_data);
317 if (!pop_data->conn->data)
318 mutt_socket_free (pop_data->conn);
323 /* fetch message from POP server */
324 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
328 char buf[LONG_STRING];
329 char path[_POSIX_PATH_MAX];
331 POP_DATA *pop_data = (POP_DATA *) ctx->data;
333 HEADER *h = ctx->hdrs[msgno];
335 /* see if we already have the message in our cache */
336 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
339 if (cache->index == h->index) {
340 /* yes, so just return a pointer to the message */
341 msg->fp = fopen (cache->path, "r");
345 mutt_perror (cache->path);
350 /* clear the previous entry */
351 unlink (cache->path);
352 mem_free (&cache->path);
357 if (pop_reconnect (ctx) != PQ_OK)
360 /* verify that massage index is correct */
363 _("The message index is incorrect. Try reopening the mailbox.");
368 bar.size = h->content->length + h->content->offset - 1;
369 bar.msg = _("Fetching message...");
370 mutt_progress_bar (&bar, 0);
373 msg->fp = safe_fopen (path, "w+");
380 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
382 ret = pop_fetch_data (pop_data, buf, &bar, fetch_message, msg->fp);
386 safe_fclose (&msg->fp);
390 mutt_error ("%s", pop_data->err_msg);
395 if (ret == PFD_FUNCT_ERROR) {
396 mutt_error _("Can't write message to temporary file!");
403 /* Update the header information. Previously, we only downloaded a
404 * portion of the headers, those required for the main display.
406 cache->index = h->index;
407 cache->path = str_dup (path);
410 mutt_free_envelope (&h->env);
411 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
414 fgets (buf, sizeof (buf), msg->fp);
415 while (!feof (msg->fp)) {
416 ctx->hdrs[msgno]->lines++;
417 fgets (buf, sizeof (buf), msg->fp);
420 h->content->length = ftello (msg->fp) - h->content->offset;
422 /* This needs to be done in case this is a multipart message */
424 h->security = crypt_query (h->content);
432 /* update POP mailbox - delete messages from server */
433 pop_query_status pop_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
436 pop_query_status ret;
437 char buf[LONG_STRING];
438 POP_DATA *pop_data = (POP_DATA *) ctx->data;
440 pop_data->check_time = 0;
443 if (pop_reconnect (ctx) != PQ_OK)
444 return PQ_NOT_CONNECTED;
446 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
448 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
449 if (ctx->hdrs[i]->deleted) {
450 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
451 ret = pop_query (pop_data, buf, sizeof (buf));
456 strfcpy (buf, "QUIT\r\n", sizeof (buf));
457 ret = pop_query (pop_data, buf, sizeof (buf));
461 pop_data->clear_cache = 1;
462 pop_clear_cache (pop_data);
463 pop_data->status = POP_DISCONNECTED;
468 mutt_error ("%s", pop_data->err_msg);
470 return PQ_NOT_CONNECTED;
475 /* Check for new messages and fetch headers */
476 int pop_check_mailbox (CONTEXT * ctx, int *index_hint, int unused)
479 POP_DATA *pop_data = (POP_DATA *) ctx->data;
481 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
486 mutt_socket_close (pop_data->conn);
488 if (pop_open_connection (pop_data) < 0)
491 ctx->size = pop_data->size;
493 mutt_message _("Checking for new messages...");
495 ret = pop_fetch_headers (ctx);
496 pop_clear_cache (pop_data);
507 /* Fetch messages and save them in $spoolfile */
508 void pop_fetch_mail (void)
510 char buffer[LONG_STRING];
511 char msgbuf[SHORT_STRING];
513 int i, delanswer, last = 0, msgs, bytes, rset = 0;
514 pop_query_status ret;
522 mutt_error _("POP host is not defined.");
527 url = p = mem_calloc (strlen (PopHost) + 7, sizeof (char));
528 if (url_check_scheme (PopHost) == U_UNKNOWN) {
529 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
530 p = strchr (url, '\0');
532 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
534 ret = pop_parse_path (url, &acct);
537 mutt_error (_("%s is an invalid POP path"), PopHost);
541 conn = mutt_conn_find (NULL, &acct);
545 pop_data = mem_calloc (1, sizeof (POP_DATA));
546 pop_data->conn = conn;
548 if (pop_open_connection (pop_data) < 0) {
549 mutt_socket_free (pop_data->conn);
550 mem_free (&pop_data);
554 conn->data = pop_data;
556 mutt_message _("Checking for new messages...");
558 /* find out how many messages are in the mailbox. */
559 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
560 ret = pop_query (pop_data, buffer, sizeof (buffer));
561 if (ret == PQ_NOT_CONNECTED)
564 mutt_error ("%s", pop_data->err_msg);
568 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
570 /* only get unread messages */
571 if (msgs > 0 && option (OPTPOPLAST)) {
572 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
573 ret = pop_query (pop_data, buffer, sizeof (buffer));
574 if (ret == PQ_NOT_CONNECTED)
577 sscanf (buffer, "+OK %d", &last);
581 mutt_message _("No new mail in POP mailbox.");
586 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
590 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
592 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
594 mutt_message ("%s", msgbuf);
596 for (i = last + 1; i <= msgs; i++) {
597 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
600 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
601 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
602 if (ret == PFD_FUNCT_ERROR)
605 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
607 ret = PFD_FUNCT_ERROR;
610 mx_close_message (&msg);
613 if (ret == PQ_OK && delanswer == M_YES) {
614 /* delete the message on the server */
615 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
616 ret = pop_query (pop_data, buffer, sizeof (buffer));
619 if (ret == PQ_NOT_CONNECTED) {
620 mx_close_mailbox (&ctx, NULL);
624 mutt_error ("%s", pop_data->err_msg);
627 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
628 mutt_error _("Error while writing mailbox!");
633 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
637 mx_close_mailbox (&ctx, NULL);
640 /* make sure no messages get deleted */
641 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
642 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
647 /* exit gracefully */
648 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
649 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
651 mutt_socket_close (conn);
652 mem_free (&pop_data);
656 mutt_error _("Server closed connection!");
657 mutt_socket_close (conn);
658 mem_free (&pop_data);