2 * Copyright notice from original mutt:
3 * Copyright (C) 2000-2003 Vsevolod Volkov <vvv@mutt.org.ua>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
19 # include "mutt_ssl.h"
24 #include "lib/debug.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) {
47 if (url.scheme == U_POPS) {
48 acct->flags |= M_ACCT_SSL;
49 acct->port = POP_SSL_PORT;
52 if ((!url.path || !*url.path) && mutt_account_fromurl (acct, &url) == 0)
60 /* Copy error message to err_msg buffer */
61 void pop_error (POP_DATA * pop_data, char *msg)
65 t = strchr (pop_data->err_msg, '\0');
68 if (!safe_strncmp (msg, "-ERR ", 5)) {
76 strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
77 str_skip_trailws (pop_data->err_msg);
80 /* Parse CAPA output */
81 static int fetch_capa (char *line, void *data)
83 POP_DATA *pop_data = (POP_DATA *) data;
86 if (!ascii_strncasecmp (line, "SASL", 4)) {
87 FREE (&pop_data->auth_list);
90 pop_data->auth_list = safe_strdup (c);
93 else if (!ascii_strncasecmp (line, "STLS", 4))
94 pop_data->cmd_stls = CMD_AVAILABLE;
96 else if (!ascii_strncasecmp (line, "USER", 4))
97 pop_data->cmd_user = CMD_AVAILABLE;
99 else if (!ascii_strncasecmp (line, "UIDL", 4))
100 pop_data->cmd_uidl = CMD_AVAILABLE;
102 else if (!ascii_strncasecmp (line, "TOP", 3))
103 pop_data->cmd_top = CMD_AVAILABLE;
108 /* Fetch list of the authentication mechanisms */
109 static int fetch_auth (char *line, void *data)
111 POP_DATA *pop_data = (POP_DATA *) data;
113 if (!pop_data->auth_list) {
114 pop_data->auth_list = safe_malloc (strlen (line) + 1);
115 *pop_data->auth_list = '\0';
118 safe_realloc (&pop_data->auth_list,
119 strlen (pop_data->auth_list) + strlen (line) + 2);
120 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
122 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
130 * -1 - conection lost,
131 * -2 - execution error.
133 static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode)
135 char buf[LONG_STRING];
137 /* don't check capabilities on reconnect */
138 if (pop_data->capabilities)
141 /* init capabilities */
143 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
144 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
145 pop_data->cmd_user = CMD_NOT_AVAILABLE;
146 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
147 pop_data->cmd_top = CMD_NOT_AVAILABLE;
148 pop_data->resp_codes = 0;
149 pop_data->expire = 1;
150 pop_data->login_delay = 0;
151 FREE (&pop_data->auth_list);
154 /* Execute CAPA command */
155 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
156 strfcpy (buf, "CAPA\r\n", sizeof (buf));
157 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
160 pop_data->cmd_capa = CMD_AVAILABLE;
165 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
168 case PQ_NOT_CONNECTED:
169 return PQ_NOT_CONNECTED;
173 /* CAPA not supported, use defaults */
174 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
175 pop_data->cmd_user = CMD_UNKNOWN;
176 pop_data->cmd_uidl = CMD_UNKNOWN;
177 pop_data->cmd_top = CMD_UNKNOWN;
179 strfcpy (buf, "AUTH\r\n", sizeof (buf));
180 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
181 return PQ_NOT_CONNECTED;
184 /* Check capabilities */
188 if (!pop_data->expire)
189 msg = _("Unable to leave messages on server.");
190 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
191 msg = _("Command TOP is not supported by server.");
192 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
193 msg = _("Command UIDL is not supported by server.");
194 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
198 pop_data->capabilities = 1;
207 * -1 - conection lost,
208 * -2 - invalid response.
210 pop_query_status pop_connect (POP_DATA * pop_data)
212 char buf[LONG_STRING];
214 pop_data->status = POP_NONE;
215 if (mutt_socket_open (pop_data->conn) < 0 ||
216 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
217 mutt_error (_("Error connecting to server: %s"),
218 pop_data->conn->account.host);
219 return PQ_NOT_CONNECTED;
222 pop_data->status = POP_CONNECTED;
224 if (safe_strncmp (buf, "+OK", 3)) {
225 *pop_data->err_msg = '\0';
226 pop_error (pop_data, buf);
227 mutt_error ("%s", pop_data->err_msg);
231 pop_apop_timestamp (pop_data, buf);
237 * Open connection and authenticate
239 * -1 - conection lost,
240 * -2 - invalid command or execution error,
241 * -3 - authentication canceled.
243 pop_query_status pop_open_connection (POP_DATA * pop_data)
245 pop_query_status ret;
246 unsigned int n, size;
247 char buf[LONG_STRING];
249 ret = pop_connect (pop_data);
255 ret = pop_capabilities (pop_data, 0);
256 if (ret == PQ_NOT_CONNECTED)
263 #if (defined(USE_SSL) || defined(USE_GNUTLS)) && !defined(USE_NSS)
264 /* Attempt STLS if available and desired. */
265 if (pop_data->cmd_stls && !pop_data->conn->ssf) {
266 if (pop_data->use_stls == 0) {
267 ret = query_quadoption (OPT_SSLSTARTTLS,
268 _("Secure connection with TLS?"));
271 pop_data->use_stls = 1;
273 pop_data->use_stls = 2;
275 if (pop_data->use_stls == 2) {
276 strfcpy (buf, "STLS\r\n", sizeof (buf));
277 ret = pop_query (pop_data, buf, sizeof (buf));
278 if (ret == PQ_NOT_CONNECTED)
281 mutt_error ("%s", pop_data->err_msg);
285 else if (mutt_ssl_starttls (pop_data->conn))
287 else if (mutt_gnutls_starttls (pop_data->conn))
290 mutt_error (_("Could not negotiate TLS connection"));
295 /* recheck capabilities after STLS completes */
296 ret = pop_capabilities (pop_data, 1);
297 if (ret == PQ_NOT_CONNECTED)
308 ret = pop_authenticate (pop_data);
309 if (ret == PQ_NOT_CONNECTED)
311 if (ret == PFD_FUNCT_ERROR)
316 /* recheck capabilities after authentication */
317 ret = pop_capabilities (pop_data, 2);
318 if (ret == PQ_NOT_CONNECTED)
325 /* get total size of mailbox */
326 strfcpy (buf, "STAT\r\n", sizeof (buf));
327 ret = pop_query (pop_data, buf, sizeof (buf));
328 if (ret == PQ_NOT_CONNECTED)
331 mutt_error ("%s", pop_data->err_msg);
336 sscanf (buf, "+OK %u %u", &n, &size);
337 pop_data->size = size;
341 pop_data->status = POP_DISCONNECTED;
342 mutt_error _("Server closed connection!");
345 return PQ_NOT_CONNECTED;
348 /* logout from POP server */
349 void pop_logout (CONTEXT * ctx)
351 pop_query_status ret = 0;
352 char buf[LONG_STRING];
353 POP_DATA *pop_data = (POP_DATA *) ctx->data;
355 if (pop_data->status == POP_CONNECTED) {
356 mutt_message _("Closing connection to POP server...");
359 strfcpy (buf, "RSET\r\n", sizeof (buf));
360 ret = pop_query (pop_data, buf, sizeof (buf));
363 if (ret != PQ_NOT_CONNECTED) {
364 strfcpy (buf, "QUIT\r\n", sizeof (buf));
365 pop_query (pop_data, buf, sizeof (buf));
371 pop_data->status = POP_DISCONNECTED;
376 * Send data from buffer and receive answer to the same buffer
378 * -1 - conection lost,
379 * -2 - invalid command or execution error.
381 pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, char *msg)
383 int dbg = M_SOCK_LOG_CMD;
386 if (pop_data->status != POP_CONNECTED)
387 return PQ_NOT_CONNECTED;
390 /* print msg instaed of real command */
392 dbg = M_SOCK_LOG_FULL;
393 debug_print (M_SOCK_LOG_CMD, ("> %s", msg));
397 mutt_socket_write_d (pop_data->conn, buf, dbg);
399 c = strpbrk (buf, " \r\n");
401 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
403 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
404 pop_data->status = POP_DISCONNECTED;
405 return PQ_NOT_CONNECTED;
407 if (!safe_strncmp (buf, "+OK", 3))
410 pop_error (pop_data, buf);
415 * This function calls funct(*line, *data) for each received line,
416 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
419 * -1 - conection lost,
420 * -2 - invalid command or execution error,
421 * -3 - error in funct(*line, *data)
423 pop_query_status pop_fetch_data (POP_DATA * pop_data, char *query, char *msg,
424 int (*funct) (char *, void *), void *data)
426 char buf[LONG_STRING];
429 pop_query_status ret;
433 strfcpy (buf, query, sizeof (buf));
434 ret = pop_query (pop_data, buf, sizeof (buf));
438 inbuf = safe_malloc (sizeof (buf));
442 mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
445 pop_data->status = POP_DISCONNECTED;
446 ret = PQ_NOT_CONNECTED;
451 if (!lenbuf && buf[0] == '.') {
457 strfcpy (inbuf + lenbuf, p, sizeof (buf));
459 if (chunk >= sizeof (buf)) {
460 lenbuf += strlen (p);
464 if (msg && ReadInc && (line % ReadInc == 0))
465 mutt_message ("%s %d", msg, line);
466 if (ret == 0 && funct (inbuf, data) < 0)
467 ret = PFD_FUNCT_ERROR;
471 safe_realloc (&inbuf, lenbuf + sizeof (buf));
478 /* find message with this UIDL and set refno */
479 static int check_uidl (char *line, void *data)
483 CONTEXT *ctx = (CONTEXT *) data;
485 sscanf (line, "%u %s", &index, line);
486 for (i = 0; i < ctx->msgcount; i++) {
487 if (!mutt_strcmp (ctx->hdrs[i]->data, line)) {
488 ctx->hdrs[i]->refno = index;
496 /* reconnect and verify indexes if connection was lost */
497 pop_query_status pop_reconnect (CONTEXT * ctx)
499 pop_query_status ret;
500 POP_DATA *pop_data = (POP_DATA *) ctx->data;
502 if (pop_data->status == POP_CONNECTED)
504 if (pop_data->status == POP_BYE)
505 return PQ_NOT_CONNECTED;
508 mutt_socket_close (pop_data->conn);
510 ret = pop_open_connection (pop_data);
512 char *msg = _("Verifying message indexes...");
515 for (i = 0; i < ctx->msgcount; i++)
516 ctx->hdrs[i]->refno = -1;
520 ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
522 mutt_error ("%s", pop_data->err_msg);
532 return PQ_NOT_CONNECTED;
534 if (query_quadoption (OPT_POPRECONNECT,
535 _("Connection lost. Reconnect to POP server?")) !=
537 return PQ_NOT_CONNECTED;