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)
47 char buf[LONG_STRING];
48 char tempfile[_POSIX_PATH_MAX];
50 mutt_mktemp (tempfile);
51 if (!(f = safe_fopen (tempfile, "w+"))) {
52 mutt_perror (tempfile);
56 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
57 ret = pop_query (pop_data, buf, sizeof (buf));
59 sscanf (buf, "+OK %d %ld", &index, &length);
61 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
62 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
64 if (pop_data->cmd_top == 2) {
66 pop_data->cmd_top = 1;
68 dprint (1, (debugfile, "pop_read_header: set TOP capability\n"));
72 pop_data->cmd_top = 0;
74 dprint (1, (debugfile, "pop_read_header: unset TOP capability\n"));
75 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
76 _("Command TOP is not supported by server."));
85 h->env = mutt_read_rfc822_header (f, h, 0, 0);
86 h->content->length = length - h->content->offset + 1;
90 fgets (buf, sizeof (buf), f);
96 mutt_error ("%s", pop_data->err_msg);
101 mutt_error _("Can't write header to temporary file!");
113 static int fetch_uidl (char *line, void *data)
116 CONTEXT *ctx = (CONTEXT *) data;
117 POP_DATA *pop_data = (POP_DATA *) ctx->data;
119 sscanf (line, "%d %s", &index, line);
120 for (i = 0; i < ctx->msgcount; i++)
121 if (!mutt_strcmp (line, ctx->hdrs[i]->data))
124 if (i == ctx->msgcount) {
126 (debugfile, "pop_fetch_headers: new header %d %s\n", index,
129 if (i >= ctx->hdrmax)
130 mx_alloc_memory (ctx);
133 ctx->hdrs[i] = mutt_new_header ();
134 ctx->hdrs[i]->data = safe_strdup (line);
136 else if (ctx->hdrs[i]->index != index - 1)
137 pop_data->clear_cache = 1;
139 ctx->hdrs[i]->refno = index;
140 ctx->hdrs[i]->index = index - 1;
149 * -1 - conection lost,
150 * -2 - invalid command or execution error,
151 * -3 - error writing to tempfile
153 static int pop_fetch_headers (CONTEXT * ctx)
155 int i, ret, old_count, new_count;
156 POP_DATA *pop_data = (POP_DATA *) ctx->data;
158 time (&pop_data->check_time);
159 pop_data->clear_cache = 0;
161 for (i = 0; i < ctx->msgcount; i++)
162 ctx->hdrs[i]->refno = -1;
164 old_count = ctx->msgcount;
165 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
166 new_count = ctx->msgcount;
167 ctx->msgcount = old_count;
169 if (pop_data->cmd_uidl == 2) {
171 pop_data->cmd_uidl = 1;
173 dprint (1, (debugfile, "pop_fetch_headers: set UIDL capability\n"));
176 if (ret == -2 && pop_data->cmd_uidl == 2) {
177 pop_data->cmd_uidl = 0;
179 dprint (1, (debugfile, "pop_fetch_headers: unset UIDL capability\n"));
180 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
181 _("Command UIDL is not supported by server."));
186 for (i = 0; i < old_count; i++)
187 if (ctx->hdrs[i]->refno == -1)
188 ctx->hdrs[i]->deleted = 1;
190 for (i = old_count; i < new_count; i++) {
191 mutt_message (_("Fetching message headers... [%d/%d]"),
192 i + 1 - old_count, new_count - old_count);
194 ret = pop_read_header (pop_data, ctx->hdrs[i]);
202 mx_update_context (ctx, i - old_count);
206 for (i = ctx->msgcount; i < new_count; i++)
207 mutt_free_header (&ctx->hdrs[i]);
212 return (new_count - old_count);
215 /* open POP mailbox - fetch only headers */
216 int pop_open_mailbox (CONTEXT * ctx)
219 char buf[LONG_STRING];
225 if (pop_parse_path (ctx->path, &acct)) {
226 mutt_error (_("%s is an invalid POP path"), ctx->path);
231 mutt_account_tourl (&acct, &url);
233 url_ciss_tostring (&url, buf, sizeof (buf), 0);
234 conn = mutt_conn_find (NULL, &acct);
239 ctx->path = safe_strdup (buf);
241 pop_data = safe_calloc (1, sizeof (POP_DATA));
242 pop_data->conn = conn;
243 ctx->data = pop_data;
245 if (pop_open_connection (pop_data) < 0)
248 conn->data = pop_data;
251 if (pop_reconnect (ctx) < 0)
254 ctx->size = pop_data->size;
256 mutt_message _("Fetching list of messages...");
258 ret = pop_fetch_headers (ctx);
270 /* delete all cached messages */
271 static void pop_clear_cache (POP_DATA * pop_data)
275 if (!pop_data->clear_cache)
278 dprint (1, (debugfile, "pop_clear_cache: delete cached messages\n"));
280 for (i = 0; i < POP_CACHE_LEN; i++) {
281 if (pop_data->cache[i].path) {
282 unlink (pop_data->cache[i].path);
283 FREE (&pop_data->cache[i].path);
288 /* close POP mailbox */
289 void pop_close_mailbox (CONTEXT * ctx)
291 POP_DATA *pop_data = (POP_DATA *) ctx->data;
298 if (pop_data->status != POP_NONE)
299 mutt_socket_close (pop_data->conn);
301 pop_data->status = POP_NONE;
303 pop_data->clear_cache = 1;
304 pop_clear_cache (pop_data);
306 if (!pop_data->conn->data)
307 mutt_socket_free (pop_data->conn);
312 /* fetch message from POP server */
313 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
317 char buf[LONG_STRING];
318 char path[_POSIX_PATH_MAX];
319 char *m = _("Fetching message...");
320 POP_DATA *pop_data = (POP_DATA *) ctx->data;
322 HEADER *h = ctx->hdrs[msgno];
324 /* see if we already have the message in our cache */
325 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
328 if (cache->index == h->index) {
329 /* yes, so just return a pointer to the message */
330 msg->fp = fopen (cache->path, "r");
334 mutt_perror (cache->path);
339 /* clear the previous entry */
340 unlink (cache->path);
346 if (pop_reconnect (ctx) < 0)
349 /* verify that massage index is correct */
352 _("The message index is incorrect. Try reopening the mailbox.");
360 msg->fp = safe_fopen (path, "w+");
367 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
369 ret = pop_fetch_data (pop_data, buf, m, fetch_message, msg->fp);
373 safe_fclose (&msg->fp);
377 mutt_error ("%s", pop_data->err_msg);
383 mutt_error _("Can't write message to temporary file!");
390 /* Update the header information. Previously, we only downloaded a
391 * portion of the headers, those required for the main display.
393 cache->index = h->index;
394 cache->path = safe_strdup (path);
397 mutt_free_envelope (&h->env);
398 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
401 fgets (buf, sizeof (buf), msg->fp);
402 while (!feof (msg->fp)) {
403 ctx->hdrs[msgno]->lines++;
404 fgets (buf, sizeof (buf), msg->fp);
407 h->content->length = ftell (msg->fp) - h->content->offset;
409 /* This needs to be done in case this is a multipart message */
411 h->security = crypt_query (h->content);
419 /* update POP mailbox - delete messages from server */
420 int pop_sync_mailbox (CONTEXT * ctx, int *index_hint)
423 char buf[LONG_STRING];
424 POP_DATA *pop_data = (POP_DATA *) ctx->data;
426 pop_data->check_time = 0;
429 if (pop_reconnect (ctx) < 0)
432 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
434 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
435 if (ctx->hdrs[i]->deleted) {
436 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
437 ret = pop_query (pop_data, buf, sizeof (buf));
442 strfcpy (buf, "QUIT\r\n", sizeof (buf));
443 ret = pop_query (pop_data, buf, sizeof (buf));
447 pop_data->clear_cache = 1;
448 pop_clear_cache (pop_data);
449 pop_data->status = POP_DISCONNECTED;
454 mutt_error ("%s", pop_data->err_msg);
461 /* Check for new messages and fetch headers */
462 int pop_check_mailbox (CONTEXT * ctx, int *index_hint)
465 POP_DATA *pop_data = (POP_DATA *) ctx->data;
467 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
472 mutt_socket_close (pop_data->conn);
474 if (pop_open_connection (pop_data) < 0)
477 ctx->size = pop_data->size;
479 mutt_message _("Checking for new messages...");
481 ret = pop_fetch_headers (ctx);
482 pop_clear_cache (pop_data);
493 /* Fetch messages and save them in $spoolfile */
494 void pop_fetch_mail (void)
496 char buffer[LONG_STRING];
497 char msgbuf[SHORT_STRING];
499 int i, delanswer, last = 0, msgs, bytes, rset = 0, ret;
507 mutt_error _("POP host is not defined.");
512 url = p = safe_calloc (mutt_strlen (PopHost) + 7, sizeof (char));
513 if (url_check_scheme (PopHost) == U_UNKNOWN) {
514 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
515 p = strchr (url, '\0');
517 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
519 ret = pop_parse_path (url, &acct);
522 mutt_error (_("%s is an invalid POP path"), PopHost);
526 conn = mutt_conn_find (NULL, &acct);
530 pop_data = safe_calloc (1, sizeof (POP_DATA));
531 pop_data->conn = conn;
533 if (pop_open_connection (pop_data) < 0) {
534 mutt_socket_free (pop_data->conn);
539 conn->data = pop_data;
541 mutt_message _("Checking for new messages...");
543 /* find out how many messages are in the mailbox. */
544 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
545 ret = pop_query (pop_data, buffer, sizeof (buffer));
549 mutt_error ("%s", pop_data->err_msg);
553 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
555 /* only get unread messages */
556 if (msgs > 0 && option (OPTPOPLAST)) {
557 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
558 ret = pop_query (pop_data, buffer, sizeof (buffer));
562 sscanf (buffer, "+OK %d", &last);
566 mutt_message _("No new mail in POP mailbox.");
571 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
575 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
577 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
579 mutt_message ("%s", msgbuf);
581 for (i = last + 1; i <= msgs; i++) {
582 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
585 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
586 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
590 if (ret == 0 && mx_commit_message (msg, &ctx) != 0) {
595 mx_close_message (&msg);
598 if (ret == 0 && delanswer == M_YES) {
599 /* delete the message on the server */
600 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
601 ret = pop_query (pop_data, buffer, sizeof (buffer));
605 mx_close_mailbox (&ctx, NULL);
609 mutt_error ("%s", pop_data->err_msg);
613 mutt_error _("Error while writing mailbox!");
618 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
622 mx_close_mailbox (&ctx, NULL);
625 /* make sure no messages get deleted */
626 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
627 if (pop_query (pop_data, buffer, sizeof (buffer)) == -1)
632 /* exit gracefully */
633 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
634 if (pop_query (pop_data, buffer, sizeof (buffer)) == -1)
636 mutt_socket_close (conn);
641 mutt_error _("Server closed connection!");
642 mutt_socket_close (conn);