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) {
51 if (url.scheme == U_POPS) {
52 acct->flags |= M_ACCT_SSL;
53 acct->port = POP_SSL_PORT;
56 if ((!url.path || !*url.path) && mutt_account_fromurl (acct, &url) == 0)
64 /* Copy error message to err_msg buffer */
65 void pop_error (POP_DATA * pop_data, char *msg)
69 t = strchr (pop_data->err_msg, '\0');
72 if (!mutt_strncmp (msg, "-ERR ", 5)) {
80 strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
81 mutt_remove_trailing_ws (pop_data->err_msg);
84 /* Parse CAPA output */
85 static int fetch_capa (char *line, void *data)
87 POP_DATA *pop_data = (POP_DATA *) data;
90 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) {
118 pop_data->auth_list = safe_malloc (strlen (line) + 1);
119 *pop_data->auth_list = '\0';
122 safe_realloc (&pop_data->auth_list,
123 strlen (pop_data->auth_list) + strlen (line) + 2);
124 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
126 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
134 * -1 - conection lost,
135 * -2 - execution error.
137 static int pop_capabilities (POP_DATA * pop_data, int mode)
139 char buf[LONG_STRING];
141 /* don't check capabilities on reconnect */
142 if (pop_data->capabilities)
145 /* init capabilities */
147 pop_data->cmd_capa = 0;
148 pop_data->cmd_stls = 0;
149 pop_data->cmd_user = 0;
150 pop_data->cmd_uidl = 0;
151 pop_data->cmd_top = 0;
152 pop_data->resp_codes = 0;
153 pop_data->expire = 1;
154 pop_data->login_delay = 0;
155 FREE (&pop_data->auth_list);
158 /* Execute CAPA command */
159 if (mode == 0 || pop_data->cmd_capa) {
160 strfcpy (buf, "CAPA\r\n", sizeof (buf));
161 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
164 pop_data->cmd_capa = 1;
172 /* CAPA not supported, use defaults */
173 if (mode == 0 && !pop_data->cmd_capa) {
174 pop_data->cmd_user = 2;
175 pop_data->cmd_uidl = 2;
176 pop_data->cmd_top = 2;
178 strfcpy (buf, "AUTH\r\n", sizeof (buf));
179 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == -1)
183 /* Check capabilities */
187 if (!pop_data->expire)
188 msg = _("Unable to leave messages on server.");
189 if (!pop_data->cmd_top)
190 msg = _("Command TOP is not supported by server.");
191 if (!pop_data->cmd_uidl)
192 msg = _("Command UIDL is not supported by server.");
193 if (msg && pop_data->cmd_capa) {
197 pop_data->capabilities = 1;
206 * -1 - conection lost,
207 * -2 - invalid response.
209 int pop_connect (POP_DATA * pop_data)
211 char buf[LONG_STRING];
213 pop_data->status = POP_NONE;
214 if (mutt_socket_open (pop_data->conn) < 0 ||
215 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
216 mutt_error (_("Error connecting to server: %s"),
217 pop_data->conn->account.host);
221 pop_data->status = POP_CONNECTED;
223 if (mutt_strncmp (buf, "+OK", 3)) {
224 *pop_data->err_msg = '\0';
225 pop_error (pop_data, buf);
226 mutt_error ("%s", pop_data->err_msg);
230 pop_apop_timestamp (pop_data, buf);
236 * Open connection and authenticate
238 * -1 - conection lost,
239 * -2 - invalid command or execution error,
240 * -3 - authentication canceled.
242 int pop_open_connection (POP_DATA * pop_data)
245 unsigned int n, size;
246 char buf[LONG_STRING];
248 ret = pop_connect (pop_data);
254 ret = pop_capabilities (pop_data, 0);
262 #if (defined(USE_SSL) || defined(USE_GNUTLS)) && !defined(USE_NSS)
263 /* Attempt STLS if available and desired. */
264 if (pop_data->cmd_stls && !pop_data->conn->ssf) {
265 if (pop_data->use_stls == 0) {
266 ret = query_quadoption (OPT_SSLSTARTTLS,
267 _("Secure connection with TLS?"));
270 pop_data->use_stls = 1;
272 pop_data->use_stls = 2;
274 if (pop_data->use_stls == 2) {
275 strfcpy (buf, "STLS\r\n", sizeof (buf));
276 ret = pop_query (pop_data, buf, sizeof (buf));
280 mutt_error ("%s", pop_data->err_msg);
284 else if (mutt_ssl_starttls (pop_data->conn))
286 else if (mutt_gnutls_starttls (pop_data->conn))
289 mutt_error (_("Could not negotiate TLS connection"));
294 /* recheck capabilities after STLS completes */
295 ret = pop_capabilities (pop_data, 1);
307 ret = pop_authenticate (pop_data);
315 /* recheck capabilities after authentication */
316 ret = pop_capabilities (pop_data, 2);
324 /* get total size of mailbox */
325 strfcpy (buf, "STAT\r\n", sizeof (buf));
326 ret = pop_query (pop_data, buf, sizeof (buf));
330 mutt_error ("%s", pop_data->err_msg);
335 sscanf (buf, "+OK %u %u", &n, &size);
336 pop_data->size = size;
340 pop_data->status = POP_DISCONNECTED;
341 mutt_error _("Server closed connection!");
347 /* logout from POP server */
348 void pop_logout (CONTEXT * ctx)
351 char buf[LONG_STRING];
352 POP_DATA *pop_data = (POP_DATA *) ctx->data;
354 if (pop_data->status == POP_CONNECTED) {
355 mutt_message _("Closing connection to POP server...");
358 strfcpy (buf, "RSET\r\n", sizeof (buf));
359 ret = pop_query (pop_data, buf, sizeof (buf));
363 strfcpy (buf, "QUIT\r\n", sizeof (buf));
364 pop_query (pop_data, buf, sizeof (buf));
370 pop_data->status = POP_DISCONNECTED;
375 * Send data from buffer and receive answer to the same buffer
377 * -1 - conection lost,
378 * -2 - invalid command or execution error.
380 int pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, char *msg)
382 int dbg = M_SOCK_LOG_CMD;
385 if (pop_data->status != POP_CONNECTED)
389 /* print msg instaed of real command */
391 dbg = M_SOCK_LOG_FULL;
392 dprint (M_SOCK_LOG_CMD, (debugfile, "> %s", msg));
396 mutt_socket_write_d (pop_data->conn, buf, dbg);
398 c = strpbrk (buf, " \r\n");
400 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
402 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
403 pop_data->status = POP_DISCONNECTED;
406 if (!mutt_strncmp (buf, "+OK", 3))
409 pop_error (pop_data, buf);
414 * This function calls funct(*line, *data) for each received line,
415 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
418 * -1 - conection lost,
419 * -2 - invalid command or execution error,
420 * -3 - error in funct(*line, *data)
422 int pop_fetch_data (POP_DATA * pop_data, char *query, char *msg,
423 int (*funct) (char *, void *), void *data)
425 char buf[LONG_STRING];
428 int ret, chunk, line = 0;
431 strfcpy (buf, query, sizeof (buf));
432 ret = pop_query (pop_data, buf, sizeof (buf));
436 inbuf = safe_malloc (sizeof (buf));
440 mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
443 pop_data->status = POP_DISCONNECTED;
449 if (!lenbuf && buf[0] == '.') {
455 strfcpy (inbuf + lenbuf, p, sizeof (buf));
457 if (chunk >= sizeof (buf)) {
458 lenbuf += strlen (p);
462 if (msg && ReadInc && (line % ReadInc == 0))
463 mutt_message ("%s %d", msg, line);
464 if (ret == 0 && funct (inbuf, data) < 0)
469 safe_realloc (&inbuf, lenbuf + sizeof (buf));
476 /* find message with this UIDL and set refno */
477 static int check_uidl (char *line, void *data)
481 CONTEXT *ctx = (CONTEXT *) data;
483 sscanf (line, "%u %s", &index, line);
484 for (i = 0; i < ctx->msgcount; i++) {
485 if (!mutt_strcmp (ctx->hdrs[i]->data, line)) {
486 ctx->hdrs[i]->refno = index;
494 /* reconnect and verify idnexes if connection was lost */
495 int pop_reconnect (CONTEXT * ctx)
498 POP_DATA *pop_data = (POP_DATA *) ctx->data;
500 if (pop_data->status == POP_CONNECTED)
502 if (pop_data->status == POP_BYE)
506 mutt_socket_close (pop_data->conn);
508 ret = pop_open_connection (pop_data);
510 char *msg = _("Verifying message indexes...");
513 for (i = 0; i < ctx->msgcount; i++)
514 ctx->hdrs[i]->refno = -1;
518 ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
520 mutt_error ("%s", pop_data->err_msg);
532 if (query_quadoption (OPT_POPRECONNECT,
533 _("Connection lost. Reconnect to POP server?")) !=