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.
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);
277 #if (defined(USE_SSL) || defined(USE_GNUTLS)) && !defined(USE_NSS)
278 /* Attempt STLS if available and desired. */
279 if (pop_data->cmd_stls && !pop_data->conn->ssf)
281 if (pop_data->use_stls == 0)
283 ret = query_quadoption (OPT_SSLSTARTTLS,
284 _("Secure connection with TLS?"));
287 pop_data->use_stls = 1;
289 pop_data->use_stls = 2;
291 if (pop_data->use_stls == 2)
293 strfcpy (buf, "STLS\r\n", sizeof (buf));
294 ret = pop_query (pop_data, buf, sizeof (buf));
299 mutt_error ("%s", pop_data->err_msg);
303 else if (mutt_ssl_starttls (pop_data->conn))
305 else if (mutt_gnutls_starttls (pop_data->conn))
308 mutt_error (_("Could not negotiate TLS connection"));
314 /* recheck capabilities after STLS completes */
315 ret = pop_capabilities (pop_data, 1);
328 ret = pop_authenticate (pop_data);
336 /* recheck capabilities after authentication */
337 ret = pop_capabilities (pop_data, 2);
346 /* get total size of mailbox */
347 strfcpy (buf, "STAT\r\n", sizeof (buf));
348 ret = pop_query (pop_data, buf, sizeof (buf));
353 mutt_error ("%s", pop_data->err_msg);
358 sscanf (buf, "+OK %u %u", &n, &size);
359 pop_data->size = size;
363 pop_data->status = POP_DISCONNECTED;
364 mutt_error _("Server closed connection!");
369 /* logout from POP server */
370 void pop_logout (CONTEXT *ctx)
373 char buf[LONG_STRING];
374 POP_DATA *pop_data = (POP_DATA *)ctx->data;
376 if (pop_data->status == POP_CONNECTED)
378 mutt_message _("Closing connection to POP server...");
382 strfcpy (buf, "RSET\r\n", sizeof (buf));
383 ret = pop_query (pop_data, buf, sizeof (buf));
388 strfcpy (buf, "QUIT\r\n", sizeof (buf));
389 pop_query (pop_data, buf, sizeof (buf));
395 pop_data->status = POP_DISCONNECTED;
400 * Send data from buffer and receive answer to the same buffer
402 * -1 - conection lost,
403 * -2 - invalid command or execution error.
405 int pop_query_d (POP_DATA *pop_data, char *buf, size_t buflen, char *msg)
407 int dbg = M_SOCK_LOG_CMD;
410 if (pop_data->status != POP_CONNECTED)
414 /* print msg instaed of real command */
417 dbg = M_SOCK_LOG_FULL;
418 dprint (M_SOCK_LOG_CMD, (debugfile, "> %s", msg));
422 mutt_socket_write_d (pop_data->conn, buf, dbg);
424 c = strpbrk (buf, " \r\n");
426 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
428 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0)
430 pop_data->status = POP_DISCONNECTED;
433 if (!mutt_strncmp (buf, "+OK", 3))
436 pop_error (pop_data, buf);
441 * This function calls funct(*line, *data) for each received line,
442 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
445 * -1 - conection lost,
446 * -2 - invalid command or execution error,
447 * -3 - error in funct(*line, *data)
449 int pop_fetch_data (POP_DATA *pop_data, char *query, char *msg,
450 int (*funct) (char *, void *), void *data)
452 char buf[LONG_STRING];
455 int ret, chunk, line = 0;
458 strfcpy (buf, query, sizeof (buf));
459 ret = pop_query (pop_data, buf, sizeof (buf));
463 inbuf = safe_malloc (sizeof (buf));
467 chunk = mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn, M_SOCK_LOG_HDR);
470 pop_data->status = POP_DISCONNECTED;
476 if (!lenbuf && buf[0] == '.')
483 strfcpy (inbuf + lenbuf, p, sizeof (buf));
485 if (chunk >= sizeof (buf))
487 lenbuf += strlen (p);
492 if (msg && ReadInc && (line % ReadInc == 0))
493 mutt_message ("%s %d", msg, line);
494 if (ret == 0 && funct (inbuf, data) < 0)
499 safe_realloc (&inbuf, lenbuf + sizeof (buf));
506 /* find message with this UIDL and set refno */
507 static int check_uidl (char *line, void *data)
511 CONTEXT *ctx = (CONTEXT *)data;
513 sscanf (line, "%u %s", &index, line);
514 for (i = 0; i < ctx->msgcount; i++)
516 if (!mutt_strcmp (ctx->hdrs[i]->data, line))
518 ctx->hdrs[i]->refno = index;
526 /* reconnect and verify idnexes if connection was lost */
527 int pop_reconnect (CONTEXT *ctx)
530 POP_DATA *pop_data = (POP_DATA *)ctx->data;
532 if (pop_data->status == POP_CONNECTED)
534 if (pop_data->status == POP_BYE)
539 mutt_socket_close (pop_data->conn);
541 ret = pop_open_connection (pop_data);
544 char *msg = _("Verifying message indexes...");
547 for (i = 0; i < ctx->msgcount; i++)
548 ctx->hdrs[i]->refno = -1;
552 ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
555 mutt_error ("%s", pop_data->err_msg);
567 if (query_quadoption (OPT_POPRECONNECT,
568 _("Connection lost. Reconnect to POP server?")) != M_YES)