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!");
111 case PQ_NOT_CONNECTED:
113 mutt_error _("Can't fetch header: Not connected!");
124 static int fetch_uidl (char *line, void *data)
127 CONTEXT *ctx = (CONTEXT *) data;
128 POP_DATA *pop_data = (POP_DATA *) ctx->data;
130 sscanf (line, "%d %s", &index, line);
131 for (i = 0; i < ctx->msgcount; i++)
132 if (!str_cmp (line, ctx->hdrs[i]->data))
135 if (i == ctx->msgcount) {
136 debug_print (1, ("new header %d %s\n", index, line));
138 if (i >= ctx->hdrmax)
139 mx_alloc_memory (ctx);
142 ctx->hdrs[i] = mutt_new_header ();
143 ctx->hdrs[i]->data = str_dup (line);
145 else if (ctx->hdrs[i]->index != index - 1)
146 pop_data->clear_cache = 1;
148 ctx->hdrs[i]->refno = index;
149 ctx->hdrs[i]->index = index - 1;
158 * -1 - conection lost,
159 * -2 - invalid command or execution error,
160 * -3 - error writing to tempfile
162 static int pop_fetch_headers (CONTEXT * ctx)
164 int i, old_count, new_count;
165 pop_query_status ret;
166 POP_DATA *pop_data = (POP_DATA *) ctx->data;
168 time (&pop_data->check_time);
169 pop_data->clear_cache = 0;
171 for (i = 0; i < ctx->msgcount; i++)
172 ctx->hdrs[i]->refno = -1;
174 old_count = ctx->msgcount;
175 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
176 new_count = ctx->msgcount;
177 ctx->msgcount = old_count;
179 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
181 pop_data->cmd_uidl = CMD_AVAILABLE;
183 debug_print (1, ("set UIDL capability\n"));
186 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
187 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
189 debug_print (1, ("unset UIDL capability\n"));
190 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
191 _("Command UIDL is not supported by server."));
196 for (i = 0; i < old_count; i++)
197 if (ctx->hdrs[i]->refno == -1)
198 ctx->hdrs[i]->deleted = 1;
200 for (i = old_count; i < new_count; i++) {
201 mutt_message (_("Fetching message headers... [%d/%d]"),
202 i + 1 - old_count, new_count - old_count);
204 ret = pop_read_header (pop_data, ctx->hdrs[i]);
212 mx_update_context (ctx, i - old_count);
216 for (i = ctx->msgcount; i < new_count; i++)
217 mutt_free_header (&ctx->hdrs[i]);
222 return (new_count - old_count);
225 /* open POP mailbox - fetch only headers */
226 int pop_open_mailbox (CONTEXT * ctx)
229 char buf[LONG_STRING];
235 if (pop_parse_path (ctx->path, &acct)) {
236 mutt_error (_("%s is an invalid POP path"), ctx->path);
241 mutt_account_tourl (&acct, &url);
243 url_ciss_tostring (&url, buf, sizeof (buf), 0);
244 conn = mutt_conn_find (NULL, &acct);
248 mem_free (&ctx->path);
249 ctx->path = str_dup (buf);
251 pop_data = mem_calloc (1, sizeof (POP_DATA));
252 pop_data->conn = conn;
253 ctx->data = pop_data;
255 if (pop_open_connection (pop_data) < 0)
258 conn->data = pop_data;
261 if (pop_reconnect (ctx) != PQ_OK)
264 ctx->size = pop_data->size;
266 mutt_message _("Fetching list of messages...");
268 ret = pop_fetch_headers (ctx);
280 /* delete all cached messages */
281 static void pop_clear_cache (POP_DATA * pop_data)
285 if (!pop_data->clear_cache)
288 debug_print (1, ("delete cached messages\n"));
290 for (i = 0; i < POP_CACHE_LEN; i++) {
291 if (pop_data->cache[i].path) {
292 unlink (pop_data->cache[i].path);
293 mem_free (&pop_data->cache[i].path);
298 /* close POP mailbox */
299 void pop_close_mailbox (CONTEXT * ctx)
301 POP_DATA *pop_data = (POP_DATA *) ctx->data;
308 if (pop_data->status != POP_NONE)
309 mutt_socket_close (pop_data->conn);
311 pop_data->status = POP_NONE;
313 pop_data->clear_cache = 1;
314 pop_clear_cache (pop_data);
316 if (!pop_data->conn->data)
317 mutt_socket_free (pop_data->conn);
322 /* fetch message from POP server */
323 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
327 char buf[LONG_STRING];
328 char path[_POSIX_PATH_MAX];
329 char *m = _("Fetching message...");
330 POP_DATA *pop_data = (POP_DATA *) ctx->data;
332 HEADER *h = ctx->hdrs[msgno];
334 /* see if we already have the message in our cache */
335 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
338 if (cache->index == h->index) {
339 /* yes, so just return a pointer to the message */
340 msg->fp = fopen (cache->path, "r");
344 mutt_perror (cache->path);
349 /* clear the previous entry */
350 unlink (cache->path);
351 mem_free (&cache->path);
356 if (pop_reconnect (ctx) != PQ_OK)
359 /* verify that massage index is correct */
362 _("The message index is incorrect. Try reopening the mailbox.");
370 msg->fp = safe_fopen (path, "w+");
377 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
379 ret = pop_fetch_data (pop_data, buf, m, fetch_message, msg->fp);
383 safe_fclose (&msg->fp);
387 mutt_error ("%s", pop_data->err_msg);
392 if (ret == PFD_FUNCT_ERROR) {
393 mutt_error _("Can't write message to temporary file!");
400 /* Update the header information. Previously, we only downloaded a
401 * portion of the headers, those required for the main display.
403 cache->index = h->index;
404 cache->path = str_dup (path);
407 mutt_free_envelope (&h->env);
408 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
411 fgets (buf, sizeof (buf), msg->fp);
412 while (!feof (msg->fp)) {
413 ctx->hdrs[msgno]->lines++;
414 fgets (buf, sizeof (buf), msg->fp);
417 h->content->length = ftell (msg->fp) - h->content->offset;
419 /* This needs to be done in case this is a multipart message */
421 h->security = crypt_query (h->content);
429 /* update POP mailbox - delete messages from server */
430 pop_query_status pop_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
433 pop_query_status ret;
434 char buf[LONG_STRING];
435 POP_DATA *pop_data = (POP_DATA *) ctx->data;
437 pop_data->check_time = 0;
440 if (pop_reconnect (ctx) != PQ_OK)
441 return PQ_NOT_CONNECTED;
443 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
445 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
446 if (ctx->hdrs[i]->deleted) {
447 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
448 ret = pop_query (pop_data, buf, sizeof (buf));
453 strfcpy (buf, "QUIT\r\n", sizeof (buf));
454 ret = pop_query (pop_data, buf, sizeof (buf));
458 pop_data->clear_cache = 1;
459 pop_clear_cache (pop_data);
460 pop_data->status = POP_DISCONNECTED;
465 mutt_error ("%s", pop_data->err_msg);
467 return PQ_NOT_CONNECTED;
472 /* Check for new messages and fetch headers */
473 int pop_check_mailbox (CONTEXT * ctx, int *index_hint, int unused)
476 POP_DATA *pop_data = (POP_DATA *) ctx->data;
478 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
483 mutt_socket_close (pop_data->conn);
485 if (pop_open_connection (pop_data) < 0)
488 ctx->size = pop_data->size;
490 mutt_message _("Checking for new messages...");
492 ret = pop_fetch_headers (ctx);
493 pop_clear_cache (pop_data);
504 /* Fetch messages and save them in $spoolfile */
505 void pop_fetch_mail (void)
507 char buffer[LONG_STRING];
508 char msgbuf[SHORT_STRING];
510 int i, delanswer, last = 0, msgs, bytes, rset = 0;
511 pop_query_status ret;
519 mutt_error _("POP host is not defined.");
524 url = p = mem_calloc (strlen (PopHost) + 7, sizeof (char));
525 if (url_check_scheme (PopHost) == U_UNKNOWN) {
526 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
527 p = strchr (url, '\0');
529 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
531 ret = pop_parse_path (url, &acct);
534 mutt_error (_("%s is an invalid POP path"), PopHost);
538 conn = mutt_conn_find (NULL, &acct);
542 pop_data = mem_calloc (1, sizeof (POP_DATA));
543 pop_data->conn = conn;
545 if (pop_open_connection (pop_data) < 0) {
546 mutt_socket_free (pop_data->conn);
547 mem_free (&pop_data);
551 conn->data = pop_data;
553 mutt_message _("Checking for new messages...");
555 /* find out how many messages are in the mailbox. */
556 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
557 ret = pop_query (pop_data, buffer, sizeof (buffer));
558 if (ret == PQ_NOT_CONNECTED)
561 mutt_error ("%s", pop_data->err_msg);
565 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
567 /* only get unread messages */
568 if (msgs > 0 && option (OPTPOPLAST)) {
569 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
570 ret = pop_query (pop_data, buffer, sizeof (buffer));
571 if (ret == PQ_NOT_CONNECTED)
574 sscanf (buffer, "+OK %d", &last);
578 mutt_message _("No new mail in POP mailbox.");
583 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
587 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
589 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
591 mutt_message ("%s", msgbuf);
593 for (i = last + 1; i <= msgs; i++) {
594 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
597 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
598 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
599 if (ret == PFD_FUNCT_ERROR)
602 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
604 ret = PFD_FUNCT_ERROR;
607 mx_close_message (&msg);
610 if (ret == PQ_OK && delanswer == M_YES) {
611 /* delete the message on the server */
612 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
613 ret = pop_query (pop_data, buffer, sizeof (buffer));
616 if (ret == PQ_NOT_CONNECTED) {
617 mx_close_mailbox (&ctx, NULL);
621 mutt_error ("%s", pop_data->err_msg);
624 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
625 mutt_error _("Error while writing mailbox!");
630 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
634 mx_close_mailbox (&ctx, NULL);
637 /* make sure no messages get deleted */
638 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
639 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
644 /* exit gracefully */
645 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
646 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
648 mutt_socket_close (conn);
649 mem_free (&pop_data);
653 mutt_error _("Server closed connection!");
654 mutt_socket_close (conn);
655 mem_free (&pop_data);