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 <lib-lib/mem.h>
18 #include <lib-lib/file.h>
19 #include <lib-lib/str.h>
20 #include <lib-lib/macros.h>
22 #include <lib-ui/curses.h>
27 #include <lib-crypt/crypt.h>
29 /* write line to file */
30 static int fetch_message (char *line, void *file)
32 FILE *f = (FILE *) file;
35 if (fputc ('\n', f) == EOF)
45 * -1 - conection lost,
46 * -2 - invalid command or execution error,
47 * -3 - error writing to tempfile
49 static pop_query_status pop_read_header (POP_DATA * pop_data, HEADER * h)
55 char buf[LONG_STRING];
56 char tempfile[_POSIX_PATH_MAX];
58 mutt_mktemp (tempfile);
59 if (!(f = safe_fopen (tempfile, "w+"))) {
60 mutt_perror (tempfile);
61 return PFD_FUNCT_ERROR;
64 snprintf (buf, sizeof (buf), "string_list_t %d\r\n", h->refno);
65 ret = pop_query (pop_data, buf, sizeof (buf));
67 sscanf (buf, "+OK %d %ld", &idx, &length);
69 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
70 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
72 if (pop_data->cmd_top == CMD_UNKNOWN) {
74 pop_data->cmd_top = CMD_AVAILABLE;
78 pop_data->cmd_top = CMD_NOT_AVAILABLE;
79 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
80 _("Command TOP is not supported by server."));
89 h->env = mutt_read_rfc822_header (f, h, 0, 0);
90 h->content->length = length - h->content->offset + 1;
94 fgets (buf, sizeof (buf), f);
100 mutt_error ("%s", pop_data->err_msg);
103 case PFD_FUNCT_ERROR:
105 mutt_error _("Can't write header to temporary file!");
109 case PQ_NOT_CONNECTED:
111 mutt_error _("Can't fetch header: Not connected!");
122 static int fetch_uidl (char *line, void *data)
125 CONTEXT *ctx = (CONTEXT *) data;
126 POP_DATA *pop_data = (POP_DATA *) ctx->data;
128 sscanf (line, "%d %s", &idx, line);
129 for (i = 0; i < ctx->msgcount; i++)
130 if (!m_strcmp(line, ctx->hdrs[i]->data))
133 if (i == ctx->msgcount) {
134 if (i >= ctx->hdrmax)
135 mx_alloc_memory (ctx);
138 ctx->hdrs[i] = header_new();
139 ctx->hdrs[i]->data = m_strdup(line);
141 else if (ctx->hdrs[i]->index != idx - 1)
142 pop_data->clear_cache = 1;
144 ctx->hdrs[i]->refno = idx;
145 ctx->hdrs[i]->index = idx - 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;
180 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
181 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
183 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
184 _("Command UIDL is not supported by server."));
189 for (i = 0; i < old_count; i++)
190 if (ctx->hdrs[i]->refno == -1)
191 ctx->hdrs[i]->deleted = 1;
193 for (i = old_count; i < new_count; i++) {
194 mutt_message (_("Fetching message headers... [%d/%d]"),
195 i + 1 - old_count, new_count - old_count);
197 ret = pop_read_header (pop_data, ctx->hdrs[i]);
205 mx_update_context (ctx, i - old_count);
209 for (i = ctx->msgcount; i < new_count; i++)
210 header_delete(&ctx->hdrs[i]);
215 return (new_count - old_count);
218 /* open POP mailbox - fetch only headers */
219 int pop_open_mailbox (CONTEXT * ctx)
222 char buf[LONG_STRING];
228 if (pop_parse_path (ctx->path, &act)) {
229 mutt_error (_("%s is an invalid POP path"), ctx->path);
234 mutt_account_tourl (&act, &url);
236 url_ciss_tostring (&url, buf, sizeof (buf), 0);
237 conn = mutt_conn_find (NULL, &act);
241 p_delete(&ctx->path);
242 ctx->path = m_strdup(buf);
244 pop_data = p_new(POP_DATA, 1);
245 pop_data->conn = conn;
246 ctx->data = pop_data;
248 if (pop_open_connection (pop_data) < 0)
251 conn->data = pop_data;
254 if (pop_reconnect (ctx) != PQ_OK)
257 ctx->size = pop_data->size;
259 mutt_message _("Fetching list of messages...");
261 ret = pop_fetch_headers (ctx);
273 /* delete all cached messages */
274 static void pop_clear_cache (POP_DATA * pop_data)
278 if (!pop_data->clear_cache)
281 for (i = 0; i < POP_CACHE_LEN; i++) {
282 if (pop_data->cache[i].path) {
283 unlink (pop_data->cache[i].path);
284 p_delete(&pop_data->cache[i].path);
289 /* close POP mailbox */
290 void pop_close_mailbox (CONTEXT * ctx)
292 POP_DATA *pop_data = (POP_DATA *) ctx->data;
299 if (pop_data->status != POP_NONE)
300 mutt_socket_close (pop_data->conn);
302 pop_data->status = POP_NONE;
304 pop_data->clear_cache = 1;
305 pop_clear_cache (pop_data);
307 if (!pop_data->conn->data)
308 mutt_socket_free (pop_data->conn);
313 /* fetch message from POP server */
314 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
318 char buf[LONG_STRING];
319 char path[_POSIX_PATH_MAX];
321 POP_DATA *pop_data = (POP_DATA *) ctx->data;
323 HEADER *h = ctx->hdrs[msgno];
325 /* see if we already have the message in our cache */
326 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
329 if (cache->index == h->index) {
330 /* yes, so just return a pointer to the message */
331 msg->fp = fopen (cache->path, "r");
335 mutt_perror (cache->path);
340 /* clear the previous entry */
341 unlink (cache->path);
342 p_delete(&cache->path);
347 if (pop_reconnect (ctx) != PQ_OK)
350 /* verify that massage index is correct */
353 _("The message index is incorrect. Try reopening the mailbox.");
358 bar.size = h->content->length + h->content->offset - 1;
359 bar.msg = _("Fetching message...");
360 mutt_progress_bar (&bar, 0);
363 msg->fp = safe_fopen (path, "w+");
370 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
372 ret = pop_fetch_data (pop_data, buf, &bar, fetch_message, msg->fp);
376 safe_fclose (&msg->fp);
380 mutt_error ("%s", pop_data->err_msg);
385 if (ret == PFD_FUNCT_ERROR) {
386 mutt_error _("Can't write message to temporary file!");
393 /* Update the header information. Previously, we only downloaded a
394 * portion of the headers, those required for the main display.
396 cache->index = h->index;
397 cache->path = m_strdup(path);
400 envelope_delete(&h->env);
401 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
404 fgets (buf, sizeof (buf), msg->fp);
405 while (!feof (msg->fp)) {
406 ctx->hdrs[msgno]->lines++;
407 fgets (buf, sizeof (buf), msg->fp);
410 h->content->length = ftello (msg->fp) - h->content->offset;
412 /* This needs to be done in case this is a multipart message */
413 h->security = crypt_query (h->content);
421 /* update POP mailbox - delete messages from server */
422 pop_query_status pop_sync_mailbox (CONTEXT * ctx,
423 int unused __attribute__ ((unused)),
424 int *index_hint __attribute__ ((unused)))
427 pop_query_status ret;
428 char buf[LONG_STRING];
429 POP_DATA *pop_data = (POP_DATA *) ctx->data;
431 pop_data->check_time = 0;
434 if (pop_reconnect (ctx) != PQ_OK)
435 return PQ_NOT_CONNECTED;
437 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
439 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
440 if (ctx->hdrs[i]->deleted) {
441 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
442 ret = pop_query (pop_data, buf, sizeof (buf));
447 m_strcpy(buf, sizeof(buf), "QUIT\r\n");
448 ret = pop_query (pop_data, buf, sizeof (buf));
452 pop_data->clear_cache = 1;
453 pop_clear_cache (pop_data);
454 pop_data->status = POP_DISCONNECTED;
459 mutt_error ("%s", pop_data->err_msg);
461 return PQ_NOT_CONNECTED;
466 /* Check for new messages and fetch headers */
467 int pop_check_mailbox (CONTEXT * ctx,
468 int *index_hint __attribute__ ((unused)),
469 int unused __attribute__ ((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 = p_new(char, strlen (PopHost) + 7);
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, &act);
530 mutt_error (_("%s is an invalid POP path"), PopHost);
534 conn = mutt_conn_find (NULL, &act);
538 pop_data = p_new(POP_DATA, 1);
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 m_strcpy(buffer, sizeof(buffer), "STAT\r\n");
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 m_strcpy(buffer, sizeof(buffer), "LAST\r\n");
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 m_strcpy(buffer, sizeof(buffer), "RSET\r\n");
635 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
640 /* exit gracefully */
641 m_strcpy(buffer, sizeof(buffer), "QUIT\r\n");
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);