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.
19 # include "mutt_ssl.h"
29 /* given an POP mailbox name, return host, port, username and password */
30 int pop_parse_path (const char *path, ACCOUNT * acct)
38 acct->port = POP_PORT;
39 acct->type = M_ACCT_TYPE_POP;
41 c = safe_strdup (path);
42 url_parse_ciss (&url, c);
44 if (url.scheme == U_POP || url.scheme == U_POPS) {
45 if (url.scheme == U_POPS) {
46 acct->flags |= M_ACCT_SSL;
47 acct->port = POP_SSL_PORT;
50 if ((!url.path || !*url.path) && mutt_account_fromurl (acct, &url) == 0)
58 /* Copy error message to err_msg buffer */
59 void pop_error (POP_DATA * pop_data, char *msg)
63 t = strchr (pop_data->err_msg, '\0');
66 if (!safe_strncmp (msg, "-ERR ", 5)) {
74 strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
75 mutt_remove_trailing_ws (pop_data->err_msg);
78 /* Parse CAPA output */
79 static int fetch_capa (char *line, void *data)
81 POP_DATA *pop_data = (POP_DATA *) data;
84 if (!ascii_strncasecmp (line, "SASL", 4)) {
85 FREE (&pop_data->auth_list);
88 pop_data->auth_list = safe_strdup (c);
91 else if (!ascii_strncasecmp (line, "STLS", 4))
92 pop_data->cmd_stls = CMD_AVAILABLE;
94 else if (!ascii_strncasecmp (line, "USER", 4))
95 pop_data->cmd_user = CMD_AVAILABLE;
97 else if (!ascii_strncasecmp (line, "UIDL", 4))
98 pop_data->cmd_uidl = CMD_AVAILABLE;
100 else if (!ascii_strncasecmp (line, "TOP", 3))
101 pop_data->cmd_top = CMD_AVAILABLE;
106 /* Fetch list of the authentication mechanisms */
107 static int fetch_auth (char *line, void *data)
109 POP_DATA *pop_data = (POP_DATA *) data;
111 if (!pop_data->auth_list) {
112 pop_data->auth_list = safe_malloc (strlen (line) + 1);
113 *pop_data->auth_list = '\0';
116 safe_realloc (&pop_data->auth_list,
117 strlen (pop_data->auth_list) + strlen (line) + 2);
118 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
120 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
128 * -1 - conection lost,
129 * -2 - execution error.
131 static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode)
133 char buf[LONG_STRING];
135 /* don't check capabilities on reconnect */
136 if (pop_data->capabilities)
139 /* init capabilities */
141 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
142 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
143 pop_data->cmd_user = CMD_NOT_AVAILABLE;
144 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
145 pop_data->cmd_top = CMD_NOT_AVAILABLE;
146 pop_data->resp_codes = 0;
147 pop_data->expire = 1;
148 pop_data->login_delay = 0;
149 FREE (&pop_data->auth_list);
152 /* Execute CAPA command */
153 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
154 strfcpy (buf, "CAPA\r\n", sizeof (buf));
155 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
158 pop_data->cmd_capa = CMD_AVAILABLE;
163 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
166 case PQ_NOT_CONNECTED:
167 return PQ_NOT_CONNECTED;
171 /* CAPA not supported, use defaults */
172 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
173 pop_data->cmd_user = CMD_UNKNOWN;
174 pop_data->cmd_uidl = CMD_UNKNOWN;
175 pop_data->cmd_top = CMD_UNKNOWN;
177 strfcpy (buf, "AUTH\r\n", sizeof (buf));
178 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
179 return PQ_NOT_CONNECTED;
182 /* Check capabilities */
186 if (!pop_data->expire)
187 msg = _("Unable to leave messages on server.");
188 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
189 msg = _("Command TOP is not supported by server.");
190 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
191 msg = _("Command UIDL is not supported by server.");
192 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
196 pop_data->capabilities = 1;
205 * -1 - conection lost,
206 * -2 - invalid response.
208 pop_query_status pop_connect (POP_DATA * pop_data)
210 char buf[LONG_STRING];
212 pop_data->status = POP_NONE;
213 if (mutt_socket_open (pop_data->conn) < 0 ||
214 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
215 mutt_error (_("Error connecting to server: %s"),
216 pop_data->conn->account.host);
217 return PQ_NOT_CONNECTED;
220 pop_data->status = POP_CONNECTED;
222 if (safe_strncmp (buf, "+OK", 3)) {
223 *pop_data->err_msg = '\0';
224 pop_error (pop_data, buf);
225 mutt_error ("%s", pop_data->err_msg);
229 pop_apop_timestamp (pop_data, buf);
235 * Open connection and authenticate
237 * -1 - conection lost,
238 * -2 - invalid command or execution error,
239 * -3 - authentication canceled.
241 pop_query_status pop_open_connection (POP_DATA * pop_data)
243 pop_query_status ret;
244 unsigned int n, size;
245 char buf[LONG_STRING];
247 ret = pop_connect (pop_data);
253 ret = pop_capabilities (pop_data, 0);
254 if (ret == PQ_NOT_CONNECTED)
261 #if (defined(USE_SSL) || defined(USE_GNUTLS)) && !defined(USE_NSS)
262 /* Attempt STLS if available and desired. */
263 if (pop_data->cmd_stls && !pop_data->conn->ssf) {
264 if (pop_data->use_stls == 0) {
265 ret = query_quadoption (OPT_SSLSTARTTLS,
266 _("Secure connection with TLS?"));
269 pop_data->use_stls = 1;
271 pop_data->use_stls = 2;
273 if (pop_data->use_stls == 2) {
274 strfcpy (buf, "STLS\r\n", sizeof (buf));
275 ret = pop_query (pop_data, buf, sizeof (buf));
276 if (ret == PQ_NOT_CONNECTED)
279 mutt_error ("%s", pop_data->err_msg);
283 else if (mutt_ssl_starttls (pop_data->conn))
285 else if (mutt_gnutls_starttls (pop_data->conn))
288 mutt_error (_("Could not negotiate TLS connection"));
293 /* recheck capabilities after STLS completes */
294 ret = pop_capabilities (pop_data, 1);
295 if (ret == PQ_NOT_CONNECTED)
306 ret = pop_authenticate (pop_data);
307 if (ret == PQ_NOT_CONNECTED)
309 if (ret == PFD_FUNCT_ERROR)
314 /* recheck capabilities after authentication */
315 ret = pop_capabilities (pop_data, 2);
316 if (ret == PQ_NOT_CONNECTED)
323 /* get total size of mailbox */
324 strfcpy (buf, "STAT\r\n", sizeof (buf));
325 ret = pop_query (pop_data, buf, sizeof (buf));
326 if (ret == PQ_NOT_CONNECTED)
329 mutt_error ("%s", pop_data->err_msg);
334 sscanf (buf, "+OK %u %u", &n, &size);
335 pop_data->size = size;
339 pop_data->status = POP_DISCONNECTED;
340 mutt_error _("Server closed connection!");
343 return PQ_NOT_CONNECTED;
346 /* logout from POP server */
347 void pop_logout (CONTEXT * ctx)
349 pop_query_status ret = 0;
350 char buf[LONG_STRING];
351 POP_DATA *pop_data = (POP_DATA *) ctx->data;
353 if (pop_data->status == POP_CONNECTED) {
354 mutt_message _("Closing connection to POP server...");
357 strfcpy (buf, "RSET\r\n", sizeof (buf));
358 ret = pop_query (pop_data, buf, sizeof (buf));
361 if (ret != PQ_NOT_CONNECTED) {
362 strfcpy (buf, "QUIT\r\n", sizeof (buf));
363 pop_query (pop_data, buf, sizeof (buf));
369 pop_data->status = POP_DISCONNECTED;
374 * Send data from buffer and receive answer to the same buffer
376 * -1 - conection lost,
377 * -2 - invalid command or execution error.
379 pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, char *msg)
381 int dbg = M_SOCK_LOG_CMD;
384 if (pop_data->status != POP_CONNECTED)
385 return PQ_NOT_CONNECTED;
388 /* print msg instaed of real command */
390 dbg = M_SOCK_LOG_FULL;
391 dprint (M_SOCK_LOG_CMD, (debugfile, "> %s", msg));
395 mutt_socket_write_d (pop_data->conn, buf, dbg);
397 c = strpbrk (buf, " \r\n");
399 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
401 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
402 pop_data->status = POP_DISCONNECTED;
403 return PQ_NOT_CONNECTED;
405 if (!safe_strncmp (buf, "+OK", 3))
408 pop_error (pop_data, buf);
413 * This function calls funct(*line, *data) for each received line,
414 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
417 * -1 - conection lost,
418 * -2 - invalid command or execution error,
419 * -3 - error in funct(*line, *data)
421 pop_query_status pop_fetch_data (POP_DATA * pop_data, char *query, char *msg,
422 int (*funct) (char *, void *), void *data)
424 char buf[LONG_STRING];
427 pop_query_status ret;
431 strfcpy (buf, query, sizeof (buf));
432 ret = pop_query (pop_data, buf, sizeof (buf));
436 inbuf = safe_malloc (sizeof (buf));
440 mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
443 pop_data->status = POP_DISCONNECTED;
444 ret = PQ_NOT_CONNECTED;
449 if (!lenbuf && buf[0] == '.') {
455 strfcpy (inbuf + lenbuf, p, sizeof (buf));
457 if (chunk >= sizeof (buf)) {
458 lenbuf += strlen (p);
462 if (msg && ReadInc && (line % ReadInc == 0))
463 mutt_message ("%s %d", msg, line);
464 if (ret == 0 && funct (inbuf, data) < 0)
465 ret = PFD_FUNCT_ERROR;
469 safe_realloc (&inbuf, lenbuf + sizeof (buf));
476 /* find message with this UIDL and set refno */
477 static int check_uidl (char *line, void *data)
481 CONTEXT *ctx = (CONTEXT *) data;
483 sscanf (line, "%u %s", &index, line);
484 for (i = 0; i < ctx->msgcount; i++) {
485 if (!safe_strcmp (ctx->hdrs[i]->data, line)) {
486 ctx->hdrs[i]->refno = index;
494 /* reconnect and verify indexes if connection was lost */
495 pop_query_status pop_reconnect (CONTEXT * ctx)
497 pop_query_status ret;
498 POP_DATA *pop_data = (POP_DATA *) ctx->data;
500 if (pop_data->status == POP_CONNECTED)
502 if (pop_data->status == POP_BYE)
503 return PQ_NOT_CONNECTED;
506 mutt_socket_close (pop_data->conn);
508 ret = pop_open_connection (pop_data);
510 char *msg = _("Verifying message indexes...");
513 for (i = 0; i < ctx->msgcount; i++)
514 ctx->hdrs[i]->refno = -1;
518 ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
520 mutt_error ("%s", pop_data->err_msg);
530 return PQ_NOT_CONNECTED;
532 if (query_quadoption (OPT_POPRECONNECT,
533 _("Connection lost. Reconnect to POP server?")) !=
535 return PQ_NOT_CONNECTED;