2 * Copyright (C) 2000-2003 Vsevolod Volkov <vvv@mutt.org.ua>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 # include "mutt_ssl.h"
35 /* given an POP mailbox name, return host, port, username and password */
36 int pop_parse_path (const char* path, ACCOUNT* acct)
44 acct->port = POP_PORT;
45 acct->type = M_ACCT_TYPE_POP;
47 c = safe_strdup (path);
48 url_parse_ciss (&url, c);
50 if (url.scheme == U_POP || url.scheme == U_POPS)
52 if (url.scheme == U_POPS)
54 acct->flags |= M_ACCT_SSL;
55 acct->port = POP_SSL_PORT;
58 if ((!url.path || !*url.path) && mutt_account_fromurl (acct, &url) == 0)
66 /* Copy error message to err_msg buffer */
67 void pop_error (POP_DATA *pop_data, char *msg)
71 t = strchr (pop_data->err_msg, '\0');
74 if (!mutt_strncmp (msg, "-ERR ", 5))
83 strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
84 mutt_remove_trailing_ws (pop_data->err_msg);
87 /* Parse CAPA output */
88 static int fetch_capa (char *line, void *data)
90 POP_DATA *pop_data = (POP_DATA *)data;
93 if (!ascii_strncasecmp (line, "SASL", 4))
95 FREE (&pop_data->auth_list);
98 pop_data->auth_list = safe_strdup (c);
101 else if (!ascii_strncasecmp (line, "STLS", 4))
102 pop_data->cmd_stls = 1;
104 else if (!ascii_strncasecmp (line, "USER", 4))
105 pop_data->cmd_user = 1;
107 else if (!ascii_strncasecmp (line, "UIDL", 4))
108 pop_data->cmd_uidl = 1;
110 else if (!ascii_strncasecmp (line, "TOP", 3))
111 pop_data->cmd_top = 1;
116 /* Fetch list of the authentication mechanisms */
117 static int fetch_auth (char *line, void *data)
119 POP_DATA *pop_data = (POP_DATA *)data;
121 if (!pop_data->auth_list)
123 pop_data->auth_list = safe_malloc (strlen (line) + 1);
124 *pop_data->auth_list = '\0';
128 safe_realloc (&pop_data->auth_list,
129 strlen (pop_data->auth_list) + strlen (line) + 2);
130 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
132 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
140 * -1 - conection lost,
141 * -2 - execution error.
143 static int pop_capabilities (POP_DATA *pop_data, int mode)
145 char buf[LONG_STRING];
147 /* don't check capabilities on reconnect */
148 if (pop_data->capabilities)
151 /* init capabilities */
154 pop_data->cmd_capa = 0;
155 pop_data->cmd_stls = 0;
156 pop_data->cmd_user = 0;
157 pop_data->cmd_uidl = 0;
158 pop_data->cmd_top = 0;
159 pop_data->resp_codes = 0;
160 pop_data->expire = 1;
161 pop_data->login_delay = 0;
162 FREE (&pop_data->auth_list);
165 /* Execute CAPA command */
166 if (mode == 0 || pop_data->cmd_capa)
168 strfcpy (buf, "CAPA\r\n", sizeof (buf));
169 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data))
173 pop_data->cmd_capa = 1;
181 /* CAPA not supported, use defaults */
182 if (mode == 0 && !pop_data->cmd_capa)
184 pop_data->cmd_user = 2;
185 pop_data->cmd_uidl = 2;
186 pop_data->cmd_top = 2;
188 strfcpy (buf, "AUTH\r\n", sizeof (buf));
189 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == -1)
193 /* Check capabilities */
198 if (!pop_data->expire)
199 msg = _("Unable to leave messages on server.");
200 if (!pop_data->cmd_top)
201 msg = _("Command TOP is not supported by server.");
202 if (!pop_data->cmd_uidl)
203 msg = _("Command UIDL is not supported by server.");
204 if (msg && pop_data->cmd_capa)
209 pop_data->capabilities = 1;
218 * -1 - conection lost,
219 * -2 - invalid response.
221 int pop_connect (POP_DATA *pop_data)
223 char buf[LONG_STRING];
225 pop_data->status = POP_NONE;
226 if (mutt_socket_open (pop_data->conn) < 0 ||
227 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0)
229 mutt_error (_("Error connecting to server: %s"), pop_data->conn->account.host);
233 pop_data->status = POP_CONNECTED;
235 if (mutt_strncmp (buf, "+OK", 3))
237 *pop_data->err_msg = '\0';
238 pop_error (pop_data, buf);
239 mutt_error ("%s", pop_data->err_msg);
243 pop_apop_timestamp (pop_data, buf);
249 * Open connection and authenticate
251 * -1 - conection lost,
252 * -2 - invalid command or execution error,
253 * -3 - authentication canceled.
255 int pop_open_connection (POP_DATA *pop_data)
258 unsigned int n, size;
259 char buf[LONG_STRING];
261 ret = pop_connect (pop_data);
268 ret = pop_capabilities (pop_data, 0);
278 /* Attempt STLS if available and desired. */
279 if (!pop_data->conn->ssf && (pop_data->cmd_stls || option(OPTSSLFORCETLS)))
281 if (option(OPTSSLFORCETLS))
282 pop_data->use_stls = 2;
283 if (pop_data->use_stls == 0)
285 ret = query_quadoption (OPT_SSLSTARTTLS,
286 _("Secure connection with TLS?"));
289 pop_data->use_stls = 1;
291 pop_data->use_stls = 2;
293 if (pop_data->use_stls == 2)
295 strfcpy (buf, "STLS\r\n", sizeof (buf));
296 ret = pop_query (pop_data, buf, sizeof (buf));
301 mutt_error ("%s", pop_data->err_msg);
304 else if (mutt_ssl_starttls (pop_data->conn))
306 mutt_error (_("Could not negotiate TLS connection"));
312 /* recheck capabilities after STLS completes */
313 ret = pop_capabilities (pop_data, 1);
325 if (option(OPTSSLFORCETLS) && !pop_data->conn->ssf)
327 mutt_error _("Encrypted connection unavailable");
333 ret = pop_authenticate (pop_data);
341 /* recheck capabilities after authentication */
342 ret = pop_capabilities (pop_data, 2);
351 /* get total size of mailbox */
352 strfcpy (buf, "STAT\r\n", sizeof (buf));
353 ret = pop_query (pop_data, buf, sizeof (buf));
358 mutt_error ("%s", pop_data->err_msg);
363 sscanf (buf, "+OK %u %u", &n, &size);
364 pop_data->size = size;
368 pop_data->status = POP_DISCONNECTED;
369 mutt_error _("Server closed connection!");
374 /* logout from POP server */
375 void pop_logout (CONTEXT *ctx)
378 char buf[LONG_STRING];
379 POP_DATA *pop_data = (POP_DATA *)ctx->data;
381 if (pop_data->status == POP_CONNECTED)
383 mutt_message _("Closing connection to POP server...");
387 strfcpy (buf, "RSET\r\n", sizeof (buf));
388 ret = pop_query (pop_data, buf, sizeof (buf));
393 strfcpy (buf, "QUIT\r\n", sizeof (buf));
394 pop_query (pop_data, buf, sizeof (buf));
400 pop_data->status = POP_DISCONNECTED;
405 * Send data from buffer and receive answer to the same buffer
407 * -1 - conection lost,
408 * -2 - invalid command or execution error.
410 int pop_query_d (POP_DATA *pop_data, char *buf, size_t buflen, char *msg)
412 int dbg = M_SOCK_LOG_CMD;
415 if (pop_data->status != POP_CONNECTED)
419 /* print msg instaed of real command */
422 dbg = M_SOCK_LOG_FULL;
423 dprint (M_SOCK_LOG_CMD, (debugfile, "> %s", msg));
427 mutt_socket_write_d (pop_data->conn, buf, -1, dbg);
429 c = strpbrk (buf, " \r\n");
431 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
433 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0)
435 pop_data->status = POP_DISCONNECTED;
438 if (!mutt_strncmp (buf, "+OK", 3))
441 pop_error (pop_data, buf);
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 int pop_fetch_data (POP_DATA *pop_data, char *query, progress_t *progressbar,
455 int (*funct) (char *, void *), void *data)
457 char buf[LONG_STRING];
464 strfcpy (buf, query, sizeof (buf));
465 ret = pop_query (pop_data, buf, sizeof (buf));
469 inbuf = safe_malloc (sizeof (buf));
473 chunk = mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn, M_SOCK_LOG_HDR);
476 pop_data->status = POP_DISCONNECTED;
482 if (!lenbuf && buf[0] == '.')
489 strfcpy (inbuf + lenbuf, p, sizeof (buf));
492 if (chunk >= sizeof (buf))
494 lenbuf += strlen (p);
499 mutt_progress_update (progressbar, pos, -1);
500 if (ret == 0 && funct (inbuf, data) < 0)
505 safe_realloc (&inbuf, lenbuf + sizeof (buf));
512 /* find message with this UIDL and set refno */
513 static int check_uidl (char *line, void *data)
517 CONTEXT *ctx = (CONTEXT *)data;
519 sscanf (line, "%u %s", &index, line);
520 for (i = 0; i < ctx->msgcount; i++)
522 if (!mutt_strcmp (ctx->hdrs[i]->data, line))
524 ctx->hdrs[i]->refno = index;
532 /* reconnect and verify idnexes if connection was lost */
533 int pop_reconnect (CONTEXT *ctx)
536 POP_DATA *pop_data = (POP_DATA *)ctx->data;
537 progress_t progressbar;
539 if (pop_data->status == POP_CONNECTED)
541 if (pop_data->status == POP_BYE)
546 mutt_socket_close (pop_data->conn);
548 ret = pop_open_connection (pop_data);
553 mutt_progress_init (&progressbar, _("Verifying message indexes..."),
554 M_PROGRESS_SIZE, NetInc, 0);
556 for (i = 0; i < ctx->msgcount; i++)
557 ctx->hdrs[i]->refno = -1;
559 ret = pop_fetch_data (pop_data, "UIDL\r\n", &progressbar, check_uidl, ctx);
562 mutt_error ("%s", pop_data->err_msg);
574 if (query_quadoption (OPT_POPRECONNECT,
575 _("Connection lost. Reconnect to POP server?")) != M_YES)