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)
54 char buf[LONG_STRING];
55 char tempfile[_POSIX_PATH_MAX];
57 mutt_mktemp (tempfile);
58 if (!(f = safe_fopen (tempfile, "w+"))) {
59 mutt_perror (tempfile);
60 return PFD_FUNCT_ERROR;
63 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
64 ret = pop_query (pop_data, buf, sizeof (buf));
66 sscanf (buf, "+OK %d %ld", &index, &length);
68 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
69 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
71 if (pop_data->cmd_top == CMD_UNKNOWN) {
73 pop_data->cmd_top = CMD_AVAILABLE;
75 debug_print (1, ("set TOP capability\n"));
79 pop_data->cmd_top = CMD_NOT_AVAILABLE;
81 debug_print (1, ("unset TOP capability\n"));
82 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
83 _("Command TOP is not supported by server."));
92 h->env = mutt_read_rfc822_header (f, h, 0, 0);
93 h->content->length = length - h->content->offset + 1;
97 fgets (buf, sizeof (buf), f);
103 mutt_error ("%s", pop_data->err_msg);
106 case PFD_FUNCT_ERROR:
108 mutt_error _("Can't write header to temporary file!");
120 static int fetch_uidl (char *line, void *data)
123 CONTEXT *ctx = (CONTEXT *) data;
124 POP_DATA *pop_data = (POP_DATA *) ctx->data;
126 sscanf (line, "%d %s", &index, line);
127 for (i = 0; i < ctx->msgcount; i++)
128 if (!safe_strcmp (line, ctx->hdrs[i]->data))
131 if (i == ctx->msgcount) {
132 debug_print (1, ("new header %d %s\n", index, line));
134 if (i >= ctx->hdrmax)
135 mx_alloc_memory (ctx);
138 ctx->hdrs[i] = mutt_new_header ();
139 ctx->hdrs[i]->data = safe_strdup (line);
141 else if (ctx->hdrs[i]->index != index - 1)
142 pop_data->clear_cache = 1;
144 ctx->hdrs[i]->refno = index;
145 ctx->hdrs[i]->index = index - 1;
154 * -1 - conection lost,
155 * -2 - invalid command or execution error,
156 * -3 - error writing to tempfile
158 static int pop_fetch_headers (CONTEXT * ctx)
160 int i, old_count, new_count;
161 pop_query_status ret;
162 POP_DATA *pop_data = (POP_DATA *) ctx->data;
164 time (&pop_data->check_time);
165 pop_data->clear_cache = 0;
167 for (i = 0; i < ctx->msgcount; i++)
168 ctx->hdrs[i]->refno = -1;
170 old_count = ctx->msgcount;
171 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
172 new_count = ctx->msgcount;
173 ctx->msgcount = old_count;
175 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
177 pop_data->cmd_uidl = CMD_AVAILABLE;
179 debug_print (1, ("set UIDL capability\n"));
182 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
183 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
185 debug_print (1, ("unset UIDL capability\n"));
186 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
187 _("Command UIDL is not supported by server."));
192 for (i = 0; i < old_count; i++)
193 if (ctx->hdrs[i]->refno == -1)
194 ctx->hdrs[i]->deleted = 1;
196 for (i = old_count; i < new_count; i++) {
197 mutt_message (_("Fetching message headers... [%d/%d]"),
198 i + 1 - old_count, new_count - old_count);
200 ret = pop_read_header (pop_data, ctx->hdrs[i]);
208 mx_update_context (ctx, i - old_count);
212 for (i = ctx->msgcount; i < new_count; i++)
213 mutt_free_header (&ctx->hdrs[i]);
218 return (new_count - old_count);
221 /* open POP mailbox - fetch only headers */
222 int pop_open_mailbox (CONTEXT * ctx)
225 char buf[LONG_STRING];
231 if (pop_parse_path (ctx->path, &acct)) {
232 mutt_error (_("%s is an invalid POP path"), ctx->path);
237 mutt_account_tourl (&acct, &url);
239 url_ciss_tostring (&url, buf, sizeof (buf), 0);
240 conn = mutt_conn_find (NULL, &acct);
245 ctx->path = safe_strdup (buf);
247 pop_data = safe_calloc (1, sizeof (POP_DATA));
248 pop_data->conn = conn;
249 ctx->data = pop_data;
251 if (pop_open_connection (pop_data) < 0)
254 conn->data = pop_data;
257 if (pop_reconnect (ctx) != PQ_OK)
260 ctx->size = pop_data->size;
262 mutt_message _("Fetching list of messages...");
264 ret = pop_fetch_headers (ctx);
276 /* delete all cached messages */
277 static void pop_clear_cache (POP_DATA * pop_data)
281 if (!pop_data->clear_cache)
284 debug_print (1, ("delete cached messages\n"));
286 for (i = 0; i < POP_CACHE_LEN; i++) {
287 if (pop_data->cache[i].path) {
288 unlink (pop_data->cache[i].path);
289 FREE (&pop_data->cache[i].path);
294 /* close POP mailbox */
295 void pop_close_mailbox (CONTEXT * ctx)
297 POP_DATA *pop_data = (POP_DATA *) ctx->data;
304 if (pop_data->status != POP_NONE)
305 mutt_socket_close (pop_data->conn);
307 pop_data->status = POP_NONE;
309 pop_data->clear_cache = 1;
310 pop_clear_cache (pop_data);
312 if (!pop_data->conn->data)
313 mutt_socket_free (pop_data->conn);
318 /* fetch message from POP server */
319 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
323 char buf[LONG_STRING];
324 char path[_POSIX_PATH_MAX];
325 char *m = _("Fetching message...");
326 POP_DATA *pop_data = (POP_DATA *) ctx->data;
328 HEADER *h = ctx->hdrs[msgno];
330 /* see if we already have the message in our cache */
331 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
334 if (cache->index == h->index) {
335 /* yes, so just return a pointer to the message */
336 msg->fp = fopen (cache->path, "r");
340 mutt_perror (cache->path);
345 /* clear the previous entry */
346 unlink (cache->path);
352 if (pop_reconnect (ctx) != PQ_OK)
355 /* verify that massage index is correct */
358 _("The message index is incorrect. Try reopening the mailbox.");
366 msg->fp = safe_fopen (path, "w+");
373 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
375 ret = pop_fetch_data (pop_data, buf, m, fetch_message, msg->fp);
379 safe_fclose (&msg->fp);
383 mutt_error ("%s", pop_data->err_msg);
388 if (ret == PFD_FUNCT_ERROR) {
389 mutt_error _("Can't write message to temporary file!");
396 /* Update the header information. Previously, we only downloaded a
397 * portion of the headers, those required for the main display.
399 cache->index = h->index;
400 cache->path = safe_strdup (path);
403 mutt_free_envelope (&h->env);
404 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
407 fgets (buf, sizeof (buf), msg->fp);
408 while (!feof (msg->fp)) {
409 ctx->hdrs[msgno]->lines++;
410 fgets (buf, sizeof (buf), msg->fp);
413 h->content->length = ftell (msg->fp) - h->content->offset;
415 /* This needs to be done in case this is a multipart message */
417 h->security = crypt_query (h->content);
425 /* update POP mailbox - delete messages from server */
426 pop_query_status pop_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
429 pop_query_status ret;
430 char buf[LONG_STRING];
431 POP_DATA *pop_data = (POP_DATA *) ctx->data;
433 pop_data->check_time = 0;
436 if (pop_reconnect (ctx) != PQ_OK)
437 return PQ_NOT_CONNECTED;
439 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
441 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
442 if (ctx->hdrs[i]->deleted) {
443 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
444 ret = pop_query (pop_data, buf, sizeof (buf));
449 strfcpy (buf, "QUIT\r\n", sizeof (buf));
450 ret = pop_query (pop_data, buf, sizeof (buf));
454 pop_data->clear_cache = 1;
455 pop_clear_cache (pop_data);
456 pop_data->status = POP_DISCONNECTED;
461 mutt_error ("%s", pop_data->err_msg);
463 return PQ_NOT_CONNECTED;
468 /* Check for new messages and fetch headers */
469 int pop_check_mailbox (CONTEXT * ctx, int *index_hint, int unused)
472 POP_DATA *pop_data = (POP_DATA *) ctx->data;
474 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
479 mutt_socket_close (pop_data->conn);
481 if (pop_open_connection (pop_data) < 0)
484 ctx->size = pop_data->size;
486 mutt_message _("Checking for new messages...");
488 ret = pop_fetch_headers (ctx);
489 pop_clear_cache (pop_data);
500 /* Fetch messages and save them in $spoolfile */
501 void pop_fetch_mail (void)
503 char buffer[LONG_STRING];
504 char msgbuf[SHORT_STRING];
506 int i, delanswer, last = 0, msgs, bytes, rset = 0;
507 pop_query_status ret;
515 mutt_error _("POP host is not defined.");
520 url = p = safe_calloc (strlen (PopHost) + 7, sizeof (char));
521 if (url_check_scheme (PopHost) == U_UNKNOWN) {
522 strcpy (url, "pop://"); /* __STRCPY_CHECKED__ */
523 p = strchr (url, '\0');
525 strcpy (p, PopHost); /* __STRCPY_CHECKED__ */
527 ret = pop_parse_path (url, &acct);
530 mutt_error (_("%s is an invalid POP path"), PopHost);
534 conn = mutt_conn_find (NULL, &acct);
538 pop_data = safe_calloc (1, sizeof (POP_DATA));
539 pop_data->conn = conn;
541 if (pop_open_connection (pop_data) < 0) {
542 mutt_socket_free (pop_data->conn);
547 conn->data = pop_data;
549 mutt_message _("Checking for new messages...");
551 /* find out how many messages are in the mailbox. */
552 strfcpy (buffer, "STAT\r\n", sizeof (buffer));
553 ret = pop_query (pop_data, buffer, sizeof (buffer));
554 if (ret == PQ_NOT_CONNECTED)
557 mutt_error ("%s", pop_data->err_msg);
561 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
563 /* only get unread messages */
564 if (msgs > 0 && option (OPTPOPLAST)) {
565 strfcpy (buffer, "LAST\r\n", sizeof (buffer));
566 ret = pop_query (pop_data, buffer, sizeof (buffer));
567 if (ret == PQ_NOT_CONNECTED)
570 sscanf (buffer, "+OK %d", &last);
574 mutt_message _("No new mail in POP mailbox.");
579 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
583 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
585 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
587 mutt_message ("%s", msgbuf);
589 for (i = last + 1; i <= msgs; i++) {
590 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
593 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
594 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
595 if (ret == PFD_FUNCT_ERROR)
598 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
600 ret = PFD_FUNCT_ERROR;
603 mx_close_message (&msg);
606 if (ret == PQ_OK && delanswer == M_YES) {
607 /* delete the message on the server */
608 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
609 ret = pop_query (pop_data, buffer, sizeof (buffer));
612 if (ret == PQ_NOT_CONNECTED) {
613 mx_close_mailbox (&ctx, NULL);
617 mutt_error ("%s", pop_data->err_msg);
620 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
621 mutt_error _("Error while writing mailbox!");
626 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
630 mx_close_mailbox (&ctx, NULL);
633 /* make sure no messages get deleted */
634 strfcpy (buffer, "RSET\r\n", sizeof (buffer));
635 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
640 /* exit gracefully */
641 strfcpy (buffer, "QUIT\r\n", sizeof (buffer));
642 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
644 mutt_socket_close (conn);
649 mutt_error _("Server closed connection!");
650 mutt_socket_close (conn);