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/lib-ui.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;
419 mutt_socket_close(pop_data->conn);
423 for (auth = pop_authenticators; auth->do_auth; auth++) {
424 switch (auth->do_auth(pop_data, auth->method)) {
428 return PQ_NOT_CONNECTED;
435 mutt_socket_close(pop_data->conn);
440 mutt_error(_("No authenticators available"));
447 * This function calls funct(*line, *data) for each received line,
448 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
451 * -1 - conection lost,
452 * -2 - invalid command or execution error,
453 * -3 - error in funct(*line, *data)
455 static pop_query_status
456 pop_fetch_data(pop_data_t *pop_data, const char *query, progress_t *bar,
457 int (*funct)(char *, void *), void *data)
459 pop_query_status ret;
460 char buf[LONG_STRING];
466 m_strcpy(buf, sizeof(buf), query);
467 ret = _pop_query(pop_data, buf, sizeof(buf));
474 if (mutt_socket_readln2(&inbuf, pop_data->conn) < 0) {
475 pop_data->status = POP_DISCONNECTED;
476 ret = PQ_NOT_CONNECTED;
481 mutt_progress_bar(bar, pos += inbuf.len);
484 if (inbuf.data[0] == '.') {
485 if (inbuf.data[1] != '.')
490 if (funct(inbuf.data + dot, data) < 0) {
492 ret = PFD_FUNCT_ERROR;
496 buffer_reset(&inbuf);
503 static int fetch_capa (char *line, void *data)
505 pop_data_t *pop_data = (pop_data_t *) data;
508 if (!ascii_strncasecmp (line, "SASL", 4)) {
509 p_delete(&pop_data->auth_list);
510 c = vskipspaces(line + 4);
511 pop_data->auth_list = m_strdup(c);
514 else if (!ascii_strncasecmp (line, "STLS", 4))
515 pop_data->cmd_stls = CMD_AVAILABLE;
517 else if (!ascii_strncasecmp (line, "UIDL", 4))
518 pop_data->cmd_uidl = CMD_AVAILABLE;
520 else if (!ascii_strncasecmp (line, "TOP", 3))
521 pop_data->cmd_top = CMD_AVAILABLE;
526 static int fetch_auth (char *line, void *data)
528 pop_data_t *pop_data = (pop_data_t *) data;
529 ssize_t auth_list_len;
531 if (!pop_data->auth_list) {
532 auth_list_len = m_strlen(line) + 1;
533 pop_data->auth_list = p_new(char, auth_list_len);
535 auth_list_len = m_strlen(pop_data->auth_list) + m_strlen(line) + 2;
536 p_realloc(&pop_data->auth_list, auth_list_len);
537 m_strcat(pop_data->auth_list, auth_list_len, " ");
539 m_strcat(pop_data->auth_list, auth_list_len, line);
547 * -1 - conection lost,
548 * -2 - execution error.
550 static pop_query_status pop_capabilities(pop_data_t * pop_data, int mode)
552 /* don't check capabilities on reconnect */
553 if (pop_data->capabilities)
556 /* init capabilities */
558 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
559 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
560 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
561 pop_data->cmd_top = CMD_NOT_AVAILABLE;
562 pop_data->cmd_user = CMD_NOT_AVAILABLE;
563 pop_data->resp_codes = 0;
564 pop_data->expire = 1;
565 p_delete(&pop_data->auth_list);
568 /* Execute CAPA command */
569 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
570 switch (pop_fetch_data(pop_data, "CAPA\r\n", NULL, fetch_capa, pop_data)) {
572 pop_data->cmd_capa = CMD_AVAILABLE;
574 case PFD_FUNCT_ERROR:
576 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
578 case PQ_NOT_CONNECTED:
579 return PQ_NOT_CONNECTED;
583 /* CAPA not supported, use defaults */
584 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
585 pop_data->cmd_uidl = CMD_UNKNOWN;
586 pop_data->cmd_top = CMD_UNKNOWN;
588 if (pop_fetch_data(pop_data, "AUTH\r\n", NULL, fetch_auth, pop_data) ==
590 return PQ_NOT_CONNECTED;
593 /* Check capabilities */
595 const char *msg = NULL;
597 if (!pop_data->expire)
598 msg = _("Unable to leave messages on server.");
599 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
600 msg = _("Command TOP is not supported by server.");
601 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
602 msg = _("Command UIDL is not supported by server.");
603 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
607 pop_data->capabilities = 1;
613 /* given an POP mailbox name, return host, port, username and password */
614 static int pop_parse_path (const char *path, ACCOUNT * act)
622 act->port = POP_PORT;
623 act->type = M_ACCT_TYPE_POP;
626 url_parse_ciss (&url, c);
628 if (url.scheme == U_POP || url.scheme == U_POPS) {
629 if (url.scheme == U_POPS) {
631 act->port = POP_SSL_PORT;
634 if ((!url.path || !*url.path) && mutt_account_fromurl (act, &url) == 0)
643 * Open connection and authenticate
645 * -1 - conection lost,
646 * -2 - invalid command or execution error,
647 * -3 - authentication canceled.
649 static pop_query_status pop_open_connection (pop_data_t * pop_data)
651 pop_query_status ret;
653 char buf[LONG_STRING];
655 ret = pop_connect(pop_data);
661 ret = pop_capabilities (pop_data, 0);
662 if (ret == PQ_NOT_CONNECTED)
669 /* Attempt STLS if available and desired. */
670 if (!pop_data->conn->ssf && (pop_data->cmd_stls || mod_ssl.force_tls)) {
671 if (mod_ssl.force_tls)
672 pop_data->use_stls = 2;
673 if (pop_data->use_stls == 0) {
674 pop_data->use_stls = 1;
675 if (mod_ssl.starttls)
676 pop_data->use_stls = 2;
678 if (pop_data->use_stls == 2) {
679 ret = pop_query(pop_data, buf, sizeof(buf), "STLS");
680 if (ret == PQ_NOT_CONNECTED)
683 mutt_error ("%s", pop_data->err_msg);
686 else if (mutt_ssl_starttls (pop_data->conn))
688 mutt_error (_("Could not negotiate TLS connection"));
693 /* recheck capabilities after STLS completes */
694 ret = pop_capabilities (pop_data, 1);
695 if (ret == PQ_NOT_CONNECTED)
705 if (mod_ssl.force_tls && !pop_data->conn->ssf) {
706 mutt_error _("Encrypted connection unavailable");
711 ret = pop_authenticate (pop_data);
712 if (ret == PQ_NOT_CONNECTED)
714 if (ret == PFD_FUNCT_ERROR)
719 /* recheck capabilities after authentication */
720 ret = pop_capabilities (pop_data, 2);
721 if (ret == PQ_NOT_CONNECTED)
728 /* get total size of mailbox */
729 ret = pop_query(pop_data, buf, sizeof(buf), "STAT");
730 if (ret == PQ_NOT_CONNECTED)
733 mutt_error ("%s", pop_data->err_msg);
738 sscanf (buf, "+OK %u %u", &n, &size);
739 pop_data->size = size;
743 pop_data->status = POP_DISCONNECTED;
744 mutt_error _("Server closed connection!");
747 return PQ_NOT_CONNECTED;
750 /* find message with this UIDL and set refno */
751 static int check_uidl (char *line, void *data)
754 CONTEXT *ctx = (CONTEXT *)data;
756 sscanf (line, "%u %s", &idx, line);
757 for (i = 0; i < ctx->msgcount; i++) {
758 if (!m_strcmp(ctx->hdrs[i]->data, line)) {
759 ctx->hdrs[i]->refno = idx;
767 /* reconnect and verify indexes if connection was lost */
768 static pop_query_status pop_reconnect (CONTEXT * ctx)
770 pop_query_status ret;
771 pop_data_t *pop_data = (pop_data_t *) ctx->data;
774 if (pop_data->status == POP_CONNECTED)
776 if (pop_data->status == POP_BYE)
777 return PQ_NOT_CONNECTED;
780 mutt_socket_close (pop_data->conn);
782 ret = pop_open_connection(pop_data);
786 bar.msg = _("Verifying message indexes...");
788 mutt_progress_bar (&bar, 0);
790 for (i = 0; i < ctx->msgcount; i++)
791 ctx->hdrs[i]->refno = -1;
793 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
795 mutt_error ("%s", pop_data->err_msg);
805 return PQ_NOT_CONNECTED;
807 if (query_quadoption (OPT_POPRECONNECT,
808 _("Connection lost. Reconnect to POP server?")) !=
810 return PQ_NOT_CONNECTED;
814 /* write line to file */
815 static int fetch_message (char *line, void *file)
817 FILE *f = (FILE *) file;
820 if (fputc ('\n', f) == EOF)
830 * -1 - conection lost,
831 * -2 - invalid command or execution error,
832 * -3 - error writing to tempfile
834 static pop_query_status pop_read_header (pop_data_t * pop_data, HEADER * h)
838 pop_query_status ret;
840 char buf[LONG_STRING];
844 mutt_error(_("Could not create temporary file"));
845 return PFD_FUNCT_ERROR;
848 ret = pop_query(pop_data, buf, sizeof(buf), "LIST %d", h->refno);
850 sscanf (buf, "+OK %d %ld", &idx, &length);
852 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
853 ret = pop_fetch_data(pop_data, buf, NULL, fetch_message, f);
855 if (pop_data->cmd_top == CMD_UNKNOWN) {
857 pop_data->cmd_top = CMD_AVAILABLE;
861 pop_data->cmd_top = CMD_NOT_AVAILABLE;
862 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
863 _("Command TOP is not supported by server."));
872 h->env = mutt_read_rfc822_header (f, h, 0, 0);
873 h->content->length = length - h->content->offset + 1;
876 h->content->length--;
877 fgets (buf, sizeof (buf), f);
883 mutt_error ("%s", pop_data->err_msg);
886 case PFD_FUNCT_ERROR:
888 mutt_error _("Can't write header to temporary file!");
892 case PQ_NOT_CONNECTED:
894 mutt_error _("Can't fetch header: Not connected!");
904 static int fetch_uidl (char *line, void *data)
907 CONTEXT *ctx = (CONTEXT *) data;
908 pop_data_t *pop_data = (pop_data_t *) ctx->data;
910 sscanf (line, "%d %s", &idx, line);
911 for (i = 0; i < ctx->msgcount; i++)
912 if (!m_strcmp(line, ctx->hdrs[i]->data))
915 if (i == ctx->msgcount) {
916 if (i >= ctx->hdrmax)
917 mx_alloc_memory (ctx);
920 ctx->hdrs[i] = header_new();
921 ctx->hdrs[i]->data = m_strdup(line);
923 else if (ctx->hdrs[i]->index != idx - 1)
924 pop_data->clear_cache = 1;
926 ctx->hdrs[i]->refno = idx;
927 ctx->hdrs[i]->index = idx - 1;
936 * -1 - conection lost,
937 * -2 - invalid command or execution error,
938 * -3 - error writing to tempfile
940 static int pop_fetch_headers (CONTEXT * ctx)
942 int i, old_count, new_count;
943 pop_query_status ret;
944 pop_data_t *pop_data = (pop_data_t *) ctx->data;
946 time (&pop_data->check_time);
947 pop_data->clear_cache = 0;
949 for (i = 0; i < ctx->msgcount; i++)
950 ctx->hdrs[i]->refno = -1;
952 old_count = ctx->msgcount;
953 ret = pop_fetch_data(pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
954 new_count = ctx->msgcount;
955 ctx->msgcount = old_count;
957 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
959 pop_data->cmd_uidl = CMD_AVAILABLE;
962 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
963 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
965 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
966 _("Command UIDL is not supported by server."));
971 for (i = 0; i < old_count; i++)
972 if (ctx->hdrs[i]->refno == -1)
973 ctx->hdrs[i]->deleted = 1;
975 for (i = old_count; i < new_count; i++) {
976 mutt_message (_("Fetching message headers... [%d/%d]"),
977 i + 1 - old_count, new_count - old_count);
979 ret = pop_read_header (pop_data, ctx->hdrs[i]);
987 mx_update_context (ctx, i - old_count);
991 for (i = ctx->msgcount; i < new_count; i++)
992 header_delete(&ctx->hdrs[i]);
997 return new_count - old_count;
1000 /* delete all cached messages */
1001 static void pop_clear_cache (pop_data_t * pop_data)
1005 if (!pop_data->clear_cache)
1008 for (i = 0; i < POP_CACHE_LEN; i++) {
1009 if (pop_data->cache[i].path) {
1010 unlink (pop_data->cache[i].path);
1011 p_delete(&pop_data->cache[i].path);
1016 /* pop_mx functions {{{ */
1018 static int pop_is_magic(const char* path, struct stat* st)
1020 url_scheme_t s = url_check_scheme(NONULL(path));
1021 return s == U_POP || s == U_POPS ? M_POP : -1;
1024 static int pop_open_mailbox (CONTEXT * ctx)
1027 char buf[LONG_STRING];
1030 pop_data_t *pop_data;
1033 if (pop_parse_path (ctx->path, &act)) {
1034 mutt_error (_("%s is an invalid POP path"), ctx->path);
1039 mutt_account_tourl (&act, &url);
1041 url_ciss_tostring (&url, buf, sizeof (buf), 0);
1042 conn = mutt_conn_find (NULL, &act);
1046 p_delete(&ctx->path);
1047 ctx->path = m_strdup(buf);
1049 pop_data = p_new(pop_data_t, 1);
1050 pop_data->conn = conn;
1051 ctx->data = pop_data;
1053 if (pop_open_connection(pop_data) != PQ_OK)
1056 conn->data = pop_data;
1059 if (pop_reconnect (ctx) != PQ_OK)
1062 mutt_message _("Fetching list of messages...");
1063 ctx->size = pop_data->size;
1064 ret = pop_fetch_headers (ctx);
1076 static int pop_acl_check(CONTEXT *ctx, int bit)
1079 case ACL_DELETE: /* (un)deletion */
1080 case ACL_SEEN: /* mark as read */
1082 case ACL_INSERT: /* editing messages */
1083 case ACL_WRITE: /* change importance */
1089 static int pop_check_mailbox(CONTEXT * ctx, int *index_hint, int unused)
1092 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1094 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
1099 mutt_socket_close (pop_data->conn);
1101 if (pop_open_connection (pop_data) < 0)
1104 mutt_message _("Checking for new messages...");
1105 ctx->size = pop_data->size;
1106 ret = pop_fetch_headers (ctx);
1107 pop_clear_cache (pop_data);
1118 static void pop_close_mailbox (CONTEXT * ctx)
1120 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1127 if (pop_data->status != POP_NONE)
1128 mutt_socket_close (pop_data->conn);
1130 pop_data->status = POP_NONE;
1132 pop_data->clear_cache = 1;
1133 pop_clear_cache (pop_data);
1135 if (!pop_data->conn->data)
1136 mutt_socket_free (pop_data->conn);
1141 static int pop_sync_mailbox(CONTEXT * ctx, int unused, int *index_hint)
1144 pop_query_status ret;
1145 char buf[LONG_STRING];
1146 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1148 pop_data->check_time = 0;
1151 if (pop_reconnect (ctx) != PQ_OK)
1152 return PQ_NOT_CONNECTED;
1154 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
1156 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
1157 if (ctx->hdrs[i]->deleted) {
1158 ret = pop_query(pop_data, buf, sizeof(buf), "DELE %d",
1159 ctx->hdrs[i]->refno);
1164 ret = pop_query(pop_data, buf, sizeof(buf), "QUIT");
1168 pop_data->clear_cache = 1;
1169 pop_clear_cache (pop_data);
1170 pop_data->status = POP_DISCONNECTED;
1174 if (ret == PQ_ERR) {
1175 mutt_error ("%s", pop_data->err_msg);
1177 return PQ_NOT_CONNECTED;
1182 /* fetch message from POP server */
1183 static int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1187 char buf[LONG_STRING];
1188 char path[_POSIX_PATH_MAX];
1190 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1192 HEADER *h = ctx->hdrs[msgno];
1194 /* see if we already have the message in our cache */
1195 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
1198 if (cache->index == h->index) {
1199 /* yes, so just return a pointer to the message */
1200 msg->fp = fopen (cache->path, "r");
1204 mutt_perror (cache->path);
1209 /* clear the previous entry */
1210 unlink (cache->path);
1211 p_delete(&cache->path);
1216 if (pop_reconnect (ctx) != PQ_OK)
1219 /* verify that massage index is correct */
1222 _("The message index is incorrect. Try reopening the mailbox.");
1227 bar.size = h->content->length + h->content->offset - 1;
1228 bar.msg = _("Fetching message...");
1229 mutt_progress_bar (&bar, 0);
1231 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
1233 mutt_error(_("Could not create temporary file"));
1238 snprintf(buf, sizeof(buf), "RETR %d\r\n", h->refno);
1239 ret = pop_fetch_data(pop_data, buf, &bar, fetch_message, msg->fp);
1246 if (ret == PQ_ERR) {
1247 mutt_error ("%s", pop_data->err_msg);
1252 if (ret == PFD_FUNCT_ERROR) {
1253 mutt_error _("Can't write message to temporary file!");
1260 /* Update the header information. Previously, we only downloaded a
1261 * portion of the headers, those required for the main display.
1263 cache->index = h->index;
1264 cache->path = m_strdup(path);
1267 envelope_delete(&h->env);
1268 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
1271 fgets (buf, sizeof (buf), msg->fp);
1272 while (!feof (msg->fp)) {
1273 ctx->hdrs[msgno]->lines++;
1274 fgets (buf, sizeof (buf), msg->fp);
1277 h->content->length = ftello (msg->fp) - h->content->offset;
1279 /* This needs to be done in case this is a multipart message */
1280 h->security = crypt_query (h->content);
1282 mutt_clear_error ();
1290 mx_t const pop_mx = {
1306 /* public API {{{ */
1308 void pop_fetch_mail (void)
1310 char buffer[LONG_STRING];
1311 char msgbuf[STRING];
1313 int i, delanswer, last = 0, msgs, bytes, rset = 0;
1314 pop_query_status ret;
1317 MESSAGE *msg = NULL;
1319 pop_data_t *pop_data;
1322 if (m_strisempty(PopHost)) {
1323 mutt_error _("POP host is not defined.");
1327 plen = m_strlen(PopHost) + 7;
1328 url = p = p_new(char, plen);
1329 if (url_check_scheme (PopHost) == U_UNKNOWN) {
1330 snprintf(p, plen, "pop://%s", PopHost);
1332 m_strcpy(p, plen, PopHost);
1335 ret = pop_parse_path (url, &act);
1338 mutt_error (_("%s is an invalid POP path"), PopHost);
1342 conn = mutt_conn_find (NULL, &act);
1346 pop_data = p_new(pop_data_t, 1);
1347 pop_data->conn = conn;
1349 if (pop_open_connection (pop_data) < 0) {
1350 mutt_socket_free (pop_data->conn);
1351 p_delete(&pop_data);
1355 conn->data = pop_data;
1357 mutt_message _("Checking for new messages...");
1359 /* find out how many messages are in the mailbox. */
1360 ret = pop_query(pop_data, buffer, sizeof(buffer), "STAT");
1361 if (ret == PQ_NOT_CONNECTED)
1363 if (ret == PQ_ERR) {
1364 mutt_error ("%s", pop_data->err_msg);
1368 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
1370 /* only get unread messages */
1371 if (msgs > 0 && option (OPTPOPLAST)) {
1372 ret = pop_query(pop_data, buffer, sizeof(buffer), "LAST");
1373 if (ret == PQ_NOT_CONNECTED)
1376 sscanf (buffer, "+OK %d", &last);
1380 mutt_message _("No new mail in POP mailbox.");
1385 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
1389 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
1391 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
1393 mutt_message ("%s", msgbuf);
1395 for (i = last + 1; i <= msgs; i++) {
1396 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
1399 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
1400 ret = pop_fetch_data(pop_data, buffer, NULL, fetch_message, msg->fp);
1401 if (ret == PFD_FUNCT_ERROR)
1404 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
1406 ret = PFD_FUNCT_ERROR;
1409 mx_close_message (&msg);
1412 if (ret == PQ_OK && delanswer == M_YES) {
1413 ret = pop_query(pop_data, buffer, sizeof(buffer), "DELE %d", i);
1416 if (ret == PQ_NOT_CONNECTED) {
1417 mx_close_mailbox (&ctx, NULL);
1420 if (ret == PQ_ERR) {
1421 mutt_error ("%s", pop_data->err_msg);
1424 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
1425 mutt_error _("Error while writing mailbox!");
1430 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
1434 mx_close_mailbox (&ctx, NULL);
1437 /* make sure no messages get deleted */
1438 if (pop_query(pop_data, buffer, sizeof(buffer), "RSET") ==
1444 if (pop_query(pop_data, buffer, sizeof(buffer), "QUIT") == PQ_NOT_CONNECTED)
1446 mutt_socket_close (conn);
1447 p_delete(&pop_data);
1451 mutt_error _("Server closed connection!");
1452 mutt_socket_close (conn);
1453 p_delete(&pop_data);