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) - mutt_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 = 1;
91 else if (!ascii_strncasecmp (line, "USER", 4))
92 pop_data->cmd_user = 1;
94 else if (!ascii_strncasecmp (line, "UIDL", 4))
95 pop_data->cmd_uidl = 1;
97 else if (!ascii_strncasecmp (line, "TOP", 3))
98 pop_data->cmd_top = 1;
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 (mutt_strlen (line) + 1);
110 *pop_data->auth_list = '\0';
113 safe_realloc (&pop_data->auth_list,
114 mutt_strlen (pop_data->auth_list) + mutt_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 int 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 = 0;
139 pop_data->cmd_stls = 0;
140 pop_data->cmd_user = 0;
141 pop_data->cmd_uidl = 0;
142 pop_data->cmd_top = 0;
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) {
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 = 1;
163 /* CAPA not supported, use defaults */
164 if (mode == 0 && !pop_data->cmd_capa) {
165 pop_data->cmd_user = 2;
166 pop_data->cmd_uidl = 2;
167 pop_data->cmd_top = 2;
169 strfcpy (buf, "AUTH\r\n", sizeof (buf));
170 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == -1)
174 /* Check capabilities */
178 if (!pop_data->expire)
179 msg = _("Unable to leave messages on server.");
180 if (!pop_data->cmd_top)
181 msg = _("Command TOP is not supported by server.");
182 if (!pop_data->cmd_uidl)
183 msg = _("Command UIDL is not supported by server.");
184 if (msg && pop_data->cmd_capa) {
188 pop_data->capabilities = 1;
197 * -1 - conection lost,
198 * -2 - invalid response.
200 int pop_connect (POP_DATA * pop_data)
202 char buf[LONG_STRING];
204 pop_data->status = POP_NONE;
205 if (mutt_socket_open (pop_data->conn) < 0 ||
206 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
207 mutt_error (_("Error connecting to server: %s"),
208 pop_data->conn->account.host);
212 pop_data->status = POP_CONNECTED;
214 if (mutt_strncmp (buf, "+OK", 3)) {
215 *pop_data->err_msg = '\0';
216 pop_error (pop_data, buf);
217 mutt_error ("%s", pop_data->err_msg);
221 pop_apop_timestamp (pop_data, buf);
227 * Open connection and authenticate
229 * -1 - conection lost,
230 * -2 - invalid command or execution error,
231 * -3 - authentication canceled.
233 int pop_open_connection (POP_DATA * pop_data)
236 unsigned int n, size;
237 char buf[LONG_STRING];
239 ret = pop_connect (pop_data);
245 ret = pop_capabilities (pop_data, 0);
253 #if (defined(USE_SSL) || defined(USE_GNUTLS)) && !defined(USE_NSS)
254 /* Attempt STLS if available and desired. */
255 if (pop_data->cmd_stls && !pop_data->conn->ssf) {
256 if (pop_data->use_stls == 0) {
257 ret = query_quadoption (OPT_SSLSTARTTLS,
258 _("Secure connection with TLS?"));
261 pop_data->use_stls = 1;
263 pop_data->use_stls = 2;
265 if (pop_data->use_stls == 2) {
266 strfcpy (buf, "STLS\r\n", sizeof (buf));
267 ret = pop_query (pop_data, buf, sizeof (buf));
271 mutt_error ("%s", pop_data->err_msg);
275 else if (mutt_ssl_starttls (pop_data->conn))
277 else if (mutt_gnutls_starttls (pop_data->conn))
280 mutt_error (_("Could not negotiate TLS connection"));
285 /* recheck capabilities after STLS completes */
286 ret = pop_capabilities (pop_data, 1);
298 ret = pop_authenticate (pop_data);
306 /* recheck capabilities after authentication */
307 ret = pop_capabilities (pop_data, 2);
315 /* get total size of mailbox */
316 strfcpy (buf, "STAT\r\n", sizeof (buf));
317 ret = pop_query (pop_data, buf, sizeof (buf));
321 mutt_error ("%s", pop_data->err_msg);
326 sscanf (buf, "+OK %u %u", &n, &size);
327 pop_data->size = size;
331 pop_data->status = POP_DISCONNECTED;
332 mutt_error _("Server closed connection!");
338 /* logout from POP server */
339 void pop_logout (CONTEXT * ctx)
342 char buf[LONG_STRING];
343 POP_DATA *pop_data = (POP_DATA *) ctx->data;
345 if (pop_data->status == POP_CONNECTED) {
346 mutt_message _("Closing connection to POP server...");
349 strfcpy (buf, "RSET\r\n", sizeof (buf));
350 ret = pop_query (pop_data, buf, sizeof (buf));
354 strfcpy (buf, "QUIT\r\n", sizeof (buf));
355 pop_query (pop_data, buf, sizeof (buf));
361 pop_data->status = POP_DISCONNECTED;
366 * Send data from buffer and receive answer to the same buffer
368 * -1 - conection lost,
369 * -2 - invalid command or execution error.
371 int pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, char *msg)
373 int dbg = M_SOCK_LOG_CMD;
376 if (pop_data->status != POP_CONNECTED)
380 /* print msg instaed of real command */
382 dbg = M_SOCK_LOG_FULL;
383 dprint (M_SOCK_LOG_CMD, (debugfile, "> %s", msg));
387 mutt_socket_write_d (pop_data->conn, buf, dbg);
389 c = strpbrk (buf, " \r\n");
391 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
393 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
394 pop_data->status = POP_DISCONNECTED;
397 if (!mutt_strncmp (buf, "+OK", 3))
400 pop_error (pop_data, buf);
405 * This function calls funct(*line, *data) for each received line,
406 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
409 * -1 - conection lost,
410 * -2 - invalid command or execution error,
411 * -3 - error in funct(*line, *data)
413 int pop_fetch_data (POP_DATA * pop_data, char *query, char *msg,
414 int (*funct) (char *, void *), void *data)
416 char buf[LONG_STRING];
419 int ret, chunk, line = 0;
422 strfcpy (buf, query, sizeof (buf));
423 ret = pop_query (pop_data, buf, sizeof (buf));
427 inbuf = safe_malloc (sizeof (buf));
431 mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
434 pop_data->status = POP_DISCONNECTED;
440 if (!lenbuf && buf[0] == '.') {
446 strfcpy (inbuf + lenbuf, p, sizeof (buf));
448 if (chunk >= sizeof (buf)) {
449 lenbuf += mutt_strlen (p);
453 if (msg && ReadInc && (line % ReadInc == 0))
454 mutt_message ("%s %d", msg, line);
455 if (ret == 0 && funct (inbuf, data) < 0)
460 safe_realloc (&inbuf, lenbuf + sizeof (buf));
467 /* find message with this UIDL and set refno */
468 static int check_uidl (char *line, void *data)
472 CONTEXT *ctx = (CONTEXT *) data;
474 sscanf (line, "%u %s", &index, line);
475 for (i = 0; i < ctx->msgcount; i++) {
476 if (!mutt_strcmp (ctx->hdrs[i]->data, line)) {
477 ctx->hdrs[i]->refno = index;
485 /* reconnect and verify idnexes if connection was lost */
486 int pop_reconnect (CONTEXT * ctx)
489 POP_DATA *pop_data = (POP_DATA *) ctx->data;
491 if (pop_data->status == POP_CONNECTED)
493 if (pop_data->status == POP_BYE)
497 mutt_socket_close (pop_data->conn);
499 ret = pop_open_connection (pop_data);
501 char *msg = _("Verifying message indexes...");
504 for (i = 0; i < ctx->msgcount; i++)
505 ctx->hdrs[i]->refno = -1;
509 ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
511 mutt_error ("%s", pop_data->err_msg);
523 if (query_quadoption (OPT_POPRECONNECT,
524 _("Connection lost. Reconnect to POP server?")) !=