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 - 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 char buf[LONG_STRING];
460 pop_query_status ret;
465 m_strcpy(buf, sizeof(buf), query);
466 ret = _pop_query(pop_data, buf, sizeof(buf));
470 inbuf = p_new(char, sizeof(buf));
474 mutt_socket_readln(buf, sizeof (buf), pop_data->conn);
476 pop_data->status = POP_DISCONNECTED;
477 ret = PQ_NOT_CONNECTED;
482 if (!lenbuf && buf[0] == '.') {
488 m_strcpy(inbuf + lenbuf,sizeof(buf), p);
491 if (chunk >= ssizeof(buf)) {
492 lenbuf += strlen (p);
495 mutt_progress_bar (bar, pos);
496 if (ret == 0 && funct (inbuf, data) < 0)
497 ret = PFD_FUNCT_ERROR;
501 p_realloc(&inbuf, lenbuf + sizeof(buf));
508 static int fetch_capa (char *line, void *data)
510 pop_data_t *pop_data = (pop_data_t *) data;
513 if (!ascii_strncasecmp (line, "SASL", 4)) {
514 p_delete(&pop_data->auth_list);
515 c = vskipspaces(line + 4);
516 pop_data->auth_list = m_strdup(c);
519 else if (!ascii_strncasecmp (line, "STLS", 4))
520 pop_data->cmd_stls = CMD_AVAILABLE;
522 else if (!ascii_strncasecmp (line, "UIDL", 4))
523 pop_data->cmd_uidl = CMD_AVAILABLE;
525 else if (!ascii_strncasecmp (line, "TOP", 3))
526 pop_data->cmd_top = CMD_AVAILABLE;
531 static int fetch_auth (char *line, void *data)
533 pop_data_t *pop_data = (pop_data_t *) data;
534 ssize_t auth_list_len;
536 if (!pop_data->auth_list) {
537 auth_list_len = m_strlen(line) + 1;
538 pop_data->auth_list = p_new(char, auth_list_len);
540 auth_list_len = m_strlen(pop_data->auth_list) + m_strlen(line) + 2;
541 p_realloc(&pop_data->auth_list, auth_list_len);
542 m_strcat(pop_data->auth_list, auth_list_len, " ");
544 m_strcat(pop_data->auth_list, auth_list_len, line);
552 * -1 - conection lost,
553 * -2 - execution error.
555 static pop_query_status pop_capabilities(pop_data_t * pop_data, int mode)
557 /* don't check capabilities on reconnect */
558 if (pop_data->capabilities)
561 /* init capabilities */
563 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
564 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
565 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
566 pop_data->cmd_top = CMD_NOT_AVAILABLE;
567 pop_data->cmd_user = CMD_NOT_AVAILABLE;
568 pop_data->resp_codes = 0;
569 pop_data->expire = 1;
570 p_delete(&pop_data->auth_list);
573 /* Execute CAPA command */
574 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
575 switch (pop_fetch_data(pop_data, "CAPA\r\n", NULL, fetch_capa, pop_data)) {
577 pop_data->cmd_capa = CMD_AVAILABLE;
579 case PFD_FUNCT_ERROR:
581 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
583 case PQ_NOT_CONNECTED:
584 return PQ_NOT_CONNECTED;
588 /* CAPA not supported, use defaults */
589 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
590 pop_data->cmd_uidl = CMD_UNKNOWN;
591 pop_data->cmd_top = CMD_UNKNOWN;
593 if (pop_fetch_data(pop_data, "AUTH\r\n", NULL, fetch_auth, pop_data) ==
595 return PQ_NOT_CONNECTED;
598 /* Check capabilities */
602 if (!pop_data->expire)
603 msg = _("Unable to leave messages on server.");
604 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
605 msg = _("Command TOP is not supported by server.");
606 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
607 msg = _("Command UIDL is not supported by server.");
608 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
612 pop_data->capabilities = 1;
618 /* given an POP mailbox name, return host, port, username and password */
619 static int pop_parse_path (const char *path, ACCOUNT * act)
627 act->port = POP_PORT;
628 act->type = M_ACCT_TYPE_POP;
631 url_parse_ciss (&url, c);
633 if (url.scheme == U_POP || url.scheme == U_POPS) {
634 if (url.scheme == U_POPS) {
635 act->flags |= M_ACCT_SSL;
636 act->port = POP_SSL_PORT;
639 if ((!url.path || !*url.path) && mutt_account_fromurl (act, &url) == 0)
648 * Open connection and authenticate
650 * -1 - conection lost,
651 * -2 - invalid command or execution error,
652 * -3 - authentication canceled.
654 static pop_query_status pop_open_connection (pop_data_t * pop_data)
656 pop_query_status ret;
658 char buf[LONG_STRING];
660 ret = pop_connect(pop_data);
666 ret = pop_capabilities (pop_data, 0);
667 if (ret == PQ_NOT_CONNECTED)
674 /* Attempt STLS if available and desired. */
675 if (!pop_data->conn->ssf && (pop_data->cmd_stls || mod_ssl.force_tls)) {
676 if (mod_ssl.force_tls)
677 pop_data->use_stls = 2;
678 if (pop_data->use_stls == 0) {
679 pop_data->use_stls = 1;
680 if (mod_ssl.starttls)
681 pop_data->use_stls = 2;
683 if (pop_data->use_stls == 2) {
684 ret = pop_query(pop_data, buf, sizeof(buf), "STLS");
685 if (ret == PQ_NOT_CONNECTED)
688 mutt_error ("%s", pop_data->err_msg);
691 else if (mutt_ssl_starttls (pop_data->conn))
693 mutt_error (_("Could not negotiate TLS connection"));
698 /* recheck capabilities after STLS completes */
699 ret = pop_capabilities (pop_data, 1);
700 if (ret == PQ_NOT_CONNECTED)
710 if (mod_ssl.force_tls && !pop_data->conn->ssf) {
711 mutt_error _("Encrypted connection unavailable");
716 ret = pop_authenticate (pop_data);
717 if (ret == PQ_NOT_CONNECTED)
719 if (ret == PFD_FUNCT_ERROR)
724 /* recheck capabilities after authentication */
725 ret = pop_capabilities (pop_data, 2);
726 if (ret == PQ_NOT_CONNECTED)
733 /* get total size of mailbox */
734 ret = pop_query(pop_data, buf, sizeof(buf), "STAT");
735 if (ret == PQ_NOT_CONNECTED)
738 mutt_error ("%s", pop_data->err_msg);
743 sscanf (buf, "+OK %u %u", &n, &size);
744 pop_data->size = size;
748 pop_data->status = POP_DISCONNECTED;
749 mutt_error _("Server closed connection!");
752 return PQ_NOT_CONNECTED;
755 /* find message with this UIDL and set refno */
756 static int check_uidl (char *line, void *data)
759 CONTEXT *ctx = (CONTEXT *)data;
761 sscanf (line, "%u %s", &idx, line);
762 for (i = 0; i < ctx->msgcount; i++) {
763 if (!m_strcmp(ctx->hdrs[i]->data, line)) {
764 ctx->hdrs[i]->refno = idx;
772 /* reconnect and verify indexes if connection was lost */
773 static pop_query_status pop_reconnect (CONTEXT * ctx)
775 pop_query_status ret;
776 pop_data_t *pop_data = (pop_data_t *) ctx->data;
779 if (pop_data->status == POP_CONNECTED)
781 if (pop_data->status == POP_BYE)
782 return PQ_NOT_CONNECTED;
785 mutt_socket_close (pop_data->conn);
787 ret = pop_open_connection(pop_data);
791 bar.msg = _("Verifying message indexes...");
793 mutt_progress_bar (&bar, 0);
795 for (i = 0; i < ctx->msgcount; i++)
796 ctx->hdrs[i]->refno = -1;
798 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
800 mutt_error ("%s", pop_data->err_msg);
810 return PQ_NOT_CONNECTED;
812 if (query_quadoption (OPT_POPRECONNECT,
813 _("Connection lost. Reconnect to POP server?")) !=
815 return PQ_NOT_CONNECTED;
819 /* write line to file */
820 static int fetch_message (char *line, void *file)
822 FILE *f = (FILE *) file;
825 if (fputc ('\n', f) == EOF)
835 * -1 - conection lost,
836 * -2 - invalid command or execution error,
837 * -3 - error writing to tempfile
839 static pop_query_status pop_read_header (pop_data_t * pop_data, HEADER * h)
843 pop_query_status ret;
845 char buf[LONG_STRING];
849 mutt_error(_("Could not create temporary file"));
850 return PFD_FUNCT_ERROR;
853 ret = pop_query(pop_data, buf, sizeof(buf), "LIST %d", h->refno);
855 sscanf (buf, "+OK %d %ld", &idx, &length);
857 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
858 ret = pop_fetch_data(pop_data, buf, NULL, fetch_message, f);
860 if (pop_data->cmd_top == CMD_UNKNOWN) {
862 pop_data->cmd_top = CMD_AVAILABLE;
866 pop_data->cmd_top = CMD_NOT_AVAILABLE;
867 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
868 _("Command TOP is not supported by server."));
877 h->env = mutt_read_rfc822_header (f, h, 0, 0);
878 h->content->length = length - h->content->offset + 1;
881 h->content->length--;
882 fgets (buf, sizeof (buf), f);
888 mutt_error ("%s", pop_data->err_msg);
891 case PFD_FUNCT_ERROR:
893 mutt_error _("Can't write header to temporary file!");
897 case PQ_NOT_CONNECTED:
899 mutt_error _("Can't fetch header: Not connected!");
909 static int fetch_uidl (char *line, void *data)
912 CONTEXT *ctx = (CONTEXT *) data;
913 pop_data_t *pop_data = (pop_data_t *) ctx->data;
915 sscanf (line, "%d %s", &idx, line);
916 for (i = 0; i < ctx->msgcount; i++)
917 if (!m_strcmp(line, ctx->hdrs[i]->data))
920 if (i == ctx->msgcount) {
921 if (i >= ctx->hdrmax)
922 mx_alloc_memory (ctx);
925 ctx->hdrs[i] = header_new();
926 ctx->hdrs[i]->data = m_strdup(line);
928 else if (ctx->hdrs[i]->index != idx - 1)
929 pop_data->clear_cache = 1;
931 ctx->hdrs[i]->refno = idx;
932 ctx->hdrs[i]->index = idx - 1;
941 * -1 - conection lost,
942 * -2 - invalid command or execution error,
943 * -3 - error writing to tempfile
945 static int pop_fetch_headers (CONTEXT * ctx)
947 int i, old_count, new_count;
948 pop_query_status ret;
949 pop_data_t *pop_data = (pop_data_t *) ctx->data;
951 time (&pop_data->check_time);
952 pop_data->clear_cache = 0;
954 for (i = 0; i < ctx->msgcount; i++)
955 ctx->hdrs[i]->refno = -1;
957 old_count = ctx->msgcount;
958 ret = pop_fetch_data(pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
959 new_count = ctx->msgcount;
960 ctx->msgcount = old_count;
962 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
964 pop_data->cmd_uidl = CMD_AVAILABLE;
967 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
968 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
970 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
971 _("Command UIDL is not supported by server."));
976 for (i = 0; i < old_count; i++)
977 if (ctx->hdrs[i]->refno == -1)
978 ctx->hdrs[i]->deleted = 1;
980 for (i = old_count; i < new_count; i++) {
981 mutt_message (_("Fetching message headers... [%d/%d]"),
982 i + 1 - old_count, new_count - old_count);
984 ret = pop_read_header (pop_data, ctx->hdrs[i]);
992 mx_update_context (ctx, i - old_count);
996 for (i = ctx->msgcount; i < new_count; i++)
997 header_delete(&ctx->hdrs[i]);
1001 mutt_clear_error ();
1002 return (new_count - old_count);
1005 /* delete all cached messages */
1006 static void pop_clear_cache (pop_data_t * pop_data)
1010 if (!pop_data->clear_cache)
1013 for (i = 0; i < POP_CACHE_LEN; i++) {
1014 if (pop_data->cache[i].path) {
1015 unlink (pop_data->cache[i].path);
1016 p_delete(&pop_data->cache[i].path);
1021 /* pop_mx functions {{{ */
1023 static int pop_is_magic(const char* path, struct stat* st)
1025 url_scheme_t s = url_check_scheme(NONULL(path));
1026 return s == U_POP || s == U_POPS ? M_POP : -1;
1029 static int pop_open_mailbox (CONTEXT * ctx)
1032 char buf[LONG_STRING];
1035 pop_data_t *pop_data;
1038 if (pop_parse_path (ctx->path, &act)) {
1039 mutt_error (_("%s is an invalid POP path"), ctx->path);
1044 mutt_account_tourl (&act, &url);
1046 url_ciss_tostring (&url, buf, sizeof (buf), 0);
1047 conn = mutt_conn_find (NULL, &act);
1051 p_delete(&ctx->path);
1052 ctx->path = m_strdup(buf);
1054 pop_data = p_new(pop_data_t, 1);
1055 pop_data->conn = conn;
1056 ctx->data = pop_data;
1058 if (pop_open_connection(pop_data) != PQ_OK)
1061 conn->data = pop_data;
1064 if (pop_reconnect (ctx) != PQ_OK)
1067 mutt_message _("Fetching list of messages...");
1068 ctx->size = pop_data->size;
1069 ret = pop_fetch_headers (ctx);
1081 static int pop_acl_check(CONTEXT *ctx, int bit)
1084 case ACL_DELETE: /* (un)deletion */
1085 case ACL_SEEN: /* mark as read */
1087 case ACL_INSERT: /* editing messages */
1088 case ACL_WRITE: /* change importance */
1094 static int pop_check_mailbox(CONTEXT * ctx, int *index_hint, int unused)
1097 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1099 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
1104 mutt_socket_close (pop_data->conn);
1106 if (pop_open_connection (pop_data) < 0)
1109 mutt_message _("Checking for new messages...");
1110 ctx->size = pop_data->size;
1111 ret = pop_fetch_headers (ctx);
1112 pop_clear_cache (pop_data);
1123 static void pop_close_mailbox (CONTEXT * ctx)
1125 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1132 if (pop_data->status != POP_NONE)
1133 mutt_socket_close (pop_data->conn);
1135 pop_data->status = POP_NONE;
1137 pop_data->clear_cache = 1;
1138 pop_clear_cache (pop_data);
1140 if (!pop_data->conn->data)
1141 mutt_socket_free (pop_data->conn);
1146 static int pop_sync_mailbox(CONTEXT * ctx, int unused, int *index_hint)
1149 pop_query_status ret;
1150 char buf[LONG_STRING];
1151 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1153 pop_data->check_time = 0;
1156 if (pop_reconnect (ctx) != PQ_OK)
1157 return PQ_NOT_CONNECTED;
1159 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
1161 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
1162 if (ctx->hdrs[i]->deleted) {
1163 ret = pop_query(pop_data, buf, sizeof(buf), "DELE %d",
1164 ctx->hdrs[i]->refno);
1169 ret = pop_query(pop_data, buf, sizeof(buf), "QUIT");
1173 pop_data->clear_cache = 1;
1174 pop_clear_cache (pop_data);
1175 pop_data->status = POP_DISCONNECTED;
1179 if (ret == PQ_ERR) {
1180 mutt_error ("%s", pop_data->err_msg);
1182 return PQ_NOT_CONNECTED;
1189 mx_t const pop_mx = {
1204 /* public API {{{ */
1206 void pop_fetch_mail (void)
1208 char buffer[LONG_STRING];
1209 char msgbuf[STRING];
1211 int i, delanswer, last = 0, msgs, bytes, rset = 0;
1212 pop_query_status ret;
1215 MESSAGE *msg = NULL;
1217 pop_data_t *pop_data;
1221 mutt_error _("POP host is not defined.");
1225 plen = m_strlen(PopHost) + 7;
1226 url = p = p_new(char, plen);
1227 if (url_check_scheme (PopHost) == U_UNKNOWN) {
1228 plen -= m_strcpy(url, plen, "pop://");
1231 m_strcpy(p, plen, PopHost);
1233 ret = pop_parse_path (url, &act);
1236 mutt_error (_("%s is an invalid POP path"), PopHost);
1240 conn = mutt_conn_find (NULL, &act);
1244 pop_data = p_new(pop_data_t, 1);
1245 pop_data->conn = conn;
1247 if (pop_open_connection (pop_data) < 0) {
1248 mutt_socket_free (pop_data->conn);
1249 p_delete(&pop_data);
1253 conn->data = pop_data;
1255 mutt_message _("Checking for new messages...");
1257 /* find out how many messages are in the mailbox. */
1258 ret = pop_query(pop_data, buffer, sizeof(buffer), "STAT");
1259 if (ret == PQ_NOT_CONNECTED)
1261 if (ret == PQ_ERR) {
1262 mutt_error ("%s", pop_data->err_msg);
1266 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
1268 /* only get unread messages */
1269 if (msgs > 0 && option (OPTPOPLAST)) {
1270 ret = pop_query(pop_data, buffer, sizeof(buffer), "LAST");
1271 if (ret == PQ_NOT_CONNECTED)
1274 sscanf (buffer, "+OK %d", &last);
1278 mutt_message _("No new mail in POP mailbox.");
1283 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
1287 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
1289 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
1291 mutt_message ("%s", msgbuf);
1293 for (i = last + 1; i <= msgs; i++) {
1294 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
1297 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
1298 ret = pop_fetch_data(pop_data, buffer, NULL, fetch_message, msg->fp);
1299 if (ret == PFD_FUNCT_ERROR)
1302 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
1304 ret = PFD_FUNCT_ERROR;
1307 mx_close_message (&msg);
1310 if (ret == PQ_OK && delanswer == M_YES) {
1311 ret = pop_query(pop_data, buffer, sizeof(buffer), "DELE %d", i);
1314 if (ret == PQ_NOT_CONNECTED) {
1315 mx_close_mailbox (&ctx, NULL);
1318 if (ret == PQ_ERR) {
1319 mutt_error ("%s", pop_data->err_msg);
1322 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
1323 mutt_error _("Error while writing mailbox!");
1328 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
1332 mx_close_mailbox (&ctx, NULL);
1335 /* make sure no messages get deleted */
1336 if (pop_query(pop_data, buffer, sizeof(buffer), "RSET") ==
1342 if (pop_query(pop_data, buffer, sizeof(buffer), "QUIT") == PQ_NOT_CONNECTED)
1344 mutt_socket_close (conn);
1345 p_delete(&pop_data);
1349 mutt_error _("Server closed connection!");
1350 mutt_socket_close (conn);
1351 p_delete(&pop_data);
1354 /* fetch message from POP server */
1355 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1359 char buf[LONG_STRING];
1360 char path[_POSIX_PATH_MAX];
1362 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1364 HEADER *h = ctx->hdrs[msgno];
1366 /* see if we already have the message in our cache */
1367 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
1370 if (cache->index == h->index) {
1371 /* yes, so just return a pointer to the message */
1372 msg->fp = fopen (cache->path, "r");
1376 mutt_perror (cache->path);
1381 /* clear the previous entry */
1382 unlink (cache->path);
1383 p_delete(&cache->path);
1388 if (pop_reconnect (ctx) != PQ_OK)
1391 /* verify that massage index is correct */
1394 _("The message index is incorrect. Try reopening the mailbox.");
1399 bar.size = h->content->length + h->content->offset - 1;
1400 bar.msg = _("Fetching message...");
1401 mutt_progress_bar (&bar, 0);
1403 msg->fp = m_tempfile(path, sizeof(path), NONULL(MCore.tmpdir), NULL);
1405 mutt_error(_("Could not create temporary file"));
1410 snprintf(buf, sizeof(buf), "RETR %d\r\n", h->refno);
1411 ret = pop_fetch_data(pop_data, buf, &bar, fetch_message, msg->fp);
1418 if (ret == PQ_ERR) {
1419 mutt_error ("%s", pop_data->err_msg);
1424 if (ret == PFD_FUNCT_ERROR) {
1425 mutt_error _("Can't write message to temporary file!");
1432 /* Update the header information. Previously, we only downloaded a
1433 * portion of the headers, those required for the main display.
1435 cache->index = h->index;
1436 cache->path = m_strdup(path);
1439 envelope_delete(&h->env);
1440 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
1443 fgets (buf, sizeof (buf), msg->fp);
1444 while (!feof (msg->fp)) {
1445 ctx->hdrs[msgno]->lines++;
1446 fgets (buf, sizeof (buf), msg->fp);
1449 h->content->length = ftello (msg->fp) - h->content->offset;
1451 /* This needs to be done in case this is a multipart message */
1452 h->security = crypt_query (h->content);
1454 mutt_clear_error ();