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 unsigned resp_codes : 1; /* server supports extended response codes */
74 unsigned expire : 1; /* expire is greater than 0 */
75 unsigned clear_cache : 1;
79 time_t login_delay; /* minimal login delay capability */
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);
133 * -1 - conection lost,
134 * -2 - invalid response.
136 static pop_query_status pop_connect(pop_data_t * pop_data)
138 char buf[LONG_STRING];
141 if (mutt_socket_open(pop_data->conn) < 0
142 || mutt_socket_readln(buf, sizeof(buf), pop_data->conn) < 0)
144 mutt_error(_("Error connecting to server: %s"),
145 pop_data->conn->account.host);
146 pop_data->status = POP_NONE;
147 return PQ_NOT_CONNECTED;
150 pop_data->status = POP_CONNECTED;
151 if (m_strncmp(buf, "+OK", 3)) {
152 pop_data->err_msg[0] = '\0';
153 pop_error(pop_data, buf);
154 mutt_error("%s", pop_data->err_msg);
158 p_delete(&pop_data->timestamp);
159 if ((p = strchr(buf, '<')) && (q = strchr(p, '>'))) {
160 pop_data->timestamp = p_dupstr(p, q - p);
165 static void pop_logout (CONTEXT * ctx)
167 pop_query_status ret = 0;
168 char buf[LONG_STRING];
169 pop_data_t *pop_data = (pop_data_t *) ctx->data;
171 if (pop_data->status == POP_CONNECTED) {
172 mutt_message _("Closing connection to POP server...");
175 m_strcpy(buf, sizeof(buf), "RSET\r\n");
176 ret = pop_query (pop_data, buf, sizeof (buf));
179 if (ret != PQ_NOT_CONNECTED) {
180 m_strcpy(buf, sizeof(buf), "QUIT\r\n");
181 pop_query (pop_data, buf, sizeof (buf));
187 pop_data->status = POP_DISCONNECTED;
192 /* Authentication {{{ */
194 /* SASL authenticator */
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 /* APOP authenticator */
300 static pop_auth_res_t pop_auth_apop(pop_data_t *pop_data, const char *method)
303 unsigned char digest[16];
305 char buf[LONG_STRING];
308 if (!pop_data->timestamp)
309 return POP_A_UNAVAIL;
311 mutt_message _("Authenticating (APOP)...");
313 /* Compute the authentication hash to send to the server */
315 MD5Update(&mdContext, (unsigned char *)pop_data->timestamp,
316 strlen(pop_data->timestamp));
317 MD5Update(&mdContext, (unsigned char *)pop_data->conn->account.pass,
318 strlen(pop_data->conn->account.pass));
319 MD5Final(digest, &mdContext);
321 for (i = 0; i < countof(digest); i++)
322 sprintf(hash + 2 * i, "%02x", digest[i]);
324 /* Send APOP command to server */
325 snprintf(buf, sizeof(buf), "APOP %s %s\r\n", pop_data->conn->account.user,
328 switch (pop_query(pop_data, buf, sizeof(buf))) {
330 return POP_A_SUCCESS;
331 case PQ_NOT_CONNECTED:
333 case PFD_FUNCT_ERROR:
339 mutt_error("%s %s", _("APOP authentication failed."), pop_data->err_msg);
342 return POP_A_FAILURE;
345 /* USER authenticator */
346 static pop_auth_res_t pop_auth_user(pop_data_t *pop_data, const char *method)
348 char buf[LONG_STRING];
349 pop_query_status ret;
351 mutt_message(_("Logging in..."));
352 snprintf(buf, sizeof(buf), "USER %s\r\n", pop_data->conn->account.user);
353 ret = pop_query(pop_data, buf, sizeof (buf));
356 snprintf(pop_data->err_msg, sizeof(pop_data->err_msg),
357 _("Command USER is not supported by server."));
361 snprintf(buf, sizeof(buf), "PASS %s\r\n",
362 pop_data->conn->account.pass);
363 ret = pop_query(pop_data, buf, sizeof(buf));
368 return POP_A_SUCCESS;
369 case PQ_NOT_CONNECTED:
372 mutt_error ("%s %s", _("Login failed."), pop_data->err_msg);
374 return POP_A_FAILURE;
379 pop_auth_res_t (*do_auth)(pop_data_t *, const char *);
383 static pop_query_status pop_authenticate (pop_data_t * pop_data)
385 static pop_auth_t const pop_authenticators[] = {
386 {pop_auth_sasl, NULL},
387 {pop_auth_apop, "apop"},
388 {pop_auth_user, "user"},
392 ACCOUNT *act = &pop_data->conn->account;
393 int attempts = 0, ret = POP_A_UNAVAIL;
394 const pop_auth_t *auth;
396 if (mutt_account_getuser(act) || !act->user[0]
397 || mutt_account_getpass(act) || !act->pass[0])
399 return PFD_FUNCT_ERROR;
402 if (!m_strisempty(PopAuthenticators)) {
406 for (p = PopAuthenticators; ; p = q) {
412 q = strchrnul(p, ':');
413 m_strncpy(buf, sizeof(buf), p, q - p);
415 for (auth = pop_authenticators; auth->do_auth; auth++) {
416 if (auth->method && ascii_strcasecmp(auth->method, buf))
419 ret = auth->do_auth(pop_data, buf);
420 if (ret == POP_A_SOCKET) {
421 if (pop_connect(pop_data) == PQ_OK) {
422 ret = auth->do_auth(pop_data, buf);
428 if (ret != POP_A_UNAVAIL)
430 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET) {
436 /* Fall back to default: any authenticator */
437 for (auth = pop_authenticators; auth->do_auth; auth++) {
438 ret = auth->do_auth(pop_data, auth->method);
439 if (ret == POP_A_SOCKET) {
440 if (pop_connect(pop_data) == PQ_OK) {
441 ret = auth->do_auth(pop_data, auth->method);
447 if (ret != POP_A_UNAVAIL)
449 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET)
459 return PQ_NOT_CONNECTED;
462 mutt_error(_("No authenticators available"));
471 * This function calls funct(*line, *data) for each received line,
472 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
475 * -1 - conection lost,
476 * -2 - invalid command or execution error,
477 * -3 - error in funct(*line, *data)
479 static pop_query_status
480 pop_fetch_data(pop_data_t *pop_data, const char *query, progress_t *bar,
481 int (*funct)(char *, void *), void *data)
483 char buf[LONG_STRING];
486 pop_query_status ret;
491 m_strcpy(buf, sizeof(buf), query);
492 ret = pop_query (pop_data, buf, sizeof (buf));
496 inbuf = p_new(char, sizeof(buf));
500 mutt_socket_readln(buf, sizeof (buf), pop_data->conn);
502 pop_data->status = POP_DISCONNECTED;
503 ret = PQ_NOT_CONNECTED;
508 if (!lenbuf && buf[0] == '.') {
514 m_strcpy(inbuf + lenbuf,sizeof(buf), p);
517 if (chunk >= ssizeof(buf)) {
518 lenbuf += strlen (p);
521 mutt_progress_bar (bar, pos);
522 if (ret == 0 && funct (inbuf, data) < 0)
523 ret = PFD_FUNCT_ERROR;
527 p_realloc(&inbuf, lenbuf + sizeof(buf));
534 static int fetch_capa (char *line, void *data)
536 pop_data_t *pop_data = (pop_data_t *) data;
539 if (!ascii_strncasecmp (line, "SASL", 4)) {
540 p_delete(&pop_data->auth_list);
541 c = vskipspaces(line + 4);
542 pop_data->auth_list = m_strdup(c);
545 else if (!ascii_strncasecmp (line, "STLS", 4))
546 pop_data->cmd_stls = CMD_AVAILABLE;
548 else if (!ascii_strncasecmp (line, "UIDL", 4))
549 pop_data->cmd_uidl = CMD_AVAILABLE;
551 else if (!ascii_strncasecmp (line, "TOP", 3))
552 pop_data->cmd_top = CMD_AVAILABLE;
557 static int fetch_auth (char *line, void *data)
559 pop_data_t *pop_data = (pop_data_t *) data;
560 ssize_t auth_list_len;
562 if (!pop_data->auth_list) {
563 auth_list_len = m_strlen(line) + 1;
564 pop_data->auth_list = p_new(char, auth_list_len);
566 auth_list_len = m_strlen(pop_data->auth_list) + m_strlen(line) + 2;
567 p_realloc(&pop_data->auth_list, auth_list_len);
568 m_strcat(pop_data->auth_list, auth_list_len, " ");
570 m_strcat(pop_data->auth_list, auth_list_len, line);
578 * -1 - conection lost,
579 * -2 - execution error.
581 static pop_query_status pop_capabilities (pop_data_t * pop_data, int mode)
583 char buf[LONG_STRING];
585 /* don't check capabilities on reconnect */
586 if (pop_data->capabilities)
589 /* init capabilities */
591 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
592 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
593 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
594 pop_data->cmd_top = CMD_NOT_AVAILABLE;
595 pop_data->resp_codes = 0;
596 pop_data->expire = 1;
597 pop_data->login_delay = 0;
598 p_delete(&pop_data->auth_list);
601 /* Execute CAPA command */
602 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
603 m_strcpy(buf, sizeof(buf), "CAPA\r\n");
604 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
606 pop_data->cmd_capa = CMD_AVAILABLE;
608 case PFD_FUNCT_ERROR:
610 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
612 case PQ_NOT_CONNECTED:
613 return PQ_NOT_CONNECTED;
617 /* CAPA not supported, use defaults */
618 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
619 pop_data->cmd_uidl = CMD_UNKNOWN;
620 pop_data->cmd_top = CMD_UNKNOWN;
622 m_strcpy(buf, sizeof(buf), "AUTH\r\n");
623 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
624 return PQ_NOT_CONNECTED;
627 /* Check capabilities */
631 if (!pop_data->expire)
632 msg = _("Unable to leave messages on server.");
633 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
634 msg = _("Command TOP is not supported by server.");
635 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
636 msg = _("Command UIDL is not supported by server.");
637 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
641 pop_data->capabilities = 1;
647 /* given an POP mailbox name, return host, port, username and password */
648 static int pop_parse_path (const char *path, ACCOUNT * act)
656 act->port = POP_PORT;
657 act->type = M_ACCT_TYPE_POP;
660 url_parse_ciss (&url, c);
662 if (url.scheme == U_POP || url.scheme == U_POPS) {
663 if (url.scheme == U_POPS) {
664 act->flags |= M_ACCT_SSL;
665 act->port = POP_SSL_PORT;
668 if ((!url.path || !*url.path) && mutt_account_fromurl (act, &url) == 0)
677 * Open connection and authenticate
679 * -1 - conection lost,
680 * -2 - invalid command or execution error,
681 * -3 - authentication canceled.
683 static pop_query_status pop_open_connection (pop_data_t * pop_data)
685 pop_query_status ret;
687 char buf[LONG_STRING];
689 ret = pop_connect(pop_data);
695 ret = pop_capabilities (pop_data, 0);
696 if (ret == PQ_NOT_CONNECTED)
703 /* Attempt STLS if available and desired. */
704 if (!pop_data->conn->ssf && (pop_data->cmd_stls || mod_ssl.force_tls)) {
705 if (mod_ssl.force_tls)
706 pop_data->use_stls = 2;
707 if (pop_data->use_stls == 0) {
708 pop_data->use_stls = 1;
709 if (mod_ssl.starttls)
710 pop_data->use_stls = 2;
712 if (pop_data->use_stls == 2) {
713 m_strcpy(buf, sizeof(buf), "STLS\r\n");
714 ret = pop_query (pop_data, buf, sizeof (buf));
715 if (ret == PQ_NOT_CONNECTED)
718 mutt_error ("%s", pop_data->err_msg);
721 else if (mutt_ssl_starttls (pop_data->conn))
723 mutt_error (_("Could not negotiate TLS connection"));
728 /* recheck capabilities after STLS completes */
729 ret = pop_capabilities (pop_data, 1);
730 if (ret == PQ_NOT_CONNECTED)
740 if (mod_ssl.force_tls && !pop_data->conn->ssf) {
741 mutt_error _("Encrypted connection unavailable");
746 ret = pop_authenticate (pop_data);
747 if (ret == PQ_NOT_CONNECTED)
749 if (ret == PFD_FUNCT_ERROR)
754 /* recheck capabilities after authentication */
755 ret = pop_capabilities (pop_data, 2);
756 if (ret == PQ_NOT_CONNECTED)
763 /* get total size of mailbox */
764 m_strcpy(buf, sizeof(buf), "STAT\r\n");
765 ret = pop_query (pop_data, buf, sizeof (buf));
766 if (ret == PQ_NOT_CONNECTED)
769 mutt_error ("%s", pop_data->err_msg);
774 sscanf (buf, "+OK %u %u", &n, &size);
775 pop_data->size = size;
779 pop_data->status = POP_DISCONNECTED;
780 mutt_error _("Server closed connection!");
783 return PQ_NOT_CONNECTED;
786 /* find message with this UIDL and set refno */
787 static int check_uidl (char *line, void *data)
790 CONTEXT *ctx = (CONTEXT *)data;
792 sscanf (line, "%u %s", &idx, line);
793 for (i = 0; i < ctx->msgcount; i++) {
794 if (!m_strcmp(ctx->hdrs[i]->data, line)) {
795 ctx->hdrs[i]->refno = idx;
803 /* reconnect and verify indexes if connection was lost */
804 static pop_query_status pop_reconnect (CONTEXT * ctx)
806 pop_query_status ret;
807 pop_data_t *pop_data = (pop_data_t *) ctx->data;
810 if (pop_data->status == POP_CONNECTED)
812 if (pop_data->status == POP_BYE)
813 return PQ_NOT_CONNECTED;
816 mutt_socket_close (pop_data->conn);
818 ret = pop_open_connection(pop_data);
822 bar.msg = _("Verifying message indexes...");
824 mutt_progress_bar (&bar, 0);
826 for (i = 0; i < ctx->msgcount; i++)
827 ctx->hdrs[i]->refno = -1;
829 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
831 mutt_error ("%s", pop_data->err_msg);
841 return PQ_NOT_CONNECTED;
843 if (query_quadoption (OPT_POPRECONNECT,
844 _("Connection lost. Reconnect to POP server?")) !=
846 return PQ_NOT_CONNECTED;
850 /* write line to file */
851 static int fetch_message (char *line, void *file)
853 FILE *f = (FILE *) file;
856 if (fputc ('\n', f) == EOF)
866 * -1 - conection lost,
867 * -2 - invalid command or execution error,
868 * -3 - error writing to tempfile
870 static pop_query_status pop_read_header (pop_data_t * pop_data, HEADER * h)
874 pop_query_status ret;
876 char buf[LONG_STRING];
877 char tempfile[_POSIX_PATH_MAX];
879 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
881 mutt_error(_("Could not create temporary file"));
882 return PFD_FUNCT_ERROR;
885 snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
886 ret = pop_query (pop_data, buf, sizeof (buf));
888 sscanf (buf, "+OK %d %ld", &idx, &length);
890 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
891 ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
893 if (pop_data->cmd_top == CMD_UNKNOWN) {
895 pop_data->cmd_top = CMD_AVAILABLE;
899 pop_data->cmd_top = CMD_NOT_AVAILABLE;
900 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
901 _("Command TOP is not supported by server."));
910 h->env = mutt_read_rfc822_header (f, h, 0, 0);
911 h->content->length = length - h->content->offset + 1;
914 h->content->length--;
915 fgets (buf, sizeof (buf), f);
921 mutt_error ("%s", pop_data->err_msg);
924 case PFD_FUNCT_ERROR:
926 mutt_error _("Can't write header to temporary file!");
930 case PQ_NOT_CONNECTED:
932 mutt_error _("Can't fetch header: Not connected!");
943 static int fetch_uidl (char *line, void *data)
946 CONTEXT *ctx = (CONTEXT *) data;
947 pop_data_t *pop_data = (pop_data_t *) ctx->data;
949 sscanf (line, "%d %s", &idx, line);
950 for (i = 0; i < ctx->msgcount; i++)
951 if (!m_strcmp(line, ctx->hdrs[i]->data))
954 if (i == ctx->msgcount) {
955 if (i >= ctx->hdrmax)
956 mx_alloc_memory (ctx);
959 ctx->hdrs[i] = header_new();
960 ctx->hdrs[i]->data = m_strdup(line);
962 else if (ctx->hdrs[i]->index != idx - 1)
963 pop_data->clear_cache = 1;
965 ctx->hdrs[i]->refno = idx;
966 ctx->hdrs[i]->index = idx - 1;
975 * -1 - conection lost,
976 * -2 - invalid command or execution error,
977 * -3 - error writing to tempfile
979 static int pop_fetch_headers (CONTEXT * ctx)
981 int i, old_count, new_count;
982 pop_query_status ret;
983 pop_data_t *pop_data = (pop_data_t *) ctx->data;
985 time (&pop_data->check_time);
986 pop_data->clear_cache = 0;
988 for (i = 0; i < ctx->msgcount; i++)
989 ctx->hdrs[i]->refno = -1;
991 old_count = ctx->msgcount;
992 ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
993 new_count = ctx->msgcount;
994 ctx->msgcount = old_count;
996 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
998 pop_data->cmd_uidl = CMD_AVAILABLE;
1001 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
1002 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
1004 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
1005 _("Command UIDL is not supported by server."));
1010 for (i = 0; i < old_count; i++)
1011 if (ctx->hdrs[i]->refno == -1)
1012 ctx->hdrs[i]->deleted = 1;
1014 for (i = old_count; i < new_count; i++) {
1015 mutt_message (_("Fetching message headers... [%d/%d]"),
1016 i + 1 - old_count, new_count - old_count);
1018 ret = pop_read_header (pop_data, ctx->hdrs[i]);
1026 mx_update_context (ctx, i - old_count);
1030 for (i = ctx->msgcount; i < new_count; i++)
1031 header_delete(&ctx->hdrs[i]);
1035 mutt_clear_error ();
1036 return (new_count - old_count);
1039 /* delete all cached messages */
1040 static void pop_clear_cache (pop_data_t * pop_data)
1044 if (!pop_data->clear_cache)
1047 for (i = 0; i < POP_CACHE_LEN; i++) {
1048 if (pop_data->cache[i].path) {
1049 unlink (pop_data->cache[i].path);
1050 p_delete(&pop_data->cache[i].path);
1055 /* pop_mx functions {{{ */
1057 static int pop_is_magic(const char* path, struct stat* st)
1059 url_scheme_t s = url_check_scheme(NONULL(path));
1060 return s == U_POP || s == U_POPS ? M_POP : -1;
1063 static int pop_open_mailbox (CONTEXT * ctx)
1066 char buf[LONG_STRING];
1069 pop_data_t *pop_data;
1072 if (pop_parse_path (ctx->path, &act)) {
1073 mutt_error (_("%s is an invalid POP path"), ctx->path);
1078 mutt_account_tourl (&act, &url);
1080 url_ciss_tostring (&url, buf, sizeof (buf), 0);
1081 conn = mutt_conn_find (NULL, &act);
1085 p_delete(&ctx->path);
1086 ctx->path = m_strdup(buf);
1088 pop_data = p_new(pop_data_t, 1);
1089 pop_data->conn = conn;
1090 ctx->data = pop_data;
1092 if (pop_open_connection(pop_data) != PQ_OK)
1095 conn->data = pop_data;
1098 if (pop_reconnect (ctx) != PQ_OK)
1101 ctx->size = pop_data->size;
1103 mutt_message _("Fetching list of messages...");
1105 ret = pop_fetch_headers (ctx);
1117 static int pop_acl_check(CONTEXT *ctx, int bit)
1120 case ACL_DELETE: /* (un)deletion */
1121 case ACL_SEEN: /* mark as read */
1123 case ACL_INSERT: /* editing messages */
1124 case ACL_WRITE: /* change importance */
1130 static int pop_check_mailbox(CONTEXT * ctx, int *index_hint, int unused)
1133 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1135 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
1140 mutt_socket_close (pop_data->conn);
1142 if (pop_open_connection (pop_data) < 0)
1145 ctx->size = pop_data->size;
1147 mutt_message _("Checking for new messages...");
1149 ret = pop_fetch_headers (ctx);
1150 pop_clear_cache (pop_data);
1161 static void pop_close_mailbox (CONTEXT * ctx)
1163 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1170 if (pop_data->status != POP_NONE)
1171 mutt_socket_close (pop_data->conn);
1173 pop_data->status = POP_NONE;
1175 pop_data->clear_cache = 1;
1176 pop_clear_cache (pop_data);
1178 if (!pop_data->conn->data)
1179 mutt_socket_free (pop_data->conn);
1184 static int pop_sync_mailbox(CONTEXT * ctx, int unused, int *index_hint)
1187 pop_query_status ret;
1188 char buf[LONG_STRING];
1189 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1191 pop_data->check_time = 0;
1194 if (pop_reconnect (ctx) != PQ_OK)
1195 return PQ_NOT_CONNECTED;
1197 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
1199 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
1200 if (ctx->hdrs[i]->deleted) {
1201 snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
1202 ret = pop_query (pop_data, buf, sizeof (buf));
1207 m_strcpy(buf, sizeof(buf), "QUIT\r\n");
1208 ret = pop_query (pop_data, buf, sizeof (buf));
1212 pop_data->clear_cache = 1;
1213 pop_clear_cache (pop_data);
1214 pop_data->status = POP_DISCONNECTED;
1218 if (ret == PQ_ERR) {
1219 mutt_error ("%s", pop_data->err_msg);
1221 return PQ_NOT_CONNECTED;
1228 mx_t const pop_mx = {
1243 /* public API {{{ */
1245 void pop_fetch_mail (void)
1247 char buffer[LONG_STRING];
1248 char msgbuf[STRING];
1250 int i, delanswer, last = 0, msgs, bytes, rset = 0;
1251 pop_query_status ret;
1254 MESSAGE *msg = NULL;
1256 pop_data_t *pop_data;
1260 mutt_error _("POP host is not defined.");
1265 plen = m_strlen(PopHost) + 7;
1266 url = p = p_new(char, plen);
1267 if (url_check_scheme (PopHost) == U_UNKNOWN) {
1268 plen -= m_strcpy(url, plen, "pop://");
1271 m_strcpy(p, plen, PopHost);
1273 ret = pop_parse_path (url, &act);
1276 mutt_error (_("%s is an invalid POP path"), PopHost);
1280 conn = mutt_conn_find (NULL, &act);
1284 pop_data = p_new(pop_data_t, 1);
1285 pop_data->conn = conn;
1287 if (pop_open_connection (pop_data) < 0) {
1288 mutt_socket_free (pop_data->conn);
1289 p_delete(&pop_data);
1293 conn->data = pop_data;
1295 mutt_message _("Checking for new messages...");
1297 /* find out how many messages are in the mailbox. */
1298 m_strcpy(buffer, sizeof(buffer), "STAT\r\n");
1299 ret = pop_query (pop_data, buffer, sizeof (buffer));
1300 if (ret == PQ_NOT_CONNECTED)
1302 if (ret == PQ_ERR) {
1303 mutt_error ("%s", pop_data->err_msg);
1307 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
1309 /* only get unread messages */
1310 if (msgs > 0 && option (OPTPOPLAST)) {
1311 m_strcpy(buffer, sizeof(buffer), "LAST\r\n");
1312 ret = pop_query (pop_data, buffer, sizeof (buffer));
1313 if (ret == PQ_NOT_CONNECTED)
1316 sscanf (buffer, "+OK %d", &last);
1320 mutt_message _("No new mail in POP mailbox.");
1325 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
1329 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
1331 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
1333 mutt_message ("%s", msgbuf);
1335 for (i = last + 1; i <= msgs; i++) {
1336 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
1339 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
1340 ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
1341 if (ret == PFD_FUNCT_ERROR)
1344 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
1346 ret = PFD_FUNCT_ERROR;
1349 mx_close_message (&msg);
1352 if (ret == PQ_OK && delanswer == M_YES) {
1353 /* delete the message on the server */
1354 snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
1355 ret = pop_query (pop_data, buffer, sizeof (buffer));
1358 if (ret == PQ_NOT_CONNECTED) {
1359 mx_close_mailbox (&ctx, NULL);
1362 if (ret == PQ_ERR) {
1363 mutt_error ("%s", pop_data->err_msg);
1366 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
1367 mutt_error _("Error while writing mailbox!");
1372 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
1376 mx_close_mailbox (&ctx, NULL);
1379 /* make sure no messages get deleted */
1380 m_strcpy(buffer, sizeof(buffer), "RSET\r\n");
1381 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
1386 /* exit gracefully */
1387 m_strcpy(buffer, sizeof(buffer), "QUIT\r\n");
1388 if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
1390 mutt_socket_close (conn);
1391 p_delete(&pop_data);
1395 mutt_error _("Server closed connection!");
1396 mutt_socket_close (conn);
1397 p_delete(&pop_data);
1400 /* fetch message from POP server */
1401 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1405 char buf[LONG_STRING];
1406 char path[_POSIX_PATH_MAX];
1408 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1410 HEADER *h = ctx->hdrs[msgno];
1412 /* see if we already have the message in our cache */
1413 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
1416 if (cache->index == h->index) {
1417 /* yes, so just return a pointer to the message */
1418 msg->fp = fopen (cache->path, "r");
1422 mutt_perror (cache->path);
1427 /* clear the previous entry */
1428 unlink (cache->path);
1429 p_delete(&cache->path);
1434 if (pop_reconnect (ctx) != PQ_OK)
1437 /* verify that massage index is correct */
1440 _("The message index is incorrect. Try reopening the mailbox.");
1445 bar.size = h->content->length + h->content->offset - 1;
1446 bar.msg = _("Fetching message...");
1447 mutt_progress_bar (&bar, 0);
1449 msg->fp = m_tempfile(path, sizeof(path), NONULL(MCore.tmpdir), NULL);
1451 mutt_error(_("Could not create temporary file"));
1456 snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
1458 ret = pop_fetch_data (pop_data, buf, &bar, fetch_message, msg->fp);
1465 if (ret == PQ_ERR) {
1466 mutt_error ("%s", pop_data->err_msg);
1471 if (ret == PFD_FUNCT_ERROR) {
1472 mutt_error _("Can't write message to temporary file!");
1479 /* Update the header information. Previously, we only downloaded a
1480 * portion of the headers, those required for the main display.
1482 cache->index = h->index;
1483 cache->path = m_strdup(path);
1486 envelope_delete(&h->env);
1487 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
1490 fgets (buf, sizeof (buf), msg->fp);
1491 while (!feof (msg->fp)) {
1492 ctx->hdrs[msgno]->lines++;
1493 fgets (buf, sizeof (buf), msg->fp);
1496 h->content->length = ftello (msg->fp) - h->content->offset;
1498 /* This needs to be done in case this is a multipart message */
1499 h->security = crypt_query (h->content);
1501 mutt_clear_error ();