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;
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 (!str_ncmp (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 mem_free (&pop_data->auth_list);
90 pop_data->auth_list = str_dup (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 = mem_malloc (strlen (line) + 1);
115 *pop_data->auth_list = '\0';
118 mem_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 mem_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 (str_ncmp (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))
264 /* Attempt STLS if available and desired. */
265 if (!pop_data->conn->ssf && (pop_data->cmd_stls || option(OPTSSLFORCETLS))) {
266 if (option (OPTSSLFORCETLS))
267 pop_data->use_stls = 2;
268 if (pop_data->use_stls == 0) {
269 ret = query_quadoption (OPT_SSLSTARTTLS,
270 _("Secure connection with TLS?"));
273 pop_data->use_stls = 1;
275 pop_data->use_stls = 2;
277 if (pop_data->use_stls == 2) {
278 strfcpy (buf, "STLS\r\n", sizeof (buf));
279 ret = pop_query (pop_data, buf, sizeof (buf));
280 if (ret == PQ_NOT_CONNECTED)
283 mutt_error ("%s", pop_data->err_msg);
287 else if (mutt_ssl_starttls (pop_data->conn))
289 else if (mutt_gnutls_starttls (pop_data->conn))
292 mutt_error (_("Could not negotiate TLS connection"));
297 /* recheck capabilities after STLS completes */
298 ret = pop_capabilities (pop_data, 1);
299 if (ret == PQ_NOT_CONNECTED)
309 if (option(OPTSSLFORCETLS) && !pop_data->conn->ssf) {
310 mutt_error _("Encrypted connection unavailable");
316 ret = pop_authenticate (pop_data);
317 if (ret == PQ_NOT_CONNECTED)
319 if (ret == PFD_FUNCT_ERROR)
324 /* recheck capabilities after authentication */
325 ret = pop_capabilities (pop_data, 2);
326 if (ret == PQ_NOT_CONNECTED)
333 /* get total size of mailbox */
334 strfcpy (buf, "STAT\r\n", sizeof (buf));
335 ret = pop_query (pop_data, buf, sizeof (buf));
336 if (ret == PQ_NOT_CONNECTED)
339 mutt_error ("%s", pop_data->err_msg);
344 sscanf (buf, "+OK %u %u", &n, &size);
345 pop_data->size = size;
349 pop_data->status = POP_DISCONNECTED;
350 mutt_error _("Server closed connection!");
353 return PQ_NOT_CONNECTED;
356 /* logout from POP server */
357 void pop_logout (CONTEXT * ctx)
359 pop_query_status ret = 0;
360 char buf[LONG_STRING];
361 POP_DATA *pop_data = (POP_DATA *) ctx->data;
363 if (pop_data->status == POP_CONNECTED) {
364 mutt_message _("Closing connection to POP server...");
367 strfcpy (buf, "RSET\r\n", sizeof (buf));
368 ret = pop_query (pop_data, buf, sizeof (buf));
371 if (ret != PQ_NOT_CONNECTED) {
372 strfcpy (buf, "QUIT\r\n", sizeof (buf));
373 pop_query (pop_data, buf, sizeof (buf));
379 pop_data->status = POP_DISCONNECTED;
384 * Send data from buffer and receive answer to the same buffer
386 * -1 - conection lost,
387 * -2 - invalid command or execution error.
389 pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, char *msg)
391 int dbg = M_SOCK_LOG_CMD;
394 if (pop_data->status != POP_CONNECTED)
395 return PQ_NOT_CONNECTED;
398 /* print msg instaed of real command */
400 dbg = M_SOCK_LOG_FULL;
401 debug_print (M_SOCK_LOG_CMD, ("> %s", msg));
405 mutt_socket_write_d (pop_data->conn, buf, dbg);
407 c = strpbrk (buf, " \r\n");
409 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
411 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
412 pop_data->status = POP_DISCONNECTED;
413 return PQ_NOT_CONNECTED;
415 if (!str_ncmp (buf, "+OK", 3))
418 pop_error (pop_data, buf);
423 * This function calls funct(*line, *data) for each received line,
424 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
427 * -1 - conection lost,
428 * -2 - invalid command or execution error,
429 * -3 - error in funct(*line, *data)
431 pop_query_status pop_fetch_data (POP_DATA * pop_data, char *query, char *msg,
432 int (*funct) (char *, void *), void *data)
434 char buf[LONG_STRING];
437 pop_query_status ret;
441 strfcpy (buf, query, sizeof (buf));
442 ret = pop_query (pop_data, buf, sizeof (buf));
446 inbuf = mem_malloc (sizeof (buf));
450 mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
453 pop_data->status = POP_DISCONNECTED;
454 ret = PQ_NOT_CONNECTED;
459 if (!lenbuf && buf[0] == '.') {
465 strfcpy (inbuf + lenbuf, p, sizeof (buf));
467 if (chunk >= sizeof (buf)) {
468 lenbuf += strlen (p);
472 if (msg && ReadInc && (line % ReadInc == 0))
473 mutt_message ("%s %d", msg, line);
474 if (ret == 0 && funct (inbuf, data) < 0)
475 ret = PFD_FUNCT_ERROR;
479 mem_realloc (&inbuf, lenbuf + sizeof (buf));
486 /* find message with this UIDL and set refno */
487 static int check_uidl (char *line, void *data)
491 CONTEXT *ctx = (CONTEXT *) data;
493 sscanf (line, "%u %s", &index, line);
494 for (i = 0; i < ctx->msgcount; i++) {
495 if (!str_cmp (ctx->hdrs[i]->data, line)) {
496 ctx->hdrs[i]->refno = index;
504 /* reconnect and verify indexes if connection was lost */
505 pop_query_status pop_reconnect (CONTEXT * ctx)
507 pop_query_status ret;
508 POP_DATA *pop_data = (POP_DATA *) ctx->data;
510 if (pop_data->status == POP_CONNECTED)
512 if (pop_data->status == POP_BYE)
513 return PQ_NOT_CONNECTED;
516 mutt_socket_close (pop_data->conn);
518 ret = pop_open_connection (pop_data);
520 char *msg = _("Verifying message indexes...");
523 for (i = 0; i < ctx->msgcount; i++)
524 ctx->hdrs[i]->refno = -1;
528 ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
530 mutt_error ("%s", pop_data->err_msg);
540 return PQ_NOT_CONNECTED;
542 if (query_quadoption (OPT_POPRECONNECT,
543 _("Connection lost. Reconnect to POP server?")) !=
545 return PQ_NOT_CONNECTED;