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 time_t login_delay; /* minimal login delay capability */
81 char *auth_list; /* list of auth mechanisms */
83 char err_msg[POP_CMD_RESPONSE];
84 POP_CACHE cache[POP_CACHE_LEN];
87 /* pop low level functions {{{ */
89 static void pop_error(pop_data_t *pop_data, const char *msg)
91 if (!m_strncmp(msg, "-ERR ", 5)) {
92 const char *s = skipspaces(msg + 5);
97 m_strcat(pop_data->err_msg, sizeof(pop_data->err_msg), msg);
98 m_strrtrim(pop_data->err_msg);
102 * Send data from buffer and receive answer to the same buffer
104 * -1 - conection lost,
105 * -2 - invalid command or execution error.
107 static pop_query_status pop_query(pop_data_t *pop_data, char *buf, ssize_t buflen)
111 if (pop_data->status != POP_CONNECTED)
112 return PQ_NOT_CONNECTED;
114 mutt_socket_write(pop_data->conn, buf);
116 c = strpbrk(buf, " \r\n");
118 snprintf(pop_data->err_msg, sizeof(pop_data->err_msg), "%s: ", buf);
120 if (mutt_socket_readln(buf, buflen, pop_data->conn) < 0) {
121 pop_data->status = POP_DISCONNECTED;
122 return PQ_NOT_CONNECTED;
124 if (!m_strncmp(buf, "+OK", 3))
127 pop_error(pop_data, buf);
134 * -1 - conection lost,
135 * -2 - invalid response.
137 static pop_query_status pop_connect(pop_data_t * pop_data)
139 char buf[LONG_STRING];
142 if (mutt_socket_open(pop_data->conn) < 0
143 || mutt_socket_readln(buf, sizeof(buf), pop_data->conn) < 0)
145 mutt_error(_("Error connecting to server: %s"),
146 pop_data->conn->account.host);
147 pop_data->status = POP_NONE;
148 return PQ_NOT_CONNECTED;
151 pop_data->status = POP_CONNECTED;
152 if (m_strncmp(buf, "+OK", 3)) {
153 pop_data->err_msg[0] = '\0';
154 pop_error(pop_data, buf);
155 mutt_error("%s", pop_data->err_msg);
159 p_delete(&pop_data->timestamp);
160 if ((p = strchr(buf, '<')) && (q = strchr(p, '>'))) {
161 pop_data->timestamp = p_dupstr(p, q - p);
166 static void pop_logout (CONTEXT * ctx)
168 pop_query_status ret = 0;
169 char buf[LONG_STRING];
170 pop_data_t *pop_data = (pop_data_t *) ctx->data;
172 if (pop_data->status == POP_CONNECTED) {
173 mutt_message _("Closing connection to POP server...");
176 m_strcpy(buf, sizeof(buf), "RSET\r\n");
177 ret = pop_query (pop_data, buf, sizeof (buf));
180 if (ret != PQ_NOT_CONNECTED) {
181 m_strcpy(buf, sizeof(buf), "QUIT\r\n");
182 pop_query (pop_data, buf, sizeof (buf));
188 pop_data->status = POP_DISCONNECTED;
193 /* Authentication {{{ */
195 static pop_auth_res_t pop_auth_sasl(pop_data_t *pop_data, const char *method)
197 sasl_conn_t *saslconn;
198 sasl_interact_t *interaction = NULL;
200 char buf[LONG_STRING], inbuf[LONG_STRING];
201 const char *mech, *pc = NULL;
202 unsigned int len, olen;
203 unsigned char client_start;
206 if (mutt_sasl_client_new(pop_data->conn, &saslconn) < 0) {
207 return POP_A_FAILURE;
211 method = pop_data->auth_list;
214 rc = sasl_client_start(saslconn, method, &interaction, &pc, &olen,
216 if (rc != SASL_INTERACT)
218 mutt_sasl_interact(interaction);
221 if (rc != SASL_OK && rc != SASL_CONTINUE) {
222 return POP_A_UNAVAIL;
225 client_start = (olen > 0);
226 mutt_message(_("Authenticating (SASL)..."));
227 snprintf(buf, sizeof (buf), "AUTH %s", mech);
230 /* looping protocol */
232 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
233 mutt_socket_write(pop_data->conn, buf);
234 if (mutt_socket_readln(inbuf, sizeof(inbuf), pop_data->conn) < 0) {
235 sasl_dispose(&saslconn);
236 pop_data->status = POP_DISCONNECTED;
240 if (rc != SASL_CONTINUE)
243 if (!m_strncmp(inbuf, "+ ", 2)
244 && sasl_decode64(inbuf, strlen(inbuf),
245 buf, sizeof(buf) - 1, &len) != SASL_OK)
252 rc = sasl_client_step(saslconn, buf, len, &interaction, &pc,
254 if (rc != SASL_INTERACT)
256 mutt_sasl_interact(interaction);
262 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
265 /* send out response, or line break if none needed */
267 if (sasl_encode64(pc, olen, buf, sizeof(buf), &olen) != SASL_OK) {
270 p_delete((char **)&pc);
277 if (!m_strncmp(inbuf, "+OK", 3)) {
278 mutt_sasl_setup_conn(pop_data->conn, saslconn);
279 return POP_A_SUCCESS;
283 sasl_dispose(&saslconn);
284 p_delete((char **)&pc);
286 /* terminate SASL session if the last responce is not +OK nor -ERR */
287 if (!m_strncmp(inbuf, "+ ", 2)) {
288 snprintf(buf, sizeof(buf), "*\r\n");
289 if (pop_query(pop_data, buf, sizeof(buf)) == PQ_NOT_CONNECTED)
293 mutt_error(_("SASL authentication failed."));
296 return POP_A_FAILURE;
299 static pop_auth_res_t pop_auth_apop(pop_data_t *pop_data, const char *method)
302 unsigned char digest[16];
304 char buf[LONG_STRING];
307 if (!pop_data->timestamp)
308 return POP_A_UNAVAIL;
310 mutt_message _("Authenticating (APOP)...");
312 /* Compute the authentication hash to send to the server */
314 MD5Update(&mdContext, (unsigned char *)pop_data->timestamp,
315 strlen(pop_data->timestamp));
316 MD5Update(&mdContext, (unsigned char *)pop_data->conn->account.pass,
317 strlen(pop_data->conn->account.pass));
318 MD5Final(digest, &mdContext);
320 for (i = 0; i < countof(digest); i++)
321 sprintf(hash + 2 * i, "%02x", digest[i]);
323 /* Send APOP command to server */
324 snprintf(buf, sizeof(buf), "APOP %s %s\r\n", pop_data->conn->account.user,
327 switch (pop_query(pop_data, buf, sizeof(buf))) {
329 return POP_A_SUCCESS;
330 case PQ_NOT_CONNECTED:
333 mutt_error("%s %s", _("APOP authentication failed."), pop_data->err_msg);
335 return POP_A_FAILURE;
339 static pop_auth_res_t pop_auth_user(pop_data_t *pop_data, const char *method)
341 char buf[LONG_STRING];
342 pop_query_status ret;
344 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
345 return POP_A_UNAVAIL;
347 mutt_message _("Authenticating (USER)...");
348 snprintf(buf, sizeof(buf), "USER %s\r\n", pop_data->conn->account.user);
349 ret = pop_query(pop_data, buf, sizeof (buf));
351 if (pop_data->cmd_user == CMD_UNKNOWN) {
353 pop_data->cmd_user = CMD_AVAILABLE;
355 pop_data->cmd_user = CMD_NOT_AVAILABLE;
359 snprintf(buf, sizeof(buf), "PASS %s\r\n",
360 pop_data->conn->account.pass);
361 ret = pop_query(pop_data, buf, sizeof(buf));
366 return POP_A_SUCCESS;
367 case PQ_NOT_CONNECTED:
370 mutt_error("%s %s", _("USER authentication failed."), pop_data->err_msg);
372 return POP_A_FAILURE;
377 pop_auth_res_t (*do_auth)(pop_data_t *, const char *);
381 static pop_query_status pop_authenticate (pop_data_t * pop_data)
383 static pop_auth_t const pop_authenticators[] = {
384 {pop_auth_sasl, NULL},
385 {pop_auth_apop, "apop"},
386 {pop_auth_user, "user"},
390 ACCOUNT *act = &pop_data->conn->account;
391 const pop_auth_t *auth;
394 if (mutt_account_getuser(act) || !act->user[0]
395 || mutt_account_getpass(act) || !act->pass[0])
397 return PFD_FUNCT_ERROR;
400 if (!m_strisempty(PopAuthenticators)) {
404 for (p = PopAuthenticators; ; p = q) {
410 q = strchrnul(p, ':');
411 m_strncpy(buf, sizeof(buf), p, q - p);
413 for (auth = pop_authenticators; auth->do_auth; auth++) {
414 if (auth->method && ascii_strcasecmp(auth->method, buf))
417 switch (auth->do_auth(pop_data, buf)) {
421 return PQ_NOT_CONNECTED;
431 for (auth = pop_authenticators; auth->do_auth; auth++) {
432 switch (auth->do_auth(pop_data, auth->method)) {
436 return PQ_NOT_CONNECTED;
447 mutt_error(_("No authenticators available"));
454 * This function calls funct(*line, *data) for each received line,
455 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
458 * -1 - conection lost,
459 * -2 - invalid command or execution error,
460 * -3 - error in funct(*line, *data)
462 static pop_query_status
463 pop_fetch_data(pop_data_t *pop_data, const char *query, progress_t *bar,
464 int (*funct)(char *, void *), void *data)
466 char buf[LONG_STRING];
469 pop_query_status ret;
474 m_strcpy(buf, sizeof(buf), query);
475 ret = pop_query (pop_data, buf, sizeof (buf));
479 inbuf = p_new(char, sizeof(buf));
483 mutt_socket_readln(buf, sizeof (buf), pop_data->conn);
485 pop_data->status = POP_DISCONNECTED;
486 ret = PQ_NOT_CONNECTED;
491 if (!lenbuf && buf[0] == '.') {
497 m_strcpy(inbuf + lenbuf,sizeof(buf), p);
500 if (chunk >= ssizeof(buf)) {
501 lenbuf += strlen (p);
504 mutt_progress_bar (bar, pos);
505 if (ret == 0 && funct (inbuf, data) < 0)
506 ret = PFD_FUNCT_ERROR;
510 p_realloc(&inbuf, lenbuf + sizeof(buf));
517 static int fetch_capa (char *line, void *data)
519 pop_data_t *pop_data = (pop_data_t *) data;
522 if (!ascii_strncasecmp (line, "SASL", 4)) {
523 p_delete(&pop_data->auth_list);
524 c = vskipspaces(line + 4);
525 pop_data->auth_list = m_strdup(c);
528 else if (!ascii_strncasecmp (line, "STLS", 4))
529 pop_data->cmd_stls = CMD_AVAILABLE;
531 else if (!ascii_strncasecmp (line, "UIDL", 4))
532 pop_data->cmd_uidl = CMD_AVAILABLE;
534 else if (!ascii_strncasecmp (line, "TOP", 3))
535 pop_data->cmd_top = CMD_AVAILABLE;
540 static int fetch_auth (char *line, void *data)
542 pop_data_t *pop_data = (pop_data_t *) data;
543 ssize_t auth_list_len;
545 if (!pop_data->auth_list) {
546 auth_list_len = m_strlen(line) + 1;
547 pop_data->auth_list = p_new(char, auth_list_len);
549 auth_list_len = m_strlen(pop_data->auth_list) + m_strlen(line) + 2;
550 p_realloc(&pop_data->auth_list, auth_list_len);
551 m_strcat(pop_data->auth_list, auth_list_len, " ");
553 m_strcat(pop_data->auth_list, auth_list_len, line);
561 * -1 - conection lost,
562 * -2 - execution error.
564 static pop_query_status pop_capabilities (pop_data_t * pop_data, int mode)
566 char buf[LONG_STRING];
568 /* don't check capabilities on reconnect */
569 if (pop_data->capabilities)
572 /* init capabilities */
574 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
575 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
576 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
577 pop_data->cmd_top = CMD_NOT_AVAILABLE;
578 pop_data->resp_codes = 0;
579 pop_data->expire = 1;
580 pop_data->login_delay = 0;
581 p_delete(&pop_data->auth_list);
584 /* Execute CAPA command */
585 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
586 m_strcpy(buf, sizeof(buf), "CAPA\r\n");
587 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
589 pop_data->cmd_capa = CMD_AVAILABLE;
591 case PFD_FUNCT_ERROR:
593 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
595 case PQ_NOT_CONNECTED:
596 return PQ_NOT_CONNECTED;
600 /* CAPA not supported, use defaults */
601 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
602 pop_data->cmd_uidl = CMD_UNKNOWN;
603 pop_data->cmd_top = CMD_UNKNOWN;
605 m_strcpy(buf, sizeof(buf), "AUTH\r\n");
606 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
607 return PQ_NOT_CONNECTED;
610 /* Check capabilities */
614 if (!pop_data->expire)
615 msg = _("Unable to leave messages on server.");
616 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
617 msg = _("Command TOP is not supported by server.");
618 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
619 msg = _("Command UIDL is not supported by server.");
620 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
624 pop_data->capabilities = 1;
630 /* given an POP mailbox name, return host, port, username and password */
631 static int pop_parse_path (const char *path, ACCOUNT * act)
639 act->port = POP_PORT;
640 act->type = M_ACCT_TYPE_POP;
643 url_parse_ciss (&url, c);
645 if (url.scheme == U_POP || url.scheme == U_POPS) {
646 if (url.scheme == U_POPS) {
647 act->flags |= M_ACCT_SSL;
648 act->port = POP_SSL_PORT;
651 if ((!url.path || !*url.path) && mutt_account_fromurl (act, &url) == 0)
660 * Open connection and authenticate
662 * -1 - conection lost,
663 * -2 - invalid command or execution error,
664 * -3 - authentication canceled.
666 static pop_query_status pop_open_connection (pop_data_t * pop_data)
668 pop_query_status ret;
670 char buf[LONG_STRING];
672 ret = pop_connect(pop_data);
678 ret = pop_capabilities (pop_data, 0);
679 if (ret == PQ_NOT_CONNECTED)
686 /* Attempt STLS if available and desired. */
687 if (!pop_data->conn->ssf && (pop_data->cmd_stls || mod_ssl.force_tls)) {
688 if (mod_ssl.force_tls)
689 pop_data->use_stls = 2;
690 if (pop_data->use_stls == 0) {
691 pop_data->use_stls = 1;
692 if (mod_ssl.starttls)
693 pop_data->use_stls = 2;
695 if (pop_data->use_stls == 2) {
696 m_strcpy(buf, sizeof(buf), "STLS\r\n");
697 ret = pop_query (pop_data, buf, sizeof (buf));
698 if (ret == PQ_NOT_CONNECTED)
701 mutt_error ("%s", pop_data->err_msg);
704 else if (mutt_ssl_starttls (pop_data->conn))
706 mutt_error (_("Could not negotiate TLS connection"));
711 /* recheck capabilities after STLS completes */
712 ret = pop_capabilities (pop_data, 1);
713 if (ret == PQ_NOT_CONNECTED)
723 if (mod_ssl.force_tls && !pop_data->conn->ssf) {
724 mutt_error _("Encrypted connection unavailable");
729 ret = pop_authenticate (pop_data);
730 if (ret == PQ_NOT_CONNECTED)
732 if (ret == PFD_FUNCT_ERROR)
737 /* recheck capabilities after authentication */
738 ret = pop_capabilities (pop_data, 2);
739 if (ret == PQ_NOT_CONNECTED)
746 /* get total size of mailbox */
747 m_strcpy(buf, sizeof(buf), "STAT\r\n");
748 ret = pop_query (pop_data, buf, sizeof (buf));
749 if (ret == PQ_NOT_CONNECTED)
752 mutt_error ("%s", pop_data->err_msg);
757 sscanf (buf, "+OK %u %u", &n, &size);
758 pop_data->size = size;
762 pop_data->status = POP_DISCONNECTED;
763 mutt_error _("Server closed connection!");
766 return PQ_NOT_CONNECTED;
769 /* find message with this UIDL and set refno */
770 static int check_uidl (char *line, void *data)
773 CONTEXT *ctx = (CONTEXT *)data;
775 sscanf (line, "%u %s", &idx, line);
776 for (i = 0; i < ctx->msgcount; i++) {
777 if (!m_strcmp(ctx->hdrs[i]->data, line)) {
778 ctx->hdrs[i]->refno = idx;
786 /* reconnect and verify indexes if connection was lost */
787 static pop_query_status pop_reconnect (CONTEXT * ctx)
789 pop_query_status ret;
790 pop_data_t *pop_data = (pop_data_t *) ctx->data;
793 if (pop_data->status == POP_CONNECTED)
795 if (pop_data->status == POP_BYE)
796 return PQ_NOT_CONNECTED;
799 mutt_socket_close (pop_data->conn);
801 ret = pop_open_connection(pop_data);
805 bar.msg = _("Verifying message indexes...");
807 mutt_progress_bar (&bar, 0);
809 for (i = 0; i < ctx->msgcount; i++)
810 ctx->hdrs[i]->refno = -1;
812 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
814 mutt_error ("%s", pop_data->err_msg);
824 return PQ_NOT_CONNECTED;
826 if (query_quadoption (OPT_POPRECONNECT,
827 _("Connection lost. Reconnect to POP server?")) !=
829 return PQ_NOT_CONNECTED;
833 /* write line to file */
834 static int fetch_message (char *line, void *file)
836 FILE *f = (FILE *) file;
839 if (fputc ('\n', f) == EOF)
849 * -1 - conection lost,
850 * -2 - invalid command or execution error,
851 * -3 - error writing to tempfile
853 static pop_query_status pop_read_header (pop_data_t * pop_data, HEADER * h)
857 pop_query_status ret;
859 char buf[LONG_STRING];
860 char tempfile[_POSIX_PATH_MAX];
862 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
864 mutt_error(_("Could not create temporary file"));
865 return PFD_FUNCT_ERROR;
868 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
869 ret = pop_query (pop_data, buf, sizeof (buf));
871 sscanf (buf, "+OK %d %ld", &idx, &length);
873 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
874 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
876 if (pop_data->cmd_top == CMD_UNKNOWN) {
878 pop_data->cmd_top = CMD_AVAILABLE;
882 pop_data->cmd_top = CMD_NOT_AVAILABLE;
883 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
884 _("Command TOP is not supported by server."));
893 h->env = mutt_read_rfc822_header (f, h, 0, 0);
894 h->content->length = length - h->content->offset + 1;
897 h->content->length--;
898 fgets (buf, sizeof (buf), f);
904 mutt_error ("%s", pop_data->err_msg);
907 case PFD_FUNCT_ERROR:
909 mutt_error _("Can't write header to temporary file!");
913 case PQ_NOT_CONNECTED:
915 mutt_error _("Can't fetch header: Not connected!");
926 static int fetch_uidl (char *line, void *data)
929 CONTEXT *ctx = (CONTEXT *) data;
930 pop_data_t *pop_data = (pop_data_t *) ctx->data;
932 sscanf (line, "%d %s", &idx, line);
933 for (i = 0; i < ctx->msgcount; i++)
934 if (!m_strcmp(line, ctx->hdrs[i]->data))
937 if (i == ctx->msgcount) {
938 if (i >= ctx->hdrmax)
939 mx_alloc_memory (ctx);
942 ctx->hdrs[i] = header_new();
943 ctx->hdrs[i]->data = m_strdup(line);
945 else if (ctx->hdrs[i]->index != idx - 1)
946 pop_data->clear_cache = 1;
948 ctx->hdrs[i]->refno = idx;
949 ctx->hdrs[i]->index = idx - 1;
958 * -1 - conection lost,
959 * -2 - invalid command or execution error,
960 * -3 - error writing to tempfile
962 static int pop_fetch_headers (CONTEXT * ctx)
964 int i, old_count, new_count;
965 pop_query_status ret;
966 pop_data_t *pop_data = (pop_data_t *) ctx->data;
968 time (&pop_data->check_time);
969 pop_data->clear_cache = 0;
971 for (i = 0; i < ctx->msgcount; i++)
972 ctx->hdrs[i]->refno = -1;
974 old_count = ctx->msgcount;
975 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
976 new_count = ctx->msgcount;
977 ctx->msgcount = old_count;
979 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
981 pop_data->cmd_uidl = CMD_AVAILABLE;
984 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
985 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
987 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
988 _("Command UIDL is not supported by server."));
993 for (i = 0; i < old_count; i++)
994 if (ctx->hdrs[i]->refno == -1)
995 ctx->hdrs[i]->deleted = 1;
997 for (i = old_count; i < new_count; i++) {
998 mutt_message (_("Fetching message headers... [%d/%d]"),
999 i + 1 - old_count, new_count - old_count);
1001 ret = pop_read_header (pop_data, ctx->hdrs[i]);
1009 mx_update_context (ctx, i - old_count);
1013 for (i = ctx->msgcount; i < new_count; i++)
1014 header_delete(&ctx->hdrs[i]);
1018 mutt_clear_error ();
1019 return (new_count - old_count);
1022 /* delete all cached messages */
1023 static void pop_clear_cache (pop_data_t * pop_data)
1027 if (!pop_data->clear_cache)
1030 for (i = 0; i < POP_CACHE_LEN; i++) {
1031 if (pop_data->cache[i].path) {
1032 unlink (pop_data->cache[i].path);
1033 p_delete(&pop_data->cache[i].path);
1038 /* pop_mx functions {{{ */
1040 static int pop_is_magic(const char* path, struct stat* st)
1042 url_scheme_t s = url_check_scheme(NONULL(path));
1043 return s == U_POP || s == U_POPS ? M_POP : -1;
1046 static int pop_open_mailbox (CONTEXT * ctx)
1049 char buf[LONG_STRING];
1052 pop_data_t *pop_data;
1055 if (pop_parse_path (ctx->path, &act)) {
1056 mutt_error (_("%s is an invalid POP path"), ctx->path);
1061 mutt_account_tourl (&act, &url);
1063 url_ciss_tostring (&url, buf, sizeof (buf), 0);
1064 conn = mutt_conn_find (NULL, &act);
1068 p_delete(&ctx->path);
1069 ctx->path = m_strdup(buf);
1071 pop_data = p_new(pop_data_t, 1);
1072 pop_data->conn = conn;
1073 ctx->data = pop_data;
1075 if (pop_open_connection(pop_data) != PQ_OK)
1078 conn->data = pop_data;
1081 if (pop_reconnect (ctx) != PQ_OK)
1084 ctx->size = pop_data->size;
1086 mutt_message _("Fetching list of messages...");
1088 ret = pop_fetch_headers (ctx);
1100 static int pop_acl_check(CONTEXT *ctx, int bit)
1103 case ACL_DELETE: /* (un)deletion */
1104 case ACL_SEEN: /* mark as read */
1106 case ACL_INSERT: /* editing messages */
1107 case ACL_WRITE: /* change importance */
1113 static int pop_check_mailbox(CONTEXT * ctx, int *index_hint, int unused)
1116 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1118 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
1123 mutt_socket_close (pop_data->conn);
1125 if (pop_open_connection (pop_data) < 0)
1128 ctx->size = pop_data->size;
1130 mutt_message _("Checking for new messages...");
1132 ret = pop_fetch_headers (ctx);
1133 pop_clear_cache (pop_data);
1144 static void pop_close_mailbox (CONTEXT * ctx)
1146 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1153 if (pop_data->status != POP_NONE)
1154 mutt_socket_close (pop_data->conn);
1156 pop_data->status = POP_NONE;
1158 pop_data->clear_cache = 1;
1159 pop_clear_cache (pop_data);
1161 if (!pop_data->conn->data)
1162 mutt_socket_free (pop_data->conn);
1167 static int pop_sync_mailbox(CONTEXT * ctx, int unused, int *index_hint)
1170 pop_query_status ret;
1171 char buf[LONG_STRING];
1172 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1174 pop_data->check_time = 0;
1177 if (pop_reconnect (ctx) != PQ_OK)
1178 return PQ_NOT_CONNECTED;
1180 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
1182 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
1183 if (ctx->hdrs[i]->deleted) {
1184 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
1185 ret = pop_query (pop_data, buf, sizeof (buf));
1190 m_strcpy(buf, sizeof(buf), "QUIT\r\n");
1191 ret = pop_query (pop_data, buf, sizeof (buf));
1195 pop_data->clear_cache = 1;
1196 pop_clear_cache (pop_data);
1197 pop_data->status = POP_DISCONNECTED;
1201 if (ret == PQ_ERR) {
1202 mutt_error ("%s", pop_data->err_msg);
1204 return PQ_NOT_CONNECTED;
1211 mx_t const pop_mx = {
1226 /* public API {{{ */
1228 void pop_fetch_mail (void)
1230 char buffer[LONG_STRING];
1231 char msgbuf[STRING];
1233 int i, delanswer, last = 0, msgs, bytes, rset = 0;
1234 pop_query_status ret;
1237 MESSAGE *msg = NULL;
1239 pop_data_t *pop_data;
1243 mutt_error _("POP host is not defined.");
1248 plen = m_strlen(PopHost) + 7;
1249 url = p = p_new(char, plen);
1250 if (url_check_scheme (PopHost) == U_UNKNOWN) {
1251 plen -= m_strcpy(url, plen, "pop://");
1254 m_strcpy(p, plen, PopHost);
1256 ret = pop_parse_path (url, &act);
1259 mutt_error (_("%s is an invalid POP path"), PopHost);
1263 conn = mutt_conn_find (NULL, &act);
1267 pop_data = p_new(pop_data_t, 1);
1268 pop_data->conn = conn;
1270 if (pop_open_connection (pop_data) < 0) {
1271 mutt_socket_free (pop_data->conn);
1272 p_delete(&pop_data);
1276 conn->data = pop_data;
1278 mutt_message _("Checking for new messages...");
1280 /* find out how many messages are in the mailbox. */
1281 m_strcpy(buffer, sizeof(buffer), "STAT\r\n");
1282 ret = pop_query (pop_data, buffer, sizeof (buffer));
1283 if (ret == PQ_NOT_CONNECTED)
1285 if (ret == PQ_ERR) {
1286 mutt_error ("%s", pop_data->err_msg);
1290 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
1292 /* only get unread messages */
1293 if (msgs > 0 && option (OPTPOPLAST)) {
1294 m_strcpy(buffer, sizeof(buffer), "LAST\r\n");
1295 ret = pop_query (pop_data, buffer, sizeof (buffer));
1296 if (ret == PQ_NOT_CONNECTED)
1299 sscanf (buffer, "+OK %d", &last);
1303 mutt_message _("No new mail in POP mailbox.");
1308 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
1312 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
1314 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
1316 mutt_message ("%s", msgbuf);
1318 for (i = last + 1; i <= msgs; i++) {
1319 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
1322 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
1323 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
1324 if (ret == PFD_FUNCT_ERROR)
1327 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
1329 ret = PFD_FUNCT_ERROR;
1332 mx_close_message (&msg);
1335 if (ret == PQ_OK && delanswer == M_YES) {
1336 /* delete the message on the server */
1337 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
1338 ret = pop_query (pop_data, buffer, sizeof (buffer));
1341 if (ret == PQ_NOT_CONNECTED) {
1342 mx_close_mailbox (&ctx, NULL);
1345 if (ret == PQ_ERR) {
1346 mutt_error ("%s", pop_data->err_msg);
1349 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
1350 mutt_error _("Error while writing mailbox!");
1355 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
1359 mx_close_mailbox (&ctx, NULL);
1362 /* make sure no messages get deleted */
1363 m_strcpy(buffer, sizeof(buffer), "RSET\r\n");
1364 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
1369 /* exit gracefully */
1370 m_strcpy(buffer, sizeof(buffer), "QUIT\r\n");
1371 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
1373 mutt_socket_close (conn);
1374 p_delete(&pop_data);
1378 mutt_error _("Server closed connection!");
1379 mutt_socket_close (conn);
1380 p_delete(&pop_data);
1383 /* fetch message from POP server */
1384 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1388 char buf[LONG_STRING];
1389 char path[_POSIX_PATH_MAX];
1391 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1393 HEADER *h = ctx->hdrs[msgno];
1395 /* see if we already have the message in our cache */
1396 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
1399 if (cache->index == h->index) {
1400 /* yes, so just return a pointer to the message */
1401 msg->fp = fopen (cache->path, "r");
1405 mutt_perror (cache->path);
1410 /* clear the previous entry */
1411 unlink (cache->path);
1412 p_delete(&cache->path);
1417 if (pop_reconnect (ctx) != PQ_OK)
1420 /* verify that massage index is correct */
1423 _("The message index is incorrect. Try reopening the mailbox.");
1428 bar.size = h->content->length + h->content->offset - 1;
1429 bar.msg = _("Fetching message...");
1430 mutt_progress_bar (&bar, 0);
1432 msg->fp = m_tempfile(path, sizeof(path), NONULL(MCore.tmpdir), NULL);
1434 mutt_error(_("Could not create temporary file"));
1439 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
1441 ret = pop_fetch_data (pop_data, buf, &bar, fetch_message, msg->fp);
1448 if (ret == PQ_ERR) {
1449 mutt_error ("%s", pop_data->err_msg);
1454 if (ret == PFD_FUNCT_ERROR) {
1455 mutt_error _("Can't write message to temporary file!");
1462 /* Update the header information. Previously, we only downloaded a
1463 * portion of the headers, those required for the main display.
1465 cache->index = h->index;
1466 cache->path = m_strdup(path);
1469 envelope_delete(&h->env);
1470 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
1473 fgets (buf, sizeof (buf), msg->fp);
1474 while (!feof (msg->fp)) {
1475 ctx->hdrs[msgno]->lines++;
1476 fgets (buf, sizeof (buf), msg->fp);
1479 h->content->length = ftello (msg->fp) - h->content->offset;
1481 /* This needs to be done in case this is a multipart message */
1482 h->security = crypt_query (h->content);
1484 mutt_clear_error ();