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/str.h>
16 #include <lib-lib/macros.h>
21 #include "mutt_crypt.h"
22 #include "mutt_curses.h"
24 #include "lib/debug.h"
29 /* write line to file */
30 static int fetch_message (char *line, void *file)
32 FILE *f = (FILE *) file;
35 if (fputc ('\n', f) == EOF)
45 * -1 - conection lost,
46 * -2 - invalid command or execution error,
47 * -3 - error writing to tempfile
49 static pop_query_status pop_read_header (POP_DATA * pop_data, HEADER * h)
55 char buf[LONG_STRING];
56 char tempfile[_POSIX_PATH_MAX];
58 mutt_mktemp (tempfile);
59 if (!(f = safe_fopen (tempfile, "w+"))) {
60 mutt_perror (tempfile);
61 return PFD_FUNCT_ERROR;
64 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
65 ret = pop_query (pop_data, buf, sizeof (buf));
67 sscanf (buf, "+OK %d %ld", &index, &length);
69 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
70 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
72 if (pop_data->cmd_top == CMD_UNKNOWN) {
74 pop_data->cmd_top = CMD_AVAILABLE;
76 debug_print (1, ("set TOP capability\n"));
80 pop_data->cmd_top = CMD_NOT_AVAILABLE;
82 debug_print (1, ("unset TOP capability\n"));
83 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
84 _("Command TOP is not supported by server."));
93 h->env = mutt_read_rfc822_header (f, h, 0, 0);
94 h->content->length = length - h->content->offset + 1;
98 fgets (buf, sizeof (buf), f);
104 mutt_error ("%s", pop_data->err_msg);
107 case PFD_FUNCT_ERROR:
109 mutt_error _("Can't write header to temporary file!");
113 case PQ_NOT_CONNECTED:
115 mutt_error _("Can't fetch header: Not connected!");
126 static int fetch_uidl (char *line, void *data)
129 CONTEXT *ctx = (CONTEXT *) data;
130 POP_DATA *pop_data = (POP_DATA *) ctx->data;
132 sscanf (line, "%d %s", &index, line);
133 for (i = 0; i < ctx->msgcount; i++)
134 if (!str_cmp (line, ctx->hdrs[i]->data))
137 if (i == ctx->msgcount) {
138 debug_print (1, ("new header %d %s\n", index, line));
140 if (i >= ctx->hdrmax)
141 mx_alloc_memory (ctx);
144 ctx->hdrs[i] = mutt_new_header ();
145 ctx->hdrs[i]->data = m_strdup(line);
147 else if (ctx->hdrs[i]->index != index - 1)
148 pop_data->clear_cache = 1;
150 ctx->hdrs[i]->refno = index;
151 ctx->hdrs[i]->index = index - 1;
160 * -1 - conection lost,
161 * -2 - invalid command or execution error,
162 * -3 - error writing to tempfile
164 static int pop_fetch_headers (CONTEXT * ctx)
166 int i, old_count, new_count;
167 pop_query_status ret;
168 POP_DATA *pop_data = (POP_DATA *) ctx->data;
170 time (&pop_data->check_time);
171 pop_data->clear_cache = 0;
173 for (i = 0; i < ctx->msgcount; i++)
174 ctx->hdrs[i]->refno = -1;
176 old_count = ctx->msgcount;
177 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
178 new_count = ctx->msgcount;
179 ctx->msgcount = old_count;
181 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
183 pop_data->cmd_uidl = CMD_AVAILABLE;
185 debug_print (1, ("set UIDL capability\n"));
188 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
189 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
191 debug_print (1, ("unset UIDL capability\n"));
192 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
193 _("Command UIDL is not supported by server."));
198 for (i = 0; i < old_count; i++)
199 if (ctx->hdrs[i]->refno == -1)
200 ctx->hdrs[i]->deleted = 1;
202 for (i = old_count; i < new_count; i++) {
203 mutt_message (_("Fetching message headers... [%d/%d]"),
204 i + 1 - old_count, new_count - old_count);
206 ret = pop_read_header (pop_data, ctx->hdrs[i]);
214 mx_update_context (ctx, i - old_count);
218 for (i = ctx->msgcount; i < new_count; i++)
219 mutt_free_header (&ctx->hdrs[i]);
224 return (new_count - old_count);
227 /* open POP mailbox - fetch only headers */
228 int pop_open_mailbox (CONTEXT * ctx)
231 char buf[LONG_STRING];
237 if (pop_parse_path (ctx->path, &acct)) {
238 mutt_error (_("%s is an invalid POP path"), ctx->path);
243 mutt_account_tourl (&acct, &url);
245 url_ciss_tostring (&url, buf, sizeof (buf), 0);
246 conn = mutt_conn_find (NULL, &acct);
250 p_delete(&ctx->path);
251 ctx->path = m_strdup(buf);
253 pop_data = p_new(POP_DATA, 1);
254 pop_data->conn = conn;
255 ctx->data = pop_data;
257 if (pop_open_connection (pop_data) < 0)
260 conn->data = pop_data;
263 if (pop_reconnect (ctx) != PQ_OK)
266 ctx->size = pop_data->size;
268 mutt_message _("Fetching list of messages...");
270 ret = pop_fetch_headers (ctx);
282 /* delete all cached messages */
283 static void pop_clear_cache (POP_DATA * pop_data)
287 if (!pop_data->clear_cache)
290 debug_print (1, ("delete cached messages\n"));
292 for (i = 0; i < POP_CACHE_LEN; i++) {
293 if (pop_data->cache[i].path) {
294 unlink (pop_data->cache[i].path);
295 p_delete(&pop_data->cache[i].path);
300 /* close POP mailbox */
301 void pop_close_mailbox (CONTEXT * ctx)
303 POP_DATA *pop_data = (POP_DATA *) ctx->data;
310 if (pop_data->status != POP_NONE)
311 mutt_socket_close (pop_data->conn);
313 pop_data->status = POP_NONE;
315 pop_data->clear_cache = 1;
316 pop_clear_cache (pop_data);
318 if (!pop_data->conn->data)
319 mutt_socket_free (pop_data->conn);
324 /* fetch message from POP server */
325 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
329 char buf[LONG_STRING];
330 char path[_POSIX_PATH_MAX];
332 POP_DATA *pop_data = (POP_DATA *) ctx->data;
334 HEADER *h = ctx->hdrs[msgno];
336 /* see if we already have the message in our cache */
337 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
340 if (cache->index == h->index) {
341 /* yes, so just return a pointer to the message */
342 msg->fp = fopen (cache->path, "r");
346 mutt_perror (cache->path);
351 /* clear the previous entry */
352 unlink (cache->path);
353 p_delete(&cache->path);
358 if (pop_reconnect (ctx) != PQ_OK)
361 /* verify that massage index is correct */
364 _("The message index is incorrect. Try reopening the mailbox.");
369 bar.size = h->content->length + h->content->offset - 1;
370 bar.msg = _("Fetching message...");
371 mutt_progress_bar (&bar, 0);
374 msg->fp = safe_fopen (path, "w+");
381 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
383 ret = pop_fetch_data (pop_data, buf, &bar, fetch_message, msg->fp);
387 safe_fclose (&msg->fp);
391 mutt_error ("%s", pop_data->err_msg);
396 if (ret == PFD_FUNCT_ERROR) {
397 mutt_error _("Can't write message to temporary file!");
404 /* Update the header information. Previously, we only downloaded a
405 * portion of the headers, those required for the main display.
407 cache->index = h->index;
408 cache->path = m_strdup(path);
411 mutt_free_envelope (&h->env);
412 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
415 fgets (buf, sizeof (buf), msg->fp);
416 while (!feof (msg->fp)) {
417 ctx->hdrs[msgno]->lines++;
418 fgets (buf, sizeof (buf), msg->fp);
421 h->content->length = ftello (msg->fp) - h->content->offset;
423 /* This needs to be done in case this is a multipart message */
425 h->security = crypt_query (h->content);
433 /* update POP mailbox - delete messages from server */
434 pop_query_status pop_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
437 pop_query_status ret;
438 char buf[LONG_STRING];
439 POP_DATA *pop_data = (POP_DATA *) ctx->data;
441 pop_data->check_time = 0;
444 if (pop_reconnect (ctx) != PQ_OK)
445 return PQ_NOT_CONNECTED;
447 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
449 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
450 if (ctx->hdrs[i]->deleted) {
451 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
452 ret = pop_query (pop_data, buf, sizeof (buf));
457 strfcpy (buf, "QUIT\r\n", sizeof (buf));
458 ret = pop_query (pop_data, buf, sizeof (buf));
462 pop_data->clear_cache = 1;
463 pop_clear_cache (pop_data);
464 pop_data->status = POP_DISCONNECTED;
469 mutt_error ("%s", pop_data->err_msg);
471 return PQ_NOT_CONNECTED;
476 /* Check for new messages and fetch headers */
477 int pop_check_mailbox (CONTEXT * ctx, int *index_hint, int unused)
480 POP_DATA *pop_data = (POP_DATA *) ctx->data;
482 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
487 mutt_socket_close (pop_data->conn);
489 if (pop_open_connection (pop_data) < 0)
492 ctx->size = pop_data->size;
494 mutt_message _("Checking for new messages...");
496 ret = pop_fetch_headers (ctx);
497 pop_clear_cache (pop_data);
508 /* Fetch messages and save them in $spoolfile */
509 void pop_fetch_mail (void)
511 char buffer[LONG_STRING];
512 char msgbuf[SHORT_STRING];
514 int i, delanswer, last = 0, msgs, bytes, rset = 0;
515 pop_query_status ret;
523 mutt_error _("POP host is not defined.");
528 url = p = p_new(char, strlen (PopHost) + 7);
529 if (url_check_scheme (PopHost) == U_UNKNOWN) {
530 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
531 p = strchr (url, '\0');
533 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
535 ret = pop_parse_path (url, &acct);
538 mutt_error (_("%s is an invalid POP path"), PopHost);
542 conn = mutt_conn_find (NULL, &acct);
546 pop_data = p_new(POP_DATA, 1);
547 pop_data->conn = conn;
549 if (pop_open_connection (pop_data) < 0) {
550 mutt_socket_free (pop_data->conn);
555 conn->data = pop_data;
557 mutt_message _("Checking for new messages...");
559 /* find out how many messages are in the mailbox. */
560 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
561 ret = pop_query (pop_data, buffer, sizeof (buffer));
562 if (ret == PQ_NOT_CONNECTED)
565 mutt_error ("%s", pop_data->err_msg);
569 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
571 /* only get unread messages */
572 if (msgs > 0 && option (OPTPOPLAST)) {
573 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
574 ret = pop_query (pop_data, buffer, sizeof (buffer));
575 if (ret == PQ_NOT_CONNECTED)
578 sscanf (buffer, "+OK %d", &last);
582 mutt_message _("No new mail in POP mailbox.");
587 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
591 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
593 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
595 mutt_message ("%s", msgbuf);
597 for (i = last + 1; i <= msgs; i++) {
598 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
601 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
602 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
603 if (ret == PFD_FUNCT_ERROR)
606 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
608 ret = PFD_FUNCT_ERROR;
611 mx_close_message (&msg);
614 if (ret == PQ_OK && delanswer == M_YES) {
615 /* delete the message on the server */
616 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
617 ret = pop_query (pop_data, buffer, sizeof (buffer));
620 if (ret == PQ_NOT_CONNECTED) {
621 mx_close_mailbox (&ctx, NULL);
625 mutt_error ("%s", pop_data->err_msg);
628 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
629 mutt_error _("Error while writing mailbox!");
634 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
638 mx_close_mailbox (&ctx, NULL);
641 /* make sure no messages get deleted */
642 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
643 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
648 /* exit gracefully */
649 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
650 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
652 mutt_socket_close (conn);
657 mutt_error _("Server closed connection!");
658 mutt_socket_close (conn);