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 #include "lib/debug.h"
27 /* write line to file */
28 static int fetch_message (char *line, void *file)
30 FILE *f = (FILE *) file;
33 if (fputc ('\n', f) == EOF)
43 * -1 - conection lost,
44 * -2 - invalid command or execution error,
45 * -3 - error writing to tempfile
47 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 debug_print (1, ("set TOP capability\n"));
78 pop_data->cmd_top = CMD_NOT_AVAILABLE;
80 debug_print (1, ("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) {
131 debug_print (1, ("new header %d %s\n", index, line));
133 if (i >= ctx->hdrmax)
134 mx_alloc_memory (ctx);
137 ctx->hdrs[i] = mutt_new_header ();
138 ctx->hdrs[i]->data = safe_strdup (line);
140 else if (ctx->hdrs[i]->index != index - 1)
141 pop_data->clear_cache = 1;
143 ctx->hdrs[i]->refno = index;
144 ctx->hdrs[i]->index = index - 1;
153 * -1 - conection lost,
154 * -2 - invalid command or execution error,
155 * -3 - error writing to tempfile
157 static int pop_fetch_headers (CONTEXT * ctx)
159 int i, old_count, new_count;
160 pop_query_status ret;
161 POP_DATA *pop_data = (POP_DATA *) ctx->data;
163 time (&pop_data->check_time);
164 pop_data->clear_cache = 0;
166 for (i = 0; i < ctx->msgcount; i++)
167 ctx->hdrs[i]->refno = -1;
169 old_count = ctx->msgcount;
170 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
171 new_count = ctx->msgcount;
172 ctx->msgcount = old_count;
174 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
176 pop_data->cmd_uidl = CMD_AVAILABLE;
178 debug_print (1, ("set UIDL capability\n"));
181 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
182 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
184 debug_print (1, ("unset UIDL capability\n"));
185 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
186 _("Command UIDL is not supported by server."));
191 for (i = 0; i < old_count; i++)
192 if (ctx->hdrs[i]->refno == -1)
193 ctx->hdrs[i]->deleted = 1;
195 for (i = old_count; i < new_count; i++) {
196 mutt_message (_("Fetching message headers... [%d/%d]"),
197 i + 1 - old_count, new_count - old_count);
199 ret = pop_read_header (pop_data, ctx->hdrs[i]);
207 mx_update_context (ctx, i - old_count);
211 for (i = ctx->msgcount; i < new_count; i++)
212 mutt_free_header (&ctx->hdrs[i]);
217 return (new_count - old_count);
220 /* open POP mailbox - fetch only headers */
221 int pop_open_mailbox (CONTEXT * ctx)
224 char buf[LONG_STRING];
230 if (pop_parse_path (ctx->path, &acct)) {
231 mutt_error (_("%s is an invalid POP path"), ctx->path);
236 mutt_account_tourl (&acct, &url);
238 url_ciss_tostring (&url, buf, sizeof (buf), 0);
239 conn = mutt_conn_find (NULL, &acct);
244 ctx->path = safe_strdup (buf);
246 pop_data = safe_calloc (1, sizeof (POP_DATA));
247 pop_data->conn = conn;
248 ctx->data = pop_data;
250 if (pop_open_connection (pop_data) < 0)
253 conn->data = pop_data;
256 if (pop_reconnect (ctx) != PQ_OK)
259 ctx->size = pop_data->size;
261 mutt_message _("Fetching list of messages...");
263 ret = pop_fetch_headers (ctx);
275 /* delete all cached messages */
276 static void pop_clear_cache (POP_DATA * pop_data)
280 if (!pop_data->clear_cache)
283 debug_print (1, ("delete cached messages\n"));
285 for (i = 0; i < POP_CACHE_LEN; i++) {
286 if (pop_data->cache[i].path) {
287 unlink (pop_data->cache[i].path);
288 FREE (&pop_data->cache[i].path);
293 /* close POP mailbox */
294 void pop_close_mailbox (CONTEXT * ctx)
296 POP_DATA *pop_data = (POP_DATA *) ctx->data;
303 if (pop_data->status != POP_NONE)
304 mutt_socket_close (pop_data->conn);
306 pop_data->status = POP_NONE;
308 pop_data->clear_cache = 1;
309 pop_clear_cache (pop_data);
311 if (!pop_data->conn->data)
312 mutt_socket_free (pop_data->conn);
317 /* fetch message from POP server */
318 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
322 char buf[LONG_STRING];
323 char path[_POSIX_PATH_MAX];
324 char *m = _("Fetching message...");
325 POP_DATA *pop_data = (POP_DATA *) ctx->data;
327 HEADER *h = ctx->hdrs[msgno];
329 /* see if we already have the message in our cache */
330 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
333 if (cache->index == h->index) {
334 /* yes, so just return a pointer to the message */
335 msg->fp = fopen (cache->path, "r");
339 mutt_perror (cache->path);
344 /* clear the previous entry */
345 unlink (cache->path);
351 if (pop_reconnect (ctx) != PQ_OK)
354 /* verify that massage index is correct */
357 _("The message index is incorrect. Try reopening the mailbox.");
365 msg->fp = safe_fopen (path, "w+");
372 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
374 ret = pop_fetch_data (pop_data, buf, m, fetch_message, msg->fp);
378 safe_fclose (&msg->fp);
382 mutt_error ("%s", pop_data->err_msg);
387 if (ret == PFD_FUNCT_ERROR) {
388 mutt_error _("Can't write message to temporary file!");
395 /* Update the header information. Previously, we only downloaded a
396 * portion of the headers, those required for the main display.
398 cache->index = h->index;
399 cache->path = safe_strdup (path);
402 mutt_free_envelope (&h->env);
403 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
406 fgets (buf, sizeof (buf), msg->fp);
407 while (!feof (msg->fp)) {
408 ctx->hdrs[msgno]->lines++;
409 fgets (buf, sizeof (buf), msg->fp);
412 h->content->length = ftell (msg->fp) - h->content->offset;
414 /* This needs to be done in case this is a multipart message */
416 h->security = crypt_query (h->content);
424 /* update POP mailbox - delete messages from server */
425 pop_query_status pop_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
428 pop_query_status ret;
429 char buf[LONG_STRING];
430 POP_DATA *pop_data = (POP_DATA *) ctx->data;
432 pop_data->check_time = 0;
435 if (pop_reconnect (ctx) != PQ_OK)
436 return PQ_NOT_CONNECTED;
438 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
440 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
441 if (ctx->hdrs[i]->deleted) {
442 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
443 ret = pop_query (pop_data, buf, sizeof (buf));
448 strfcpy (buf, "QUIT\r\n", sizeof (buf));
449 ret = pop_query (pop_data, buf, sizeof (buf));
453 pop_data->clear_cache = 1;
454 pop_clear_cache (pop_data);
455 pop_data->status = POP_DISCONNECTED;
460 mutt_error ("%s", pop_data->err_msg);
462 return PQ_NOT_CONNECTED;
467 /* Check for new messages and fetch headers */
468 int pop_check_mailbox (CONTEXT * ctx, int *index_hint, int unused)
471 POP_DATA *pop_data = (POP_DATA *) ctx->data;
473 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
478 mutt_socket_close (pop_data->conn);
480 if (pop_open_connection (pop_data) < 0)
483 ctx->size = pop_data->size;
485 mutt_message _("Checking for new messages...");
487 ret = pop_fetch_headers (ctx);
488 pop_clear_cache (pop_data);
499 /* Fetch messages and save them in $spoolfile */
500 void pop_fetch_mail (void)
502 char buffer[LONG_STRING];
503 char msgbuf[SHORT_STRING];
505 int i, delanswer, last = 0, msgs, bytes, rset = 0;
506 pop_query_status ret;
514 mutt_error _("POP host is not defined.");
519 url = p = safe_calloc (strlen (PopHost) + 7, sizeof (char));
520 if (url_check_scheme (PopHost) == U_UNKNOWN) {
521 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
522 p = strchr (url, '\0');
524 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
526 ret = pop_parse_path (url, &acct);
529 mutt_error (_("%s is an invalid POP path"), PopHost);
533 conn = mutt_conn_find (NULL, &acct);
537 pop_data = safe_calloc (1, sizeof (POP_DATA));
538 pop_data->conn = conn;
540 if (pop_open_connection (pop_data) < 0) {
541 mutt_socket_free (pop_data->conn);
546 conn->data = pop_data;
548 mutt_message _("Checking for new messages...");
550 /* find out how many messages are in the mailbox. */
551 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
552 ret = pop_query (pop_data, buffer, sizeof (buffer));
553 if (ret == PQ_NOT_CONNECTED)
556 mutt_error ("%s", pop_data->err_msg);
560 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
562 /* only get unread messages */
563 if (msgs > 0 && option (OPTPOPLAST)) {
564 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
565 ret = pop_query (pop_data, buffer, sizeof (buffer));
566 if (ret == PQ_NOT_CONNECTED)
569 sscanf (buffer, "+OK %d", &last);
573 mutt_message _("No new mail in POP mailbox.");
578 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
582 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
584 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
586 mutt_message ("%s", msgbuf);
588 for (i = last + 1; i <= msgs; i++) {
589 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
592 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
593 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
594 if (ret == PFD_FUNCT_ERROR)
597 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
599 ret = PFD_FUNCT_ERROR;
602 mx_close_message (&msg);
605 if (ret == PQ_OK && delanswer == M_YES) {
606 /* delete the message on the server */
607 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
608 ret = pop_query (pop_data, buffer, sizeof (buffer));
611 if (ret == PQ_NOT_CONNECTED) {
612 mx_close_mailbox (&ctx, NULL);
616 mutt_error ("%s", pop_data->err_msg);
619 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
620 mutt_error _("Error while writing mailbox!");
625 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
629 mx_close_mailbox (&ctx, NULL);
632 /* make sure no messages get deleted */
633 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
634 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
639 /* exit gracefully */
640 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
641 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
643 mutt_socket_close (conn);
648 mutt_error _("Server closed connection!");
649 mutt_socket_close (conn);