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 #include <netinet/in.h>
38 /* given an POP mailbox name, return host, port, username and password */
39 int pop_parse_path (const char* path, ACCOUNT* acct)
43 struct servent *service;
47 acct->type = MUTT_ACCT_TYPE_POP;
50 c = safe_strdup (path);
51 url_parse_ciss (&url, c);
53 if ((url.scheme != U_POP && url.scheme != U_POPS) ||
54 mutt_account_fromurl (acct, &url) < 0)
57 mutt_error(_("Invalid POP URL: %s\n"), path);
62 if (url.scheme == U_POPS)
63 acct->flags |= MUTT_ACCT_SSL;
65 service = getservbyname (url.scheme == U_POP ? "pop3" : "pop3s", "tcp");
69 acct->port = ntohs (service->s_port);
71 acct->port = url.scheme == U_POP ? POP_PORT : POP_SSL_PORT;;
78 /* Copy error message to err_msg buffer */
79 void pop_error (POP_DATA *pop_data, char *msg)
83 t = strchr (pop_data->err_msg, '\0');
86 if (!mutt_strncmp (msg, "-ERR ", 5))
88 c2 = skip_email_wsp(msg + 5);
94 strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
95 mutt_remove_trailing_ws (pop_data->err_msg);
98 /* Parse CAPA output */
99 static int fetch_capa (char *line, void *data)
101 POP_DATA *pop_data = (POP_DATA *)data;
104 if (!ascii_strncasecmp (line, "SASL", 4))
106 FREE (&pop_data->auth_list);
107 c = skip_email_wsp(line + 4);
108 pop_data->auth_list = safe_strdup (c);
111 else if (!ascii_strncasecmp (line, "STLS", 4))
112 pop_data->cmd_stls = 1;
114 else if (!ascii_strncasecmp (line, "USER", 4))
115 pop_data->cmd_user = 1;
117 else if (!ascii_strncasecmp (line, "UIDL", 4))
118 pop_data->cmd_uidl = 1;
120 else if (!ascii_strncasecmp (line, "TOP", 3))
121 pop_data->cmd_top = 1;
126 /* Fetch list of the authentication mechanisms */
127 static int fetch_auth (char *line, void *data)
129 POP_DATA *pop_data = (POP_DATA *)data;
131 if (!pop_data->auth_list)
133 pop_data->auth_list = safe_malloc (strlen (line) + 1);
134 *pop_data->auth_list = '\0';
138 safe_realloc (&pop_data->auth_list,
139 strlen (pop_data->auth_list) + strlen (line) + 2);
140 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
142 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
150 * -1 - connection lost,
151 * -2 - execution error.
153 static int pop_capabilities (POP_DATA *pop_data, int mode)
155 char buf[LONG_STRING];
157 /* don't check capabilities on reconnect */
158 if (pop_data->capabilities)
161 /* init capabilities */
164 pop_data->cmd_capa = 0;
165 pop_data->cmd_stls = 0;
166 pop_data->cmd_user = 0;
167 pop_data->cmd_uidl = 0;
168 pop_data->cmd_top = 0;
169 pop_data->resp_codes = 0;
170 pop_data->expire = 1;
171 pop_data->login_delay = 0;
172 FREE (&pop_data->auth_list);
175 /* Execute CAPA command */
176 if (mode == 0 || pop_data->cmd_capa)
178 strfcpy (buf, "CAPA\r\n", sizeof (buf));
179 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data))
183 pop_data->cmd_capa = 1;
191 /* CAPA not supported, use defaults */
192 if (mode == 0 && !pop_data->cmd_capa)
194 pop_data->cmd_user = 2;
195 pop_data->cmd_uidl = 2;
196 pop_data->cmd_top = 2;
198 strfcpy (buf, "AUTH\r\n", sizeof (buf));
199 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == -1)
203 /* Check capabilities */
208 if (!pop_data->expire)
209 msg = _("Unable to leave messages on server.");
210 if (!pop_data->cmd_top)
211 msg = _("Command TOP is not supported by server.");
212 if (!pop_data->cmd_uidl)
213 msg = _("Command UIDL is not supported by server.");
214 if (msg && pop_data->cmd_capa)
219 pop_data->capabilities = 1;
228 * -1 - connection lost,
229 * -2 - invalid response.
231 int pop_connect (POP_DATA *pop_data)
233 char buf[LONG_STRING];
235 pop_data->status = POP_NONE;
236 if (mutt_socket_open (pop_data->conn) < 0 ||
237 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0)
239 mutt_error (_("Error connecting to server: %s"), pop_data->conn->account.host);
243 pop_data->status = POP_CONNECTED;
245 if (mutt_strncmp (buf, "+OK", 3))
247 *pop_data->err_msg = '\0';
248 pop_error (pop_data, buf);
249 mutt_error ("%s", pop_data->err_msg);
253 pop_apop_timestamp (pop_data, buf);
259 * Open connection and authenticate
261 * -1 - connection lost,
262 * -2 - invalid command or execution error,
263 * -3 - authentication canceled.
265 int pop_open_connection (POP_DATA *pop_data)
268 unsigned int n, size;
269 char buf[LONG_STRING];
271 ret = pop_connect (pop_data);
278 ret = pop_capabilities (pop_data, 0);
288 /* Attempt STLS if available and desired. */
289 if (!pop_data->conn->ssf && (pop_data->cmd_stls || option(OPTSSLFORCETLS)))
291 if (option(OPTSSLFORCETLS))
292 pop_data->use_stls = 2;
293 if (pop_data->use_stls == 0)
295 ret = query_quadoption (OPT_SSLSTARTTLS,
296 _("Secure connection with TLS?"));
299 pop_data->use_stls = 1;
301 pop_data->use_stls = 2;
303 if (pop_data->use_stls == 2)
305 strfcpy (buf, "STLS\r\n", sizeof (buf));
306 ret = pop_query (pop_data, buf, sizeof (buf));
311 mutt_error ("%s", pop_data->err_msg);
314 else if (mutt_ssl_starttls (pop_data->conn))
316 mutt_error (_("Could not negotiate TLS connection"));
322 /* recheck capabilities after STLS completes */
323 ret = pop_capabilities (pop_data, 1);
335 if (option(OPTSSLFORCETLS) && !pop_data->conn->ssf)
337 mutt_error _("Encrypted connection unavailable");
343 ret = pop_authenticate (pop_data);
351 /* recheck capabilities after authentication */
352 ret = pop_capabilities (pop_data, 2);
361 /* get total size of mailbox */
362 strfcpy (buf, "STAT\r\n", sizeof (buf));
363 ret = pop_query (pop_data, buf, sizeof (buf));
368 mutt_error ("%s", pop_data->err_msg);
373 sscanf (buf, "+OK %u %u", &n, &size);
374 pop_data->size = size;
378 pop_data->status = POP_DISCONNECTED;
379 mutt_error _("Server closed connection!");
384 /* logout from POP server */
385 void pop_logout (CONTEXT *ctx)
388 char buf[LONG_STRING];
389 POP_DATA *pop_data = (POP_DATA *)ctx->data;
391 if (pop_data->status == POP_CONNECTED)
393 mutt_message _("Closing connection to POP server...");
397 strfcpy (buf, "RSET\r\n", sizeof (buf));
398 ret = pop_query (pop_data, buf, sizeof (buf));
403 strfcpy (buf, "QUIT\r\n", sizeof (buf));
404 pop_query (pop_data, buf, sizeof (buf));
410 pop_data->status = POP_DISCONNECTED;
415 * Send data from buffer and receive answer to the same buffer
417 * -1 - connection lost,
418 * -2 - invalid command or execution error.
420 int pop_query_d (POP_DATA *pop_data, char *buf, size_t buflen, char *msg)
422 int dbg = MUTT_SOCK_LOG_CMD;
425 if (pop_data->status != POP_CONNECTED)
429 /* print msg instead of real command */
432 dbg = MUTT_SOCK_LOG_FULL;
433 dprint (MUTT_SOCK_LOG_CMD, (debugfile, "> %s", msg));
437 mutt_socket_write_d (pop_data->conn, buf, -1, dbg);
439 c = strpbrk (buf, " \r\n");
441 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
443 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0)
445 pop_data->status = POP_DISCONNECTED;
448 if (!mutt_strncmp (buf, "+OK", 3))
451 pop_error (pop_data, buf);
456 * This function calls funct(*line, *data) for each received line,
457 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
460 * -1 - connection lost,
461 * -2 - invalid command or execution error,
462 * -3 - error in funct(*line, *data)
464 int pop_fetch_data (POP_DATA *pop_data, char *query, progress_t *progressbar,
465 int (*funct) (char *, void *), void *data)
467 char buf[LONG_STRING];
474 strfcpy (buf, query, sizeof (buf));
475 ret = pop_query (pop_data, buf, sizeof (buf));
479 inbuf = safe_malloc (sizeof (buf));
483 chunk = mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn, MUTT_SOCK_LOG_HDR);
486 pop_data->status = POP_DISCONNECTED;
492 if (!lenbuf && buf[0] == '.')
499 strfcpy (inbuf + lenbuf, p, sizeof (buf));
502 /* cast is safe since we break out of the loop when chunk<=0 */
503 if ((size_t)chunk >= sizeof (buf))
505 lenbuf += strlen (p);
510 mutt_progress_update (progressbar, pos, -1);
511 if (ret == 0 && funct (inbuf, data) < 0)
516 safe_realloc (&inbuf, lenbuf + sizeof (buf));
523 /* find message with this UIDL and set refno */
524 static int check_uidl (char *line, void *data)
528 CONTEXT *ctx = (CONTEXT *)data;
532 index = strtoul(line, &endp, 10);
537 memmove(line, endp, strlen(endp) + 1);
539 for (i = 0; i < ctx->msgcount; i++)
541 if (!mutt_strcmp (ctx->hdrs[i]->data, line))
543 ctx->hdrs[i]->refno = index;
551 /* reconnect and verify idnexes if connection was lost */
552 int pop_reconnect (CONTEXT *ctx)
555 POP_DATA *pop_data = (POP_DATA *)ctx->data;
556 progress_t progressbar;
558 if (pop_data->status == POP_CONNECTED)
560 if (pop_data->status == POP_BYE)
565 mutt_socket_close (pop_data->conn);
567 ret = pop_open_connection (pop_data);
572 mutt_progress_init (&progressbar, _("Verifying message indexes..."),
573 MUTT_PROGRESS_SIZE, NetInc, 0);
575 for (i = 0; i < ctx->msgcount; i++)
576 ctx->hdrs[i]->refno = -1;
578 ret = pop_fetch_data (pop_data, "UIDL\r\n", &progressbar, check_uidl, ctx);
581 mutt_error ("%s", pop_data->err_msg);
593 if (query_quadoption (OPT_POPRECONNECT,
594 _("Connection lost. Reconnect to POP server?")) != MUTT_YES)