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.
14 #include <lib-lib/mem.h>
15 #include <lib-lib/str.h>
16 #include <lib-lib/ascii.h>
17 #include <lib-lib/macros.h>
23 #if defined (USE_SSL) || defined (USE_GNUTLS)
24 # include "mutt_ssl.h"
27 #include "lib/debug.h"
33 /* given an POP mailbox name, return host, port, username and password */
34 int pop_parse_path (const char *path, ACCOUNT * acct)
42 acct->port = POP_PORT;
43 acct->type = M_ACCT_TYPE_POP;
46 url_parse_ciss (&url, c);
48 if (url.scheme == U_POP || url.scheme == U_POPS) {
49 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 (!m_strncmp(msg, "-ERR ", 5)) {
71 c2 = vskipspaces(msg + 5);
76 m_strcpy(t, sizeof(pop_data->err_msg) - strlen(pop_data->err_msg), c);
77 m_strrtrim(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 p_delete(&pop_data->auth_list);
88 c = vskipspaces(line + 4);
89 pop_data->auth_list = m_strdup(c);
92 else if (!ascii_strncasecmp (line, "STLS", 4))
93 pop_data->cmd_stls = CMD_AVAILABLE;
95 else if (!ascii_strncasecmp (line, "USER", 4))
96 pop_data->cmd_user = CMD_AVAILABLE;
98 else if (!ascii_strncasecmp (line, "UIDL", 4))
99 pop_data->cmd_uidl = CMD_AVAILABLE;
101 else if (!ascii_strncasecmp (line, "TOP", 3))
102 pop_data->cmd_top = CMD_AVAILABLE;
107 /* Fetch list of the authentication mechanisms */
108 static int fetch_auth (char *line, void *data)
110 POP_DATA *pop_data = (POP_DATA *) data;
112 if (!pop_data->auth_list) {
113 pop_data->auth_list = p_new(char, strlen(line) + 1);
115 p_realloc(&pop_data->auth_list,
116 strlen(pop_data->auth_list) + strlen(line) + 2);
117 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
119 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
127 * -1 - conection lost,
128 * -2 - execution error.
130 static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode)
132 char buf[LONG_STRING];
134 /* don't check capabilities on reconnect */
135 if (pop_data->capabilities)
138 /* init capabilities */
140 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
141 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
142 pop_data->cmd_user = CMD_NOT_AVAILABLE;
143 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
144 pop_data->cmd_top = CMD_NOT_AVAILABLE;
145 pop_data->resp_codes = 0;
146 pop_data->expire = 1;
147 pop_data->login_delay = 0;
148 p_delete(&pop_data->auth_list);
151 /* Execute CAPA command */
152 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
153 m_strcpy(buf, sizeof(buf), "CAPA\r\n");
154 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
157 pop_data->cmd_capa = CMD_AVAILABLE;
160 case PFD_FUNCT_ERROR:
163 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
166 case PQ_NOT_CONNECTED:
167 return PQ_NOT_CONNECTED;
171 /* CAPA not supported, use defaults */
172 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
173 pop_data->cmd_user = CMD_UNKNOWN;
174 pop_data->cmd_uidl = CMD_UNKNOWN;
175 pop_data->cmd_top = CMD_UNKNOWN;
177 m_strcpy(buf, sizeof(buf), "AUTH\r\n");
178 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
179 return PQ_NOT_CONNECTED;
182 /* Check capabilities */
186 if (!pop_data->expire)
187 msg = _("Unable to leave messages on server.");
188 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
189 msg = _("Command TOP is not supported by server.");
190 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
191 msg = _("Command UIDL is not supported by server.");
192 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
196 pop_data->capabilities = 1;
205 * -1 - conection lost,
206 * -2 - invalid response.
208 pop_query_status pop_connect (POP_DATA * pop_data)
210 char buf[LONG_STRING];
212 pop_data->status = POP_NONE;
213 if (mutt_socket_open (pop_data->conn) < 0 ||
214 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
215 mutt_error (_("Error connecting to server: %s"),
216 pop_data->conn->account.host);
217 return PQ_NOT_CONNECTED;
220 pop_data->status = POP_CONNECTED;
222 if (m_strncmp(buf, "+OK", 3)) {
223 *pop_data->err_msg = '\0';
224 pop_error (pop_data, buf);
225 mutt_error ("%s", pop_data->err_msg);
229 pop_apop_timestamp (pop_data, buf);
235 * Open connection and authenticate
237 * -1 - conection lost,
238 * -2 - invalid command or execution error,
239 * -3 - authentication canceled.
241 pop_query_status pop_open_connection (POP_DATA * pop_data)
243 pop_query_status ret;
244 unsigned int n, size;
245 char buf[LONG_STRING];
247 ret = pop_connect (pop_data);
253 ret = pop_capabilities (pop_data, 0);
254 if (ret == PQ_NOT_CONNECTED)
261 #if (defined(USE_SSL) || defined(USE_GNUTLS))
262 /* Attempt STLS if available and desired. */
263 if (!pop_data->conn->ssf && (pop_data->cmd_stls || option(OPTSSLFORCETLS))) {
264 if (option (OPTSSLFORCETLS))
265 pop_data->use_stls = 2;
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 m_strcpy(buf, sizeof(buf), "STLS\r\n");
277 ret = pop_query (pop_data, buf, sizeof (buf));
278 if (ret == PQ_NOT_CONNECTED)
281 mutt_error ("%s", pop_data->err_msg);
284 #if defined (USE_SSL) || defined (USE_GNUTLS)
285 else if (mutt_ssl_starttls (pop_data->conn))
288 mutt_error (_("Could not negotiate TLS connection"));
293 /* recheck capabilities after STLS completes */
294 ret = pop_capabilities (pop_data, 1);
295 if (ret == PQ_NOT_CONNECTED)
305 if (option(OPTSSLFORCETLS) && !pop_data->conn->ssf) {
306 mutt_error _("Encrypted connection unavailable");
312 ret = pop_authenticate (pop_data);
313 if (ret == PQ_NOT_CONNECTED)
315 if (ret == PFD_FUNCT_ERROR)
320 /* recheck capabilities after authentication */
321 ret = pop_capabilities (pop_data, 2);
322 if (ret == PQ_NOT_CONNECTED)
329 /* get total size of mailbox */
330 m_strcpy(buf, sizeof(buf), "STAT\r\n");
331 ret = pop_query (pop_data, buf, sizeof (buf));
332 if (ret == PQ_NOT_CONNECTED)
335 mutt_error ("%s", pop_data->err_msg);
340 sscanf (buf, "+OK %u %u", &n, &size);
341 pop_data->size = size;
345 pop_data->status = POP_DISCONNECTED;
346 mutt_error _("Server closed connection!");
349 return PQ_NOT_CONNECTED;
352 /* logout from POP server */
353 void pop_logout (CONTEXT * ctx)
355 pop_query_status ret = 0;
356 char buf[LONG_STRING];
357 POP_DATA *pop_data = (POP_DATA *) ctx->data;
359 if (pop_data->status == POP_CONNECTED) {
360 mutt_message _("Closing connection to POP server...");
363 m_strcpy(buf, sizeof(buf), "RSET\r\n");
364 ret = pop_query (pop_data, buf, sizeof (buf));
367 if (ret != PQ_NOT_CONNECTED) {
368 m_strcpy(buf, sizeof(buf), "QUIT\r\n");
369 pop_query (pop_data, buf, sizeof (buf));
375 pop_data->status = POP_DISCONNECTED;
380 * Send data from buffer and receive answer to the same buffer
382 * -1 - conection lost,
383 * -2 - invalid command or execution error.
385 pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, const char *msg)
387 int dbg = M_SOCK_LOG_CMD;
390 if (pop_data->status != POP_CONNECTED)
391 return PQ_NOT_CONNECTED;
394 /* print msg instaed of real command */
396 dbg = M_SOCK_LOG_FULL;
397 debug_print (M_SOCK_LOG_CMD, ("> %s", msg));
401 mutt_socket_write_d (pop_data->conn, buf, dbg);
403 c = strpbrk (buf, " \r\n");
405 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
407 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
408 pop_data->status = POP_DISCONNECTED;
409 return PQ_NOT_CONNECTED;
411 if (!m_strncmp(buf, "+OK", 3))
414 pop_error (pop_data, buf);
419 * This function calls funct(*line, *data) for each received line,
420 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
423 * -1 - conection lost,
424 * -2 - invalid command or execution error,
425 * -3 - error in funct(*line, *data)
427 pop_query_status pop_fetch_data (POP_DATA * pop_data, const char *query, progress_t* bar,
428 int (*funct) (char *, void *), void *data)
430 char buf[LONG_STRING];
433 pop_query_status ret;
438 m_strcpy(buf, sizeof(buf), query);
439 ret = pop_query (pop_data, buf, sizeof (buf));
443 inbuf = p_new(char, sizeof(buf));
447 mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
450 pop_data->status = POP_DISCONNECTED;
451 ret = PQ_NOT_CONNECTED;
456 if (!lenbuf && buf[0] == '.') {
462 m_strcpy(inbuf + lenbuf,sizeof(buf), p);
465 if (chunk >= sizeof (buf)) {
466 lenbuf += strlen (p);
470 mutt_progress_bar (bar, pos);
471 if (ret == 0 && funct (inbuf, data) < 0)
472 ret = PFD_FUNCT_ERROR;
476 p_realloc(&inbuf, lenbuf + sizeof(buf));
483 /* find message with this UIDL and set refno */
484 static int check_uidl (char *line, void *data)
488 CONTEXT *ctx = (CONTEXT *) data;
490 sscanf (line, "%u %s", &index, line);
491 for (i = 0; i < ctx->msgcount; i++) {
492 if (!m_strcmp(ctx->hdrs[i]->data, line)) {
493 ctx->hdrs[i]->refno = index;
501 /* reconnect and verify indexes if connection was lost */
502 pop_query_status pop_reconnect (CONTEXT * ctx)
504 pop_query_status ret;
505 POP_DATA *pop_data = (POP_DATA *) ctx->data;
508 if (pop_data->status == POP_CONNECTED)
510 if (pop_data->status == POP_BYE)
511 return PQ_NOT_CONNECTED;
514 mutt_socket_close (pop_data->conn);
516 ret = pop_open_connection (pop_data);
520 bar.msg = _("Verifying message indexes...");
522 mutt_progress_bar (&bar, 0);
524 for (i = 0; i < ctx->msgcount; i++)
525 ctx->hdrs[i]->refno = -1;
527 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
529 mutt_error ("%s", pop_data->err_msg);
539 return PQ_NOT_CONNECTED;
541 if (query_quadoption (OPT_POPRECONNECT,
542 _("Connection lost. Reconnect to POP server?")) !=
544 return PQ_NOT_CONNECTED;