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_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);
298 else if (mutt_ssl_starttls (pop_data->conn))
300 mutt_error (_("Could not negotiate TLS connection"));
306 /* recheck capabilities after STLS completes */
307 ret = pop_capabilities (pop_data, 1);
320 ret = pop_authenticate (pop_data);
328 /* recheck capabilities after authentication */
329 ret = pop_capabilities (pop_data, 2);
338 /* get total size of mailbox */
339 strfcpy (buf, "STAT\r\n", sizeof (buf));
340 ret = pop_query (pop_data, buf, sizeof (buf));
345 mutt_error ("%s", pop_data->err_msg);
350 sscanf (buf, "+OK %u %u", &n, &size);
351 pop_data->size = size;
355 pop_data->status = POP_DISCONNECTED;
356 mutt_error _("Server closed connection!");
361 /* logout from POP server */
362 void pop_logout (CONTEXT *ctx)
365 char buf[LONG_STRING];
366 POP_DATA *pop_data = (POP_DATA *)ctx->data;
368 if (pop_data->status == POP_CONNECTED)
370 mutt_message _("Closing connection to POP server...");
374 strfcpy (buf, "RSET\r\n", sizeof (buf));
375 ret = pop_query (pop_data, buf, sizeof (buf));
380 strfcpy (buf, "QUIT\r\n", sizeof (buf));
381 pop_query (pop_data, buf, sizeof (buf));
387 pop_data->status = POP_DISCONNECTED;
392 * Send data from buffer and receive answer to the same buffer
394 * -1 - conection lost,
395 * -2 - invalid command or execution error.
397 int pop_query_d (POP_DATA *pop_data, char *buf, size_t buflen, char *msg)
399 int dbg = M_SOCK_LOG_CMD;
402 if (pop_data->status != POP_CONNECTED)
406 /* print msg instaed of real command */
409 dbg = M_SOCK_LOG_FULL;
410 dprint (M_SOCK_LOG_CMD, (debugfile, "> %s", msg));
414 mutt_socket_write_d (pop_data->conn, buf, dbg);
416 c = strpbrk (buf, " \r\n");
418 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
420 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0)
422 pop_data->status = POP_DISCONNECTED;
425 if (!mutt_strncmp (buf, "+OK", 3))
428 pop_error (pop_data, buf);
433 * This function calls funct(*line, *data) for each received line,
434 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
437 * -1 - conection lost,
438 * -2 - invalid command or execution error,
439 * -3 - error in funct(*line, *data)
441 int pop_fetch_data (POP_DATA *pop_data, char *query, char *msg,
442 int (*funct) (char *, void *), void *data)
444 char buf[LONG_STRING];
447 int ret, chunk, line = 0;
450 strfcpy (buf, query, sizeof (buf));
451 ret = pop_query (pop_data, buf, sizeof (buf));
455 inbuf = safe_malloc (sizeof (buf));
459 chunk = mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn, M_SOCK_LOG_HDR);
462 pop_data->status = POP_DISCONNECTED;
468 if (!lenbuf && buf[0] == '.')
475 strfcpy (inbuf + lenbuf, p, sizeof (buf));
477 if (chunk >= sizeof (buf))
479 lenbuf += strlen (p);
484 if (msg && ReadInc && (line % ReadInc == 0))
485 mutt_message ("%s %d", msg, line);
486 if (ret == 0 && funct (inbuf, data) < 0)
491 safe_realloc (&inbuf, lenbuf + sizeof (buf));
498 /* find message with this UIDL and set refno */
499 static int check_uidl (char *line, void *data)
503 CONTEXT *ctx = (CONTEXT *)data;
505 sscanf (line, "%u %s", &index, line);
506 for (i = 0; i < ctx->msgcount; i++)
508 if (!mutt_strcmp (ctx->hdrs[i]->data, line))
510 ctx->hdrs[i]->refno = index;
518 /* reconnect and verify idnexes if connection was lost */
519 int pop_reconnect (CONTEXT *ctx)
522 POP_DATA *pop_data = (POP_DATA *)ctx->data;
524 if (pop_data->status == POP_CONNECTED)
526 if (pop_data->status == POP_BYE)
531 mutt_socket_close (pop_data->conn);
533 ret = pop_open_connection (pop_data);
536 char *msg = _("Verifying message indexes...");
539 for (i = 0; i < ctx->msgcount; i++)
540 ctx->hdrs[i]->refno = -1;
544 ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
547 mutt_error ("%s", pop_data->err_msg);
559 if (query_quadoption (OPT_POPRECONNECT,
560 _("Connection lost. Reconnect to POP server?")) != M_YES)