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.
11 #include <sasl/sasl.h>
12 #include <sasl/saslutil.h>
14 #include <lib-sys/mutt_socket.h>
15 #include <lib-ui/lib-ui.h>
19 #include "mutt_sasl.h"
23 #define POP_SSL_PORT 995
24 #define POP_CACHE_LEN 10
25 /* maximal length of the server response (RFC1939) */
26 #define POP_CMD_RESPONSE 512
49 /* pop_fetch_data uses pop_query_status and this return value */
52 PQ_NOT_CONNECTED = -1,
57 CMD_NOT_AVAILABLE = 0,
59 CMD_UNKNOWN /* unknown whether it is available or not */
66 unsigned capabilities : 1;
67 unsigned use_stls : 2;
68 cmd_status cmd_capa : 2; /* optional command CAPA */
69 cmd_status cmd_stls : 2; /* optional command STLS */
70 cmd_status cmd_uidl : 2; /* optional command UIDL */
71 cmd_status cmd_top : 2; /* optional command TOP */
72 cmd_status cmd_user : 2; /* optional command USER */
73 unsigned clear_cache : 1;
77 char *auth_list; /* list of auth mechanisms */
79 char err_msg[POP_CMD_RESPONSE];
80 POP_CACHE cache[POP_CACHE_LEN];
83 /* pop low level functions {{{ */
85 static void pop_error(pop_data_t *pop_data, const char *msg)
87 if (!m_strncmp(msg, "-ERR ", 5)) {
88 const char *s = skipspaces(msg + 5);
93 m_strcat(pop_data->err_msg, sizeof(pop_data->err_msg), msg);
94 m_strrtrim(pop_data->err_msg);
98 * Send data from buffer and receive answer to the same buffer
100 * -1 - conection lost,
101 * -2 - invalid command or execution error.
103 static pop_query_status _pop_query(pop_data_t *pop_data, char *buf, ssize_t buflen)
105 if (pop_data->status != POP_CONNECTED)
106 return PQ_NOT_CONNECTED;
108 mutt_socket_write(pop_data->conn, buf);
109 snprintf(pop_data->err_msg, sizeof(pop_data->err_msg), "%.*s: ",
110 (int)(strpbrk(buf, " \r\n") - buf), buf);
112 if (mutt_socket_readln(buf, buflen, pop_data->conn) < 0) {
113 pop_data->status = POP_DISCONNECTED;
114 return PQ_NOT_CONNECTED;
116 if (!m_strncmp(buf, "+OK", 3))
119 pop_error(pop_data, buf);
122 #define pop_query(pd, b, l, fmt, ...) \
123 (snprintf(b, l, fmt "\r\n", ##__VA_ARGS__), _pop_query(pd, b, l))
128 * -1 - conection lost,
129 * -2 - invalid response.
131 static pop_query_status pop_connect(pop_data_t * pop_data)
133 char buf[LONG_STRING];
136 if (mutt_socket_open(pop_data->conn) < 0
137 || mutt_socket_readln(buf, sizeof(buf), pop_data->conn) < 0)
139 mutt_error(_("Error connecting to server: %s"),
140 pop_data->conn->account.host);
141 pop_data->status = POP_NONE;
142 return PQ_NOT_CONNECTED;
145 pop_data->status = POP_CONNECTED;
146 if (m_strncmp(buf, "+OK", 3)) {
147 pop_data->err_msg[0] = '\0';
148 pop_error(pop_data, buf);
149 mutt_error("%s", pop_data->err_msg);
153 p_delete(&pop_data->timestamp);
154 if ((p = strchr(buf, '<')) && (q = strchr(p, '>'))) {
155 pop_data->timestamp = p_dupstr(p, q + 1 - p);
160 static void pop_logout (CONTEXT * ctx)
162 pop_query_status ret = 0;
163 char buf[LONG_STRING];
164 pop_data_t *pop_data = (pop_data_t *) ctx->data;
166 if (pop_data->status == POP_CONNECTED) {
167 mutt_message _("Closing connection to POP server...");
170 ret = pop_query(pop_data, buf, sizeof(buf), "RSET");
173 if (ret != PQ_NOT_CONNECTED) {
174 pop_query(pop_data, buf, sizeof(buf), "QUIT");
180 pop_data->status = POP_DISCONNECTED;
185 /* Authentication {{{ */
187 static pop_auth_res_t pop_auth_sasl(pop_data_t *pop_data, const char *method)
189 sasl_conn_t *saslconn;
190 sasl_interact_t *interaction = NULL;
192 char buf[LONG_STRING], inbuf[LONG_STRING];
193 const char *mech, *pc = NULL;
194 unsigned int len, olen;
195 unsigned char client_start;
198 if (mutt_sasl_client_new(pop_data->conn, &saslconn) < 0) {
199 return POP_A_FAILURE;
203 method = pop_data->auth_list;
206 rc = sasl_client_start(saslconn, method, &interaction, &pc, &olen,
208 if (rc != SASL_INTERACT)
210 mutt_sasl_interact(interaction);
213 if (rc != SASL_OK && rc != SASL_CONTINUE) {
214 return POP_A_UNAVAIL;
217 client_start = (olen > 0);
218 mutt_message(_("Authenticating (SASL)..."));
219 snprintf(buf, sizeof (buf), "AUTH %s", mech);
222 /* looping protocol */
224 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
225 mutt_socket_write(pop_data->conn, buf);
226 if (mutt_socket_readln(inbuf, sizeof(inbuf), pop_data->conn) < 0) {
227 sasl_dispose(&saslconn);
228 pop_data->status = POP_DISCONNECTED;
232 if (rc != SASL_CONTINUE)
235 if (!m_strncmp(inbuf, "+ ", 2)
236 && sasl_decode64(inbuf, strlen(inbuf),
237 buf, sizeof(buf) - 1, &len) != SASL_OK)
244 rc = sasl_client_step(saslconn, buf, len, &interaction, &pc,
246 if (rc != SASL_INTERACT)
248 mutt_sasl_interact(interaction);
254 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
257 /* send out response, or line break if none needed */
259 if (sasl_encode64(pc, olen, buf, sizeof(buf), &olen) != SASL_OK) {
262 p_delete((char **)&pc);
269 if (!m_strncmp(inbuf, "+OK", 3)) {
270 mutt_sasl_setup_conn(pop_data->conn, saslconn);
271 return POP_A_SUCCESS;
275 sasl_dispose(&saslconn);
276 p_delete((char **)&pc);
278 /* terminate SASL session if the last responce is not +OK nor -ERR */
279 if (!m_strncmp(inbuf, "+ ", 2)) {
280 if (pop_query(pop_data, buf, sizeof(buf), "*") == PQ_NOT_CONNECTED)
284 mutt_error(_("SASL authentication failed."));
287 return POP_A_FAILURE;
290 static pop_auth_res_t pop_auth_apop(pop_data_t *pop_data, const char *method)
293 unsigned char digest[16];
295 char buf[LONG_STRING];
298 if (!pop_data->timestamp)
299 return POP_A_UNAVAIL;
301 mutt_message _("Authenticating (APOP)...");
303 /* Compute the authentication hash to send to the server */
305 MD5Update(&mdContext, (unsigned char *)pop_data->timestamp,
306 strlen(pop_data->timestamp));
307 MD5Update(&mdContext, (unsigned char *)pop_data->conn->account.pass,
308 strlen(pop_data->conn->account.pass));
309 MD5Final(digest, &mdContext);
311 for (i = 0; i < countof(digest); i++)
312 sprintf(hash + 2 * i, "%02x", digest[i]);
314 switch (pop_query(pop_data, buf, sizeof(buf), "APOP %s %s",
315 pop_data->conn->account.user, hash))
318 return POP_A_SUCCESS;
319 case PQ_NOT_CONNECTED:
322 mutt_error("%s %s", _("APOP authentication failed."), pop_data->err_msg);
324 return POP_A_FAILURE;
328 static pop_auth_res_t pop_auth_user(pop_data_t *pop_data, const char *method)
330 char buf[LONG_STRING];
331 pop_query_status ret;
333 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
334 return POP_A_UNAVAIL;
336 mutt_message _("Authenticating (USER)...");
337 ret = pop_query(pop_data, buf, sizeof(buf), "USER %s",
338 pop_data->conn->account.user);
340 if (pop_data->cmd_user == CMD_UNKNOWN) {
342 pop_data->cmd_user = CMD_AVAILABLE;
344 pop_data->cmd_user = CMD_NOT_AVAILABLE;
348 ret = pop_query(pop_data, buf, sizeof(buf), "PASS %s",
349 pop_data->conn->account.pass);
354 return POP_A_SUCCESS;
355 case PQ_NOT_CONNECTED:
358 mutt_error("%s %s", _("USER authentication failed."), pop_data->err_msg);
360 return POP_A_FAILURE;
365 pop_auth_res_t (*do_auth)(pop_data_t *, const char *);
369 static pop_query_status pop_authenticate (pop_data_t * pop_data)
371 static pop_auth_t const pop_authenticators[] = {
372 {pop_auth_sasl, NULL},
373 {pop_auth_apop, "apop"},
374 {pop_auth_user, "user"},
378 ACCOUNT *act = &pop_data->conn->account;
379 const pop_auth_t *auth;
382 if (mutt_account_getuser(act) || !act->user[0]
383 || mutt_account_getpass(act) || !act->pass[0])
385 return PFD_FUNCT_ERROR;
388 if (!m_strisempty(PopAuthenticators)) {
392 for (p = PopAuthenticators; ; p = q) {
398 q = strchrnul(p, ':');
399 m_strncpy(buf, sizeof(buf), p, q - p);
401 for (auth = pop_authenticators; auth->do_auth; auth++) {
402 if (auth->method && ascii_strcasecmp(auth->method, buf))
405 switch (auth->do_auth(pop_data, buf)) {
409 return PQ_NOT_CONNECTED;
416 mutt_socket_close(pop_data->conn);
420 for (auth = pop_authenticators; auth->do_auth; auth++) {
421 switch (auth->do_auth(pop_data, auth->method)) {
425 return PQ_NOT_CONNECTED;
432 mutt_socket_close(pop_data->conn);
437 mutt_error(_("No authenticators available"));
444 * This function calls funct(*line, *data) for each received line,
445 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
448 * -1 - conection lost,
449 * -2 - invalid command or execution error,
450 * -3 - error in funct(*line, *data)
452 static pop_query_status
453 pop_fetch_data(pop_data_t *pop_data, const char *query, progress_t *bar,
454 int (*funct)(char *, void *), void *data)
456 pop_query_status ret;
457 char buf[LONG_STRING];
463 m_strcpy(buf, sizeof(buf), query);
464 ret = _pop_query(pop_data, buf, sizeof(buf));
471 if (mutt_socket_readln2(&inbuf, pop_data->conn) < 0) {
472 pop_data->status = POP_DISCONNECTED;
473 ret = PQ_NOT_CONNECTED;
478 mutt_progress_bar(bar, pos += inbuf.len);
481 if (inbuf.data[0] == '.') {
482 if (inbuf.data[1] != '.')
487 if (funct(inbuf.data + dot, data) < 0) {
489 ret = PFD_FUNCT_ERROR;
493 buffer_reset(&inbuf);
500 static int fetch_capa (char *line, void *data)
502 pop_data_t *pop_data = (pop_data_t *) data;
505 if (!ascii_strncasecmp (line, "SASL", 4)) {
506 p_delete(&pop_data->auth_list);
507 c = vskipspaces(line + 4);
508 pop_data->auth_list = m_strdup(c);
511 else if (!ascii_strncasecmp (line, "STLS", 4))
512 pop_data->cmd_stls = CMD_AVAILABLE;
514 else if (!ascii_strncasecmp (line, "UIDL", 4))
515 pop_data->cmd_uidl = CMD_AVAILABLE;
517 else if (!ascii_strncasecmp (line, "TOP", 3))
518 pop_data->cmd_top = CMD_AVAILABLE;
523 static int fetch_auth (char *line, void *data)
525 pop_data_t *pop_data = (pop_data_t *) data;
526 ssize_t auth_list_len;
528 if (!pop_data->auth_list) {
529 auth_list_len = m_strlen(line) + 1;
530 pop_data->auth_list = p_new(char, auth_list_len);
532 auth_list_len = m_strlen(pop_data->auth_list) + m_strlen(line) + 2;
533 p_realloc(&pop_data->auth_list, auth_list_len);
534 m_strcat(pop_data->auth_list, auth_list_len, " ");
536 m_strcat(pop_data->auth_list, auth_list_len, line);
544 * -1 - conection lost,
545 * -2 - execution error.
547 static pop_query_status pop_capabilities(pop_data_t * pop_data, int mode)
549 /* don't check capabilities on reconnect */
550 if (pop_data->capabilities)
553 /* init capabilities */
555 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
556 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
557 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
558 pop_data->cmd_top = CMD_NOT_AVAILABLE;
559 pop_data->cmd_user = CMD_NOT_AVAILABLE;
560 p_delete(&pop_data->auth_list);
563 /* Execute CAPA command */
564 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
565 switch (pop_fetch_data(pop_data, "CAPA\r\n", NULL, fetch_capa, pop_data)) {
567 pop_data->cmd_capa = CMD_AVAILABLE;
569 case PFD_FUNCT_ERROR:
571 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
573 case PQ_NOT_CONNECTED:
574 return PQ_NOT_CONNECTED;
578 /* CAPA not supported, use defaults */
579 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
580 pop_data->cmd_uidl = CMD_UNKNOWN;
581 pop_data->cmd_top = CMD_UNKNOWN;
583 if (pop_fetch_data(pop_data, "AUTH\r\n", NULL, fetch_auth, pop_data) ==
585 return PQ_NOT_CONNECTED;
588 /* Check capabilities */
590 const char *msg = NULL;
592 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
593 msg = _("Command TOP is not supported by server.");
594 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
595 msg = _("Command UIDL is not supported by server.");
596 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
600 pop_data->capabilities = 1;
606 /* given an POP mailbox name, return host, port, username and password */
607 static int pop_parse_path (const char *path, ACCOUNT * act)
615 act->port = POP_PORT;
616 act->type = M_ACCT_TYPE_POP;
619 url_parse_ciss (&url, c);
621 if (url.scheme == U_POP || url.scheme == U_POPS) {
622 if (url.scheme == U_POPS) {
624 act->port = POP_SSL_PORT;
627 if ((!url.path || !*url.path) && mutt_account_fromurl (act, &url) == 0)
636 * Open connection and authenticate
638 * -1 - conection lost,
639 * -2 - invalid command or execution error,
640 * -3 - authentication canceled.
642 static pop_query_status pop_open_connection (pop_data_t * pop_data)
644 pop_query_status ret;
646 char buf[LONG_STRING];
648 ret = pop_connect(pop_data);
654 ret = pop_capabilities (pop_data, 0);
655 if (ret == PQ_NOT_CONNECTED)
662 /* Attempt STLS if available and desired. */
663 if (!pop_data->conn->ssf && (pop_data->cmd_stls || mod_ssl.force_tls)) {
664 if (mod_ssl.force_tls)
665 pop_data->use_stls = 2;
666 if (pop_data->use_stls == 0) {
667 pop_data->use_stls = 1;
668 if (mod_ssl.starttls)
669 pop_data->use_stls = 2;
671 if (pop_data->use_stls == 2) {
672 ret = pop_query(pop_data, buf, sizeof(buf), "STLS");
673 if (ret == PQ_NOT_CONNECTED)
676 mutt_error ("%s", pop_data->err_msg);
679 else if (mutt_ssl_starttls (pop_data->conn))
681 mutt_error (_("Could not negotiate TLS connection"));
686 /* recheck capabilities after STLS completes */
687 ret = pop_capabilities (pop_data, 1);
688 if (ret == PQ_NOT_CONNECTED)
698 if (mod_ssl.force_tls && !pop_data->conn->ssf) {
699 mutt_error _("Encrypted connection unavailable");
704 ret = pop_authenticate (pop_data);
705 if (ret == PQ_NOT_CONNECTED)
707 if (ret == PFD_FUNCT_ERROR)
712 /* recheck capabilities after authentication */
713 ret = pop_capabilities (pop_data, 2);
714 if (ret == PQ_NOT_CONNECTED)
721 /* get total size of mailbox */
722 ret = pop_query(pop_data, buf, sizeof(buf), "STAT");
723 if (ret == PQ_NOT_CONNECTED)
726 mutt_error ("%s", pop_data->err_msg);
731 sscanf (buf, "+OK %u %u", &n, &size);
732 pop_data->size = size;
736 pop_data->status = POP_DISCONNECTED;
737 mutt_error _("Server closed connection!");
740 return PQ_NOT_CONNECTED;
743 /* find message with this UIDL and set refno */
744 static int check_uidl (char *line, void *data)
747 CONTEXT *ctx = (CONTEXT *)data;
749 sscanf (line, "%u %s", &idx, line);
750 for (i = 0; i < ctx->msgcount; i++) {
751 if (!m_strcmp(ctx->hdrs[i]->data, line)) {
752 ctx->hdrs[i]->refno = idx;
760 /* reconnect and verify indexes if connection was lost */
761 static pop_query_status pop_reconnect (CONTEXT * ctx)
763 pop_query_status ret;
764 pop_data_t *pop_data = (pop_data_t *) ctx->data;
767 if (pop_data->status == POP_CONNECTED)
769 if (pop_data->status == POP_BYE)
770 return PQ_NOT_CONNECTED;
773 mutt_socket_close (pop_data->conn);
775 ret = pop_open_connection(pop_data);
779 bar.msg = _("Verifying message indexes...");
781 mutt_progress_bar (&bar, 0);
783 for (i = 0; i < ctx->msgcount; i++)
784 ctx->hdrs[i]->refno = -1;
786 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
788 mutt_error ("%s", pop_data->err_msg);
798 return PQ_NOT_CONNECTED;
800 if (query_quadoption (OPT_POPRECONNECT,
801 _("Connection lost. Reconnect to POP server?")) !=
803 return PQ_NOT_CONNECTED;
807 /* write line to file */
808 static int fetch_message (char *line, void *file)
810 FILE *f = (FILE *) file;
813 if (fputc ('\n', f) == EOF)
823 * -1 - conection lost,
824 * -2 - invalid command or execution error,
825 * -3 - error writing to tempfile
827 static pop_query_status pop_read_header (pop_data_t * pop_data, HEADER * h)
831 pop_query_status ret;
833 char buf[LONG_STRING];
837 mutt_error(_("Could not create temporary file"));
838 return PFD_FUNCT_ERROR;
841 ret = pop_query(pop_data, buf, sizeof(buf), "LIST %d", h->refno);
843 sscanf (buf, "+OK %d %ld", &idx, &length);
845 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
846 ret = pop_fetch_data(pop_data, buf, NULL, fetch_message, f);
848 if (pop_data->cmd_top == CMD_UNKNOWN) {
850 pop_data->cmd_top = CMD_AVAILABLE;
854 pop_data->cmd_top = CMD_NOT_AVAILABLE;
855 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
856 _("Command TOP is not supported by server."));
865 h->env = mutt_read_rfc822_header (f, h, 0, 0);
866 h->content->length = length - h->content->offset + 1;
869 h->content->length--;
870 fgets (buf, sizeof (buf), f);
876 mutt_error ("%s", pop_data->err_msg);
879 case PFD_FUNCT_ERROR:
881 mutt_error _("Can't write header to temporary file!");
885 case PQ_NOT_CONNECTED:
887 mutt_error _("Can't fetch header: Not connected!");
897 static int fetch_uidl (char *line, void *data)
900 CONTEXT *ctx = (CONTEXT *) data;
901 pop_data_t *pop_data = (pop_data_t *) ctx->data;
903 sscanf (line, "%d %s", &idx, line);
904 for (i = 0; i < ctx->msgcount; i++)
905 if (!m_strcmp(line, ctx->hdrs[i]->data))
908 if (i == ctx->msgcount) {
909 if (i >= ctx->hdrmax)
910 mx_alloc_memory (ctx);
913 ctx->hdrs[i] = header_new();
914 ctx->hdrs[i]->data = m_strdup(line);
916 else if (ctx->hdrs[i]->index != idx - 1)
917 pop_data->clear_cache = 1;
919 ctx->hdrs[i]->refno = idx;
920 ctx->hdrs[i]->index = idx - 1;
929 * -1 - conection lost,
930 * -2 - invalid command or execution error,
931 * -3 - error writing to tempfile
933 static int pop_fetch_headers (CONTEXT * ctx)
935 int i, old_count, new_count;
936 pop_query_status ret;
937 pop_data_t *pop_data = (pop_data_t *) ctx->data;
939 time (&pop_data->check_time);
940 pop_data->clear_cache = 0;
942 for (i = 0; i < ctx->msgcount; i++)
943 ctx->hdrs[i]->refno = -1;
945 old_count = ctx->msgcount;
946 ret = pop_fetch_data(pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
947 new_count = ctx->msgcount;
948 ctx->msgcount = old_count;
950 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
952 pop_data->cmd_uidl = CMD_AVAILABLE;
955 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
956 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
958 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
959 _("Command UIDL is not supported by server."));
964 for (i = 0; i < old_count; i++)
965 if (ctx->hdrs[i]->refno == -1)
966 ctx->hdrs[i]->deleted = 1;
968 for (i = old_count; i < new_count; i++) {
969 mutt_message (_("Fetching message headers... [%d/%d]"),
970 i + 1 - old_count, new_count - old_count);
972 ret = pop_read_header (pop_data, ctx->hdrs[i]);
980 mx_update_context (ctx, i - old_count);
984 for (i = ctx->msgcount; i < new_count; i++)
985 header_delete(&ctx->hdrs[i]);
990 return new_count - old_count;
993 /* delete all cached messages */
994 static void pop_clear_cache (pop_data_t * pop_data)
998 if (!pop_data->clear_cache)
1001 for (i = 0; i < POP_CACHE_LEN; i++) {
1002 if (pop_data->cache[i].path) {
1003 unlink (pop_data->cache[i].path);
1004 p_delete(&pop_data->cache[i].path);
1009 /* pop_mx functions {{{ */
1011 static int pop_is_magic(const char* path, struct stat* st)
1013 url_scheme_t s = url_check_scheme(NONULL(path));
1014 return s == U_POP || s == U_POPS ? M_POP : -1;
1017 static int pop_open_mailbox (CONTEXT * ctx)
1020 char buf[LONG_STRING];
1023 pop_data_t *pop_data;
1026 if (pop_parse_path (ctx->path, &act)) {
1027 mutt_error (_("%s is an invalid POP path"), ctx->path);
1032 mutt_account_tourl (&act, &url);
1034 url_ciss_tostring (&url, buf, sizeof (buf), 0);
1035 conn = mutt_conn_find (NULL, &act);
1039 p_delete(&ctx->path);
1040 ctx->path = m_strdup(buf);
1042 pop_data = p_new(pop_data_t, 1);
1043 pop_data->conn = conn;
1044 ctx->data = pop_data;
1046 if (pop_open_connection(pop_data) != PQ_OK)
1049 conn->data = pop_data;
1052 if (pop_reconnect (ctx) != PQ_OK)
1055 mutt_message _("Fetching list of messages...");
1056 ctx->size = pop_data->size;
1057 ret = pop_fetch_headers (ctx);
1069 static int pop_acl_check(CONTEXT *ctx, int bit)
1072 case ACL_DELETE: /* (un)deletion */
1073 case ACL_SEEN: /* mark as read */
1075 case ACL_INSERT: /* editing messages */
1076 case ACL_WRITE: /* change importance */
1082 static int pop_check_mailbox(CONTEXT * ctx, int *index_hint, int unused)
1085 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1087 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
1092 mutt_socket_close (pop_data->conn);
1094 if (pop_open_connection (pop_data) < 0)
1097 mutt_message _("Checking for new messages...");
1098 ctx->size = pop_data->size;
1099 ret = pop_fetch_headers (ctx);
1100 pop_clear_cache (pop_data);
1111 static void pop_close_mailbox (CONTEXT * ctx)
1113 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1120 if (pop_data->status != POP_NONE)
1121 mutt_socket_close (pop_data->conn);
1123 pop_data->status = POP_NONE;
1125 pop_data->clear_cache = 1;
1126 pop_clear_cache (pop_data);
1128 if (!pop_data->conn->data)
1129 mutt_socket_free (pop_data->conn);
1134 static int pop_sync_mailbox(CONTEXT * ctx, int unused, int *index_hint)
1137 pop_query_status ret;
1138 char buf[LONG_STRING];
1139 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1141 pop_data->check_time = 0;
1144 if (pop_reconnect (ctx) != PQ_OK)
1145 return PQ_NOT_CONNECTED;
1147 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
1149 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
1150 if (ctx->hdrs[i]->deleted) {
1151 ret = pop_query(pop_data, buf, sizeof(buf), "DELE %d",
1152 ctx->hdrs[i]->refno);
1157 ret = pop_query(pop_data, buf, sizeof(buf), "QUIT");
1161 pop_data->clear_cache = 1;
1162 pop_clear_cache (pop_data);
1163 pop_data->status = POP_DISCONNECTED;
1167 if (ret == PQ_ERR) {
1168 mutt_error ("%s", pop_data->err_msg);
1170 return PQ_NOT_CONNECTED;
1175 /* fetch message from POP server */
1176 static int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1180 char buf[LONG_STRING];
1181 char path[_POSIX_PATH_MAX];
1183 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1185 HEADER *h = ctx->hdrs[msgno];
1187 /* see if we already have the message in our cache */
1188 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
1191 if (cache->index == h->index) {
1192 /* yes, so just return a pointer to the message */
1193 msg->fp = fopen (cache->path, "r");
1197 mutt_perror (cache->path);
1202 /* clear the previous entry */
1203 unlink (cache->path);
1204 p_delete(&cache->path);
1209 if (pop_reconnect (ctx) != PQ_OK)
1212 /* verify that massage index is correct */
1215 _("The message index is incorrect. Try reopening the mailbox.");
1220 bar.size = h->content->length + h->content->offset - 1;
1221 bar.msg = _("Fetching message...");
1222 mutt_progress_bar (&bar, 0);
1224 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
1226 mutt_error(_("Could not create temporary file"));
1231 snprintf(buf, sizeof(buf), "RETR %d\r\n", h->refno);
1232 ret = pop_fetch_data(pop_data, buf, &bar, fetch_message, msg->fp);
1239 if (ret == PQ_ERR) {
1240 mutt_error ("%s", pop_data->err_msg);
1245 if (ret == PFD_FUNCT_ERROR) {
1246 mutt_error _("Can't write message to temporary file!");
1253 /* Update the header information. Previously, we only downloaded a
1254 * portion of the headers, those required for the main display.
1256 cache->index = h->index;
1257 cache->path = m_strdup(path);
1260 envelope_delete(&h->env);
1261 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
1264 fgets (buf, sizeof (buf), msg->fp);
1265 while (!feof (msg->fp)) {
1266 ctx->hdrs[msgno]->lines++;
1267 fgets (buf, sizeof (buf), msg->fp);
1270 h->content->length = ftello (msg->fp) - h->content->offset;
1272 /* This needs to be done in case this is a multipart message */
1273 h->security = crypt_query (h->content);
1275 mutt_clear_error ();
1283 mx_t const pop_mx = {
1299 /* public API {{{ */
1301 void pop_fetch_mail (void)
1303 char buffer[LONG_STRING];
1304 char msgbuf[STRING];
1306 int i, delanswer, last = 0, msgs, bytes, rset = 0;
1307 pop_query_status ret;
1310 MESSAGE *msg = NULL;
1312 pop_data_t *pop_data;
1315 if (m_strisempty(PopHost)) {
1316 mutt_error _("POP host is not defined.");
1320 plen = m_strlen(PopHost) + 7;
1321 url = p = p_new(char, plen);
1322 if (url_check_scheme (PopHost) == U_UNKNOWN) {
1323 snprintf(p, plen, "pop://%s", PopHost);
1325 m_strcpy(p, plen, PopHost);
1328 ret = pop_parse_path (url, &act);
1331 mutt_error (_("%s is an invalid POP path"), PopHost);
1335 conn = mutt_conn_find (NULL, &act);
1339 pop_data = p_new(pop_data_t, 1);
1340 pop_data->conn = conn;
1342 if (pop_open_connection (pop_data) < 0) {
1343 mutt_socket_free (pop_data->conn);
1344 p_delete(&pop_data);
1348 conn->data = pop_data;
1350 mutt_message _("Checking for new messages...");
1352 /* find out how many messages are in the mailbox. */
1353 ret = pop_query(pop_data, buffer, sizeof(buffer), "STAT");
1354 if (ret == PQ_NOT_CONNECTED)
1356 if (ret == PQ_ERR) {
1357 mutt_error ("%s", pop_data->err_msg);
1361 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
1363 /* only get unread messages */
1364 if (msgs > 0 && option (OPTPOPLAST)) {
1365 ret = pop_query(pop_data, buffer, sizeof(buffer), "LAST");
1366 if (ret == PQ_NOT_CONNECTED)
1369 sscanf (buffer, "+OK %d", &last);
1373 mutt_message _("No new mail in POP mailbox.");
1378 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
1382 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
1384 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
1386 mutt_message ("%s", msgbuf);
1388 for (i = last + 1; i <= msgs; i++) {
1389 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
1392 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
1393 ret = pop_fetch_data(pop_data, buffer, NULL, fetch_message, msg->fp);
1394 if (ret == PFD_FUNCT_ERROR)
1397 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
1399 ret = PFD_FUNCT_ERROR;
1402 mx_close_message (&msg);
1405 if (ret == PQ_OK && delanswer == M_YES) {
1406 ret = pop_query(pop_data, buffer, sizeof(buffer), "DELE %d", i);
1409 if (ret == PQ_NOT_CONNECTED) {
1410 mx_close_mailbox (&ctx, NULL);
1413 if (ret == PQ_ERR) {
1414 mutt_error ("%s", pop_data->err_msg);
1417 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
1418 mutt_error _("Error while writing mailbox!");
1423 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
1427 mx_close_mailbox (&ctx, NULL);
1430 /* make sure no messages get deleted */
1431 if (pop_query(pop_data, buffer, sizeof(buffer), "RSET") ==
1437 if (pop_query(pop_data, buffer, sizeof(buffer), "QUIT") == PQ_NOT_CONNECTED)
1439 mutt_socket_close (conn);
1440 p_delete(&pop_data);
1444 mutt_error _("Server closed connection!");
1445 mutt_socket_close (conn);
1446 p_delete(&pop_data);