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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 # include "mutt_ssl.h"
31 /* given an POP mailbox name, return host, port, username and password */
32 int pop_parse_path (const char* path, ACCOUNT* acct)
40 acct->port = POP_PORT;
41 acct->type = M_ACCT_TYPE_POP;
43 c = safe_strdup (path);
44 url_parse_ciss (&url, c);
46 if (url.scheme == U_POP || url.scheme == U_POPS)
48 if (url.scheme == U_POPS)
50 acct->flags |= M_ACCT_SSL;
51 acct->port = POP_SSL_PORT;
54 if ((!url.path || !*url.path) && mutt_account_fromurl (acct, &url) == 0)
62 /* Copy error message to err_msg buffer */
63 void pop_error (POP_DATA *pop_data, char *msg)
67 t = strchr (pop_data->err_msg, '\0');
70 if (!mutt_strncmp (msg, "-ERR ", 5))
79 strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
80 mutt_remove_trailing_ws (pop_data->err_msg);
83 /* Parse CAPA output */
84 static int fetch_capa (char *line, void *data)
86 POP_DATA *pop_data = (POP_DATA *)data;
89 if (!ascii_strncasecmp (line, "SASL", 4))
91 FREE (&pop_data->auth_list);
94 pop_data->auth_list = safe_strdup (c);
97 else if (!ascii_strncasecmp (line, "STLS", 4))
98 pop_data->cmd_stls = 1;
100 else if (!ascii_strncasecmp (line, "USER", 4))
101 pop_data->cmd_user = 1;
103 else if (!ascii_strncasecmp (line, "UIDL", 4))
104 pop_data->cmd_uidl = 1;
106 else if (!ascii_strncasecmp (line, "TOP", 3))
107 pop_data->cmd_top = 1;
112 /* Fetch list of the authentication mechanisms */
113 static int fetch_auth (char *line, void *data)
115 POP_DATA *pop_data = (POP_DATA *)data;
117 if (!pop_data->auth_list)
119 pop_data->auth_list = safe_malloc (strlen (line) + 1);
120 *pop_data->auth_list = '\0';
124 safe_realloc (&pop_data->auth_list,
125 strlen (pop_data->auth_list) + strlen (line) + 2);
126 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
128 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
136 * -1 - conection lost,
137 * -2 - execution error.
139 static int pop_capabilities (POP_DATA *pop_data, int mode)
141 char buf[LONG_STRING];
143 /* don't check capabilities on reconnect */
144 if (pop_data->capabilities)
147 /* init capabilities */
150 pop_data->cmd_capa = 0;
151 pop_data->cmd_stls = 0;
152 pop_data->cmd_user = 0;
153 pop_data->cmd_uidl = 0;
154 pop_data->cmd_top = 0;
155 pop_data->resp_codes = 0;
156 pop_data->expire = 1;
157 pop_data->login_delay = 0;
158 FREE (&pop_data->auth_list);
161 /* Execute CAPA command */
162 if (mode == 0 || pop_data->cmd_capa)
164 strfcpy (buf, "CAPA\r\n", sizeof (buf));
165 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data))
169 pop_data->cmd_capa = 1;
177 /* CAPA not supported, use defaults */
178 if (mode == 0 && !pop_data->cmd_capa)
180 pop_data->cmd_user = 2;
181 pop_data->cmd_uidl = 2;
182 pop_data->cmd_top = 2;
184 strfcpy (buf, "AUTH\r\n", sizeof (buf));
185 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == -1)
189 /* Check capabilities */
194 if (!pop_data->expire)
195 msg = _("Unable to leave messages on server.");
196 if (!pop_data->cmd_top)
197 msg = _("Command TOP is not supported by server.");
198 if (!pop_data->cmd_uidl)
199 msg = _("Command UIDL is not supported by server.");
200 if (msg && pop_data->cmd_capa)
205 pop_data->capabilities = 1;
214 * -1 - conection lost,
215 * -2 - invalid response.
217 int pop_connect (POP_DATA *pop_data)
219 char buf[LONG_STRING];
221 pop_data->status = POP_NONE;
222 if (mutt_socket_open (pop_data->conn) < 0 ||
223 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0)
225 mutt_error (_("Error connecting to server: %s"), pop_data->conn->account.host);
229 pop_data->status = POP_CONNECTED;
231 if (mutt_strncmp (buf, "+OK", 3))
233 *pop_data->err_msg = '\0';
234 pop_error (pop_data, buf);
235 mutt_error ("%s", pop_data->err_msg);
239 pop_apop_timestamp (pop_data, buf);
245 * Open connection and authenticate
247 * -1 - conection lost,
248 * -2 - invalid command or execution error,
249 * -3 - authentication canceled.
251 int pop_open_connection (POP_DATA *pop_data)
254 unsigned int n, size;
255 char buf[LONG_STRING];
257 ret = pop_connect (pop_data);
264 ret = pop_capabilities (pop_data, 0);
273 #if (defined(USE_SSL) || defined(USE_GNUTLS)) && !defined(USE_NSS)
274 /* Attempt STLS if available and desired. */
275 if (pop_data->cmd_stls && !pop_data->conn->ssf)
277 if (pop_data->use_stls == 0)
279 ret = query_quadoption (OPT_SSLSTARTTLS,
280 _("Secure connection with TLS?"));
283 pop_data->use_stls = 1;
285 pop_data->use_stls = 2;
287 if (pop_data->use_stls == 2)
289 strfcpy (buf, "STLS\r\n", sizeof (buf));
290 ret = pop_query (pop_data, buf, sizeof (buf));
295 mutt_error ("%s", pop_data->err_msg);
299 else if (mutt_ssl_starttls (pop_data->conn))
301 else if (mutt_gnutls_starttls (pop_data->conn))
304 mutt_error (_("Could not negotiate TLS connection"));
310 /* recheck capabilities after STLS completes */
311 ret = pop_capabilities (pop_data, 1);
324 ret = pop_authenticate (pop_data);
332 /* recheck capabilities after authentication */
333 ret = pop_capabilities (pop_data, 2);
342 /* get total size of mailbox */
343 strfcpy (buf, "STAT\r\n", sizeof (buf));
344 ret = pop_query (pop_data, buf, sizeof (buf));
349 mutt_error ("%s", pop_data->err_msg);
354 sscanf (buf, "+OK %u %u", &n, &size);
355 pop_data->size = size;
359 pop_data->status = POP_DISCONNECTED;
360 mutt_error _("Server closed connection!");
365 /* logout from POP server */
366 void pop_logout (CONTEXT *ctx)
369 char buf[LONG_STRING];
370 POP_DATA *pop_data = (POP_DATA *)ctx->data;
372 if (pop_data->status == POP_CONNECTED)
374 mutt_message _("Closing connection to POP server...");
378 strfcpy (buf, "RSET\r\n", sizeof (buf));
379 ret = pop_query (pop_data, buf, sizeof (buf));
384 strfcpy (buf, "QUIT\r\n", sizeof (buf));
385 pop_query (pop_data, buf, sizeof (buf));
391 pop_data->status = POP_DISCONNECTED;
396 * Send data from buffer and receive answer to the same buffer
398 * -1 - conection lost,
399 * -2 - invalid command or execution error.
401 int pop_query_d (POP_DATA *pop_data, char *buf, size_t buflen, char *msg)
403 int dbg = M_SOCK_LOG_CMD;
406 if (pop_data->status != POP_CONNECTED)
410 /* print msg instaed of real command */
413 dbg = M_SOCK_LOG_FULL;
414 dprint (M_SOCK_LOG_CMD, (debugfile, "> %s", msg));
418 mutt_socket_write_d (pop_data->conn, buf, dbg);
420 c = strpbrk (buf, " \r\n");
422 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
424 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0)
426 pop_data->status = POP_DISCONNECTED;
429 if (!mutt_strncmp (buf, "+OK", 3))
432 pop_error (pop_data, buf);
437 * This function calls funct(*line, *data) for each received line,
438 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
441 * -1 - conection lost,
442 * -2 - invalid command or execution error,
443 * -3 - error in funct(*line, *data)
445 int pop_fetch_data (POP_DATA *pop_data, char *query, char *msg,
446 int (*funct) (char *, void *), void *data)
448 char buf[LONG_STRING];
451 int ret, chunk, line = 0;
454 strfcpy (buf, query, sizeof (buf));
455 ret = pop_query (pop_data, buf, sizeof (buf));
459 inbuf = safe_malloc (sizeof (buf));
463 chunk = mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn, M_SOCK_LOG_HDR);
466 pop_data->status = POP_DISCONNECTED;
472 if (!lenbuf && buf[0] == '.')
479 strfcpy (inbuf + lenbuf, p, sizeof (buf));
481 if (chunk >= sizeof (buf))
483 lenbuf += strlen (p);
488 if (msg && ReadInc && (line % ReadInc == 0))
489 mutt_message ("%s %d", msg, line);
490 if (ret == 0 && funct (inbuf, data) < 0)
495 safe_realloc (&inbuf, lenbuf + sizeof (buf));
502 /* find message with this UIDL and set refno */
503 static int check_uidl (char *line, void *data)
507 CONTEXT *ctx = (CONTEXT *)data;
509 sscanf (line, "%u %s", &index, line);
510 for (i = 0; i < ctx->msgcount; i++)
512 if (!mutt_strcmp (ctx->hdrs[i]->data, line))
514 ctx->hdrs[i]->refno = index;
522 /* reconnect and verify idnexes if connection was lost */
523 int pop_reconnect (CONTEXT *ctx)
526 POP_DATA *pop_data = (POP_DATA *)ctx->data;
528 if (pop_data->status == POP_CONNECTED)
530 if (pop_data->status == POP_BYE)
535 mutt_socket_close (pop_data->conn);
537 ret = pop_open_connection (pop_data);
540 char *msg = _("Verifying message indexes...");
543 for (i = 0; i < ctx->msgcount; i++)
544 ctx->hdrs[i]->refno = -1;
548 ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
551 mutt_error ("%s", pop_data->err_msg);
563 if (query_quadoption (OPT_POPRECONNECT,
564 _("Connection lost. Reconnect to POP server?")) != M_YES)