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 <sasl/sasl.h>
13 #include <sasl/saslutil.h>
15 #include <lib-sys/mutt_socket.h>
16 #include <lib-ui/curses.h>
20 #include "mutt_sasl.h"
24 #define POP_SSL_PORT 995
25 #define POP_CACHE_LEN 10
26 /* maximal length of the server response (RFC1939) */
27 #define POP_CMD_RESPONSE 512
50 /* pop_fetch_data uses pop_query_status and this return value */
53 PQ_NOT_CONNECTED = -1,
58 CMD_NOT_AVAILABLE = 0,
60 CMD_UNKNOWN /* unknown whether it is available or not */
67 unsigned capabilities : 1;
68 unsigned use_stls : 2;
69 cmd_status cmd_capa : 2; /* optional command CAPA */
70 cmd_status cmd_stls : 2; /* optional command STLS */
71 cmd_status cmd_uidl : 2; /* optional command UIDL */
72 cmd_status cmd_top : 2; /* optional command TOP */
73 cmd_status cmd_user : 2; /* optional command USER */
74 unsigned resp_codes : 1; /* server supports extended response codes */
75 unsigned expire : 1; /* expire is greater than 0 */
76 unsigned clear_cache : 1;
80 char *auth_list; /* list of auth mechanisms */
82 char err_msg[POP_CMD_RESPONSE];
83 POP_CACHE cache[POP_CACHE_LEN];
86 /* pop low level functions {{{ */
88 static void pop_error(pop_data_t *pop_data, const char *msg)
90 if (!m_strncmp(msg, "-ERR ", 5)) {
91 const char *s = skipspaces(msg + 5);
96 m_strcat(pop_data->err_msg, sizeof(pop_data->err_msg), msg);
97 m_strrtrim(pop_data->err_msg);
101 * Send data from buffer and receive answer to the same buffer
103 * -1 - conection lost,
104 * -2 - invalid command or execution error.
106 static pop_query_status _pop_query(pop_data_t *pop_data, char *buf, ssize_t buflen)
110 if (pop_data->status != POP_CONNECTED)
111 return PQ_NOT_CONNECTED;
113 mutt_socket_write(pop_data->conn, buf);
115 c = strpbrk(buf, " \r\n");
117 snprintf(pop_data->err_msg, sizeof(pop_data->err_msg), "%s: ", buf);
119 if (mutt_socket_readln(buf, buflen, pop_data->conn) < 0) {
120 pop_data->status = POP_DISCONNECTED;
121 return PQ_NOT_CONNECTED;
123 if (!m_strncmp(buf, "+OK", 3))
126 pop_error(pop_data, buf);
129 #define pop_query(pd, b, l, fmt, ...) \
130 (snprintf(b, l, fmt "\r\n", ##__VA_ARGS__), _pop_query(pd, b, l))
135 * -1 - conection lost,
136 * -2 - invalid response.
138 static pop_query_status pop_connect(pop_data_t * pop_data)
140 char buf[LONG_STRING];
143 if (mutt_socket_open(pop_data->conn) < 0
144 || mutt_socket_readln(buf, sizeof(buf), pop_data->conn) < 0)
146 mutt_error(_("Error connecting to server: %s"),
147 pop_data->conn->account.host);
148 pop_data->status = POP_NONE;
149 return PQ_NOT_CONNECTED;
152 pop_data->status = POP_CONNECTED;
153 if (m_strncmp(buf, "+OK", 3)) {
154 pop_data->err_msg[0] = '\0';
155 pop_error(pop_data, buf);
156 mutt_error("%s", pop_data->err_msg);
160 p_delete(&pop_data->timestamp);
161 if ((p = strchr(buf, '<')) && (q = strchr(p, '>'))) {
162 pop_data->timestamp = p_dupstr(p, q - p);
167 static void pop_logout (CONTEXT * ctx)
169 pop_query_status ret = 0;
170 char buf[LONG_STRING];
171 pop_data_t *pop_data = (pop_data_t *) ctx->data;
173 if (pop_data->status == POP_CONNECTED) {
174 mutt_message _("Closing connection to POP server...");
177 ret = pop_query(pop_data, buf, sizeof(buf), "RSET");
180 if (ret != PQ_NOT_CONNECTED) {
181 pop_query(pop_data, buf, sizeof(buf), "QUIT");
187 pop_data->status = POP_DISCONNECTED;
192 /* Authentication {{{ */
194 static pop_auth_res_t pop_auth_sasl(pop_data_t *pop_data, const char *method)
196 sasl_conn_t *saslconn;
197 sasl_interact_t *interaction = NULL;
199 char buf[LONG_STRING], inbuf[LONG_STRING];
200 const char *mech, *pc = NULL;
201 unsigned int len, olen;
202 unsigned char client_start;
205 if (mutt_sasl_client_new(pop_data->conn, &saslconn) < 0) {
206 return POP_A_FAILURE;
210 method = pop_data->auth_list;
213 rc = sasl_client_start(saslconn, method, &interaction, &pc, &olen,
215 if (rc != SASL_INTERACT)
217 mutt_sasl_interact(interaction);
220 if (rc != SASL_OK && rc != SASL_CONTINUE) {
221 return POP_A_UNAVAIL;
224 client_start = (olen > 0);
225 mutt_message(_("Authenticating (SASL)..."));
226 snprintf(buf, sizeof (buf), "AUTH %s", mech);
229 /* looping protocol */
231 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
232 mutt_socket_write(pop_data->conn, buf);
233 if (mutt_socket_readln(inbuf, sizeof(inbuf), pop_data->conn) < 0) {
234 sasl_dispose(&saslconn);
235 pop_data->status = POP_DISCONNECTED;
239 if (rc != SASL_CONTINUE)
242 if (!m_strncmp(inbuf, "+ ", 2)
243 && sasl_decode64(inbuf, strlen(inbuf),
244 buf, sizeof(buf) - 1, &len) != SASL_OK)
251 rc = sasl_client_step(saslconn, buf, len, &interaction, &pc,
253 if (rc != SASL_INTERACT)
255 mutt_sasl_interact(interaction);
261 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
264 /* send out response, or line break if none needed */
266 if (sasl_encode64(pc, olen, buf, sizeof(buf), &olen) != SASL_OK) {
269 p_delete((char **)&pc);
276 if (!m_strncmp(inbuf, "+OK", 3)) {
277 mutt_sasl_setup_conn(pop_data->conn, saslconn);
278 return POP_A_SUCCESS;
282 sasl_dispose(&saslconn);
283 p_delete((char **)&pc);
285 /* terminate SASL session if the last responce is not +OK nor -ERR */
286 if (!m_strncmp(inbuf, "+ ", 2)) {
287 if (pop_query(pop_data, buf, sizeof(buf), "*") == PQ_NOT_CONNECTED)
291 mutt_error(_("SASL authentication failed."));
294 return POP_A_FAILURE;
297 static pop_auth_res_t pop_auth_apop(pop_data_t *pop_data, const char *method)
300 unsigned char digest[16];
302 char buf[LONG_STRING];
305 if (!pop_data->timestamp)
306 return POP_A_UNAVAIL;
308 mutt_message _("Authenticating (APOP)...");
310 /* Compute the authentication hash to send to the server */
312 MD5Update(&mdContext, (unsigned char *)pop_data->timestamp,
313 strlen(pop_data->timestamp));
314 MD5Update(&mdContext, (unsigned char *)pop_data->conn->account.pass,
315 strlen(pop_data->conn->account.pass));
316 MD5Final(digest, &mdContext);
318 for (i = 0; i < countof(digest); i++)
319 sprintf(hash + 2 * i, "%02x", digest[i]);
321 switch (pop_query(pop_data, buf, sizeof(buf), "APOP %s %s",
322 pop_data->conn->account.user, hash))
325 return POP_A_SUCCESS;
326 case PQ_NOT_CONNECTED:
329 mutt_error("%s %s", _("APOP authentication failed."), pop_data->err_msg);
331 return POP_A_FAILURE;
335 static pop_auth_res_t pop_auth_user(pop_data_t *pop_data, const char *method)
337 char buf[LONG_STRING];
338 pop_query_status ret;
340 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
341 return POP_A_UNAVAIL;
343 mutt_message _("Authenticating (USER)...");
344 ret = pop_query(pop_data, buf, sizeof(buf), "USER %s",
345 pop_data->conn->account.user);
347 if (pop_data->cmd_user == CMD_UNKNOWN) {
349 pop_data->cmd_user = CMD_AVAILABLE;
351 pop_data->cmd_user = CMD_NOT_AVAILABLE;
355 ret = pop_query(pop_data, buf, sizeof(buf), "PASS %s",
356 pop_data->conn->account.pass);
361 return POP_A_SUCCESS;
362 case PQ_NOT_CONNECTED:
365 mutt_error("%s %s", _("USER authentication failed."), pop_data->err_msg);
367 return POP_A_FAILURE;
372 pop_auth_res_t (*do_auth)(pop_data_t *, const char *);
376 static pop_query_status pop_authenticate (pop_data_t * pop_data)
378 static pop_auth_t const pop_authenticators[] = {
379 {pop_auth_sasl, NULL},
380 {pop_auth_apop, "apop"},
381 {pop_auth_user, "user"},
385 ACCOUNT *act = &pop_data->conn->account;
386 const pop_auth_t *auth;
389 if (mutt_account_getuser(act) || !act->user[0]
390 || mutt_account_getpass(act) || !act->pass[0])
392 return PFD_FUNCT_ERROR;
395 if (!m_strisempty(PopAuthenticators)) {
399 for (p = PopAuthenticators; ; p = q) {
405 q = strchrnul(p, ':');
406 m_strncpy(buf, sizeof(buf), p, q - p);
408 for (auth = pop_authenticators; auth->do_auth; auth++) {
409 if (auth->method && ascii_strcasecmp(auth->method, buf))
412 switch (auth->do_auth(pop_data, buf)) {
416 return PQ_NOT_CONNECTED;
426 for (auth = pop_authenticators; auth->do_auth; auth++) {
427 switch (auth->do_auth(pop_data, auth->method)) {
431 return PQ_NOT_CONNECTED;
442 mutt_error(_("No authenticators available"));
449 * This function calls funct(*line, *data) for each received line,
450 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
453 * -1 - conection lost,
454 * -2 - invalid command or execution error,
455 * -3 - error in funct(*line, *data)
457 static pop_query_status
458 pop_fetch_data(pop_data_t *pop_data, const char *query, progress_t *bar,
459 int (*funct)(char *, void *), void *data)
461 char buf[LONG_STRING];
464 pop_query_status ret;
469 m_strcpy(buf, sizeof(buf), query);
470 ret = _pop_query(pop_data, buf, sizeof(buf));
474 inbuf = p_new(char, sizeof(buf));
478 mutt_socket_readln(buf, sizeof (buf), pop_data->conn);
480 pop_data->status = POP_DISCONNECTED;
481 ret = PQ_NOT_CONNECTED;
486 if (!lenbuf && buf[0] == '.') {
492 m_strcpy(inbuf + lenbuf,sizeof(buf), p);
495 if (chunk >= ssizeof(buf)) {
496 lenbuf += strlen (p);
499 mutt_progress_bar (bar, pos);
500 if (ret == 0 && funct (inbuf, data) < 0)
501 ret = PFD_FUNCT_ERROR;
505 p_realloc(&inbuf, lenbuf + sizeof(buf));
512 static int fetch_capa (char *line, void *data)
514 pop_data_t *pop_data = (pop_data_t *) data;
517 if (!ascii_strncasecmp (line, "SASL", 4)) {
518 p_delete(&pop_data->auth_list);
519 c = vskipspaces(line + 4);
520 pop_data->auth_list = m_strdup(c);
523 else if (!ascii_strncasecmp (line, "STLS", 4))
524 pop_data->cmd_stls = CMD_AVAILABLE;
526 else if (!ascii_strncasecmp (line, "UIDL", 4))
527 pop_data->cmd_uidl = CMD_AVAILABLE;
529 else if (!ascii_strncasecmp (line, "TOP", 3))
530 pop_data->cmd_top = CMD_AVAILABLE;
535 static int fetch_auth (char *line, void *data)
537 pop_data_t *pop_data = (pop_data_t *) data;
538 ssize_t auth_list_len;
540 if (!pop_data->auth_list) {
541 auth_list_len = m_strlen(line) + 1;
542 pop_data->auth_list = p_new(char, auth_list_len);
544 auth_list_len = m_strlen(pop_data->auth_list) + m_strlen(line) + 2;
545 p_realloc(&pop_data->auth_list, auth_list_len);
546 m_strcat(pop_data->auth_list, auth_list_len, " ");
548 m_strcat(pop_data->auth_list, auth_list_len, line);
556 * -1 - conection lost,
557 * -2 - execution error.
559 static pop_query_status pop_capabilities(pop_data_t * pop_data, int mode)
561 /* don't check capabilities on reconnect */
562 if (pop_data->capabilities)
565 /* init capabilities */
567 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
568 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
569 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
570 pop_data->cmd_top = CMD_NOT_AVAILABLE;
571 pop_data->cmd_user = CMD_NOT_AVAILABLE;
572 pop_data->resp_codes = 0;
573 pop_data->expire = 1;
574 p_delete(&pop_data->auth_list);
577 /* Execute CAPA command */
578 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
579 switch (pop_fetch_data(pop_data, "CAPA\r\n", NULL, fetch_capa, pop_data)) {
581 pop_data->cmd_capa = CMD_AVAILABLE;
583 case PFD_FUNCT_ERROR:
585 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
587 case PQ_NOT_CONNECTED:
588 return PQ_NOT_CONNECTED;
592 /* CAPA not supported, use defaults */
593 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
594 pop_data->cmd_uidl = CMD_UNKNOWN;
595 pop_data->cmd_top = CMD_UNKNOWN;
597 if (pop_fetch_data(pop_data, "AUTH\r\n", NULL, fetch_auth, pop_data) ==
599 return PQ_NOT_CONNECTED;
602 /* Check capabilities */
606 if (!pop_data->expire)
607 msg = _("Unable to leave messages on server.");
608 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
609 msg = _("Command TOP is not supported by server.");
610 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
611 msg = _("Command UIDL is not supported by server.");
612 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
616 pop_data->capabilities = 1;
622 /* given an POP mailbox name, return host, port, username and password */
623 static int pop_parse_path (const char *path, ACCOUNT * act)
631 act->port = POP_PORT;
632 act->type = M_ACCT_TYPE_POP;
635 url_parse_ciss (&url, c);
637 if (url.scheme == U_POP || url.scheme == U_POPS) {
638 if (url.scheme == U_POPS) {
639 act->flags |= M_ACCT_SSL;
640 act->port = POP_SSL_PORT;
643 if ((!url.path || !*url.path) && mutt_account_fromurl (act, &url) == 0)
652 * Open connection and authenticate
654 * -1 - conection lost,
655 * -2 - invalid command or execution error,
656 * -3 - authentication canceled.
658 static pop_query_status pop_open_connection (pop_data_t * pop_data)
660 pop_query_status ret;
662 char buf[LONG_STRING];
664 ret = pop_connect(pop_data);
670 ret = pop_capabilities (pop_data, 0);
671 if (ret == PQ_NOT_CONNECTED)
678 /* Attempt STLS if available and desired. */
679 if (!pop_data->conn->ssf && (pop_data->cmd_stls || mod_ssl.force_tls)) {
680 if (mod_ssl.force_tls)
681 pop_data->use_stls = 2;
682 if (pop_data->use_stls == 0) {
683 pop_data->use_stls = 1;
684 if (mod_ssl.starttls)
685 pop_data->use_stls = 2;
687 if (pop_data->use_stls == 2) {
688 ret = pop_query(pop_data, buf, sizeof(buf), "STLS");
689 if (ret == PQ_NOT_CONNECTED)
692 mutt_error ("%s", pop_data->err_msg);
695 else if (mutt_ssl_starttls (pop_data->conn))
697 mutt_error (_("Could not negotiate TLS connection"));
702 /* recheck capabilities after STLS completes */
703 ret = pop_capabilities (pop_data, 1);
704 if (ret == PQ_NOT_CONNECTED)
714 if (mod_ssl.force_tls && !pop_data->conn->ssf) {
715 mutt_error _("Encrypted connection unavailable");
720 ret = pop_authenticate (pop_data);
721 if (ret == PQ_NOT_CONNECTED)
723 if (ret == PFD_FUNCT_ERROR)
728 /* recheck capabilities after authentication */
729 ret = pop_capabilities (pop_data, 2);
730 if (ret == PQ_NOT_CONNECTED)
737 /* get total size of mailbox */
738 ret = pop_query(pop_data, buf, sizeof(buf), "STAT");
739 if (ret == PQ_NOT_CONNECTED)
742 mutt_error ("%s", pop_data->err_msg);
747 sscanf (buf, "+OK %u %u", &n, &size);
748 pop_data->size = size;
752 pop_data->status = POP_DISCONNECTED;
753 mutt_error _("Server closed connection!");
756 return PQ_NOT_CONNECTED;
759 /* find message with this UIDL and set refno */
760 static int check_uidl (char *line, void *data)
763 CONTEXT *ctx = (CONTEXT *)data;
765 sscanf (line, "%u %s", &idx, line);
766 for (i = 0; i < ctx->msgcount; i++) {
767 if (!m_strcmp(ctx->hdrs[i]->data, line)) {
768 ctx->hdrs[i]->refno = idx;
776 /* reconnect and verify indexes if connection was lost */
777 static pop_query_status pop_reconnect (CONTEXT * ctx)
779 pop_query_status ret;
780 pop_data_t *pop_data = (pop_data_t *) ctx->data;
783 if (pop_data->status == POP_CONNECTED)
785 if (pop_data->status == POP_BYE)
786 return PQ_NOT_CONNECTED;
789 mutt_socket_close (pop_data->conn);
791 ret = pop_open_connection(pop_data);
795 bar.msg = _("Verifying message indexes...");
797 mutt_progress_bar (&bar, 0);
799 for (i = 0; i < ctx->msgcount; i++)
800 ctx->hdrs[i]->refno = -1;
802 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
804 mutt_error ("%s", pop_data->err_msg);
814 return PQ_NOT_CONNECTED;
816 if (query_quadoption (OPT_POPRECONNECT,
817 _("Connection lost. Reconnect to POP server?")) !=
819 return PQ_NOT_CONNECTED;
823 /* write line to file */
824 static int fetch_message (char *line, void *file)
826 FILE *f = (FILE *) file;
829 if (fputc ('\n', f) == EOF)
839 * -1 - conection lost,
840 * -2 - invalid command or execution error,
841 * -3 - error writing to tempfile
843 static pop_query_status pop_read_header (pop_data_t * pop_data, HEADER * h)
847 pop_query_status ret;
849 char buf[LONG_STRING];
853 mutt_error(_("Could not create temporary file"));
854 return PFD_FUNCT_ERROR;
857 ret = pop_query(pop_data, buf, sizeof(buf), "LIST %d", h->refno);
859 sscanf (buf, "+OK %d %ld", &idx, &length);
861 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
862 ret = pop_fetch_data(pop_data, buf, NULL, fetch_message, f);
864 if (pop_data->cmd_top == CMD_UNKNOWN) {
866 pop_data->cmd_top = CMD_AVAILABLE;
870 pop_data->cmd_top = CMD_NOT_AVAILABLE;
871 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
872 _("Command TOP is not supported by server."));
881 h->env = mutt_read_rfc822_header (f, h, 0, 0);
882 h->content->length = length - h->content->offset + 1;
885 h->content->length--;
886 fgets (buf, sizeof (buf), f);
892 mutt_error ("%s", pop_data->err_msg);
895 case PFD_FUNCT_ERROR:
897 mutt_error _("Can't write header to temporary file!");
901 case PQ_NOT_CONNECTED:
903 mutt_error _("Can't fetch header: Not connected!");
913 static int fetch_uidl (char *line, void *data)
916 CONTEXT *ctx = (CONTEXT *) data;
917 pop_data_t *pop_data = (pop_data_t *) ctx->data;
919 sscanf (line, "%d %s", &idx, line);
920 for (i = 0; i < ctx->msgcount; i++)
921 if (!m_strcmp(line, ctx->hdrs[i]->data))
924 if (i == ctx->msgcount) {
925 if (i >= ctx->hdrmax)
926 mx_alloc_memory (ctx);
929 ctx->hdrs[i] = header_new();
930 ctx->hdrs[i]->data = m_strdup(line);
932 else if (ctx->hdrs[i]->index != idx - 1)
933 pop_data->clear_cache = 1;
935 ctx->hdrs[i]->refno = idx;
936 ctx->hdrs[i]->index = idx - 1;
945 * -1 - conection lost,
946 * -2 - invalid command or execution error,
947 * -3 - error writing to tempfile
949 static int pop_fetch_headers (CONTEXT * ctx)
951 int i, old_count, new_count;
952 pop_query_status ret;
953 pop_data_t *pop_data = (pop_data_t *) ctx->data;
955 time (&pop_data->check_time);
956 pop_data->clear_cache = 0;
958 for (i = 0; i < ctx->msgcount; i++)
959 ctx->hdrs[i]->refno = -1;
961 old_count = ctx->msgcount;
962 ret = pop_fetch_data(pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
963 new_count = ctx->msgcount;
964 ctx->msgcount = old_count;
966 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
968 pop_data->cmd_uidl = CMD_AVAILABLE;
971 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
972 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
974 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
975 _("Command UIDL is not supported by server."));
980 for (i = 0; i < old_count; i++)
981 if (ctx->hdrs[i]->refno == -1)
982 ctx->hdrs[i]->deleted = 1;
984 for (i = old_count; i < new_count; i++) {
985 mutt_message (_("Fetching message headers... [%d/%d]"),
986 i + 1 - old_count, new_count - old_count);
988 ret = pop_read_header (pop_data, ctx->hdrs[i]);
996 mx_update_context (ctx, i - old_count);
1000 for (i = ctx->msgcount; i < new_count; i++)
1001 header_delete(&ctx->hdrs[i]);
1005 mutt_clear_error ();
1006 return (new_count - old_count);
1009 /* delete all cached messages */
1010 static void pop_clear_cache (pop_data_t * pop_data)
1014 if (!pop_data->clear_cache)
1017 for (i = 0; i < POP_CACHE_LEN; i++) {
1018 if (pop_data->cache[i].path) {
1019 unlink (pop_data->cache[i].path);
1020 p_delete(&pop_data->cache[i].path);
1025 /* pop_mx functions {{{ */
1027 static int pop_is_magic(const char* path, struct stat* st)
1029 url_scheme_t s = url_check_scheme(NONULL(path));
1030 return s == U_POP || s == U_POPS ? M_POP : -1;
1033 static int pop_open_mailbox (CONTEXT * ctx)
1036 char buf[LONG_STRING];
1039 pop_data_t *pop_data;
1042 if (pop_parse_path (ctx->path, &act)) {
1043 mutt_error (_("%s is an invalid POP path"), ctx->path);
1048 mutt_account_tourl (&act, &url);
1050 url_ciss_tostring (&url, buf, sizeof (buf), 0);
1051 conn = mutt_conn_find (NULL, &act);
1055 p_delete(&ctx->path);
1056 ctx->path = m_strdup(buf);
1058 pop_data = p_new(pop_data_t, 1);
1059 pop_data->conn = conn;
1060 ctx->data = pop_data;
1062 if (pop_open_connection(pop_data) != PQ_OK)
1065 conn->data = pop_data;
1068 if (pop_reconnect (ctx) != PQ_OK)
1071 mutt_message _("Fetching list of messages...");
1072 ctx->size = pop_data->size;
1073 ret = pop_fetch_headers (ctx);
1085 static int pop_acl_check(CONTEXT *ctx, int bit)
1088 case ACL_DELETE: /* (un)deletion */
1089 case ACL_SEEN: /* mark as read */
1091 case ACL_INSERT: /* editing messages */
1092 case ACL_WRITE: /* change importance */
1098 static int pop_check_mailbox(CONTEXT * ctx, int *index_hint, int unused)
1101 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1103 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
1108 mutt_socket_close (pop_data->conn);
1110 if (pop_open_connection (pop_data) < 0)
1113 mutt_message _("Checking for new messages...");
1114 ctx->size = pop_data->size;
1115 ret = pop_fetch_headers (ctx);
1116 pop_clear_cache (pop_data);
1127 static void pop_close_mailbox (CONTEXT * ctx)
1129 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1136 if (pop_data->status != POP_NONE)
1137 mutt_socket_close (pop_data->conn);
1139 pop_data->status = POP_NONE;
1141 pop_data->clear_cache = 1;
1142 pop_clear_cache (pop_data);
1144 if (!pop_data->conn->data)
1145 mutt_socket_free (pop_data->conn);
1150 static int pop_sync_mailbox(CONTEXT * ctx, int unused, int *index_hint)
1153 pop_query_status ret;
1154 char buf[LONG_STRING];
1155 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1157 pop_data->check_time = 0;
1160 if (pop_reconnect (ctx) != PQ_OK)
1161 return PQ_NOT_CONNECTED;
1163 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
1165 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
1166 if (ctx->hdrs[i]->deleted) {
1167 ret = pop_query(pop_data, buf, sizeof(buf), "DELE %d",
1168 ctx->hdrs[i]->refno);
1173 ret = pop_query(pop_data, buf, sizeof(buf), "QUIT");
1177 pop_data->clear_cache = 1;
1178 pop_clear_cache (pop_data);
1179 pop_data->status = POP_DISCONNECTED;
1183 if (ret == PQ_ERR) {
1184 mutt_error ("%s", pop_data->err_msg);
1186 return PQ_NOT_CONNECTED;
1193 mx_t const pop_mx = {
1208 /* public API {{{ */
1210 void pop_fetch_mail (void)
1212 char buffer[LONG_STRING];
1213 char msgbuf[STRING];
1215 int i, delanswer, last = 0, msgs, bytes, rset = 0;
1216 pop_query_status ret;
1219 MESSAGE *msg = NULL;
1221 pop_data_t *pop_data;
1225 mutt_error _("POP host is not defined.");
1229 plen = m_strlen(PopHost) + 7;
1230 url = p = p_new(char, plen);
1231 if (url_check_scheme (PopHost) == U_UNKNOWN) {
1232 plen -= m_strcpy(url, plen, "pop://");
1235 m_strcpy(p, plen, PopHost);
1237 ret = pop_parse_path (url, &act);
1240 mutt_error (_("%s is an invalid POP path"), PopHost);
1244 conn = mutt_conn_find (NULL, &act);
1248 pop_data = p_new(pop_data_t, 1);
1249 pop_data->conn = conn;
1251 if (pop_open_connection (pop_data) < 0) {
1252 mutt_socket_free (pop_data->conn);
1253 p_delete(&pop_data);
1257 conn->data = pop_data;
1259 mutt_message _("Checking for new messages...");
1261 /* find out how many messages are in the mailbox. */
1262 ret = pop_query(pop_data, buffer, sizeof(buffer), "STAT");
1263 if (ret == PQ_NOT_CONNECTED)
1265 if (ret == PQ_ERR) {
1266 mutt_error ("%s", pop_data->err_msg);
1270 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
1272 /* only get unread messages */
1273 if (msgs > 0 && option (OPTPOPLAST)) {
1274 ret = pop_query(pop_data, buffer, sizeof(buffer), "LAST");
1275 if (ret == PQ_NOT_CONNECTED)
1278 sscanf (buffer, "+OK %d", &last);
1282 mutt_message _("No new mail in POP mailbox.");
1287 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
1291 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
1293 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
1295 mutt_message ("%s", msgbuf);
1297 for (i = last + 1; i <= msgs; i++) {
1298 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
1301 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
1302 ret = pop_fetch_data(pop_data, buffer, NULL, fetch_message, msg->fp);
1303 if (ret == PFD_FUNCT_ERROR)
1306 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
1308 ret = PFD_FUNCT_ERROR;
1311 mx_close_message (&msg);
1314 if (ret == PQ_OK && delanswer == M_YES) {
1315 ret = pop_query(pop_data, buffer, sizeof(buffer), "DELE %d", i);
1318 if (ret == PQ_NOT_CONNECTED) {
1319 mx_close_mailbox (&ctx, NULL);
1322 if (ret == PQ_ERR) {
1323 mutt_error ("%s", pop_data->err_msg);
1326 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
1327 mutt_error _("Error while writing mailbox!");
1332 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
1336 mx_close_mailbox (&ctx, NULL);
1339 /* make sure no messages get deleted */
1340 if (pop_query(pop_data, buffer, sizeof(buffer), "RSET") ==
1346 if (pop_query(pop_data, buffer, sizeof(buffer), "QUIT") == PQ_NOT_CONNECTED)
1348 mutt_socket_close (conn);
1349 p_delete(&pop_data);
1353 mutt_error _("Server closed connection!");
1354 mutt_socket_close (conn);
1355 p_delete(&pop_data);
1358 /* fetch message from POP server */
1359 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1363 char buf[LONG_STRING];
1364 char path[_POSIX_PATH_MAX];
1366 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1368 HEADER *h = ctx->hdrs[msgno];
1370 /* see if we already have the message in our cache */
1371 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
1374 if (cache->index == h->index) {
1375 /* yes, so just return a pointer to the message */
1376 msg->fp = fopen (cache->path, "r");
1380 mutt_perror (cache->path);
1385 /* clear the previous entry */
1386 unlink (cache->path);
1387 p_delete(&cache->path);
1392 if (pop_reconnect (ctx) != PQ_OK)
1395 /* verify that massage index is correct */
1398 _("The message index is incorrect. Try reopening the mailbox.");
1403 bar.size = h->content->length + h->content->offset - 1;
1404 bar.msg = _("Fetching message...");
1405 mutt_progress_bar (&bar, 0);
1407 msg->fp = m_tempfile(path, sizeof(path), NONULL(MCore.tmpdir), NULL);
1409 mutt_error(_("Could not create temporary file"));
1414 snprintf(buf, sizeof(buf), "RETR %d\r\n", h->refno);
1415 ret = pop_fetch_data(pop_data, buf, &bar, fetch_message, msg->fp);
1422 if (ret == PQ_ERR) {
1423 mutt_error ("%s", pop_data->err_msg);
1428 if (ret == PFD_FUNCT_ERROR) {
1429 mutt_error _("Can't write message to temporary file!");
1436 /* Update the header information. Previously, we only downloaded a
1437 * portion of the headers, those required for the main display.
1439 cache->index = h->index;
1440 cache->path = m_strdup(path);
1443 envelope_delete(&h->env);
1444 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
1447 fgets (buf, sizeof (buf), msg->fp);
1448 while (!feof (msg->fp)) {
1449 ctx->hdrs[msgno]->lines++;
1450 fgets (buf, sizeof (buf), msg->fp);
1453 h->content->length = ftello (msg->fp) - h->content->offset;
1455 /* This needs to be done in case this is a multipart message */
1456 h->security = crypt_query (h->content);
1458 mutt_clear_error ();