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"
26 /* given an POP mailbox name, return host, port, username and password */
27 int pop_parse_path (const char *path, ACCOUNT * acct)
35 acct->port = POP_PORT;
36 acct->type = M_ACCT_TYPE_POP;
38 c = safe_strdup (path);
39 url_parse_ciss (&url, c);
41 if (url.scheme == U_POP || url.scheme == U_POPS) {
42 if (url.scheme == U_POPS) {
43 acct->flags |= M_ACCT_SSL;
44 acct->port = POP_SSL_PORT;
47 if ((!url.path || !*url.path) && mutt_account_fromurl (acct, &url) == 0)
55 /* Copy error message to err_msg buffer */
56 void pop_error (POP_DATA * pop_data, char *msg)
60 t = strchr (pop_data->err_msg, '\0');
63 if (!mutt_strncmp (msg, "-ERR ", 5)) {
71 strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
72 mutt_remove_trailing_ws (pop_data->err_msg);
75 /* Parse CAPA output */
76 static int fetch_capa (char *line, void *data)
78 POP_DATA *pop_data = (POP_DATA *) data;
81 if (!ascii_strncasecmp (line, "SASL", 4)) {
82 FREE (&pop_data->auth_list);
85 pop_data->auth_list = safe_strdup (c);
88 else if (!ascii_strncasecmp (line, "STLS", 4))
89 pop_data->cmd_stls = CMD_AVAILABLE;
91 else if (!ascii_strncasecmp (line, "USER", 4))
92 pop_data->cmd_user = CMD_AVAILABLE;
94 else if (!ascii_strncasecmp (line, "UIDL", 4))
95 pop_data->cmd_uidl = CMD_AVAILABLE;
97 else if (!ascii_strncasecmp (line, "TOP", 3))
98 pop_data->cmd_top = CMD_AVAILABLE;
103 /* Fetch list of the authentication mechanisms */
104 static int fetch_auth (char *line, void *data)
106 POP_DATA *pop_data = (POP_DATA *) data;
108 if (!pop_data->auth_list) {
109 pop_data->auth_list = safe_malloc (strlen (line) + 1);
110 *pop_data->auth_list = '\0';
113 safe_realloc (&pop_data->auth_list,
114 strlen (pop_data->auth_list) + strlen (line) + 2);
115 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
117 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
125 * -1 - conection lost,
126 * -2 - execution error.
128 static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode)
130 char buf[LONG_STRING];
132 /* don't check capabilities on reconnect */
133 if (pop_data->capabilities)
136 /* init capabilities */
138 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
139 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
140 pop_data->cmd_user = CMD_NOT_AVAILABLE;
141 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
142 pop_data->cmd_top = CMD_NOT_AVAILABLE;
143 pop_data->resp_codes = 0;
144 pop_data->expire = 1;
145 pop_data->login_delay = 0;
146 FREE (&pop_data->auth_list);
149 /* Execute CAPA command */
150 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
151 strfcpy (buf, "CAPA\r\n", sizeof (buf));
152 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
155 pop_data->cmd_capa = CMD_AVAILABLE;
160 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
163 case PQ_NOT_CONNECTED:
164 return PQ_NOT_CONNECTED;
168 /* CAPA not supported, use defaults */
169 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
170 pop_data->cmd_user = CMD_UNKNOWN;
171 pop_data->cmd_uidl = CMD_UNKNOWN;
172 pop_data->cmd_top = CMD_UNKNOWN;
174 strfcpy (buf, "AUTH\r\n", sizeof (buf));
175 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
176 return PQ_NOT_CONNECTED;
179 /* Check capabilities */
183 if (!pop_data->expire)
184 msg = _("Unable to leave messages on server.");
185 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
186 msg = _("Command TOP is not supported by server.");
187 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
188 msg = _("Command UIDL is not supported by server.");
189 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
193 pop_data->capabilities = 1;
202 * -1 - conection lost,
203 * -2 - invalid response.
205 pop_query_status pop_connect (POP_DATA * pop_data)
207 char buf[LONG_STRING];
209 pop_data->status = POP_NONE;
210 if (mutt_socket_open (pop_data->conn) < 0 ||
211 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
212 mutt_error (_("Error connecting to server: %s"),
213 pop_data->conn->account.host);
214 return PQ_NOT_CONNECTED;
217 pop_data->status = POP_CONNECTED;
219 if (mutt_strncmp (buf, "+OK", 3)) {
220 *pop_data->err_msg = '\0';
221 pop_error (pop_data, buf);
222 mutt_error ("%s", pop_data->err_msg);
226 pop_apop_timestamp (pop_data, buf);
232 * Open connection and authenticate
234 * -1 - conection lost,
235 * -2 - invalid command or execution error,
236 * -3 - authentication canceled.
238 pop_query_status pop_open_connection (POP_DATA * pop_data)
240 pop_query_status ret;
241 unsigned int n, size;
242 char buf[LONG_STRING];
244 ret = pop_connect (pop_data);
250 ret = pop_capabilities (pop_data, 0);
251 if (ret == PQ_NOT_CONNECTED)
258 #if (defined(USE_SSL) || defined(USE_GNUTLS)) && !defined(USE_NSS)
259 /* Attempt STLS if available and desired. */
260 if (pop_data->cmd_stls && !pop_data->conn->ssf) {
261 if (pop_data->use_stls == 0) {
262 ret = query_quadoption (OPT_SSLSTARTTLS,
263 _("Secure connection with TLS?"));
266 pop_data->use_stls = 1;
268 pop_data->use_stls = 2;
270 if (pop_data->use_stls == 2) {
271 strfcpy (buf, "STLS\r\n", sizeof (buf));
272 ret = pop_query (pop_data, buf, sizeof (buf));
273 if (ret == PQ_NOT_CONNECTED)
276 mutt_error ("%s", pop_data->err_msg);
280 else if (mutt_ssl_starttls (pop_data->conn))
282 else if (mutt_gnutls_starttls (pop_data->conn))
285 mutt_error (_("Could not negotiate TLS connection"));
290 /* recheck capabilities after STLS completes */
291 ret = pop_capabilities (pop_data, 1);
292 if (ret == PQ_NOT_CONNECTED)
303 ret = pop_authenticate (pop_data);
304 if (ret == PQ_NOT_CONNECTED)
306 if (ret == PFD_FUNCT_ERROR)
311 /* recheck capabilities after authentication */
312 ret = pop_capabilities (pop_data, 2);
313 if (ret == PQ_NOT_CONNECTED)
320 /* get total size of mailbox */
321 strfcpy (buf, "STAT\r\n", sizeof (buf));
322 ret = pop_query (pop_data, buf, sizeof (buf));
323 if (ret == PQ_NOT_CONNECTED)
326 mutt_error ("%s", pop_data->err_msg);
331 sscanf (buf, "+OK %u %u", &n, &size);
332 pop_data->size = size;
336 pop_data->status = POP_DISCONNECTED;
337 mutt_error _("Server closed connection!");
340 return PQ_NOT_CONNECTED;
343 /* logout from POP server */
344 void pop_logout (CONTEXT * ctx)
346 pop_query_status ret = 0;
347 char buf[LONG_STRING];
348 POP_DATA *pop_data = (POP_DATA *) ctx->data;
350 if (pop_data->status == POP_CONNECTED) {
351 mutt_message _("Closing connection to POP server...");
354 strfcpy (buf, "RSET\r\n", sizeof (buf));
355 ret = pop_query (pop_data, buf, sizeof (buf));
358 if (ret != PQ_NOT_CONNECTED) {
359 strfcpy (buf, "QUIT\r\n", sizeof (buf));
360 pop_query (pop_data, buf, sizeof (buf));
366 pop_data->status = POP_DISCONNECTED;
371 * Send data from buffer and receive answer to the same buffer
373 * -1 - conection lost,
374 * -2 - invalid command or execution error.
376 pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, char *msg)
378 int dbg = M_SOCK_LOG_CMD;
381 if (pop_data->status != POP_CONNECTED)
382 return PQ_NOT_CONNECTED;
385 /* print msg instaed of real command */
387 dbg = M_SOCK_LOG_FULL;
388 dprint (M_SOCK_LOG_CMD, (debugfile, "> %s", msg));
392 mutt_socket_write_d (pop_data->conn, buf, dbg);
394 c = strpbrk (buf, " \r\n");
396 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
398 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
399 pop_data->status = POP_DISCONNECTED;
400 return PQ_NOT_CONNECTED;
402 if (!mutt_strncmp (buf, "+OK", 3))
405 pop_error (pop_data, buf);
410 * This function calls funct(*line, *data) for each received line,
411 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
414 * -1 - conection lost,
415 * -2 - invalid command or execution error,
416 * -3 - error in funct(*line, *data)
418 pop_query_status pop_fetch_data (POP_DATA * pop_data, char *query, char *msg,
419 int (*funct) (char *, void *), void *data)
421 char buf[LONG_STRING];
424 pop_query_status ret;
428 strfcpy (buf, query, sizeof (buf));
429 ret = pop_query (pop_data, buf, sizeof (buf));
433 inbuf = safe_malloc (sizeof (buf));
437 mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
440 pop_data->status = POP_DISCONNECTED;
441 ret = PQ_NOT_CONNECTED;
446 if (!lenbuf && buf[0] == '.') {
452 strfcpy (inbuf + lenbuf, p, sizeof (buf));
454 if (chunk >= sizeof (buf)) {
455 lenbuf += strlen (p);
459 if (msg && ReadInc && (line % ReadInc == 0))
460 mutt_message ("%s %d", msg, line);
461 if (ret == 0 && funct (inbuf, data) < 0)
462 ret = PFD_FUNCT_ERROR;
466 safe_realloc (&inbuf, lenbuf + sizeof (buf));
473 /* find message with this UIDL and set refno */
474 static int check_uidl (char *line, void *data)
478 CONTEXT *ctx = (CONTEXT *) data;
480 sscanf (line, "%u %s", &index, line);
481 for (i = 0; i < ctx->msgcount; i++) {
482 if (!mutt_strcmp (ctx->hdrs[i]->data, line)) {
483 ctx->hdrs[i]->refno = index;
491 /* reconnect and verify indexes if connection was lost */
492 pop_query_status pop_reconnect (CONTEXT * ctx)
494 pop_query_status ret;
495 POP_DATA *pop_data = (POP_DATA *) ctx->data;
497 if (pop_data->status == POP_CONNECTED)
499 if (pop_data->status == POP_BYE)
500 return PQ_NOT_CONNECTED;
503 mutt_socket_close (pop_data->conn);
505 ret = pop_open_connection (pop_data);
507 char *msg = _("Verifying message indexes...");
510 for (i = 0; i < ctx->msgcount; i++)
511 ctx->hdrs[i]->refno = -1;
515 ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
517 mutt_error ("%s", pop_data->err_msg);
527 return PQ_NOT_CONNECTED;
529 if (query_quadoption (OPT_POPRECONNECT,
530 _("Connection lost. Reconnect to POP server?")) !=
532 return PQ_NOT_CONNECTED;