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"
36 /* given an POP mailbox name, return host, port, username and password */
37 int pop_parse_path (const char* path, ACCOUNT* acct)
41 struct servent *service;
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) ||
51 mutt_account_fromurl (acct, &url) < 0)
54 mutt_error(_("Invalid POP URL: %s\n"), path);
59 if (url.scheme == U_POPS)
60 acct->flags |= M_ACCT_SSL;
62 service = getservbyname (url.scheme == U_POP ? "pop3" : "pop3s", "tcp");
64 acct->port = ntohs (service->s_port);
66 acct->port = url.scheme == U_POP ? POP_PORT : POP_SSL_PORT;;
72 /* Copy error message to err_msg buffer */
73 void pop_error (POP_DATA *pop_data, char *msg)
77 t = strchr (pop_data->err_msg, '\0');
80 if (!mutt_strncmp (msg, "-ERR ", 5))
89 strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
90 mutt_remove_trailing_ws (pop_data->err_msg);
93 /* Parse CAPA output */
94 static int fetch_capa (char *line, void *data)
96 POP_DATA *pop_data = (POP_DATA *)data;
99 if (!ascii_strncasecmp (line, "SASL", 4))
101 FREE (&pop_data->auth_list);
104 pop_data->auth_list = safe_strdup (c);
107 else if (!ascii_strncasecmp (line, "STLS", 4))
108 pop_data->cmd_stls = 1;
110 else if (!ascii_strncasecmp (line, "USER", 4))
111 pop_data->cmd_user = 1;
113 else if (!ascii_strncasecmp (line, "UIDL", 4))
114 pop_data->cmd_uidl = 1;
116 else if (!ascii_strncasecmp (line, "TOP", 3))
117 pop_data->cmd_top = 1;
122 /* Fetch list of the authentication mechanisms */
123 static int fetch_auth (char *line, void *data)
125 POP_DATA *pop_data = (POP_DATA *)data;
127 if (!pop_data->auth_list)
129 pop_data->auth_list = safe_malloc (strlen (line) + 1);
130 *pop_data->auth_list = '\0';
134 safe_realloc (&pop_data->auth_list,
135 strlen (pop_data->auth_list) + strlen (line) + 2);
136 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
138 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
146 * -1 - conection lost,
147 * -2 - execution error.
149 static int pop_capabilities (POP_DATA *pop_data, int mode)
151 char buf[LONG_STRING];
153 /* don't check capabilities on reconnect */
154 if (pop_data->capabilities)
157 /* init capabilities */
160 pop_data->cmd_capa = 0;
161 pop_data->cmd_stls = 0;
162 pop_data->cmd_user = 0;
163 pop_data->cmd_uidl = 0;
164 pop_data->cmd_top = 0;
165 pop_data->resp_codes = 0;
166 pop_data->expire = 1;
167 pop_data->login_delay = 0;
168 FREE (&pop_data->auth_list);
171 /* Execute CAPA command */
172 if (mode == 0 || pop_data->cmd_capa)
174 strfcpy (buf, "CAPA\r\n", sizeof (buf));
175 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data))
179 pop_data->cmd_capa = 1;
187 /* CAPA not supported, use defaults */
188 if (mode == 0 && !pop_data->cmd_capa)
190 pop_data->cmd_user = 2;
191 pop_data->cmd_uidl = 2;
192 pop_data->cmd_top = 2;
194 strfcpy (buf, "AUTH\r\n", sizeof (buf));
195 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == -1)
199 /* Check capabilities */
204 if (!pop_data->expire)
205 msg = _("Unable to leave messages on server.");
206 if (!pop_data->cmd_top)
207 msg = _("Command TOP is not supported by server.");
208 if (!pop_data->cmd_uidl)
209 msg = _("Command UIDL is not supported by server.");
210 if (msg && pop_data->cmd_capa)
215 pop_data->capabilities = 1;
224 * -1 - conection lost,
225 * -2 - invalid response.
227 int pop_connect (POP_DATA *pop_data)
229 char buf[LONG_STRING];
231 pop_data->status = POP_NONE;
232 if (mutt_socket_open (pop_data->conn) < 0 ||
233 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0)
235 mutt_error (_("Error connecting to server: %s"), pop_data->conn->account.host);
239 pop_data->status = POP_CONNECTED;
241 if (mutt_strncmp (buf, "+OK", 3))
243 *pop_data->err_msg = '\0';
244 pop_error (pop_data, buf);
245 mutt_error ("%s", pop_data->err_msg);
249 pop_apop_timestamp (pop_data, buf);
255 * Open connection and authenticate
257 * -1 - conection lost,
258 * -2 - invalid command or execution error,
259 * -3 - authentication canceled.
261 int pop_open_connection (POP_DATA *pop_data)
264 unsigned int n, size;
265 char buf[LONG_STRING];
267 ret = pop_connect (pop_data);
274 ret = pop_capabilities (pop_data, 0);
284 /* Attempt STLS if available and desired. */
285 if (!pop_data->conn->ssf && (pop_data->cmd_stls || option(OPTSSLFORCETLS)))
287 if (option(OPTSSLFORCETLS))
288 pop_data->use_stls = 2;
289 if (pop_data->use_stls == 0)
291 ret = query_quadoption (OPT_SSLSTARTTLS,
292 _("Secure connection with TLS?"));
295 pop_data->use_stls = 1;
297 pop_data->use_stls = 2;
299 if (pop_data->use_stls == 2)
301 strfcpy (buf, "STLS\r\n", sizeof (buf));
302 ret = pop_query (pop_data, buf, sizeof (buf));
307 mutt_error ("%s", pop_data->err_msg);
310 else if (mutt_ssl_starttls (pop_data->conn))
312 mutt_error (_("Could not negotiate TLS connection"));
318 /* recheck capabilities after STLS completes */
319 ret = pop_capabilities (pop_data, 1);
331 if (option(OPTSSLFORCETLS) && !pop_data->conn->ssf)
333 mutt_error _("Encrypted connection unavailable");
339 ret = pop_authenticate (pop_data);
347 /* recheck capabilities after authentication */
348 ret = pop_capabilities (pop_data, 2);
357 /* get total size of mailbox */
358 strfcpy (buf, "STAT\r\n", sizeof (buf));
359 ret = pop_query (pop_data, buf, sizeof (buf));
364 mutt_error ("%s", pop_data->err_msg);
369 sscanf (buf, "+OK %u %u", &n, &size);
370 pop_data->size = size;
374 pop_data->status = POP_DISCONNECTED;
375 mutt_error _("Server closed connection!");
380 /* logout from POP server */
381 void pop_logout (CONTEXT *ctx)
384 char buf[LONG_STRING];
385 POP_DATA *pop_data = (POP_DATA *)ctx->data;
387 if (pop_data->status == POP_CONNECTED)
389 mutt_message _("Closing connection to POP server...");
393 strfcpy (buf, "RSET\r\n", sizeof (buf));
394 ret = pop_query (pop_data, buf, sizeof (buf));
399 strfcpy (buf, "QUIT\r\n", sizeof (buf));
400 pop_query (pop_data, buf, sizeof (buf));
406 pop_data->status = POP_DISCONNECTED;
411 * Send data from buffer and receive answer to the same buffer
413 * -1 - conection lost,
414 * -2 - invalid command or execution error.
416 int pop_query_d (POP_DATA *pop_data, char *buf, size_t buflen, char *msg)
418 int dbg = M_SOCK_LOG_CMD;
421 if (pop_data->status != POP_CONNECTED)
425 /* print msg instaed of real command */
428 dbg = M_SOCK_LOG_FULL;
429 dprint (M_SOCK_LOG_CMD, (debugfile, "> %s", msg));
433 mutt_socket_write_d (pop_data->conn, buf, -1, dbg);
435 c = strpbrk (buf, " \r\n");
437 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
439 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0)
441 pop_data->status = POP_DISCONNECTED;
444 if (!mutt_strncmp (buf, "+OK", 3))
447 pop_error (pop_data, buf);
452 * This function calls funct(*line, *data) for each received line,
453 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
456 * -1 - conection lost,
457 * -2 - invalid command or execution error,
458 * -3 - error in funct(*line, *data)
460 int pop_fetch_data (POP_DATA *pop_data, char *query, progress_t *progressbar,
461 int (*funct) (char *, void *), void *data)
463 char buf[LONG_STRING];
470 strfcpy (buf, query, sizeof (buf));
471 ret = pop_query (pop_data, buf, sizeof (buf));
475 inbuf = safe_malloc (sizeof (buf));
479 chunk = mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn, M_SOCK_LOG_HDR);
482 pop_data->status = POP_DISCONNECTED;
488 if (!lenbuf && buf[0] == '.')
495 strfcpy (inbuf + lenbuf, p, sizeof (buf));
498 if (chunk >= sizeof (buf))
500 lenbuf += strlen (p);
505 mutt_progress_update (progressbar, pos, -1);
506 if (ret == 0 && funct (inbuf, data) < 0)
511 safe_realloc (&inbuf, lenbuf + sizeof (buf));
518 /* find message with this UIDL and set refno */
519 static int check_uidl (char *line, void *data)
523 CONTEXT *ctx = (CONTEXT *)data;
525 sscanf (line, "%u %s", &index, line);
526 for (i = 0; i < ctx->msgcount; i++)
528 if (!mutt_strcmp (ctx->hdrs[i]->data, line))
530 ctx->hdrs[i]->refno = index;
538 /* reconnect and verify idnexes if connection was lost */
539 int pop_reconnect (CONTEXT *ctx)
542 POP_DATA *pop_data = (POP_DATA *)ctx->data;
543 progress_t progressbar;
545 if (pop_data->status == POP_CONNECTED)
547 if (pop_data->status == POP_BYE)
552 mutt_socket_close (pop_data->conn);
554 ret = pop_open_connection (pop_data);
559 mutt_progress_init (&progressbar, _("Verifying message indexes..."),
560 M_PROGRESS_SIZE, NetInc, 0);
562 for (i = 0; i < ctx->msgcount; i++)
563 ctx->hdrs[i]->refno = -1;
565 ret = pop_fetch_data (pop_data, "UIDL\r\n", &progressbar, check_uidl, ctx);
568 mutt_error ("%s", pop_data->err_msg);
580 if (query_quadoption (OPT_POPRECONNECT,
581 _("Connection lost. Reconnect to POP server?")) != M_YES)