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)
108 if (pop_data->status != POP_CONNECTED)
109 return PQ_NOT_CONNECTED;
111 mutt_socket_write(pop_data->conn, buf);
112 snprintf(pop_data->err_msg, sizeof(pop_data->err_msg), "%.*s: ",
113 (int)(strpbrk(buf, " \r\n") - buf), buf);
115 if (mutt_socket_readln(buf, buflen, pop_data->conn) < 0) {
116 pop_data->status = POP_DISCONNECTED;
117 return PQ_NOT_CONNECTED;
119 if (!m_strncmp(buf, "+OK", 3))
122 pop_error(pop_data, buf);
125 #define pop_query(pd, b, l, fmt, ...) \
126 (snprintf(b, l, fmt "\r\n", ##__VA_ARGS__), _pop_query(pd, b, l))
131 * -1 - conection lost,
132 * -2 - invalid response.
134 static pop_query_status pop_connect(pop_data_t * pop_data)
136 char buf[LONG_STRING];
139 if (mutt_socket_open(pop_data->conn) < 0
140 || mutt_socket_readln(buf, sizeof(buf), pop_data->conn) < 0)
142 mutt_error(_("Error connecting to server: %s"),
143 pop_data->conn->account.host);
144 pop_data->status = POP_NONE;
145 return PQ_NOT_CONNECTED;
148 pop_data->status = POP_CONNECTED;
149 if (m_strncmp(buf, "+OK", 3)) {
150 pop_data->err_msg[0] = '\0';
151 pop_error(pop_data, buf);
152 mutt_error("%s", pop_data->err_msg);
156 p_delete(&pop_data->timestamp);
157 if ((p = strchr(buf, '<')) && (q = strchr(p, '>'))) {
158 pop_data->timestamp = p_dupstr(p, q + 1 - p);
163 static void pop_logout (CONTEXT * ctx)
165 pop_query_status ret = 0;
166 char buf[LONG_STRING];
167 pop_data_t *pop_data = (pop_data_t *) ctx->data;
169 if (pop_data->status == POP_CONNECTED) {
170 mutt_message _("Closing connection to POP server...");
173 ret = pop_query(pop_data, buf, sizeof(buf), "RSET");
176 if (ret != PQ_NOT_CONNECTED) {
177 pop_query(pop_data, buf, sizeof(buf), "QUIT");
183 pop_data->status = POP_DISCONNECTED;
188 /* Authentication {{{ */
190 static pop_auth_res_t pop_auth_sasl(pop_data_t *pop_data, const char *method)
192 sasl_conn_t *saslconn;
193 sasl_interact_t *interaction = NULL;
195 char buf[LONG_STRING], inbuf[LONG_STRING];
196 const char *mech, *pc = NULL;
197 unsigned int len, olen;
198 unsigned char client_start;
201 if (mutt_sasl_client_new(pop_data->conn, &saslconn) < 0) {
202 return POP_A_FAILURE;
206 method = pop_data->auth_list;
209 rc = sasl_client_start(saslconn, method, &interaction, &pc, &olen,
211 if (rc != SASL_INTERACT)
213 mutt_sasl_interact(interaction);
216 if (rc != SASL_OK && rc != SASL_CONTINUE) {
217 return POP_A_UNAVAIL;
220 client_start = (olen > 0);
221 mutt_message(_("Authenticating (SASL)..."));
222 snprintf(buf, sizeof (buf), "AUTH %s", mech);
225 /* looping protocol */
227 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
228 mutt_socket_write(pop_data->conn, buf);
229 if (mutt_socket_readln(inbuf, sizeof(inbuf), pop_data->conn) < 0) {
230 sasl_dispose(&saslconn);
231 pop_data->status = POP_DISCONNECTED;
235 if (rc != SASL_CONTINUE)
238 if (!m_strncmp(inbuf, "+ ", 2)
239 && sasl_decode64(inbuf, strlen(inbuf),
240 buf, sizeof(buf) - 1, &len) != SASL_OK)
247 rc = sasl_client_step(saslconn, buf, len, &interaction, &pc,
249 if (rc != SASL_INTERACT)
251 mutt_sasl_interact(interaction);
257 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
260 /* send out response, or line break if none needed */
262 if (sasl_encode64(pc, olen, buf, sizeof(buf), &olen) != SASL_OK) {
265 p_delete((char **)&pc);
272 if (!m_strncmp(inbuf, "+OK", 3)) {
273 mutt_sasl_setup_conn(pop_data->conn, saslconn);
274 return POP_A_SUCCESS;
278 sasl_dispose(&saslconn);
279 p_delete((char **)&pc);
281 /* terminate SASL session if the last responce is not +OK nor -ERR */
282 if (!m_strncmp(inbuf, "+ ", 2)) {
283 if (pop_query(pop_data, buf, sizeof(buf), "*") == PQ_NOT_CONNECTED)
287 mutt_error(_("SASL authentication failed."));
290 return POP_A_FAILURE;
293 static pop_auth_res_t pop_auth_apop(pop_data_t *pop_data, const char *method)
296 unsigned char digest[16];
298 char buf[LONG_STRING];
301 if (!pop_data->timestamp)
302 return POP_A_UNAVAIL;
304 mutt_message _("Authenticating (APOP)...");
306 /* Compute the authentication hash to send to the server */
308 MD5Update(&mdContext, (unsigned char *)pop_data->timestamp,
309 strlen(pop_data->timestamp));
310 MD5Update(&mdContext, (unsigned char *)pop_data->conn->account.pass,
311 strlen(pop_data->conn->account.pass));
312 MD5Final(digest, &mdContext);
314 for (i = 0; i < countof(digest); i++)
315 sprintf(hash + 2 * i, "%02x", digest[i]);
317 switch (pop_query(pop_data, buf, sizeof(buf), "APOP %s %s",
318 pop_data->conn->account.user, hash))
321 return POP_A_SUCCESS;
322 case PQ_NOT_CONNECTED:
325 mutt_error("%s %s", _("APOP authentication failed."), pop_data->err_msg);
327 return POP_A_FAILURE;
331 static pop_auth_res_t pop_auth_user(pop_data_t *pop_data, const char *method)
333 char buf[LONG_STRING];
334 pop_query_status ret;
336 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
337 return POP_A_UNAVAIL;
339 mutt_message _("Authenticating (USER)...");
340 ret = pop_query(pop_data, buf, sizeof(buf), "USER %s",
341 pop_data->conn->account.user);
343 if (pop_data->cmd_user == CMD_UNKNOWN) {
345 pop_data->cmd_user = CMD_AVAILABLE;
347 pop_data->cmd_user = CMD_NOT_AVAILABLE;
351 ret = pop_query(pop_data, buf, sizeof(buf), "PASS %s",
352 pop_data->conn->account.pass);
357 return POP_A_SUCCESS;
358 case PQ_NOT_CONNECTED:
361 mutt_error("%s %s", _("USER authentication failed."), pop_data->err_msg);
363 return POP_A_FAILURE;
368 pop_auth_res_t (*do_auth)(pop_data_t *, const char *);
372 static pop_query_status pop_authenticate (pop_data_t * pop_data)
374 static pop_auth_t const pop_authenticators[] = {
375 {pop_auth_sasl, NULL},
376 {pop_auth_apop, "apop"},
377 {pop_auth_user, "user"},
381 ACCOUNT *act = &pop_data->conn->account;
382 const pop_auth_t *auth;
385 if (mutt_account_getuser(act) || !act->user[0]
386 || mutt_account_getpass(act) || !act->pass[0])
388 return PFD_FUNCT_ERROR;
391 if (!m_strisempty(PopAuthenticators)) {
395 for (p = PopAuthenticators; ; p = q) {
401 q = strchrnul(p, ':');
402 m_strncpy(buf, sizeof(buf), p, q - p);
404 for (auth = pop_authenticators; auth->do_auth; auth++) {
405 if (auth->method && ascii_strcasecmp(auth->method, buf))
408 switch (auth->do_auth(pop_data, buf)) {
412 return PQ_NOT_CONNECTED;
422 for (auth = pop_authenticators; auth->do_auth; auth++) {
423 switch (auth->do_auth(pop_data, auth->method)) {
427 return PQ_NOT_CONNECTED;
438 mutt_error(_("No authenticators available"));
445 * This function calls funct(*line, *data) for each received line,
446 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
449 * -1 - conection lost,
450 * -2 - invalid command or execution error,
451 * -3 - error in funct(*line, *data)
453 static pop_query_status
454 pop_fetch_data(pop_data_t *pop_data, const char *query, progress_t *bar,
455 int (*funct)(char *, void *), void *data)
457 pop_query_status ret;
458 char buf[LONG_STRING];
464 m_strcpy(buf, sizeof(buf), query);
465 ret = _pop_query(pop_data, buf, sizeof(buf));
472 if (mutt_socket_readln2(&inbuf, pop_data->conn) < 0) {
473 pop_data->status = POP_DISCONNECTED;
474 ret = PQ_NOT_CONNECTED;
479 mutt_progress_bar(bar, pos += inbuf.len);
482 if (inbuf.data[0] == '.') {
483 if (inbuf.data[1] != '.')
488 if (funct(inbuf.data + dot, data) < 0) {
490 ret = PFD_FUNCT_ERROR;
494 buffer_reset(&inbuf);
501 static int fetch_capa (char *line, void *data)
503 pop_data_t *pop_data = (pop_data_t *) data;
506 if (!ascii_strncasecmp (line, "SASL", 4)) {
507 p_delete(&pop_data->auth_list);
508 c = vskipspaces(line + 4);
509 pop_data->auth_list = m_strdup(c);
512 else if (!ascii_strncasecmp (line, "STLS", 4))
513 pop_data->cmd_stls = CMD_AVAILABLE;
515 else if (!ascii_strncasecmp (line, "UIDL", 4))
516 pop_data->cmd_uidl = CMD_AVAILABLE;
518 else if (!ascii_strncasecmp (line, "TOP", 3))
519 pop_data->cmd_top = CMD_AVAILABLE;
524 static int fetch_auth (char *line, void *data)
526 pop_data_t *pop_data = (pop_data_t *) data;
527 ssize_t auth_list_len;
529 if (!pop_data->auth_list) {
530 auth_list_len = m_strlen(line) + 1;
531 pop_data->auth_list = p_new(char, auth_list_len);
533 auth_list_len = m_strlen(pop_data->auth_list) + m_strlen(line) + 2;
534 p_realloc(&pop_data->auth_list, auth_list_len);
535 m_strcat(pop_data->auth_list, auth_list_len, " ");
537 m_strcat(pop_data->auth_list, auth_list_len, line);
545 * -1 - conection lost,
546 * -2 - execution error.
548 static pop_query_status pop_capabilities(pop_data_t * pop_data, int mode)
550 /* don't check capabilities on reconnect */
551 if (pop_data->capabilities)
554 /* init capabilities */
556 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
557 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
558 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
559 pop_data->cmd_top = CMD_NOT_AVAILABLE;
560 pop_data->cmd_user = CMD_NOT_AVAILABLE;
561 pop_data->resp_codes = 0;
562 pop_data->expire = 1;
563 p_delete(&pop_data->auth_list);
566 /* Execute CAPA command */
567 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
568 switch (pop_fetch_data(pop_data, "CAPA\r\n", NULL, fetch_capa, pop_data)) {
570 pop_data->cmd_capa = CMD_AVAILABLE;
572 case PFD_FUNCT_ERROR:
574 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
576 case PQ_NOT_CONNECTED:
577 return PQ_NOT_CONNECTED;
581 /* CAPA not supported, use defaults */
582 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
583 pop_data->cmd_uidl = CMD_UNKNOWN;
584 pop_data->cmd_top = CMD_UNKNOWN;
586 if (pop_fetch_data(pop_data, "AUTH\r\n", NULL, fetch_auth, pop_data) ==
588 return PQ_NOT_CONNECTED;
591 /* Check capabilities */
593 const char *msg = NULL;
595 if (!pop_data->expire)
596 msg = _("Unable to leave messages on server.");
597 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
598 msg = _("Command TOP is not supported by server.");
599 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
600 msg = _("Command UIDL is not supported by server.");
601 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
605 pop_data->capabilities = 1;
611 /* given an POP mailbox name, return host, port, username and password */
612 static int pop_parse_path (const char *path, ACCOUNT * act)
620 act->port = POP_PORT;
621 act->type = M_ACCT_TYPE_POP;
624 url_parse_ciss (&url, c);
626 if (url.scheme == U_POP || url.scheme == U_POPS) {
627 if (url.scheme == U_POPS) {
629 act->port = POP_SSL_PORT;
632 if ((!url.path || !*url.path) && mutt_account_fromurl (act, &url) == 0)
641 * Open connection and authenticate
643 * -1 - conection lost,
644 * -2 - invalid command or execution error,
645 * -3 - authentication canceled.
647 static pop_query_status pop_open_connection (pop_data_t * pop_data)
649 pop_query_status ret;
651 char buf[LONG_STRING];
653 ret = pop_connect(pop_data);
659 ret = pop_capabilities (pop_data, 0);
660 if (ret == PQ_NOT_CONNECTED)
667 /* Attempt STLS if available and desired. */
668 if (!pop_data->conn->ssf && (pop_data->cmd_stls || mod_ssl.force_tls)) {
669 if (mod_ssl.force_tls)
670 pop_data->use_stls = 2;
671 if (pop_data->use_stls == 0) {
672 pop_data->use_stls = 1;
673 if (mod_ssl.starttls)
674 pop_data->use_stls = 2;
676 if (pop_data->use_stls == 2) {
677 ret = pop_query(pop_data, buf, sizeof(buf), "STLS");
678 if (ret == PQ_NOT_CONNECTED)
681 mutt_error ("%s", pop_data->err_msg);
684 else if (mutt_ssl_starttls (pop_data->conn))
686 mutt_error (_("Could not negotiate TLS connection"));
691 /* recheck capabilities after STLS completes */
692 ret = pop_capabilities (pop_data, 1);
693 if (ret == PQ_NOT_CONNECTED)
703 if (mod_ssl.force_tls && !pop_data->conn->ssf) {
704 mutt_error _("Encrypted connection unavailable");
709 ret = pop_authenticate (pop_data);
710 if (ret == PQ_NOT_CONNECTED)
712 if (ret == PFD_FUNCT_ERROR)
717 /* recheck capabilities after authentication */
718 ret = pop_capabilities (pop_data, 2);
719 if (ret == PQ_NOT_CONNECTED)
726 /* get total size of mailbox */
727 ret = pop_query(pop_data, buf, sizeof(buf), "STAT");
728 if (ret == PQ_NOT_CONNECTED)
731 mutt_error ("%s", pop_data->err_msg);
736 sscanf (buf, "+OK %u %u", &n, &size);
737 pop_data->size = size;
741 pop_data->status = POP_DISCONNECTED;
742 mutt_error _("Server closed connection!");
745 return PQ_NOT_CONNECTED;
748 /* find message with this UIDL and set refno */
749 static int check_uidl (char *line, void *data)
752 CONTEXT *ctx = (CONTEXT *)data;
754 sscanf (line, "%u %s", &idx, line);
755 for (i = 0; i < ctx->msgcount; i++) {
756 if (!m_strcmp(ctx->hdrs[i]->data, line)) {
757 ctx->hdrs[i]->refno = idx;
765 /* reconnect and verify indexes if connection was lost */
766 static pop_query_status pop_reconnect (CONTEXT * ctx)
768 pop_query_status ret;
769 pop_data_t *pop_data = (pop_data_t *) ctx->data;
772 if (pop_data->status == POP_CONNECTED)
774 if (pop_data->status == POP_BYE)
775 return PQ_NOT_CONNECTED;
778 mutt_socket_close (pop_data->conn);
780 ret = pop_open_connection(pop_data);
784 bar.msg = _("Verifying message indexes...");
786 mutt_progress_bar (&bar, 0);
788 for (i = 0; i < ctx->msgcount; i++)
789 ctx->hdrs[i]->refno = -1;
791 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
793 mutt_error ("%s", pop_data->err_msg);
803 return PQ_NOT_CONNECTED;
805 if (query_quadoption (OPT_POPRECONNECT,
806 _("Connection lost. Reconnect to POP server?")) !=
808 return PQ_NOT_CONNECTED;
812 /* write line to file */
813 static int fetch_message (char *line, void *file)
815 FILE *f = (FILE *) file;
818 if (fputc ('\n', f) == EOF)
828 * -1 - conection lost,
829 * -2 - invalid command or execution error,
830 * -3 - error writing to tempfile
832 static pop_query_status pop_read_header (pop_data_t * pop_data, HEADER * h)
836 pop_query_status ret;
838 char buf[LONG_STRING];
842 mutt_error(_("Could not create temporary file"));
843 return PFD_FUNCT_ERROR;
846 ret = pop_query(pop_data, buf, sizeof(buf), "LIST %d", h->refno);
848 sscanf (buf, "+OK %d %ld", &idx, &length);
850 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
851 ret = pop_fetch_data(pop_data, buf, NULL, fetch_message, f);
853 if (pop_data->cmd_top == CMD_UNKNOWN) {
855 pop_data->cmd_top = CMD_AVAILABLE;
859 pop_data->cmd_top = CMD_NOT_AVAILABLE;
860 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
861 _("Command TOP is not supported by server."));
870 h->env = mutt_read_rfc822_header (f, h, 0, 0);
871 h->content->length = length - h->content->offset + 1;
874 h->content->length--;
875 fgets (buf, sizeof (buf), f);
881 mutt_error ("%s", pop_data->err_msg);
884 case PFD_FUNCT_ERROR:
886 mutt_error _("Can't write header to temporary file!");
890 case PQ_NOT_CONNECTED:
892 mutt_error _("Can't fetch header: Not connected!");
902 static int fetch_uidl (char *line, void *data)
905 CONTEXT *ctx = (CONTEXT *) data;
906 pop_data_t *pop_data = (pop_data_t *) ctx->data;
908 sscanf (line, "%d %s", &idx, line);
909 for (i = 0; i < ctx->msgcount; i++)
910 if (!m_strcmp(line, ctx->hdrs[i]->data))
913 if (i == ctx->msgcount) {
914 if (i >= ctx->hdrmax)
915 mx_alloc_memory (ctx);
918 ctx->hdrs[i] = header_new();
919 ctx->hdrs[i]->data = m_strdup(line);
921 else if (ctx->hdrs[i]->index != idx - 1)
922 pop_data->clear_cache = 1;
924 ctx->hdrs[i]->refno = idx;
925 ctx->hdrs[i]->index = idx - 1;
934 * -1 - conection lost,
935 * -2 - invalid command or execution error,
936 * -3 - error writing to tempfile
938 static int pop_fetch_headers (CONTEXT * ctx)
940 int i, old_count, new_count;
941 pop_query_status ret;
942 pop_data_t *pop_data = (pop_data_t *) ctx->data;
944 time (&pop_data->check_time);
945 pop_data->clear_cache = 0;
947 for (i = 0; i < ctx->msgcount; i++)
948 ctx->hdrs[i]->refno = -1;
950 old_count = ctx->msgcount;
951 ret = pop_fetch_data(pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
952 new_count = ctx->msgcount;
953 ctx->msgcount = old_count;
955 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
957 pop_data->cmd_uidl = CMD_AVAILABLE;
960 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
961 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
963 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
964 _("Command UIDL is not supported by server."));
969 for (i = 0; i < old_count; i++)
970 if (ctx->hdrs[i]->refno == -1)
971 ctx->hdrs[i]->deleted = 1;
973 for (i = old_count; i < new_count; i++) {
974 mutt_message (_("Fetching message headers... [%d/%d]"),
975 i + 1 - old_count, new_count - old_count);
977 ret = pop_read_header (pop_data, ctx->hdrs[i]);
985 mx_update_context (ctx, i - old_count);
989 for (i = ctx->msgcount; i < new_count; i++)
990 header_delete(&ctx->hdrs[i]);
995 return (new_count - old_count);
998 /* delete all cached messages */
999 static void pop_clear_cache (pop_data_t * pop_data)
1003 if (!pop_data->clear_cache)
1006 for (i = 0; i < POP_CACHE_LEN; i++) {
1007 if (pop_data->cache[i].path) {
1008 unlink (pop_data->cache[i].path);
1009 p_delete(&pop_data->cache[i].path);
1014 /* pop_mx functions {{{ */
1016 static int pop_is_magic(const char* path, struct stat* st)
1018 url_scheme_t s = url_check_scheme(NONULL(path));
1019 return s == U_POP || s == U_POPS ? M_POP : -1;
1022 static int pop_open_mailbox (CONTEXT * ctx)
1025 char buf[LONG_STRING];
1028 pop_data_t *pop_data;
1031 if (pop_parse_path (ctx->path, &act)) {
1032 mutt_error (_("%s is an invalid POP path"), ctx->path);
1037 mutt_account_tourl (&act, &url);
1039 url_ciss_tostring (&url, buf, sizeof (buf), 0);
1040 conn = mutt_conn_find (NULL, &act);
1044 p_delete(&ctx->path);
1045 ctx->path = m_strdup(buf);
1047 pop_data = p_new(pop_data_t, 1);
1048 pop_data->conn = conn;
1049 ctx->data = pop_data;
1051 if (pop_open_connection(pop_data) != PQ_OK)
1054 conn->data = pop_data;
1057 if (pop_reconnect (ctx) != PQ_OK)
1060 mutt_message _("Fetching list of messages...");
1061 ctx->size = pop_data->size;
1062 ret = pop_fetch_headers (ctx);
1074 static int pop_acl_check(CONTEXT *ctx, int bit)
1077 case ACL_DELETE: /* (un)deletion */
1078 case ACL_SEEN: /* mark as read */
1080 case ACL_INSERT: /* editing messages */
1081 case ACL_WRITE: /* change importance */
1087 static int pop_check_mailbox(CONTEXT * ctx, int *index_hint, int unused)
1090 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1092 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
1097 mutt_socket_close (pop_data->conn);
1099 if (pop_open_connection (pop_data) < 0)
1102 mutt_message _("Checking for new messages...");
1103 ctx->size = pop_data->size;
1104 ret = pop_fetch_headers (ctx);
1105 pop_clear_cache (pop_data);
1116 static void pop_close_mailbox (CONTEXT * ctx)
1118 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1125 if (pop_data->status != POP_NONE)
1126 mutt_socket_close (pop_data->conn);
1128 pop_data->status = POP_NONE;
1130 pop_data->clear_cache = 1;
1131 pop_clear_cache (pop_data);
1133 if (!pop_data->conn->data)
1134 mutt_socket_free (pop_data->conn);
1139 static int pop_sync_mailbox(CONTEXT * ctx, int unused, int *index_hint)
1142 pop_query_status ret;
1143 char buf[LONG_STRING];
1144 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1146 pop_data->check_time = 0;
1149 if (pop_reconnect (ctx) != PQ_OK)
1150 return PQ_NOT_CONNECTED;
1152 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
1154 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
1155 if (ctx->hdrs[i]->deleted) {
1156 ret = pop_query(pop_data, buf, sizeof(buf), "DELE %d",
1157 ctx->hdrs[i]->refno);
1162 ret = pop_query(pop_data, buf, sizeof(buf), "QUIT");
1166 pop_data->clear_cache = 1;
1167 pop_clear_cache (pop_data);
1168 pop_data->status = POP_DISCONNECTED;
1172 if (ret == PQ_ERR) {
1173 mutt_error ("%s", pop_data->err_msg);
1175 return PQ_NOT_CONNECTED;
1182 mx_t const pop_mx = {
1197 /* public API {{{ */
1199 void pop_fetch_mail (void)
1201 char buffer[LONG_STRING];
1202 char msgbuf[STRING];
1204 int i, delanswer, last = 0, msgs, bytes, rset = 0;
1205 pop_query_status ret;
1208 MESSAGE *msg = NULL;
1210 pop_data_t *pop_data;
1213 if (m_strisempty(PopHost)) {
1214 mutt_error _("POP host is not defined.");
1218 plen = m_strlen(PopHost) + 7;
1219 url = p = p_new(char, plen);
1220 if (url_check_scheme (PopHost) == U_UNKNOWN) {
1221 snprintf(p, plen, "pop://%s", PopHost);
1223 m_strcpy(p, plen, PopHost);
1226 ret = pop_parse_path (url, &act);
1229 mutt_error (_("%s is an invalid POP path"), PopHost);
1233 conn = mutt_conn_find (NULL, &act);
1237 pop_data = p_new(pop_data_t, 1);
1238 pop_data->conn = conn;
1240 if (pop_open_connection (pop_data) < 0) {
1241 mutt_socket_free (pop_data->conn);
1242 p_delete(&pop_data);
1246 conn->data = pop_data;
1248 mutt_message _("Checking for new messages...");
1250 /* find out how many messages are in the mailbox. */
1251 ret = pop_query(pop_data, buffer, sizeof(buffer), "STAT");
1252 if (ret == PQ_NOT_CONNECTED)
1254 if (ret == PQ_ERR) {
1255 mutt_error ("%s", pop_data->err_msg);
1259 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
1261 /* only get unread messages */
1262 if (msgs > 0 && option (OPTPOPLAST)) {
1263 ret = pop_query(pop_data, buffer, sizeof(buffer), "LAST");
1264 if (ret == PQ_NOT_CONNECTED)
1267 sscanf (buffer, "+OK %d", &last);
1271 mutt_message _("No new mail in POP mailbox.");
1276 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
1280 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
1282 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
1284 mutt_message ("%s", msgbuf);
1286 for (i = last + 1; i <= msgs; i++) {
1287 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
1290 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
1291 ret = pop_fetch_data(pop_data, buffer, NULL, fetch_message, msg->fp);
1292 if (ret == PFD_FUNCT_ERROR)
1295 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
1297 ret = PFD_FUNCT_ERROR;
1300 mx_close_message (&msg);
1303 if (ret == PQ_OK && delanswer == M_YES) {
1304 ret = pop_query(pop_data, buffer, sizeof(buffer), "DELE %d", i);
1307 if (ret == PQ_NOT_CONNECTED) {
1308 mx_close_mailbox (&ctx, NULL);
1311 if (ret == PQ_ERR) {
1312 mutt_error ("%s", pop_data->err_msg);
1315 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
1316 mutt_error _("Error while writing mailbox!");
1321 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
1325 mx_close_mailbox (&ctx, NULL);
1328 /* make sure no messages get deleted */
1329 if (pop_query(pop_data, buffer, sizeof(buffer), "RSET") ==
1335 if (pop_query(pop_data, buffer, sizeof(buffer), "QUIT") == PQ_NOT_CONNECTED)
1337 mutt_socket_close (conn);
1338 p_delete(&pop_data);
1342 mutt_error _("Server closed connection!");
1343 mutt_socket_close (conn);
1344 p_delete(&pop_data);
1347 /* fetch message from POP server */
1348 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1352 char buf[LONG_STRING];
1353 char path[_POSIX_PATH_MAX];
1355 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1357 HEADER *h = ctx->hdrs[msgno];
1359 /* see if we already have the message in our cache */
1360 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
1363 if (cache->index == h->index) {
1364 /* yes, so just return a pointer to the message */
1365 msg->fp = fopen (cache->path, "r");
1369 mutt_perror (cache->path);
1374 /* clear the previous entry */
1375 unlink (cache->path);
1376 p_delete(&cache->path);
1381 if (pop_reconnect (ctx) != PQ_OK)
1384 /* verify that massage index is correct */
1387 _("The message index is incorrect. Try reopening the mailbox.");
1392 bar.size = h->content->length + h->content->offset - 1;
1393 bar.msg = _("Fetching message...");
1394 mutt_progress_bar (&bar, 0);
1396 msg->fp = m_tempfile(path, sizeof(path), NONULL(MCore.tmpdir), NULL);
1398 mutt_error(_("Could not create temporary file"));
1403 snprintf(buf, sizeof(buf), "RETR %d\r\n", h->refno);
1404 ret = pop_fetch_data(pop_data, buf, &bar, fetch_message, msg->fp);
1411 if (ret == PQ_ERR) {
1412 mutt_error ("%s", pop_data->err_msg);
1417 if (ret == PFD_FUNCT_ERROR) {
1418 mutt_error _("Can't write message to temporary file!");
1425 /* Update the header information. Previously, we only downloaded a
1426 * portion of the headers, those required for the main display.
1428 cache->index = h->index;
1429 cache->path = m_strdup(path);
1432 envelope_delete(&h->env);
1433 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
1436 fgets (buf, sizeof (buf), msg->fp);
1437 while (!feof (msg->fp)) {
1438 ctx->hdrs[msgno]->lines++;
1439 fgets (buf, sizeof (buf), msg->fp);
1442 h->content->length = ftello (msg->fp) - h->content->offset;
1444 /* This needs to be done in case this is a multipart message */
1445 h->security = crypt_query (h->content);
1447 mutt_clear_error ();