2 * Copyright notice from original mutt:
3 * Copyright (C) 2000-2003 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.
20 # include "mutt_ssl.h"
25 #include "lib/debug.h"
32 /* given an POP mailbox name, return host, port, username and password */
33 int pop_parse_path (const char *path, ACCOUNT * acct)
41 acct->port = POP_PORT;
42 acct->type = M_ACCT_TYPE_POP;
45 url_parse_ciss (&url, c);
47 if (url.scheme == U_POP || url.scheme == U_POPS) {
48 if (url.scheme == U_POPS) {
49 acct->flags |= M_ACCT_SSL;
50 acct->port = POP_SSL_PORT;
53 if ((!url.path || !*url.path) && mutt_account_fromurl (acct, &url) == 0)
61 /* Copy error message to err_msg buffer */
62 void pop_error (POP_DATA * pop_data, char *msg)
66 t = strchr (pop_data->err_msg, '\0');
69 if (!str_ncmp (msg, "-ERR ", 5)) {
77 strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
78 str_skip_trailws (pop_data->err_msg);
81 /* Parse CAPA output */
82 static int fetch_capa (char *line, void *data)
84 POP_DATA *pop_data = (POP_DATA *) data;
87 if (!ascii_strncasecmp (line, "SASL", 4)) {
88 mem_free (&pop_data->auth_list);
91 pop_data->auth_list = str_dup (c);
94 else if (!ascii_strncasecmp (line, "STLS", 4))
95 pop_data->cmd_stls = CMD_AVAILABLE;
97 else if (!ascii_strncasecmp (line, "USER", 4))
98 pop_data->cmd_user = CMD_AVAILABLE;
100 else if (!ascii_strncasecmp (line, "UIDL", 4))
101 pop_data->cmd_uidl = CMD_AVAILABLE;
103 else if (!ascii_strncasecmp (line, "TOP", 3))
104 pop_data->cmd_top = CMD_AVAILABLE;
109 /* Fetch list of the authentication mechanisms */
110 static int fetch_auth (char *line, void *data)
112 POP_DATA *pop_data = (POP_DATA *) data;
114 if (!pop_data->auth_list) {
115 pop_data->auth_list = mem_malloc (strlen (line) + 1);
116 *pop_data->auth_list = '\0';
119 mem_realloc (&pop_data->auth_list,
120 strlen (pop_data->auth_list) + strlen (line) + 2);
121 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
123 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
131 * -1 - conection lost,
132 * -2 - execution error.
134 static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode)
136 char buf[LONG_STRING];
138 /* don't check capabilities on reconnect */
139 if (pop_data->capabilities)
142 /* init capabilities */
144 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
145 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
146 pop_data->cmd_user = CMD_NOT_AVAILABLE;
147 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
148 pop_data->cmd_top = CMD_NOT_AVAILABLE;
149 pop_data->resp_codes = 0;
150 pop_data->expire = 1;
151 pop_data->login_delay = 0;
152 mem_free (&pop_data->auth_list);
155 /* Execute CAPA command */
156 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
157 strfcpy (buf, "CAPA\r\n", sizeof (buf));
158 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
161 pop_data->cmd_capa = CMD_AVAILABLE;
166 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
169 case PQ_NOT_CONNECTED:
170 return PQ_NOT_CONNECTED;
174 /* CAPA not supported, use defaults */
175 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
176 pop_data->cmd_user = CMD_UNKNOWN;
177 pop_data->cmd_uidl = CMD_UNKNOWN;
178 pop_data->cmd_top = CMD_UNKNOWN;
180 strfcpy (buf, "AUTH\r\n", sizeof (buf));
181 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
182 return PQ_NOT_CONNECTED;
185 /* Check capabilities */
189 if (!pop_data->expire)
190 msg = _("Unable to leave messages on server.");
191 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
192 msg = _("Command TOP is not supported by server.");
193 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
194 msg = _("Command UIDL is not supported by server.");
195 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
199 pop_data->capabilities = 1;
208 * -1 - conection lost,
209 * -2 - invalid response.
211 pop_query_status pop_connect (POP_DATA * pop_data)
213 char buf[LONG_STRING];
215 pop_data->status = POP_NONE;
216 if (mutt_socket_open (pop_data->conn) < 0 ||
217 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
218 mutt_error (_("Error connecting to server: %s"),
219 pop_data->conn->account.host);
220 return PQ_NOT_CONNECTED;
223 pop_data->status = POP_CONNECTED;
225 if (str_ncmp (buf, "+OK", 3)) {
226 *pop_data->err_msg = '\0';
227 pop_error (pop_data, buf);
228 mutt_error ("%s", pop_data->err_msg);
232 pop_apop_timestamp (pop_data, buf);
238 * Open connection and authenticate
240 * -1 - conection lost,
241 * -2 - invalid command or execution error,
242 * -3 - authentication canceled.
244 pop_query_status pop_open_connection (POP_DATA * pop_data)
246 pop_query_status ret;
247 unsigned int n, size;
248 char buf[LONG_STRING];
250 ret = pop_connect (pop_data);
256 ret = pop_capabilities (pop_data, 0);
257 if (ret == PQ_NOT_CONNECTED)
264 #if (defined(USE_SSL) || defined(USE_GNUTLS))
265 /* Attempt STLS if available and desired. */
266 if (!pop_data->conn->ssf && (pop_data->cmd_stls || option(OPTSSLFORCETLS))) {
267 if (option (OPTSSLFORCETLS))
268 pop_data->use_stls = 2;
269 if (pop_data->use_stls == 0) {
270 ret = query_quadoption (OPT_SSLSTARTTLS,
271 _("Secure connection with TLS?"));
274 pop_data->use_stls = 1;
276 pop_data->use_stls = 2;
278 if (pop_data->use_stls == 2) {
279 strfcpy (buf, "STLS\r\n", sizeof (buf));
280 ret = pop_query (pop_data, buf, sizeof (buf));
281 if (ret == PQ_NOT_CONNECTED)
284 mutt_error ("%s", pop_data->err_msg);
288 else if (mutt_ssl_starttls (pop_data->conn))
290 else if (mutt_gnutls_starttls (pop_data->conn))
293 mutt_error (_("Could not negotiate TLS connection"));
298 /* recheck capabilities after STLS completes */
299 ret = pop_capabilities (pop_data, 1);
300 if (ret == PQ_NOT_CONNECTED)
310 if (option(OPTSSLFORCETLS) && !pop_data->conn->ssf) {
311 mutt_error _("Encrypted connection unavailable");
317 ret = pop_authenticate (pop_data);
318 if (ret == PQ_NOT_CONNECTED)
320 if (ret == PFD_FUNCT_ERROR)
325 /* recheck capabilities after authentication */
326 ret = pop_capabilities (pop_data, 2);
327 if (ret == PQ_NOT_CONNECTED)
334 /* get total size of mailbox */
335 strfcpy (buf, "STAT\r\n", sizeof (buf));
336 ret = pop_query (pop_data, buf, sizeof (buf));
337 if (ret == PQ_NOT_CONNECTED)
340 mutt_error ("%s", pop_data->err_msg);
345 sscanf (buf, "+OK %u %u", &n, &size);
346 pop_data->size = size;
350 pop_data->status = POP_DISCONNECTED;
351 mutt_error _("Server closed connection!");
354 return PQ_NOT_CONNECTED;
357 /* logout from POP server */
358 void pop_logout (CONTEXT * ctx)
360 pop_query_status ret = 0;
361 char buf[LONG_STRING];
362 POP_DATA *pop_data = (POP_DATA *) ctx->data;
364 if (pop_data->status == POP_CONNECTED) {
365 mutt_message _("Closing connection to POP server...");
368 strfcpy (buf, "RSET\r\n", sizeof (buf));
369 ret = pop_query (pop_data, buf, sizeof (buf));
372 if (ret != PQ_NOT_CONNECTED) {
373 strfcpy (buf, "QUIT\r\n", sizeof (buf));
374 pop_query (pop_data, buf, sizeof (buf));
380 pop_data->status = POP_DISCONNECTED;
385 * Send data from buffer and receive answer to the same buffer
387 * -1 - conection lost,
388 * -2 - invalid command or execution error.
390 pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, char *msg)
392 int dbg = M_SOCK_LOG_CMD;
395 if (pop_data->status != POP_CONNECTED)
396 return PQ_NOT_CONNECTED;
399 /* print msg instaed of real command */
401 dbg = M_SOCK_LOG_FULL;
402 debug_print (M_SOCK_LOG_CMD, ("> %s", msg));
406 mutt_socket_write_d (pop_data->conn, buf, dbg);
408 c = strpbrk (buf, " \r\n");
410 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
412 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
413 pop_data->status = POP_DISCONNECTED;
414 return PQ_NOT_CONNECTED;
416 if (!str_ncmp (buf, "+OK", 3))
419 pop_error (pop_data, buf);
424 * This function calls funct(*line, *data) for each received line,
425 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
428 * -1 - conection lost,
429 * -2 - invalid command or execution error,
430 * -3 - error in funct(*line, *data)
432 pop_query_status pop_fetch_data (POP_DATA * pop_data, char *query, char *msg,
433 int (*funct) (char *, void *), void *data)
435 char buf[LONG_STRING];
438 pop_query_status ret;
442 strfcpy (buf, query, sizeof (buf));
443 ret = pop_query (pop_data, buf, sizeof (buf));
447 inbuf = mem_malloc (sizeof (buf));
451 mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
454 pop_data->status = POP_DISCONNECTED;
455 ret = PQ_NOT_CONNECTED;
460 if (!lenbuf && buf[0] == '.') {
466 strfcpy (inbuf + lenbuf, p, sizeof (buf));
468 if (chunk >= sizeof (buf)) {
469 lenbuf += strlen (p);
473 if (msg && ReadInc && (line % ReadInc == 0))
474 mutt_message ("%s %d", msg, line);
475 if (ret == 0 && funct (inbuf, data) < 0)
476 ret = PFD_FUNCT_ERROR;
480 mem_realloc (&inbuf, lenbuf + sizeof (buf));
487 /* find message with this UIDL and set refno */
488 static int check_uidl (char *line, void *data)
492 CONTEXT *ctx = (CONTEXT *) data;
494 sscanf (line, "%u %s", &index, line);
495 for (i = 0; i < ctx->msgcount; i++) {
496 if (!str_cmp (ctx->hdrs[i]->data, line)) {
497 ctx->hdrs[i]->refno = index;
505 /* reconnect and verify indexes if connection was lost */
506 pop_query_status pop_reconnect (CONTEXT * ctx)
508 pop_query_status ret;
509 POP_DATA *pop_data = (POP_DATA *) ctx->data;
511 if (pop_data->status == POP_CONNECTED)
513 if (pop_data->status == POP_BYE)
514 return PQ_NOT_CONNECTED;
517 mutt_socket_close (pop_data->conn);
519 ret = pop_open_connection (pop_data);
521 char *msg = _("Verifying message indexes...");
524 for (i = 0; i < ctx->msgcount; i++)
525 ctx->hdrs[i]->refno = -1;
529 ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
531 mutt_error ("%s", pop_data->err_msg);
541 return PQ_NOT_CONNECTED;
543 if (query_quadoption (OPT_POPRECONNECT,
544 _("Connection lost. Reconnect to POP server?")) !=
546 return PQ_NOT_CONNECTED;