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.
14 #include <lib-lib/mem.h>
15 #include <lib-lib/file.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/macros.h>
22 #include "mutt_crypt.h"
23 #include "mutt_curses.h"
25 #include "lib/debug.h"
30 /* write line to file */
31 static int fetch_message (char *line, void *file)
33 FILE *f = (FILE *) file;
36 if (fputc ('\n', f) == EOF)
46 * -1 - conection lost,
47 * -2 - invalid command or execution error,
48 * -3 - error writing to tempfile
50 static pop_query_status 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);
62 return PFD_FUNCT_ERROR;
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 == CMD_UNKNOWN) {
75 pop_data->cmd_top = CMD_AVAILABLE;
77 debug_print (1, ("set TOP capability\n"));
81 pop_data->cmd_top = CMD_NOT_AVAILABLE;
83 debug_print (1, ("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);
108 case PFD_FUNCT_ERROR:
110 mutt_error _("Can't write header to temporary file!");
114 case PQ_NOT_CONNECTED:
116 mutt_error _("Can't fetch header: Not connected!");
127 static int fetch_uidl (char *line, void *data)
130 CONTEXT *ctx = (CONTEXT *) data;
131 POP_DATA *pop_data = (POP_DATA *) ctx->data;
133 sscanf (line, "%d %s", &index, line);
134 for (i = 0; i < ctx->msgcount; i++)
135 if (!m_strcmp(line, ctx->hdrs[i]->data))
138 if (i == ctx->msgcount) {
139 debug_print (1, ("new header %d %s\n", index, line));
141 if (i >= ctx->hdrmax)
142 mx_alloc_memory (ctx);
145 ctx->hdrs[i] = mutt_new_header ();
146 ctx->hdrs[i]->data = m_strdup(line);
148 else if (ctx->hdrs[i]->index != index - 1)
149 pop_data->clear_cache = 1;
151 ctx->hdrs[i]->refno = index;
152 ctx->hdrs[i]->index = index - 1;
161 * -1 - conection lost,
162 * -2 - invalid command or execution error,
163 * -3 - error writing to tempfile
165 static int pop_fetch_headers (CONTEXT * ctx)
167 int i, old_count, new_count;
168 pop_query_status ret;
169 POP_DATA *pop_data = (POP_DATA *) ctx->data;
171 time (&pop_data->check_time);
172 pop_data->clear_cache = 0;
174 for (i = 0; i < ctx->msgcount; i++)
175 ctx->hdrs[i]->refno = -1;
177 old_count = ctx->msgcount;
178 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
179 new_count = ctx->msgcount;
180 ctx->msgcount = old_count;
182 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
184 pop_data->cmd_uidl = CMD_AVAILABLE;
186 debug_print (1, ("set UIDL capability\n"));
189 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
190 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
192 debug_print (1, ("unset UIDL capability\n"));
193 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
194 _("Command UIDL is not supported by server."));
199 for (i = 0; i < old_count; i++)
200 if (ctx->hdrs[i]->refno == -1)
201 ctx->hdrs[i]->deleted = 1;
203 for (i = old_count; i < new_count; i++) {
204 mutt_message (_("Fetching message headers... [%d/%d]"),
205 i + 1 - old_count, new_count - old_count);
207 ret = pop_read_header (pop_data, ctx->hdrs[i]);
215 mx_update_context (ctx, i - old_count);
219 for (i = ctx->msgcount; i < new_count; i++)
220 mutt_free_header (&ctx->hdrs[i]);
225 return (new_count - old_count);
228 /* open POP mailbox - fetch only headers */
229 int pop_open_mailbox (CONTEXT * ctx)
232 char buf[LONG_STRING];
238 if (pop_parse_path (ctx->path, &acct)) {
239 mutt_error (_("%s is an invalid POP path"), ctx->path);
244 mutt_account_tourl (&acct, &url);
246 url_ciss_tostring (&url, buf, sizeof (buf), 0);
247 conn = mutt_conn_find (NULL, &acct);
251 p_delete(&ctx->path);
252 ctx->path = m_strdup(buf);
254 pop_data = p_new(POP_DATA, 1);
255 pop_data->conn = conn;
256 ctx->data = pop_data;
258 if (pop_open_connection (pop_data) < 0)
261 conn->data = pop_data;
264 if (pop_reconnect (ctx) != PQ_OK)
267 ctx->size = pop_data->size;
269 mutt_message _("Fetching list of messages...");
271 ret = pop_fetch_headers (ctx);
283 /* delete all cached messages */
284 static void pop_clear_cache (POP_DATA * pop_data)
288 if (!pop_data->clear_cache)
291 debug_print (1, ("delete cached messages\n"));
293 for (i = 0; i < POP_CACHE_LEN; i++) {
294 if (pop_data->cache[i].path) {
295 unlink (pop_data->cache[i].path);
296 p_delete(&pop_data->cache[i].path);
301 /* close POP mailbox */
302 void pop_close_mailbox (CONTEXT * ctx)
304 POP_DATA *pop_data = (POP_DATA *) ctx->data;
311 if (pop_data->status != POP_NONE)
312 mutt_socket_close (pop_data->conn);
314 pop_data->status = POP_NONE;
316 pop_data->clear_cache = 1;
317 pop_clear_cache (pop_data);
319 if (!pop_data->conn->data)
320 mutt_socket_free (pop_data->conn);
325 /* fetch message from POP server */
326 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
330 char buf[LONG_STRING];
331 char path[_POSIX_PATH_MAX];
333 POP_DATA *pop_data = (POP_DATA *) ctx->data;
335 HEADER *h = ctx->hdrs[msgno];
337 /* see if we already have the message in our cache */
338 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
341 if (cache->index == h->index) {
342 /* yes, so just return a pointer to the message */
343 msg->fp = fopen (cache->path, "r");
347 mutt_perror (cache->path);
352 /* clear the previous entry */
353 unlink (cache->path);
354 p_delete(&cache->path);
359 if (pop_reconnect (ctx) != PQ_OK)
362 /* verify that massage index is correct */
365 _("The message index is incorrect. Try reopening the mailbox.");
370 bar.size = h->content->length + h->content->offset - 1;
371 bar.msg = _("Fetching message...");
372 mutt_progress_bar (&bar, 0);
375 msg->fp = safe_fopen (path, "w+");
382 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
384 ret = pop_fetch_data (pop_data, buf, &bar, fetch_message, msg->fp);
388 safe_fclose (&msg->fp);
392 mutt_error ("%s", pop_data->err_msg);
397 if (ret == PFD_FUNCT_ERROR) {
398 mutt_error _("Can't write message to temporary file!");
405 /* Update the header information. Previously, we only downloaded a
406 * portion of the headers, those required for the main display.
408 cache->index = h->index;
409 cache->path = m_strdup(path);
412 mutt_free_envelope (&h->env);
413 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
416 fgets (buf, sizeof (buf), msg->fp);
417 while (!feof (msg->fp)) {
418 ctx->hdrs[msgno]->lines++;
419 fgets (buf, sizeof (buf), msg->fp);
422 h->content->length = ftello (msg->fp) - h->content->offset;
424 /* This needs to be done in case this is a multipart message */
426 h->security = crypt_query (h->content);
434 /* update POP mailbox - delete messages from server */
435 pop_query_status pop_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
438 pop_query_status ret;
439 char buf[LONG_STRING];
440 POP_DATA *pop_data = (POP_DATA *) ctx->data;
442 pop_data->check_time = 0;
445 if (pop_reconnect (ctx) != PQ_OK)
446 return PQ_NOT_CONNECTED;
448 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
450 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
451 if (ctx->hdrs[i]->deleted) {
452 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
453 ret = pop_query (pop_data, buf, sizeof (buf));
458 m_strcpy(buf, sizeof(buf), "QUIT\r\n");
459 ret = pop_query (pop_data, buf, sizeof (buf));
463 pop_data->clear_cache = 1;
464 pop_clear_cache (pop_data);
465 pop_data->status = POP_DISCONNECTED;
470 mutt_error ("%s", pop_data->err_msg);
472 return PQ_NOT_CONNECTED;
477 /* Check for new messages and fetch headers */
478 int pop_check_mailbox (CONTEXT * ctx, int *index_hint, int unused)
481 POP_DATA *pop_data = (POP_DATA *) ctx->data;
483 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
488 mutt_socket_close (pop_data->conn);
490 if (pop_open_connection (pop_data) < 0)
493 ctx->size = pop_data->size;
495 mutt_message _("Checking for new messages...");
497 ret = pop_fetch_headers (ctx);
498 pop_clear_cache (pop_data);
509 /* Fetch messages and save them in $spoolfile */
510 void pop_fetch_mail (void)
512 char buffer[LONG_STRING];
513 char msgbuf[SHORT_STRING];
515 int i, delanswer, last = 0, msgs, bytes, rset = 0;
516 pop_query_status ret;
524 mutt_error _("POP host is not defined.");
529 url = p = p_new(char, strlen (PopHost) + 7);
530 if (url_check_scheme (PopHost) == U_UNKNOWN) {
531 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
532 p = strchr (url, '\0');
534 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
536 ret = pop_parse_path (url, &acct);
539 mutt_error (_("%s is an invalid POP path"), PopHost);
543 conn = mutt_conn_find (NULL, &acct);
547 pop_data = p_new(POP_DATA, 1);
548 pop_data->conn = conn;
550 if (pop_open_connection (pop_data) < 0) {
551 mutt_socket_free (pop_data->conn);
556 conn->data = pop_data;
558 mutt_message _("Checking for new messages...");
560 /* find out how many messages are in the mailbox. */
561 m_strcpy(buffer, sizeof(buffer), "STAT\r\n");
562 ret = pop_query (pop_data, buffer, sizeof (buffer));
563 if (ret == PQ_NOT_CONNECTED)
566 mutt_error ("%s", pop_data->err_msg);
570 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
572 /* only get unread messages */
573 if (msgs > 0 && option (OPTPOPLAST)) {
574 m_strcpy(buffer, sizeof(buffer), "LAST\r\n");
575 ret = pop_query (pop_data, buffer, sizeof (buffer));
576 if (ret == PQ_NOT_CONNECTED)
579 sscanf (buffer, "+OK %d", &last);
583 mutt_message _("No new mail in POP mailbox.");
588 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
592 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
594 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
596 mutt_message ("%s", msgbuf);
598 for (i = last + 1; i <= msgs; i++) {
599 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
602 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
603 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
604 if (ret == PFD_FUNCT_ERROR)
607 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
609 ret = PFD_FUNCT_ERROR;
612 mx_close_message (&msg);
615 if (ret == PQ_OK && delanswer == M_YES) {
616 /* delete the message on the server */
617 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
618 ret = pop_query (pop_data, buffer, sizeof (buffer));
621 if (ret == PQ_NOT_CONNECTED) {
622 mx_close_mailbox (&ctx, NULL);
626 mutt_error ("%s", pop_data->err_msg);
629 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
630 mutt_error _("Error while writing mailbox!");
635 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
639 mx_close_mailbox (&ctx, NULL);
642 /* make sure no messages get deleted */
643 m_strcpy(buffer, sizeof(buffer), "RSET\r\n");
644 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
649 /* exit gracefully */
650 m_strcpy(buffer, sizeof(buffer), "QUIT\r\n");
651 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
653 mutt_socket_close (conn);
658 mutt_error _("Server closed connection!");
659 mutt_socket_close (conn);