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 cmd_status cmd_capa : 2; /* optional command CAPA */
68 cmd_status cmd_stls : 2; /* optional command STLS */
69 cmd_status cmd_uidl : 2; /* optional command UIDL */
70 cmd_status cmd_top : 2; /* optional command TOP */
71 cmd_status cmd_user : 2; /* optional command USER */
72 unsigned clear_cache : 1;
76 char *auth_list; /* list of auth mechanisms */
78 char err_msg[POP_CMD_RESPONSE];
79 POP_CACHE cache[POP_CACHE_LEN];
82 /* pop low level functions {{{ */
84 static void pop_error(pop_data_t *pop_data, const char *msg)
86 if (!m_strncmp(msg, "-ERR ", 5)) {
87 const char *s = skipspaces(msg + 5);
92 m_strcat(pop_data->err_msg, sizeof(pop_data->err_msg), msg);
93 m_strrtrim(pop_data->err_msg);
97 * Send data from buffer and receive answer to the same buffer
99 * -1 - conection lost,
100 * -2 - invalid command or execution error.
102 static pop_query_status _pop_query(pop_data_t *pop_data, char *buf, ssize_t buflen)
104 if (pop_data->status != POP_CONNECTED)
105 return PQ_NOT_CONNECTED;
107 mutt_socket_write(pop_data->conn, buf);
108 snprintf(pop_data->err_msg, sizeof(pop_data->err_msg), "%.*s: ",
109 (int)(strpbrk(buf, " \r\n") - buf), buf);
111 if (mutt_socket_readln(buf, buflen, pop_data->conn) < 0) {
112 pop_data->status = POP_DISCONNECTED;
113 return PQ_NOT_CONNECTED;
115 if (!m_strncmp(buf, "+OK", 3))
118 pop_error(pop_data, buf);
121 #define pop_query(pd, b, l, fmt, ...) \
122 (snprintf(b, l, fmt "\r\n", ##__VA_ARGS__), _pop_query(pd, b, l))
127 * -1 - conection lost,
128 * -2 - invalid response.
130 static pop_query_status pop_connect(pop_data_t * pop_data)
132 char buf[LONG_STRING];
135 if (mutt_socket_open(pop_data->conn) < 0
136 || mutt_socket_readln(buf, sizeof(buf), pop_data->conn) < 0)
138 mutt_error(_("Error connecting to server: %s"),
139 pop_data->conn->account.host);
140 pop_data->status = POP_NONE;
141 return PQ_NOT_CONNECTED;
144 pop_data->status = POP_CONNECTED;
145 if (m_strncmp(buf, "+OK", 3)) {
146 pop_data->err_msg[0] = '\0';
147 pop_error(pop_data, buf);
148 mutt_error("%s", pop_data->err_msg);
152 p_delete(&pop_data->timestamp);
153 if ((p = strchr(buf, '<')) && (q = strchr(p, '>'))) {
154 pop_data->timestamp = p_dupstr(p, q + 1 - p);
159 static void pop_logout (CONTEXT * ctx)
161 pop_query_status ret = 0;
162 char buf[LONG_STRING];
163 pop_data_t *pop_data = (pop_data_t *) ctx->data;
165 if (pop_data->status == POP_CONNECTED) {
166 mutt_message _("Closing connection to POP server...");
169 ret = pop_query(pop_data, buf, sizeof(buf), "RSET");
172 if (ret != PQ_NOT_CONNECTED) {
173 pop_query(pop_data, buf, sizeof(buf), "QUIT");
179 pop_data->status = POP_DISCONNECTED;
184 /* Authentication {{{ */
186 static pop_auth_res_t pop_auth_sasl(pop_data_t *pop_data, const char *method)
188 sasl_conn_t *saslconn;
189 sasl_interact_t *interaction = NULL;
191 char buf[LONG_STRING], inbuf[LONG_STRING];
192 const char *mech, *pc = NULL;
193 unsigned int len, olen;
194 unsigned char client_start;
197 if (mutt_sasl_client_new(pop_data->conn, &saslconn) < 0) {
198 return POP_A_FAILURE;
202 method = pop_data->auth_list;
205 rc = sasl_client_start(saslconn, method, &interaction, &pc, &olen,
207 if (rc != SASL_INTERACT)
209 mutt_sasl_interact(interaction);
212 if (rc != SASL_OK && rc != SASL_CONTINUE) {
213 return POP_A_UNAVAIL;
216 client_start = (olen > 0);
217 mutt_message(_("Authenticating (SASL)..."));
218 snprintf(buf, sizeof (buf), "AUTH %s", mech);
221 /* looping protocol */
223 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
224 mutt_socket_write(pop_data->conn, buf);
225 if (mutt_socket_readln(inbuf, sizeof(inbuf), pop_data->conn) < 0) {
226 sasl_dispose(&saslconn);
227 pop_data->status = POP_DISCONNECTED;
231 if (rc != SASL_CONTINUE)
234 if (!m_strncmp(inbuf, "+ ", 2)
235 && sasl_decode64(inbuf, strlen(inbuf),
236 buf, sizeof(buf) - 1, &len) != SASL_OK)
243 rc = sasl_client_step(saslconn, buf, len, &interaction, &pc,
245 if (rc != SASL_INTERACT)
247 mutt_sasl_interact(interaction);
253 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
256 /* send out response, or line break if none needed */
258 if (sasl_encode64(pc, olen, buf, sizeof(buf), &olen) != SASL_OK) {
261 p_delete((char **)&pc);
268 if (!m_strncmp(inbuf, "+OK", 3)) {
269 mutt_sasl_setup_conn(pop_data->conn, saslconn);
270 return POP_A_SUCCESS;
274 sasl_dispose(&saslconn);
275 p_delete((char **)&pc);
277 /* terminate SASL session if the last responce is not +OK nor -ERR */
278 if (!m_strncmp(inbuf, "+ ", 2)) {
279 if (pop_query(pop_data, buf, sizeof(buf), "*") == PQ_NOT_CONNECTED)
283 mutt_error(_("SASL authentication failed."));
286 return POP_A_FAILURE;
289 static pop_auth_res_t pop_auth_apop(pop_data_t *pop_data, const char *method)
292 unsigned char digest[16];
294 char buf[LONG_STRING];
297 if (!pop_data->timestamp)
298 return POP_A_UNAVAIL;
300 mutt_message _("Authenticating (APOP)...");
302 /* Compute the authentication hash to send to the server */
304 MD5Update(&mdContext, (unsigned char *)pop_data->timestamp,
305 strlen(pop_data->timestamp));
306 MD5Update(&mdContext, (unsigned char *)pop_data->conn->account.pass,
307 strlen(pop_data->conn->account.pass));
308 MD5Final(digest, &mdContext);
310 for (i = 0; i < countof(digest); i++)
311 sprintf(hash + 2 * i, "%02x", digest[i]);
313 switch (pop_query(pop_data, buf, sizeof(buf), "APOP %s %s",
314 pop_data->conn->account.user, hash))
317 return POP_A_SUCCESS;
318 case PQ_NOT_CONNECTED:
321 mutt_error("%s %s", _("APOP authentication failed."), pop_data->err_msg);
323 return POP_A_FAILURE;
327 static pop_auth_res_t pop_auth_user(pop_data_t *pop_data, const char *method)
329 char buf[LONG_STRING];
330 pop_query_status ret;
332 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
333 return POP_A_UNAVAIL;
335 mutt_message _("Authenticating (USER)...");
336 ret = pop_query(pop_data, buf, sizeof(buf), "USER %s",
337 pop_data->conn->account.user);
339 if (pop_data->cmd_user == CMD_UNKNOWN) {
341 pop_data->cmd_user = CMD_AVAILABLE;
343 pop_data->cmd_user = CMD_NOT_AVAILABLE;
347 ret = pop_query(pop_data, buf, sizeof(buf), "PASS %s",
348 pop_data->conn->account.pass);
353 return POP_A_SUCCESS;
354 case PQ_NOT_CONNECTED:
357 mutt_error("%s %s", _("USER authentication failed."), pop_data->err_msg);
359 return POP_A_FAILURE;
364 pop_auth_res_t (*do_auth)(pop_data_t *, const char *);
368 static pop_query_status pop_authenticate (pop_data_t * pop_data)
370 static pop_auth_t const pop_authenticators[] = {
371 {pop_auth_sasl, NULL},
372 {pop_auth_apop, "apop"},
373 {pop_auth_user, "user"},
377 ACCOUNT *act = &pop_data->conn->account;
378 const pop_auth_t *auth;
381 if (mutt_account_getuser(act) || !act->user[0]
382 || mutt_account_getpass(act) || !act->pass[0])
384 return PFD_FUNCT_ERROR;
387 if (!m_strisempty(PopAuthenticators)) {
391 for (p = PopAuthenticators; ; p = q) {
397 q = strchrnul(p, ':');
398 m_strncpy(buf, sizeof(buf), p, q - p);
400 for (auth = pop_authenticators; auth->do_auth; auth++) {
401 if (auth->method && ascii_strcasecmp(auth->method, buf))
404 switch (auth->do_auth(pop_data, buf)) {
408 return PQ_NOT_CONNECTED;
415 mutt_socket_close(pop_data->conn);
419 for (auth = pop_authenticators; auth->do_auth; auth++) {
420 switch (auth->do_auth(pop_data, auth->method)) {
424 return PQ_NOT_CONNECTED;
431 mutt_socket_close(pop_data->conn);
436 mutt_error(_("No authenticators available"));
443 * This function calls funct(*line, *data) for each received line,
444 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
447 * -1 - conection lost,
448 * -2 - invalid command or execution error,
449 * -3 - error in funct(*line, *data)
451 static pop_query_status
452 pop_fetch_data(pop_data_t *pop_data, const char *query, progress_t *bar,
453 int (*funct)(char *, void *), void *data)
455 pop_query_status ret;
456 char buf[LONG_STRING];
462 m_strcpy(buf, sizeof(buf), query);
463 ret = _pop_query(pop_data, buf, sizeof(buf));
470 if (mutt_socket_readln2(&inbuf, pop_data->conn) < 0) {
471 pop_data->status = POP_DISCONNECTED;
472 ret = PQ_NOT_CONNECTED;
477 mutt_progress_bar(bar, pos += inbuf.len);
480 if (inbuf.data[0] == '.') {
481 if (inbuf.data[1] != '.')
486 if (funct(inbuf.data + dot, data) < 0) {
488 ret = PFD_FUNCT_ERROR;
492 buffer_reset(&inbuf);
499 static int fetch_capa (char *line, void *data)
501 pop_data_t *pop_data = (pop_data_t *) data;
504 if (!ascii_strncasecmp (line, "SASL", 4)) {
505 p_delete(&pop_data->auth_list);
506 c = vskipspaces(line + 4);
507 pop_data->auth_list = m_strdup(c);
510 else if (!ascii_strncasecmp (line, "STLS", 4))
511 pop_data->cmd_stls = CMD_AVAILABLE;
513 else if (!ascii_strncasecmp (line, "UIDL", 4))
514 pop_data->cmd_uidl = CMD_AVAILABLE;
516 else if (!ascii_strncasecmp (line, "TOP", 3))
517 pop_data->cmd_top = CMD_AVAILABLE;
522 static int fetch_auth (char *line, void *data)
524 pop_data_t *pop_data = (pop_data_t *) data;
525 ssize_t auth_list_len;
527 if (!pop_data->auth_list) {
528 auth_list_len = m_strlen(line) + 1;
529 pop_data->auth_list = p_new(char, auth_list_len);
531 auth_list_len = m_strlen(pop_data->auth_list) + m_strlen(line) + 2;
532 p_realloc(&pop_data->auth_list, auth_list_len);
533 m_strcat(pop_data->auth_list, auth_list_len, " ");
535 m_strcat(pop_data->auth_list, auth_list_len, line);
543 * -1 - conection lost,
544 * -2 - execution error.
546 static pop_query_status pop_capabilities(pop_data_t * pop_data, int mode)
548 /* don't check capabilities on reconnect */
549 if (pop_data->capabilities)
552 /* init capabilities */
554 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
555 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
556 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
557 pop_data->cmd_top = CMD_NOT_AVAILABLE;
558 pop_data->cmd_user = CMD_NOT_AVAILABLE;
559 p_delete(&pop_data->auth_list);
562 /* Execute CAPA command */
563 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
564 switch (pop_fetch_data(pop_data, "CAPA\r\n", NULL, fetch_capa, pop_data)) {
566 pop_data->cmd_capa = CMD_AVAILABLE;
568 case PFD_FUNCT_ERROR:
570 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
572 case PQ_NOT_CONNECTED:
573 return PQ_NOT_CONNECTED;
577 /* CAPA not supported, use defaults */
578 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
579 pop_data->cmd_uidl = CMD_UNKNOWN;
580 pop_data->cmd_top = CMD_UNKNOWN;
582 if (pop_fetch_data(pop_data, "AUTH\r\n", NULL, fetch_auth, pop_data) ==
584 return PQ_NOT_CONNECTED;
587 /* Check capabilities */
589 const char *msg = NULL;
591 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
592 msg = _("Command TOP is not supported by server.");
593 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
594 msg = _("Command UIDL is not supported by server.");
595 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
599 pop_data->capabilities = 1;
605 /* given an POP mailbox name, return host, port, username and password */
606 static int pop_parse_path (const char *path, ACCOUNT * act)
614 act->port = POP_PORT;
615 act->type = M_ACCT_TYPE_POP;
618 url_parse_ciss (&url, c);
620 if (url.scheme == U_POP || url.scheme == U_POPS) {
621 if (url.scheme == U_POPS) {
623 act->port = POP_SSL_PORT;
626 if ((!url.path || !*url.path) && mutt_account_fromurl (act, &url) == 0)
635 * Open connection and authenticate
637 * -1 - conection lost,
638 * -2 - invalid command or execution error,
639 * -3 - authentication canceled.
641 static pop_query_status pop_open_connection (pop_data_t * pop_data)
643 pop_query_status ret;
645 char buf[LONG_STRING];
647 ret = pop_connect(pop_data);
653 ret = pop_capabilities (pop_data, 0);
654 if (ret == PQ_NOT_CONNECTED)
661 /* Attempt STLS if available and desired. */
662 if (!pop_data->conn->ssf && (pop_data->cmd_stls || mod_ssl.force_tls)) {
663 ret = pop_query(pop_data, buf, sizeof(buf), "STLS");
664 if (ret == PQ_NOT_CONNECTED)
667 mutt_error ("%s", pop_data->err_msg);
670 else if (mutt_ssl_starttls (pop_data->conn))
672 mutt_error (_("Could not negotiate TLS connection"));
678 if (mod_ssl.force_tls && !pop_data->conn->ssf) {
679 mutt_error _("Encrypted connection unavailable");
684 ret = pop_authenticate (pop_data);
685 if (ret == PQ_NOT_CONNECTED)
687 if (ret == PFD_FUNCT_ERROR)
692 /* recheck capabilities after authentication */
693 ret = pop_capabilities (pop_data, 1);
694 if (ret == PQ_NOT_CONNECTED)
701 /* get total size of mailbox */
702 ret = pop_query(pop_data, buf, sizeof(buf), "STAT");
703 if (ret == PQ_NOT_CONNECTED)
706 mutt_error ("%s", pop_data->err_msg);
711 sscanf (buf, "+OK %u %u", &n, &size);
712 pop_data->size = size;
716 pop_data->status = POP_DISCONNECTED;
717 mutt_error _("Server closed connection!");
720 return PQ_NOT_CONNECTED;
723 /* find message with this UIDL and set refno */
724 static int check_uidl (char *line, void *data)
727 CONTEXT *ctx = (CONTEXT *)data;
729 sscanf (line, "%u %s", &idx, line);
730 for (i = 0; i < ctx->msgcount; i++) {
731 if (!m_strcmp(ctx->hdrs[i]->data, line)) {
732 ctx->hdrs[i]->refno = idx;
740 /* reconnect and verify indexes if connection was lost */
741 static pop_query_status pop_reconnect (CONTEXT * ctx)
743 pop_query_status ret;
744 pop_data_t *pop_data = (pop_data_t *) ctx->data;
747 if (pop_data->status == POP_CONNECTED)
749 if (pop_data->status == POP_BYE)
750 return PQ_NOT_CONNECTED;
753 mutt_socket_close (pop_data->conn);
755 ret = pop_open_connection(pop_data);
759 bar.msg = _("Verifying message indexes...");
761 mutt_progress_bar (&bar, 0);
763 for (i = 0; i < ctx->msgcount; i++)
764 ctx->hdrs[i]->refno = -1;
766 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
768 mutt_error ("%s", pop_data->err_msg);
778 return PQ_NOT_CONNECTED;
780 if (query_quadoption (OPT_POPRECONNECT,
781 _("Connection lost. Reconnect to POP server?")) !=
783 return PQ_NOT_CONNECTED;
787 /* write line to file */
788 static int fetch_message (char *line, void *file)
790 FILE *f = (FILE *) file;
793 if (fputc ('\n', f) == EOF)
803 * -1 - conection lost,
804 * -2 - invalid command or execution error,
805 * -3 - error writing to tempfile
807 static pop_query_status pop_read_header (pop_data_t * pop_data, HEADER * h)
811 pop_query_status ret;
813 char buf[LONG_STRING];
817 mutt_error(_("Could not create temporary file"));
818 return PFD_FUNCT_ERROR;
821 ret = pop_query(pop_data, buf, sizeof(buf), "LIST %d", h->refno);
823 sscanf (buf, "+OK %d %ld", &idx, &length);
825 snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
826 ret = pop_fetch_data(pop_data, buf, NULL, fetch_message, f);
828 if (pop_data->cmd_top == CMD_UNKNOWN) {
830 pop_data->cmd_top = CMD_AVAILABLE;
834 pop_data->cmd_top = CMD_NOT_AVAILABLE;
835 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
836 _("Command TOP is not supported by server."));
845 h->env = mutt_read_rfc822_header (f, h, 0, 0);
846 h->content->length = length - h->content->offset + 1;
849 h->content->length--;
850 fgets (buf, sizeof (buf), f);
856 mutt_error ("%s", pop_data->err_msg);
859 case PFD_FUNCT_ERROR:
861 mutt_error _("Can't write header to temporary file!");
865 case PQ_NOT_CONNECTED:
867 mutt_error _("Can't fetch header: Not connected!");
877 static int fetch_uidl (char *line, void *data)
880 CONTEXT *ctx = (CONTEXT *) data;
881 pop_data_t *pop_data = (pop_data_t *) ctx->data;
883 sscanf (line, "%d %s", &idx, line);
884 for (i = 0; i < ctx->msgcount; i++)
885 if (!m_strcmp(line, ctx->hdrs[i]->data))
888 if (i == ctx->msgcount) {
889 if (i >= ctx->hdrmax)
890 mx_alloc_memory (ctx);
893 ctx->hdrs[i] = header_new();
894 ctx->hdrs[i]->data = m_strdup(line);
896 else if (ctx->hdrs[i]->index != idx - 1)
897 pop_data->clear_cache = 1;
899 ctx->hdrs[i]->refno = idx;
900 ctx->hdrs[i]->index = idx - 1;
909 * -1 - conection lost,
910 * -2 - invalid command or execution error,
911 * -3 - error writing to tempfile
913 static int pop_fetch_headers (CONTEXT * ctx)
915 int i, old_count, new_count;
916 pop_query_status ret;
917 pop_data_t *pop_data = (pop_data_t *) ctx->data;
919 time (&pop_data->check_time);
920 pop_data->clear_cache = 0;
922 for (i = 0; i < ctx->msgcount; i++)
923 ctx->hdrs[i]->refno = -1;
925 old_count = ctx->msgcount;
926 ret = pop_fetch_data(pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
927 new_count = ctx->msgcount;
928 ctx->msgcount = old_count;
930 if (pop_data->cmd_uidl == CMD_UNKNOWN) {
932 pop_data->cmd_uidl = CMD_AVAILABLE;
935 if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
936 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
938 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
939 _("Command UIDL is not supported by server."));
944 for (i = 0; i < old_count; i++)
945 if (ctx->hdrs[i]->refno == -1)
946 ctx->hdrs[i]->deleted = 1;
948 for (i = old_count; i < new_count; i++) {
949 mutt_message (_("Fetching message headers... [%d/%d]"),
950 i + 1 - old_count, new_count - old_count);
952 ret = pop_read_header (pop_data, ctx->hdrs[i]);
960 mx_update_context (ctx, i - old_count);
964 for (i = ctx->msgcount; i < new_count; i++)
965 header_delete(&ctx->hdrs[i]);
970 return new_count - old_count;
973 /* delete all cached messages */
974 static void pop_clear_cache (pop_data_t * pop_data)
978 if (!pop_data->clear_cache)
981 for (i = 0; i < POP_CACHE_LEN; i++) {
982 if (pop_data->cache[i].path) {
983 unlink (pop_data->cache[i].path);
984 p_delete(&pop_data->cache[i].path);
989 /* pop_mx functions {{{ */
991 static int pop_is_magic(const char* path, struct stat* st)
993 url_scheme_t s = url_check_scheme(NONULL(path));
994 return s == U_POP || s == U_POPS ? M_POP : -1;
997 static int pop_open_mailbox (CONTEXT * ctx)
1000 char buf[LONG_STRING];
1003 pop_data_t *pop_data;
1006 if (pop_parse_path (ctx->path, &act)) {
1007 mutt_error (_("%s is an invalid POP path"), ctx->path);
1012 mutt_account_tourl (&act, &url);
1014 url_ciss_tostring (&url, buf, sizeof (buf), 0);
1015 conn = mutt_conn_find (NULL, &act);
1019 p_delete(&ctx->path);
1020 ctx->path = m_strdup(buf);
1022 pop_data = p_new(pop_data_t, 1);
1023 pop_data->conn = conn;
1024 ctx->data = pop_data;
1026 if (pop_open_connection(pop_data) != PQ_OK)
1029 conn->data = pop_data;
1032 if (pop_reconnect (ctx) != PQ_OK)
1035 mutt_message _("Fetching list of messages...");
1036 ctx->size = pop_data->size;
1037 ret = pop_fetch_headers (ctx);
1049 static int pop_acl_check(CONTEXT *ctx, int bit)
1052 case ACL_DELETE: /* (un)deletion */
1053 case ACL_SEEN: /* mark as read */
1055 case ACL_INSERT: /* editing messages */
1056 case ACL_WRITE: /* change importance */
1062 static int pop_check_mailbox(CONTEXT * ctx, int *index_hint, int unused)
1065 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1067 if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
1072 mutt_socket_close (pop_data->conn);
1074 if (pop_open_connection (pop_data) < 0)
1077 mutt_message _("Checking for new messages...");
1078 ctx->size = pop_data->size;
1079 ret = pop_fetch_headers (ctx);
1080 pop_clear_cache (pop_data);
1091 static void pop_close_mailbox (CONTEXT * ctx)
1093 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1100 if (pop_data->status != POP_NONE)
1101 mutt_socket_close (pop_data->conn);
1103 pop_data->status = POP_NONE;
1105 pop_data->clear_cache = 1;
1106 pop_clear_cache (pop_data);
1108 if (!pop_data->conn->data)
1109 mutt_socket_free (pop_data->conn);
1114 static int pop_sync_mailbox(CONTEXT * ctx, int unused, int *index_hint)
1117 pop_query_status ret;
1118 char buf[LONG_STRING];
1119 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1121 pop_data->check_time = 0;
1124 if (pop_reconnect (ctx) != PQ_OK)
1125 return PQ_NOT_CONNECTED;
1127 mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
1129 for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
1130 if (ctx->hdrs[i]->deleted) {
1131 ret = pop_query(pop_data, buf, sizeof(buf), "DELE %d",
1132 ctx->hdrs[i]->refno);
1137 ret = pop_query(pop_data, buf, sizeof(buf), "QUIT");
1141 pop_data->clear_cache = 1;
1142 pop_clear_cache (pop_data);
1143 pop_data->status = POP_DISCONNECTED;
1147 if (ret == PQ_ERR) {
1148 mutt_error ("%s", pop_data->err_msg);
1150 return PQ_NOT_CONNECTED;
1155 /* fetch message from POP server */
1156 static int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1160 char buf[LONG_STRING];
1161 char path[_POSIX_PATH_MAX];
1163 pop_data_t *pop_data = (pop_data_t *) ctx->data;
1165 HEADER *h = ctx->hdrs[msgno];
1167 /* see if we already have the message in our cache */
1168 cache = &pop_data->cache[h->index % POP_CACHE_LEN];
1171 if (cache->index == h->index) {
1172 /* yes, so just return a pointer to the message */
1173 msg->fp = fopen (cache->path, "r");
1177 mutt_perror (cache->path);
1182 /* clear the previous entry */
1183 unlink (cache->path);
1184 p_delete(&cache->path);
1189 if (pop_reconnect (ctx) != PQ_OK)
1192 /* verify that massage index is correct */
1195 _("The message index is incorrect. Try reopening the mailbox.");
1200 bar.size = h->content->length + h->content->offset - 1;
1201 bar.msg = _("Fetching message...");
1202 mutt_progress_bar (&bar, 0);
1204 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
1206 mutt_error(_("Could not create temporary file"));
1211 snprintf(buf, sizeof(buf), "RETR %d\r\n", h->refno);
1212 ret = pop_fetch_data(pop_data, buf, &bar, fetch_message, msg->fp);
1219 if (ret == PQ_ERR) {
1220 mutt_error ("%s", pop_data->err_msg);
1225 if (ret == PFD_FUNCT_ERROR) {
1226 mutt_error _("Can't write message to temporary file!");
1233 /* Update the header information. Previously, we only downloaded a
1234 * portion of the headers, those required for the main display.
1236 cache->index = h->index;
1237 cache->path = m_strdup(path);
1240 envelope_delete(&h->env);
1241 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
1244 fgets (buf, sizeof (buf), msg->fp);
1245 while (!feof (msg->fp)) {
1246 ctx->hdrs[msgno]->lines++;
1247 fgets (buf, sizeof (buf), msg->fp);
1250 h->content->length = ftello (msg->fp) - h->content->offset;
1252 /* This needs to be done in case this is a multipart message */
1253 h->security = crypt_query (h->content);
1255 mutt_clear_error ();
1263 mx_t const pop_mx = {
1279 /* public API {{{ */
1281 void pop_fetch_mail (void)
1283 char buffer[LONG_STRING];
1284 char msgbuf[STRING];
1286 int i, delanswer, last = 0, msgs, bytes, rset = 0;
1287 pop_query_status ret;
1290 MESSAGE *msg = NULL;
1292 pop_data_t *pop_data;
1295 if (m_strisempty(PopHost)) {
1296 mutt_error _("POP host is not defined.");
1300 plen = m_strlen(PopHost) + 7;
1301 url = p = p_new(char, plen);
1302 if (url_check_scheme (PopHost) == U_UNKNOWN) {
1303 snprintf(p, plen, "pop://%s", PopHost);
1305 m_strcpy(p, plen, PopHost);
1308 ret = pop_parse_path (url, &act);
1311 mutt_error (_("%s is an invalid POP path"), PopHost);
1315 conn = mutt_conn_find (NULL, &act);
1319 pop_data = p_new(pop_data_t, 1);
1320 pop_data->conn = conn;
1322 if (pop_open_connection (pop_data) < 0) {
1323 mutt_socket_free (pop_data->conn);
1324 p_delete(&pop_data);
1328 conn->data = pop_data;
1330 mutt_message _("Checking for new messages...");
1332 /* find out how many messages are in the mailbox. */
1333 ret = pop_query(pop_data, buffer, sizeof(buffer), "STAT");
1334 if (ret == PQ_NOT_CONNECTED)
1336 if (ret == PQ_ERR) {
1337 mutt_error ("%s", pop_data->err_msg);
1341 sscanf (buffer, "+OK %d %d", &msgs, &bytes);
1343 /* only get unread messages */
1344 if (msgs > 0 && option (OPTPOPLAST)) {
1345 ret = pop_query(pop_data, buffer, sizeof(buffer), "LAST");
1346 if (ret == PQ_NOT_CONNECTED)
1349 sscanf (buffer, "+OK %d", &last);
1353 mutt_message _("No new mail in POP mailbox.");
1358 if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
1362 query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
1364 snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
1366 mutt_message ("%s", msgbuf);
1368 for (i = last + 1; i <= msgs; i++) {
1369 if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
1372 snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
1373 ret = pop_fetch_data(pop_data, buffer, NULL, fetch_message, msg->fp);
1374 if (ret == PFD_FUNCT_ERROR)
1377 if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
1379 ret = PFD_FUNCT_ERROR;
1382 mx_close_message (&msg);
1385 if (ret == PQ_OK && delanswer == M_YES) {
1386 ret = pop_query(pop_data, buffer, sizeof(buffer), "DELE %d", i);
1389 if (ret == PQ_NOT_CONNECTED) {
1390 mx_close_mailbox (&ctx, NULL);
1393 if (ret == PQ_ERR) {
1394 mutt_error ("%s", pop_data->err_msg);
1397 if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
1398 mutt_error _("Error while writing mailbox!");
1403 mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
1407 mx_close_mailbox (&ctx, NULL);
1410 /* make sure no messages get deleted */
1411 if (pop_query(pop_data, buffer, sizeof(buffer), "RSET") ==
1417 if (pop_query(pop_data, buffer, sizeof(buffer), "QUIT") == PQ_NOT_CONNECTED)
1419 mutt_socket_close (conn);
1420 p_delete(&pop_data);
1424 mutt_error _("Server closed connection!");
1425 mutt_socket_close (conn);
1426 p_delete(&pop_data);