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"
26 /* write line to file */
27 static int fetch_message (char *line, void *file)
29 FILE *f = (FILE *) file;
32 if (fputc ('\n', f) == EOF)
42 * -1 - conection lost,
43 * -2 - invalid command or execution error,
44 * -3 - error writing to tempfile
46 static pop_query_status pop_read_header (POP_DATA * pop_data, HEADER * h)
53 char buf[LONG_STRING];
54 char tempfile[_POSIX_PATH_MAX];
56 mutt_mktemp (tempfile);
57 if (!(f = safe_fopen (tempfile, "w+"))) {
58 mutt_perror (tempfile);
59 return PFD_FUNCT_ERROR;
62 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
63 ret = pop_query (pop_data, buf, sizeof (buf));
65 sscanf (buf, "+OK %d %ld", &index, &length);
67 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
68 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
70 if (pop_data->cmd_top == CMD_UNKNOWN) {
72 pop_data->cmd_top = CMD_AVAILABLE;
74 dprint (1, (debugfile, "pop_read_header: set TOP capability\n"));
78 pop_data->cmd_top = CMD_NOT_AVAILABLE;
80 dprint (1, (debugfile, "pop_read_header: unset TOP capability\n"));
81 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
82 _("Command TOP is not supported by server."));
91 h->env = mutt_read_rfc822_header (f, h, 0, 0);
92 h->content->length = length - h->content->offset + 1;
96 fgets (buf, sizeof (buf), f);
102 mutt_error ("%s", pop_data->err_msg);
105 case PFD_FUNCT_ERROR:
107 mutt_error _("Can't write header to temporary file!");
119 static int fetch_uidl (char *line, void *data)
122 CONTEXT *ctx = (CONTEXT *) data;
123 POP_DATA *pop_data = (POP_DATA *) ctx->data;
125 sscanf (line, "%d %s", &index, line);
126 for (i = 0; i < ctx->msgcount; i++)
127 if (!mutt_strcmp (line, ctx->hdrs[i]->data))
130 if (i == ctx->msgcount) {
132 (debugfile, "pop_fetch_headers: new header %d %s\n", index,
135 if (i >= ctx->hdrmax)
136 mx_alloc_memory (ctx);
139 ctx->hdrs[i] = mutt_new_header ();
140 ctx->hdrs[i]->data = safe_strdup (line);
142 else if (ctx->hdrs[i]->index != index - 1)
143 pop_data->clear_cache = 1;
145 ctx->hdrs[i]->refno = index;
146 ctx->hdrs[i]->index = index - 1;
155 * -1 - conection lost,
156 * -2 - invalid command or execution error,
157 * -3 - error writing to tempfile
159 static int pop_fetch_headers (CONTEXT * ctx)
161 int i, old_count, new_count;
162 pop_query_status ret;
163 POP_DATA *pop_data = (POP_DATA *) ctx->data;
165 time (&pop_data->check_time);
166 pop_data->clear_cache = 0;
168 for (i = 0; i < ctx->msgcount; i++)
169 ctx->hdrs[i]->refno = -1;
171 old_count = ctx->msgcount;
172 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
173 new_count = ctx->msgcount;
174 ctx->msgcount = old_count;
176 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
178 pop_data->cmd_uidl = CMD_AVAILABLE;
180 dprint (1, (debugfile, "pop_fetch_headers: set UIDL capability\n"));
183 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
184 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
186 dprint (1, (debugfile, "pop_fetch_headers: unset UIDL capability\n"));
187 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
188 _("Command UIDL is not supported by server."));
193 for (i = 0; i < old_count; i++)
194 if (ctx->hdrs[i]->refno == -1)
195 ctx->hdrs[i]->deleted = 1;
197 for (i = old_count; i < new_count; i++) {
198 mutt_message (_("Fetching message headers... [%d/%d]"),
199 i + 1 - old_count, new_count - old_count);
201 ret = pop_read_header (pop_data, ctx->hdrs[i]);
209 mx_update_context (ctx, i - old_count);
213 for (i = ctx->msgcount; i < new_count; i++)
214 mutt_free_header (&ctx->hdrs[i]);
219 return (new_count - old_count);
222 /* open POP mailbox - fetch only headers */
223 int pop_open_mailbox (CONTEXT * ctx)
226 char buf[LONG_STRING];
232 if (pop_parse_path (ctx->path, &acct)) {
233 mutt_error (_("%s is an invalid POP path"), ctx->path);
238 mutt_account_tourl (&acct, &url);
240 url_ciss_tostring (&url, buf, sizeof (buf), 0);
241 conn = mutt_conn_find (NULL, &acct);
246 ctx->path = safe_strdup (buf);
248 pop_data = safe_calloc (1, sizeof (POP_DATA));
249 pop_data->conn = conn;
250 ctx->data = pop_data;
252 if (pop_open_connection (pop_data) < 0)
255 conn->data = pop_data;
258 if (pop_reconnect (ctx) != PQ_OK)
261 ctx->size = pop_data->size;
263 mutt_message _("Fetching list of messages...");
265 ret = pop_fetch_headers (ctx);
277 /* delete all cached messages */
278 static void pop_clear_cache (POP_DATA * pop_data)
282 if (!pop_data->clear_cache)
285 dprint (1, (debugfile, "pop_clear_cache: delete cached messages\n"));
287 for (i = 0; i < POP_CACHE_LEN; i++) {
288 if (pop_data->cache[i].path) {
289 unlink (pop_data->cache[i].path);
290 FREE (&pop_data->cache[i].path);
295 /* close POP mailbox */
296 void pop_close_mailbox (CONTEXT * ctx)
298 POP_DATA *pop_data = (POP_DATA *) ctx->data;
305 if (pop_data->status != POP_NONE)
306 mutt_socket_close (pop_data->conn);
308 pop_data->status = POP_NONE;
310 pop_data->clear_cache = 1;
311 pop_clear_cache (pop_data);
313 if (!pop_data->conn->data)
314 mutt_socket_free (pop_data->conn);
319 /* fetch message from POP server */
320 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
324 char buf[LONG_STRING];
325 char path[_POSIX_PATH_MAX];
326 char *m = _("Fetching message...");
327 POP_DATA *pop_data = (POP_DATA *) ctx->data;
329 HEADER *h = ctx->hdrs[msgno];
331 /* see if we already have the message in our cache */
332 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
335 if (cache->index == h->index) {
336 /* yes, so just return a pointer to the message */
337 msg->fp = fopen (cache->path, "r");
341 mutt_perror (cache->path);
346 /* clear the previous entry */
347 unlink (cache->path);
353 if (pop_reconnect (ctx) != PQ_OK)
356 /* verify that massage index is correct */
359 _("The message index is incorrect. Try reopening the mailbox.");
367 msg->fp = safe_fopen (path, "w+");
374 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
376 ret = pop_fetch_data (pop_data, buf, m, fetch_message, msg->fp);
380 safe_fclose (&msg->fp);
384 mutt_error ("%s", pop_data->err_msg);
389 if (ret == PFD_FUNCT_ERROR) {
390 mutt_error _("Can't write message to temporary file!");
397 /* Update the header information. Previously, we only downloaded a
398 * portion of the headers, those required for the main display.
400 cache->index = h->index;
401 cache->path = safe_strdup (path);
404 mutt_free_envelope (&h->env);
405 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
408 fgets (buf, sizeof (buf), msg->fp);
409 while (!feof (msg->fp)) {
410 ctx->hdrs[msgno]->lines++;
411 fgets (buf, sizeof (buf), msg->fp);
414 h->content->length = ftell (msg->fp) - h->content->offset;
416 /* This needs to be done in case this is a multipart message */
418 h->security = crypt_query (h->content);
426 /* update POP mailbox - delete messages from server */
427 pop_query_status pop_sync_mailbox (CONTEXT * ctx, int *index_hint)
430 pop_query_status ret;
431 char buf[LONG_STRING];
432 POP_DATA *pop_data = (POP_DATA *) ctx->data;
434 pop_data->check_time = 0;
437 if (pop_reconnect (ctx) != PQ_OK)
438 return PQ_NOT_CONNECTED;
440 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
442 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
443 if (ctx->hdrs[i]->deleted) {
444 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
445 ret = pop_query (pop_data, buf, sizeof (buf));
450 strfcpy (buf, "QUIT\r\n", sizeof (buf));
451 ret = pop_query (pop_data, buf, sizeof (buf));
455 pop_data->clear_cache = 1;
456 pop_clear_cache (pop_data);
457 pop_data->status = POP_DISCONNECTED;
462 mutt_error ("%s", pop_data->err_msg);
464 return PQ_NOT_CONNECTED;
469 /* Check for new messages and fetch headers */
470 int pop_check_mailbox (CONTEXT * ctx, int *index_hint)
473 POP_DATA *pop_data = (POP_DATA *) ctx->data;
475 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
480 mutt_socket_close (pop_data->conn);
482 if (pop_open_connection (pop_data) < 0)
485 ctx->size = pop_data->size;
487 mutt_message _("Checking for new messages...");
489 ret = pop_fetch_headers (ctx);
490 pop_clear_cache (pop_data);
501 /* Fetch messages and save them in $spoolfile */
502 void pop_fetch_mail (void)
504 char buffer[LONG_STRING];
505 char msgbuf[SHORT_STRING];
507 int i, delanswer, last = 0, msgs, bytes, rset = 0;
508 pop_query_status ret;
516 mutt_error _("POP host is not defined.");
521 url = p = safe_calloc (strlen (PopHost) + 7, sizeof (char));
522 if (url_check_scheme (PopHost) == U_UNKNOWN) {
523 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
524 p = strchr (url, '\0');
526 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
528 ret = pop_parse_path (url, &acct);
531 mutt_error (_("%s is an invalid POP path"), PopHost);
535 conn = mutt_conn_find (NULL, &acct);
539 pop_data = safe_calloc (1, sizeof (POP_DATA));
540 pop_data->conn = conn;
542 if (pop_open_connection (pop_data) < 0) {
543 mutt_socket_free (pop_data->conn);
548 conn->data = pop_data;
550 mutt_message _("Checking for new messages...");
552 /* find out how many messages are in the mailbox. */
553 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
554 ret = pop_query (pop_data, buffer, sizeof (buffer));
555 if (ret == PQ_NOT_CONNECTED)
558 mutt_error ("%s", pop_data->err_msg);
562 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
564 /* only get unread messages */
565 if (msgs > 0 && option (OPTPOPLAST)) {
566 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
567 ret = pop_query (pop_data, buffer, sizeof (buffer));
568 if (ret == PQ_NOT_CONNECTED)
571 sscanf (buffer, "+OK %d", &last);
575 mutt_message _("No new mail in POP mailbox.");
580 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
584 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
586 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
588 mutt_message ("%s", msgbuf);
590 for (i = last + 1; i <= msgs; i++) {
591 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
594 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
595 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
596 if (ret == PFD_FUNCT_ERROR)
599 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
601 ret = PFD_FUNCT_ERROR;
604 mx_close_message (&msg);
607 if (ret == PQ_OK && delanswer == M_YES) {
608 /* delete the message on the server */
609 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
610 ret = pop_query (pop_data, buffer, sizeof (buffer));
613 if (ret == PQ_NOT_CONNECTED) {
614 mx_close_mailbox (&ctx, NULL);
618 mutt_error ("%s", pop_data->err_msg);
621 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
622 mutt_error _("Error while writing mailbox!");
627 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
631 mx_close_mailbox (&ctx, NULL);
634 /* make sure no messages get deleted */
635 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
636 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
641 /* exit gracefully */
642 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
643 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
645 mutt_socket_close (conn);
650 mutt_error _("Server closed connection!");
651 mutt_socket_close (conn);