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.
11 #include <sasl/sasl.h>
12 #include <sasl/saslutil.h>
14 #include <lib-sys/mutt_socket.h>
15 #include <lib-ui/lib-ui.h>
19 #include "mutt_sasl.h"
23 #define POP_SSL_PORT 995
24 #define POP_CACHE_LEN 10
25 /* maximal length of the server response (RFC1939) */
26 #define POP_CMD_RESPONSE 512
49 /* pop_fetch_data uses pop_query_status and this return value */
52 PQ_NOT_CONNECTED = -1,
57 CMD_NOT_AVAILABLE = 0,
59 CMD_UNKNOWN /* unknown whether it is available or not */
66 unsigned capabilities : 1;
67 unsigned use_stls : 2;
68 cmd_status cmd_capa : 2; /* optional command CAPA */
69 cmd_status cmd_stls : 2; /* optional command STLS */
70 cmd_status cmd_uidl : 2; /* optional command UIDL */
71 cmd_status cmd_top : 2; /* optional command TOP */
72 cmd_status cmd_user : 2; /* optional command USER */
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 char *auth_list; /* list of auth mechanisms */
81 char err_msg[POP_CMD_RESPONSE];
82 POP_CACHE cache[POP_CACHE_LEN];
85 /* pop low level functions {{{ */
87 static void pop_error(pop_data_t *pop_data, const char *msg)
89 if (!m_strncmp(msg, "-ERR ", 5)) {
90 const char *s = skipspaces(msg + 5);
95 m_strcat(pop_data->err_msg, sizeof(pop_data->err_msg), msg);
96 m_strrtrim(pop_data->err_msg);
100 * Send data from buffer and receive answer to the same buffer
102 * -1 - conection lost,
103 * -2 - invalid command or execution error.
105 static pop_query_status _pop_query(pop_data_t *pop_data, char *buf, ssize_t buflen)
107 if (pop_data->status != POP_CONNECTED)
108 return PQ_NOT_CONNECTED;
110 mutt_socket_write(pop_data->conn, buf);
111 snprintf(pop_data->err_msg, sizeof(pop_data->err_msg), "%.*s: ",
112 (int)(strpbrk(buf, " \r\n") - buf), buf);
114 if (mutt_socket_readln(buf, buflen, pop_data->conn) < 0) {
115 pop_data->status = POP_DISCONNECTED;
116 return PQ_NOT_CONNECTED;
118 if (!m_strncmp(buf, "+OK", 3))
121 pop_error(pop_data, buf);
124 #define pop_query(pd, b, l, fmt, ...) \
125 (snprintf(b, l, fmt "\r\n", ##__VA_ARGS__), _pop_query(pd, b, l))
130 * -1 - conection lost,
131 * -2 - invalid response.
133 static pop_query_status pop_connect(pop_data_t * pop_data)
135 char buf[LONG_STRING];
138 if (mutt_socket_open(pop_data->conn) < 0
139 || mutt_socket_readln(buf, sizeof(buf), pop_data->conn) < 0)
141 mutt_error(_("Error connecting to server: %s"),
142 pop_data->conn->account.host);
143 pop_data->status = POP_NONE;
144 return PQ_NOT_CONNECTED;
147 pop_data->status = POP_CONNECTED;
148 if (m_strncmp(buf, "+OK", 3)) {
149 pop_data->err_msg[0] = '\0';
150 pop_error(pop_data, buf);
151 mutt_error("%s", pop_data->err_msg);
155 p_delete(&pop_data->timestamp);
156 if ((p = strchr(buf, '<')) && (q = strchr(p, '>'))) {
157 pop_data->timestamp = p_dupstr(p, q + 1 - p);
162 static void pop_logout (CONTEXT * ctx)
164 pop_query_status ret = 0;
165 char buf[LONG_STRING];
166 pop_data_t *pop_data = (pop_data_t *) ctx->data;
168 if (pop_data->status == POP_CONNECTED) {
169 mutt_message _("Closing connection to POP server...");
172 ret = pop_query(pop_data, buf, sizeof(buf), "RSET");
175 if (ret != PQ_NOT_CONNECTED) {
176 pop_query(pop_data, buf, sizeof(buf), "QUIT");
182 pop_data->status = POP_DISCONNECTED;
187 /* Authentication {{{ */
189 static pop_auth_res_t pop_auth_sasl(pop_data_t *pop_data, const char *method)
191 sasl_conn_t *saslconn;
192 sasl_interact_t *interaction = NULL;
194 char buf[LONG_STRING], inbuf[LONG_STRING];
195 const char *mech, *pc = NULL;
196 unsigned int len, olen;
197 unsigned char client_start;
200 if (mutt_sasl_client_new(pop_data->conn, &saslconn) < 0) {
201 return POP_A_FAILURE;
205 method = pop_data->auth_list;
208 rc = sasl_client_start(saslconn, method, &interaction, &pc, &olen,
210 if (rc != SASL_INTERACT)
212 mutt_sasl_interact(interaction);
215 if (rc != SASL_OK && rc != SASL_CONTINUE) {
216 return POP_A_UNAVAIL;
219 client_start = (olen > 0);
220 mutt_message(_("Authenticating (SASL)..."));
221 snprintf(buf, sizeof (buf), "AUTH %s", mech);
224 /* looping protocol */
226 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
227 mutt_socket_write(pop_data->conn, buf);
228 if (mutt_socket_readln(inbuf, sizeof(inbuf), pop_data->conn) < 0) {
229 sasl_dispose(&saslconn);
230 pop_data->status = POP_DISCONNECTED;
234 if (rc != SASL_CONTINUE)
237 if (!m_strncmp(inbuf, "+ ", 2)
238 && sasl_decode64(inbuf, strlen(inbuf),
239 buf, sizeof(buf) - 1, &len) != SASL_OK)
246 rc = sasl_client_step(saslconn, buf, len, &interaction, &pc,
248 if (rc != SASL_INTERACT)
250 mutt_sasl_interact(interaction);
256 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
259 /* send out response, or line break if none needed */
261 if (sasl_encode64(pc, olen, buf, sizeof(buf), &olen) != SASL_OK) {
264 p_delete((char **)&pc);
271 if (!m_strncmp(inbuf, "+OK", 3)) {
272 mutt_sasl_setup_conn(pop_data->conn, saslconn);
273 return POP_A_SUCCESS;
277 sasl_dispose(&saslconn);
278 p_delete((char **)&pc);
280 /* terminate SASL session if the last responce is not +OK nor -ERR */
281 if (!m_strncmp(inbuf, "+ ", 2)) {
282 if (pop_query(pop_data, buf, sizeof(buf), "*") == PQ_NOT_CONNECTED)
286 mutt_error(_("SASL authentication failed."));
289 return POP_A_FAILURE;
292 static pop_auth_res_t pop_auth_apop(pop_data_t *pop_data, const char *method)
295 unsigned char digest[16];
297 char buf[LONG_STRING];
300 if (!pop_data->timestamp)
301 return POP_A_UNAVAIL;
303 mutt_message _("Authenticating (APOP)...");
305 /* Compute the authentication hash to send to the server */
307 MD5Update(&mdContext, (unsigned char *)pop_data->timestamp,
308 strlen(pop_data->timestamp));
309 MD5Update(&mdContext, (unsigned char *)pop_data->conn->account.pass,
310 strlen(pop_data->conn->account.pass));
311 MD5Final(digest, &mdContext);
313 for (i = 0; i < countof(digest); i++)
314 sprintf(hash + 2 * i, "%02x", digest[i]);
316 switch (pop_query(pop_data, buf, sizeof(buf), "APOP %s %s",
317 pop_data->conn->account.user, hash))
320 return POP_A_SUCCESS;
321 case PQ_NOT_CONNECTED:
324 mutt_error("%s %s", _("APOP authentication failed."), pop_data->err_msg);
326 return POP_A_FAILURE;
330 static pop_auth_res_t pop_auth_user(pop_data_t *pop_data, const char *method)
332 char buf[LONG_STRING];
333 pop_query_status ret;
335 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
336 return POP_A_UNAVAIL;
338 mutt_message _("Authenticating (USER)...");
339 ret = pop_query(pop_data, buf, sizeof(buf), "USER %s",
340 pop_data->conn->account.user);
342 if (pop_data->cmd_user == CMD_UNKNOWN) {
344 pop_data->cmd_user = CMD_AVAILABLE;
346 pop_data->cmd_user = CMD_NOT_AVAILABLE;
350 ret = pop_query(pop_data, buf, sizeof(buf), "PASS %s",
351 pop_data->conn->account.pass);
356 return POP_A_SUCCESS;
357 case PQ_NOT_CONNECTED:
360 mutt_error("%s %s", _("USER authentication failed."), pop_data->err_msg);
362 return POP_A_FAILURE;
367 pop_auth_res_t (*do_auth)(pop_data_t *, const char *);
371 static pop_query_status pop_authenticate (pop_data_t * pop_data)
373 static pop_auth_t const pop_authenticators[] = {
374 {pop_auth_sasl, NULL},
375 {pop_auth_apop, "apop"},
376 {pop_auth_user, "user"},
380 ACCOUNT *act = &pop_data->conn->account;
381 const pop_auth_t *auth;
384 if (mutt_account_getuser(act) || !act->user[0]
385 || mutt_account_getpass(act) || !act->pass[0])
387 return PFD_FUNCT_ERROR;
390 if (!m_strisempty(PopAuthenticators)) {
394 for (p = PopAuthenticators; ; p = q) {
400 q = strchrnul(p, ':');
401 m_strncpy(buf, sizeof(buf), p, q - p);
403 for (auth = pop_authenticators; auth->do_auth; auth++) {
404 if (auth->method && ascii_strcasecmp(auth->method, buf))
407 switch (auth->do_auth(pop_data, buf)) {
411 return PQ_NOT_CONNECTED;
418 mutt_socket_close(pop_data->conn);
422 for (auth = pop_authenticators; auth->do_auth; auth++) {
423 switch (auth->do_auth(pop_data, auth->method)) {
427 return PQ_NOT_CONNECTED;
434 mutt_socket_close(pop_data->conn);
439 mutt_error(_("No authenticators available"));
446 * This function calls funct(*line, *data) for each received line,
447 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
450 * -1 - conection lost,
451 * -2 - invalid command or execution error,
452 * -3 - error in funct(*line, *data)
454 static pop_query_status
455 pop_fetch_data(pop_data_t *pop_data, const char *query, progress_t *bar,
456 int (*funct)(char *, void *), void *data)
458 pop_query_status ret;
459 char buf[LONG_STRING];
465 m_strcpy(buf, sizeof(buf), query);
466 ret = _pop_query(pop_data, buf, sizeof(buf));
473 if (mutt_socket_readln2(&inbuf, pop_data->conn) < 0) {
474 pop_data->status = POP_DISCONNECTED;
475 ret = PQ_NOT_CONNECTED;
480 mutt_progress_bar(bar, pos += inbuf.len);
483 if (inbuf.data[0] == '.') {
484 if (inbuf.data[1] != '.')
489 if (funct(inbuf.data + dot, data) < 0) {
491 ret = PFD_FUNCT_ERROR;
495 buffer_reset(&inbuf);
502 static int fetch_capa (char *line, void *data)
504 pop_data_t *pop_data = (pop_data_t *) data;
507 if (!ascii_strncasecmp (line, "SASL", 4)) {
508 p_delete(&pop_data->auth_list);
509 c = vskipspaces(line + 4);
510 pop_data->auth_list = m_strdup(c);
513 else if (!ascii_strncasecmp (line, "STLS", 4))
514 pop_data->cmd_stls = CMD_AVAILABLE;
516 else if (!ascii_strncasecmp (line, "UIDL", 4))
517 pop_data->cmd_uidl = CMD_AVAILABLE;
519 else if (!ascii_strncasecmp (line, "TOP", 3))
520 pop_data->cmd_top = CMD_AVAILABLE;
525 static int fetch_auth (char *line, void *data)
527 pop_data_t *pop_data = (pop_data_t *) data;
528 ssize_t auth_list_len;
530 if (!pop_data->auth_list) {
531 auth_list_len = m_strlen(line) + 1;
532 pop_data->auth_list = p_new(char, auth_list_len);
534 auth_list_len = m_strlen(pop_data->auth_list) + m_strlen(line) + 2;
535 p_realloc(&pop_data->auth_list, auth_list_len);
536 m_strcat(pop_data->auth_list, auth_list_len, " ");
538 m_strcat(pop_data->auth_list, auth_list_len, line);
546 * -1 - conection lost,
547 * -2 - execution error.
549 static pop_query_status pop_capabilities(pop_data_t * pop_data, int mode)
551 /* don't check capabilities on reconnect */
552 if (pop_data->capabilities)
555 /* init capabilities */
557 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
558 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
559 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
560 pop_data->cmd_top = CMD_NOT_AVAILABLE;
561 pop_data->cmd_user = CMD_NOT_AVAILABLE;
562 pop_data->resp_codes = 0;
563 pop_data->expire = 1;
564 p_delete(&pop_data->auth_list);
567 /* Execute CAPA command */
568 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
569 switch (pop_fetch_data(pop_data, "CAPA\r\n", NULL, fetch_capa, pop_data)) {
571 pop_data->cmd_capa = CMD_AVAILABLE;
573 case PFD_FUNCT_ERROR:
575 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
577 case PQ_NOT_CONNECTED:
578 return PQ_NOT_CONNECTED;
582 /* CAPA not supported, use defaults */
583 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
584 pop_data->cmd_uidl = CMD_UNKNOWN;
585 pop_data->cmd_top = CMD_UNKNOWN;
587 if (pop_fetch_data(pop_data, "AUTH\r\n", NULL, fetch_auth, pop_data) ==
589 return PQ_NOT_CONNECTED;
592 /* Check capabilities */
594 const char *msg = NULL;
596 if (!pop_data->expire)
597 msg = _("Unable to leave messages on server.");
598 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
599 msg = _("Command TOP is not supported by server.");
600 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
601 msg = _("Command UIDL is not supported by server.");
602 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
606 pop_data->capabilities = 1;
612 /* given an POP mailbox name, return host, port, username and password */
613 static int pop_parse_path (const char *path, ACCOUNT * act)
621 act->port = POP_PORT;
622 act->type = M_ACCT_TYPE_POP;
625 url_parse_ciss (&url, c);
627 if (url.scheme == U_POP || url.scheme == U_POPS) {
628 if (url.scheme == U_POPS) {
630 act->port = POP_SSL_PORT;
633 if ((!url.path || !*url.path) && mutt_account_fromurl (act, &url) == 0)
642 * Open connection and authenticate
644 * -1 - conection lost,
645 * -2 - invalid command or execution error,
646 * -3 - authentication canceled.
648 static pop_query_status pop_open_connection (pop_data_t * pop_data)
650 pop_query_status ret;
652 char buf[LONG_STRING];
654 ret = pop_connect(pop_data);
660 ret = pop_capabilities (pop_data, 0);
661 if (ret == PQ_NOT_CONNECTED)
668 /* Attempt STLS if available and desired. */
669 if (!pop_data->conn->ssf && (pop_data->cmd_stls || mod_ssl.force_tls)) {
670 if (mod_ssl.force_tls)
671 pop_data->use_stls = 2;
672 if (pop_data->use_stls == 0) {
673 pop_data->use_stls = 1;
674 if (mod_ssl.starttls)
675 pop_data->use_stls = 2;
677 if (pop_data->use_stls == 2) {
678 ret = pop_query(pop_data, buf, sizeof(buf), "STLS");
679 if (ret == PQ_NOT_CONNECTED)
682 mutt_error ("%s", pop_data->err_msg);
685 else if (mutt_ssl_starttls (pop_data->conn))
687 mutt_error (_("Could not negotiate TLS connection"));
692 /* recheck capabilities after STLS completes */
693 ret = pop_capabilities (pop_data, 1);
694 if (ret == PQ_NOT_CONNECTED)
704 if (mod_ssl.force_tls && !pop_data->conn->ssf) {
705 mutt_error _("Encrypted connection unavailable");
710 ret = pop_authenticate (pop_data);
711 if (ret == PQ_NOT_CONNECTED)
713 if (ret == PFD_FUNCT_ERROR)
718 /* recheck capabilities after authentication */
719 ret = pop_capabilities (pop_data, 2);
720 if (ret == PQ_NOT_CONNECTED)
727 /* get total size of mailbox */
728 ret = pop_query(pop_data, buf, sizeof(buf), "STAT");
729 if (ret == PQ_NOT_CONNECTED)
732 mutt_error ("%s", pop_data->err_msg);
737 sscanf (buf, "+OK %u %u", &n, &size);
738 pop_data->size = size;
742 pop_data->status = POP_DISCONNECTED;
743 mutt_error _("Server closed connection!");
746 return PQ_NOT_CONNECTED;
749 /* find message with this UIDL and set refno */
750 static int check_uidl (char *line, void *data)
753 CONTEXT *ctx = (CONTEXT *)data;
755 sscanf (line, "%u %s", &idx, line);
756 for (i = 0; i < ctx->msgcount; i++) {
757 if (!m_strcmp(ctx->hdrs[i]->data, line)) {
758 ctx->hdrs[i]->refno = idx;
766 /* reconnect and verify indexes if connection was lost */
767 static pop_query_status pop_reconnect (CONTEXT * ctx)
769 pop_query_status ret;
770 pop_data_t *pop_data = (pop_data_t *) ctx->data;
773 if (pop_data->status == POP_CONNECTED)
775 if (pop_data->status == POP_BYE)
776 return PQ_NOT_CONNECTED;
779 mutt_socket_close (pop_data->conn);
781 ret = pop_open_connection(pop_data);
785 bar.msg = _("Verifying message indexes...");
787 mutt_progress_bar (&bar, 0);
789 for (i = 0; i < ctx->msgcount; i++)
790 ctx->hdrs[i]->refno = -1;
792 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
794 mutt_error ("%s", pop_data->err_msg);
804 return PQ_NOT_CONNECTED;
806 if (query_quadoption (OPT_POPRECONNECT,
807 _("Connection lost. Reconnect to POP server?")) !=
809 return PQ_NOT_CONNECTED;
813 /* write line to file */
814 static int fetch_message (char *line, void *file)
816 FILE *f = (FILE *) file;
819 if (fputc ('\n', f) == EOF)
829 * -1 - conection lost,
830 * -2 - invalid command or execution error,
831 * -3 - error writing to tempfile
833 static pop_query_status pop_read_header (pop_data_t * pop_data, HEADER * h)
837 pop_query_status ret;
839 char buf[LONG_STRING];
843 mutt_error(_("Could not create temporary file"));
844 return PFD_FUNCT_ERROR;
847 ret = pop_query(pop_data, buf, sizeof(buf), "LIST %d", h->refno);
849 sscanf (buf, "+OK %d %ld", &idx, &length);
851 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
852 ret = pop_fetch_data(pop_data, buf, NULL, fetch_message, f);
854 if (pop_data->cmd_top == CMD_UNKNOWN) {
856 pop_data->cmd_top = CMD_AVAILABLE;
860 pop_data->cmd_top = CMD_NOT_AVAILABLE;
861 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
862 _("Command TOP is not supported by server."));
871 h->env = mutt_read_rfc822_header (f, h, 0, 0);
872 h->content->length = length - h->content->offset + 1;
875 h->content->length--;
876 fgets (buf, sizeof (buf), f);
882 mutt_error ("%s", pop_data->err_msg);
885 case PFD_FUNCT_ERROR:
887 mutt_error _("Can't write header to temporary file!");
891 case PQ_NOT_CONNECTED:
893 mutt_error _("Can't fetch header: Not connected!");
903 static int fetch_uidl (char *line, void *data)
906 CONTEXT *ctx = (CONTEXT *) data;
907 pop_data_t *pop_data = (pop_data_t *) ctx->data;
909 sscanf (line, "%d %s", &idx, line);
910 for (i = 0; i < ctx->msgcount; i++)
911 if (!m_strcmp(line, ctx->hdrs[i]->data))
914 if (i == ctx->msgcount) {
915 if (i >= ctx->hdrmax)
916 mx_alloc_memory (ctx);
919 ctx->hdrs[i] = header_new();
920 ctx->hdrs[i]->data = m_strdup(line);
922 else if (ctx->hdrs[i]->index != idx - 1)
923 pop_data->clear_cache = 1;
925 ctx->hdrs[i]->refno = idx;
926 ctx->hdrs[i]->index = idx - 1;
935 * -1 - conection lost,
936 * -2 - invalid command or execution error,
937 * -3 - error writing to tempfile
939 static int pop_fetch_headers (CONTEXT * ctx)
941 int i, old_count, new_count;
942 pop_query_status ret;
943 pop_data_t *pop_data = (pop_data_t *) ctx->data;
945 time (&pop_data->check_time);
946 pop_data->clear_cache = 0;
948 for (i = 0; i < ctx->msgcount; i++)
949 ctx->hdrs[i]->refno = -1;
951 old_count = ctx->msgcount;
952 ret = pop_fetch_data(pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
953 new_count = ctx->msgcount;
954 ctx->msgcount = old_count;
956 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
958 pop_data->cmd_uidl = CMD_AVAILABLE;
961 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
962 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
964 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
965 _("Command UIDL is not supported by server."));
970 for (i = 0; i < old_count; i++)
971 if (ctx->hdrs[i]->refno == -1)
972 ctx->hdrs[i]->deleted = 1;
974 for (i = old_count; i < new_count; i++) {
975 mutt_message (_("Fetching message headers... [%d/%d]"),
976 i + 1 - old_count, new_count - old_count);
978 ret = pop_read_header (pop_data, ctx->hdrs[i]);
986 mx_update_context (ctx, i - old_count);
990 for (i = ctx->msgcount; i < new_count; i++)
991 header_delete(&ctx->hdrs[i]);
996 return new_count - old_count;
999 /* delete all cached messages */
1000 static void pop_clear_cache (pop_data_t * pop_data)
1004 if (!pop_data->clear_cache)
1007 for (i = 0; i < POP_CACHE_LEN; i++) {
1008 if (pop_data->cache[i].path) {
1009 unlink (pop_data->cache[i].path);
1010 p_delete(&pop_data->cache[i].path);
1015 /* pop_mx functions {{{ */
1017 static int pop_is_magic(const char* path, struct stat* st)
1019 url_scheme_t s = url_check_scheme(NONULL(path));
1020 return s == U_POP || s == U_POPS ? M_POP : -1;
1023 static int pop_open_mailbox (CONTEXT * ctx)
1026 char buf[LONG_STRING];
1029 pop_data_t *pop_data;
1032 if (pop_parse_path (ctx->path, &act)) {
1033 mutt_error (_("%s is an invalid POP path"), ctx->path);
1038 mutt_account_tourl (&act, &url);
1040 url_ciss_tostring (&url, buf, sizeof (buf), 0);
1041 conn = mutt_conn_find (NULL, &act);
1045 p_delete(&ctx->path);
1046 ctx->path = m_strdup(buf);
1048 pop_data = p_new(pop_data_t, 1);
1049 pop_data->conn = conn;
1050 ctx->data = pop_data;
1052 if (pop_open_connection(pop_data) != PQ_OK)
1055 conn->data = pop_data;
1058 if (pop_reconnect (ctx) != PQ_OK)
1061 mutt_message _("Fetching list of messages...");
1062 ctx->size = pop_data->size;
1063 ret = pop_fetch_headers (ctx);
1075 static int pop_acl_check(CONTEXT *ctx, int bit)
1078 case ACL_DELETE: /* (un)deletion */
1079 case ACL_SEEN: /* mark as read */
1081 case ACL_INSERT: /* editing messages */
1082 case ACL_WRITE: /* change importance */
1088 static int pop_check_mailbox(CONTEXT * ctx, int *index_hint, int unused)
1091 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1093 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
1098 mutt_socket_close (pop_data->conn);
1100 if (pop_open_connection (pop_data) < 0)
1103 mutt_message _("Checking for new messages...");
1104 ctx->size = pop_data->size;
1105 ret = pop_fetch_headers (ctx);
1106 pop_clear_cache (pop_data);
1117 static void pop_close_mailbox (CONTEXT * ctx)
1119 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1126 if (pop_data->status != POP_NONE)
1127 mutt_socket_close (pop_data->conn);
1129 pop_data->status = POP_NONE;
1131 pop_data->clear_cache = 1;
1132 pop_clear_cache (pop_data);
1134 if (!pop_data->conn->data)
1135 mutt_socket_free (pop_data->conn);
1140 static int pop_sync_mailbox(CONTEXT * ctx, int unused, int *index_hint)
1143 pop_query_status ret;
1144 char buf[LONG_STRING];
1145 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1147 pop_data->check_time = 0;
1150 if (pop_reconnect (ctx) != PQ_OK)
1151 return PQ_NOT_CONNECTED;
1153 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
1155 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
1156 if (ctx->hdrs[i]->deleted) {
1157 ret = pop_query(pop_data, buf, sizeof(buf), "DELE %d",
1158 ctx->hdrs[i]->refno);
1163 ret = pop_query(pop_data, buf, sizeof(buf), "QUIT");
1167 pop_data->clear_cache = 1;
1168 pop_clear_cache (pop_data);
1169 pop_data->status = POP_DISCONNECTED;
1173 if (ret == PQ_ERR) {
1174 mutt_error ("%s", pop_data->err_msg);
1176 return PQ_NOT_CONNECTED;
1181 /* fetch message from POP server */
1182 static int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1186 char buf[LONG_STRING];
1187 char path[_POSIX_PATH_MAX];
1189 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1191 HEADER *h = ctx->hdrs[msgno];
1193 /* see if we already have the message in our cache */
1194 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
1197 if (cache->index == h->index) {
1198 /* yes, so just return a pointer to the message */
1199 msg->fp = fopen (cache->path, "r");
1203 mutt_perror (cache->path);
1208 /* clear the previous entry */
1209 unlink (cache->path);
1210 p_delete(&cache->path);
1215 if (pop_reconnect (ctx) != PQ_OK)
1218 /* verify that massage index is correct */
1221 _("The message index is incorrect. Try reopening the mailbox.");
1226 bar.size = h->content->length + h->content->offset - 1;
1227 bar.msg = _("Fetching message...");
1228 mutt_progress_bar (&bar, 0);
1230 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
1232 mutt_error(_("Could not create temporary file"));
1237 snprintf(buf, sizeof(buf), "RETR %d\r\n", h->refno);
1238 ret = pop_fetch_data(pop_data, buf, &bar, fetch_message, msg->fp);
1245 if (ret == PQ_ERR) {
1246 mutt_error ("%s", pop_data->err_msg);
1251 if (ret == PFD_FUNCT_ERROR) {
1252 mutt_error _("Can't write message to temporary file!");
1259 /* Update the header information. Previously, we only downloaded a
1260 * portion of the headers, those required for the main display.
1262 cache->index = h->index;
1263 cache->path = m_strdup(path);
1266 envelope_delete(&h->env);
1267 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
1270 fgets (buf, sizeof (buf), msg->fp);
1271 while (!feof (msg->fp)) {
1272 ctx->hdrs[msgno]->lines++;
1273 fgets (buf, sizeof (buf), msg->fp);
1276 h->content->length = ftello (msg->fp) - h->content->offset;
1278 /* This needs to be done in case this is a multipart message */
1279 h->security = crypt_query (h->content);
1281 mutt_clear_error ();
1289 mx_t const pop_mx = {
1305 /* public API {{{ */
1307 void pop_fetch_mail (void)
1309 char buffer[LONG_STRING];
1310 char msgbuf[STRING];
1312 int i, delanswer, last = 0, msgs, bytes, rset = 0;
1313 pop_query_status ret;
1316 MESSAGE *msg = NULL;
1318 pop_data_t *pop_data;
1321 if (m_strisempty(PopHost)) {
1322 mutt_error _("POP host is not defined.");
1326 plen = m_strlen(PopHost) + 7;
1327 url = p = p_new(char, plen);
1328 if (url_check_scheme (PopHost) == U_UNKNOWN) {
1329 snprintf(p, plen, "pop://%s", PopHost);
1331 m_strcpy(p, plen, PopHost);
1334 ret = pop_parse_path (url, &act);
1337 mutt_error (_("%s is an invalid POP path"), PopHost);
1341 conn = mutt_conn_find (NULL, &act);
1345 pop_data = p_new(pop_data_t, 1);
1346 pop_data->conn = conn;
1348 if (pop_open_connection (pop_data) < 0) {
1349 mutt_socket_free (pop_data->conn);
1350 p_delete(&pop_data);
1354 conn->data = pop_data;
1356 mutt_message _("Checking for new messages...");
1358 /* find out how many messages are in the mailbox. */
1359 ret = pop_query(pop_data, buffer, sizeof(buffer), "STAT");
1360 if (ret == PQ_NOT_CONNECTED)
1362 if (ret == PQ_ERR) {
1363 mutt_error ("%s", pop_data->err_msg);
1367 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
1369 /* only get unread messages */
1370 if (msgs > 0 && option (OPTPOPLAST)) {
1371 ret = pop_query(pop_data, buffer, sizeof(buffer), "LAST");
1372 if (ret == PQ_NOT_CONNECTED)
1375 sscanf (buffer, "+OK %d", &last);
1379 mutt_message _("No new mail in POP mailbox.");
1384 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
1388 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
1390 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
1392 mutt_message ("%s", msgbuf);
1394 for (i = last + 1; i <= msgs; i++) {
1395 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
1398 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
1399 ret = pop_fetch_data(pop_data, buffer, NULL, fetch_message, msg->fp);
1400 if (ret == PFD_FUNCT_ERROR)
1403 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
1405 ret = PFD_FUNCT_ERROR;
1408 mx_close_message (&msg);
1411 if (ret == PQ_OK && delanswer == M_YES) {
1412 ret = pop_query(pop_data, buffer, sizeof(buffer), "DELE %d", i);
1415 if (ret == PQ_NOT_CONNECTED) {
1416 mx_close_mailbox (&ctx, NULL);
1419 if (ret == PQ_ERR) {
1420 mutt_error ("%s", pop_data->err_msg);
1423 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
1424 mutt_error _("Error while writing mailbox!");
1429 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
1433 mx_close_mailbox (&ctx, NULL);
1436 /* make sure no messages get deleted */
1437 if (pop_query(pop_data, buffer, sizeof(buffer), "RSET") ==
1443 if (pop_query(pop_data, buffer, sizeof(buffer), "QUIT") == PQ_NOT_CONNECTED)
1445 mutt_socket_close (conn);
1446 p_delete(&pop_data);
1450 mutt_error _("Server closed connection!");
1451 mutt_socket_close (conn);
1452 p_delete(&pop_data);