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.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-ui/curses.h>
13 #include <lib-mx/mx.h>
17 #include <lib-crypt/crypt.h>
19 /* write line to file */
20 static int fetch_message (char *line, void *file)
22 FILE *f = (FILE *) file;
25 if (fputc ('\n', f) == EOF)
35 * -1 - conection lost,
36 * -2 - invalid command or execution error,
37 * -3 - error writing to tempfile
39 static pop_query_status pop_read_header (POP_DATA * pop_data, HEADER * h)
45 char buf[LONG_STRING];
46 char tempfile[_POSIX_PATH_MAX];
48 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
50 mutt_error(_("Could not create temporary file"));
51 return PFD_FUNCT_ERROR;
54 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
55 ret = pop_query (pop_data, buf, sizeof (buf));
57 sscanf (buf, "+OK %d %ld", &idx, &length);
59 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
60 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
62 if (pop_data->cmd_top == CMD_UNKNOWN) {
64 pop_data->cmd_top = CMD_AVAILABLE;
68 pop_data->cmd_top = CMD_NOT_AVAILABLE;
69 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
70 _("Command TOP is not supported by server."));
79 h->env = mutt_read_rfc822_header (f, h, 0, 0);
80 h->content->length = length - h->content->offset + 1;
84 fgets (buf, sizeof (buf), f);
90 mutt_error ("%s", pop_data->err_msg);
95 mutt_error _("Can't write header to temporary file!");
99 case PQ_NOT_CONNECTED:
101 mutt_error _("Can't fetch header: Not connected!");
112 static int fetch_uidl (char *line, void *data)
115 CONTEXT *ctx = (CONTEXT *) data;
116 POP_DATA *pop_data = (POP_DATA *) ctx->data;
118 sscanf (line, "%d %s", &idx, line);
119 for (i = 0; i < ctx->msgcount; i++)
120 if (!m_strcmp(line, ctx->hdrs[i]->data))
123 if (i == ctx->msgcount) {
124 if (i >= ctx->hdrmax)
125 mx_alloc_memory (ctx);
128 ctx->hdrs[i] = header_new();
129 ctx->hdrs[i]->data = m_strdup(line);
131 else if (ctx->hdrs[i]->index != idx - 1)
132 pop_data->clear_cache = 1;
134 ctx->hdrs[i]->refno = idx;
135 ctx->hdrs[i]->index = idx - 1;
144 * -1 - conection lost,
145 * -2 - invalid command or execution error,
146 * -3 - error writing to tempfile
148 static int pop_fetch_headers (CONTEXT * ctx)
150 int i, old_count, new_count;
151 pop_query_status ret;
152 POP_DATA *pop_data = (POP_DATA *) ctx->data;
154 time (&pop_data->check_time);
155 pop_data->clear_cache = 0;
157 for (i = 0; i < ctx->msgcount; i++)
158 ctx->hdrs[i]->refno = -1;
160 old_count = ctx->msgcount;
161 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
162 new_count = ctx->msgcount;
163 ctx->msgcount = old_count;
165 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
167 pop_data->cmd_uidl = CMD_AVAILABLE;
170 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
171 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
173 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
174 _("Command UIDL is not supported by server."));
179 for (i = 0; i < old_count; i++)
180 if (ctx->hdrs[i]->refno == -1)
181 ctx->hdrs[i]->deleted = 1;
183 for (i = old_count; i < new_count; i++) {
184 mutt_message (_("Fetching message headers... [%d/%d]"),
185 i + 1 - old_count, new_count - old_count);
187 ret = pop_read_header (pop_data, ctx->hdrs[i]);
195 mx_update_context (ctx, i - old_count);
199 for (i = ctx->msgcount; i < new_count; i++)
200 header_delete(&ctx->hdrs[i]);
205 return (new_count - old_count);
208 /* open POP mailbox - fetch only headers */
209 static int pop_open_mailbox (CONTEXT * ctx)
212 char buf[LONG_STRING];
218 if (pop_parse_path (ctx->path, &act)) {
219 mutt_error (_("%s is an invalid POP path"), ctx->path);
224 mutt_account_tourl (&act, &url);
226 url_ciss_tostring (&url, buf, sizeof (buf), 0);
227 conn = mutt_conn_find (NULL, &act);
231 p_delete(&ctx->path);
232 ctx->path = m_strdup(buf);
234 pop_data = p_new(POP_DATA, 1);
235 pop_data->conn = conn;
236 ctx->data = pop_data;
238 if (pop_open_connection (pop_data) < 0)
241 conn->data = pop_data;
244 if (pop_reconnect (ctx) != PQ_OK)
247 ctx->size = pop_data->size;
249 mutt_message _("Fetching list of messages...");
251 ret = pop_fetch_headers (ctx);
263 /* delete all cached messages */
264 static void pop_clear_cache (POP_DATA * pop_data)
268 if (!pop_data->clear_cache)
271 for (i = 0; i < POP_CACHE_LEN; i++) {
272 if (pop_data->cache[i].path) {
273 unlink (pop_data->cache[i].path);
274 p_delete(&pop_data->cache[i].path);
279 /* close POP mailbox */
280 static void pop_close_mailbox (CONTEXT * ctx)
282 POP_DATA *pop_data = (POP_DATA *) ctx->data;
289 if (pop_data->status != POP_NONE)
290 mutt_socket_close (pop_data->conn);
292 pop_data->status = POP_NONE;
294 pop_data->clear_cache = 1;
295 pop_clear_cache (pop_data);
297 if (!pop_data->conn->data)
298 mutt_socket_free (pop_data->conn);
303 /* fetch message from POP server */
304 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
308 char buf[LONG_STRING];
309 char path[_POSIX_PATH_MAX];
311 POP_DATA *pop_data = (POP_DATA *) ctx->data;
313 HEADER *h = ctx->hdrs[msgno];
315 /* see if we already have the message in our cache */
316 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
319 if (cache->index == h->index) {
320 /* yes, so just return a pointer to the message */
321 msg->fp = fopen (cache->path, "r");
325 mutt_perror (cache->path);
330 /* clear the previous entry */
331 unlink (cache->path);
332 p_delete(&cache->path);
337 if (pop_reconnect (ctx) != PQ_OK)
340 /* verify that massage index is correct */
343 _("The message index is incorrect. Try reopening the mailbox.");
348 bar.size = h->content->length + h->content->offset - 1;
349 bar.msg = _("Fetching message...");
350 mutt_progress_bar (&bar, 0);
352 msg->fp = m_tempfile(path, sizeof(path), NONULL(MCore.tmpdir), NULL);
354 mutt_error(_("Could not create temporary file"));
359 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
361 ret = pop_fetch_data (pop_data, buf, &bar, fetch_message, msg->fp);
369 mutt_error ("%s", pop_data->err_msg);
374 if (ret == PFD_FUNCT_ERROR) {
375 mutt_error _("Can't write message to temporary file!");
382 /* Update the header information. Previously, we only downloaded a
383 * portion of the headers, those required for the main display.
385 cache->index = h->index;
386 cache->path = m_strdup(path);
389 envelope_delete(&h->env);
390 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
393 fgets (buf, sizeof (buf), msg->fp);
394 while (!feof (msg->fp)) {
395 ctx->hdrs[msgno]->lines++;
396 fgets (buf, sizeof (buf), msg->fp);
399 h->content->length = ftello (msg->fp) - h->content->offset;
401 /* This needs to be done in case this is a multipart message */
402 h->security = crypt_query (h->content);
410 /* update POP mailbox - delete messages from server */
411 static pop_query_status
412 pop_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)),
413 int *index_hint __attribute__ ((unused)))
416 pop_query_status ret;
417 char buf[LONG_STRING];
418 POP_DATA *pop_data = (POP_DATA *) ctx->data;
420 pop_data->check_time = 0;
423 if (pop_reconnect (ctx) != PQ_OK)
424 return PQ_NOT_CONNECTED;
426 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
428 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
429 if (ctx->hdrs[i]->deleted) {
430 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
431 ret = pop_query (pop_data, buf, sizeof (buf));
436 m_strcpy(buf, sizeof(buf), "QUIT\r\n");
437 ret = pop_query (pop_data, buf, sizeof (buf));
441 pop_data->clear_cache = 1;
442 pop_clear_cache (pop_data);
443 pop_data->status = POP_DISCONNECTED;
448 mutt_error ("%s", pop_data->err_msg);
450 return PQ_NOT_CONNECTED;
455 /* Check for new messages and fetch headers */
456 static int pop_check_mailbox (CONTEXT * ctx,
457 int *index_hint __attribute__ ((unused)),
458 int unused __attribute__ ((unused)))
461 POP_DATA *pop_data = (POP_DATA *) ctx->data;
463 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
468 mutt_socket_close (pop_data->conn);
470 if (pop_open_connection (pop_data) < 0)
473 ctx->size = pop_data->size;
475 mutt_message _("Checking for new messages...");
477 ret = pop_fetch_headers (ctx);
478 pop_clear_cache (pop_data);
489 /* Fetch messages and save them in $spoolfile */
490 void pop_fetch_mail (void)
492 char buffer[LONG_STRING];
495 int i, delanswer, last = 0, msgs, bytes, rset = 0;
496 pop_query_status ret;
505 mutt_error _("POP host is not defined.");
510 plen = m_strlen(PopHost) + 7;
511 url = p = p_new(char, plen);
512 if (url_check_scheme (PopHost) == U_UNKNOWN) {
513 plen -= m_strcpy(url, plen, "pop://");
516 m_strcpy(p, plen, PopHost);
518 ret = pop_parse_path (url, &act);
521 mutt_error (_("%s is an invalid POP path"), PopHost);
525 conn = mutt_conn_find (NULL, &act);
529 pop_data = p_new(POP_DATA, 1);
530 pop_data->conn = conn;
532 if (pop_open_connection (pop_data) < 0) {
533 mutt_socket_free (pop_data->conn);
538 conn->data = pop_data;
540 mutt_message _("Checking for new messages...");
542 /* find out how many messages are in the mailbox. */
543 m_strcpy(buffer, sizeof(buffer), "STAT\r\n");
544 ret = pop_query (pop_data, buffer, sizeof (buffer));
545 if (ret == PQ_NOT_CONNECTED)
548 mutt_error ("%s", pop_data->err_msg);
552 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
554 /* only get unread messages */
555 if (msgs > 0 && option (OPTPOPLAST)) {
556 m_strcpy(buffer, sizeof(buffer), "LAST\r\n");
557 ret = pop_query (pop_data, buffer, sizeof (buffer));
558 if (ret == PQ_NOT_CONNECTED)
561 sscanf (buffer, "+OK %d", &last);
565 mutt_message _("No new mail in POP mailbox.");
570 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
574 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
576 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
578 mutt_message ("%s", msgbuf);
580 for (i = last + 1; i <= msgs; i++) {
581 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
584 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
585 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
586 if (ret == PFD_FUNCT_ERROR)
589 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
591 ret = PFD_FUNCT_ERROR;
594 mx_close_message (&msg);
597 if (ret == PQ_OK && delanswer == M_YES) {
598 /* delete the message on the server */
599 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
600 ret = pop_query (pop_data, buffer, sizeof (buffer));
603 if (ret == PQ_NOT_CONNECTED) {
604 mx_close_mailbox (&ctx, NULL);
608 mutt_error ("%s", pop_data->err_msg);
611 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
612 mutt_error _("Error while writing mailbox!");
617 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
621 mx_close_mailbox (&ctx, NULL);
624 /* make sure no messages get deleted */
625 m_strcpy(buffer, sizeof(buffer), "RSET\r\n");
626 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
631 /* exit gracefully */
632 m_strcpy(buffer, sizeof(buffer), "QUIT\r\n");
633 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
635 mutt_socket_close (conn);
640 mutt_error _("Server closed connection!");
641 mutt_socket_close (conn);
645 static int pop_is_magic (const char* path, struct stat* st __attribute__ ((unused))) {
646 url_scheme_t s = url_check_scheme (NONULL (path));
647 return ((s == U_POP || s == U_POPS) ? M_POP : -1);
650 static int acl_check_pop (CONTEXT* ctx __attribute__ ((unused)), int bit) {
652 case ACL_INSERT: /* editing messages */
653 case ACL_WRITE: /* change importance */
655 case ACL_DELETE: /* (un)deletion */
656 case ACL_SEEN: /* mark as read */
663 mx_t const pop_mx = {