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.
18 #include <lib-lib/mem.h>
19 #include <lib-lib/str.h>
20 #include <lib-lib/ascii.h>
21 #include <lib-lib/macros.h>
22 #include <lib-lib/debug.h>
23 #include <lib-lib/url.h>
28 #if defined (USE_SSL) || defined (USE_GNUTLS)
29 # include <lib-sys/mutt_ssl.h>
32 /* given an POP mailbox name, return host, port, username and password */
33 int pop_parse_path (const char *path, ACCOUNT * act)
42 act->type = M_ACCT_TYPE_POP;
45 url_parse_ciss (&url, c);
47 if (url.scheme == U_POP || url.scheme == U_POPS) {
48 if (url.scheme == U_POPS) {
49 act->flags |= M_ACCT_SSL;
50 act->port = POP_SSL_PORT;
53 if ((!url.path || !*url.path) && mutt_account_fromurl (act, &url) == 0)
61 /* Copy error message to err_msg buffer */
62 void pop_error (POP_DATA * pop_data, char *msg)
66 t = strchr (pop_data->err_msg, '\0');
69 if (!m_strncmp(msg, "-ERR ", 5)) {
70 c2 = vskipspaces(msg + 5);
75 m_strcpy(t, sizeof(pop_data->err_msg) - strlen(pop_data->err_msg), c);
76 m_strrtrim(pop_data->err_msg);
79 /* Parse CAPA output */
80 static int fetch_capa (char *line, void *data)
82 POP_DATA *pop_data = (POP_DATA *) data;
85 if (!ascii_strncasecmp (line, "SASL", 4)) {
86 p_delete(&pop_data->auth_list);
87 c = vskipspaces(line + 4);
88 pop_data->auth_list = m_strdup(c);
91 else if (!ascii_strncasecmp (line, "STLS", 4))
92 pop_data->cmd_stls = CMD_AVAILABLE;
94 else if (!ascii_strncasecmp (line, "USER", 4))
95 pop_data->cmd_user = CMD_AVAILABLE;
97 else if (!ascii_strncasecmp (line, "UIDL", 4))
98 pop_data->cmd_uidl = CMD_AVAILABLE;
100 else if (!ascii_strncasecmp (line, "TOP", 3))
101 pop_data->cmd_top = CMD_AVAILABLE;
106 /* Fetch list of the authentication mechanisms */
107 static int fetch_auth (char *line, void *data)
109 POP_DATA *pop_data = (POP_DATA *) data;
111 if (!pop_data->auth_list) {
112 pop_data->auth_list = p_new(char, strlen(line) + 1);
114 p_realloc(&pop_data->auth_list,
115 strlen(pop_data->auth_list) + strlen(line) + 2);
116 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
118 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
126 * -1 - conection lost,
127 * -2 - execution error.
129 static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode)
131 char buf[LONG_STRING];
133 /* don't check capabilities on reconnect */
134 if (pop_data->capabilities)
137 /* init capabilities */
139 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
140 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
141 pop_data->cmd_user = CMD_NOT_AVAILABLE;
142 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
143 pop_data->cmd_top = CMD_NOT_AVAILABLE;
144 pop_data->resp_codes = 0;
145 pop_data->expire = 1;
146 pop_data->login_delay = 0;
147 p_delete(&pop_data->auth_list);
150 /* Execute CAPA command */
151 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
152 m_strcpy(buf, sizeof(buf), "CAPA\r\n");
153 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
156 pop_data->cmd_capa = CMD_AVAILABLE;
159 case PFD_FUNCT_ERROR:
162 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
165 case PQ_NOT_CONNECTED:
166 return PQ_NOT_CONNECTED;
170 /* CAPA not supported, use defaults */
171 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
172 pop_data->cmd_user = CMD_UNKNOWN;
173 pop_data->cmd_uidl = CMD_UNKNOWN;
174 pop_data->cmd_top = CMD_UNKNOWN;
176 m_strcpy(buf, sizeof(buf), "AUTH\r\n");
177 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
178 return PQ_NOT_CONNECTED;
181 /* Check capabilities */
185 if (!pop_data->expire)
186 msg = _("Unable to leave messages on server.");
187 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
188 msg = _("Command TOP is not supported by server.");
189 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
190 msg = _("Command UIDL is not supported by server.");
191 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
195 pop_data->capabilities = 1;
204 * -1 - conection lost,
205 * -2 - invalid response.
207 pop_query_status pop_connect (POP_DATA * pop_data)
209 char buf[LONG_STRING];
211 pop_data->status = POP_NONE;
212 if (mutt_socket_open (pop_data->conn) < 0 ||
213 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
214 mutt_error (_("Error connecting to server: %s"),
215 pop_data->conn->account.host);
216 return PQ_NOT_CONNECTED;
219 pop_data->status = POP_CONNECTED;
221 if (m_strncmp(buf, "+OK", 3)) {
222 *pop_data->err_msg = '\0';
223 pop_error (pop_data, buf);
224 mutt_error ("%s", pop_data->err_msg);
228 pop_apop_timestamp (pop_data, buf);
234 * Open connection and authenticate
236 * -1 - conection lost,
237 * -2 - invalid command or execution error,
238 * -3 - authentication canceled.
240 pop_query_status pop_open_connection (POP_DATA * pop_data)
242 pop_query_status ret;
243 unsigned int n, size;
244 char buf[LONG_STRING];
246 ret = pop_connect (pop_data);
252 ret = pop_capabilities (pop_data, 0);
253 if (ret == PQ_NOT_CONNECTED)
260 #if (defined(USE_SSL) || defined(USE_GNUTLS))
261 /* Attempt STLS if available and desired. */
262 if (!pop_data->conn->ssf && (pop_data->cmd_stls || option(OPTSSLFORCETLS))) {
263 if (option (OPTSSLFORCETLS))
264 pop_data->use_stls = 2;
265 if (pop_data->use_stls == 0) {
266 ret = query_quadoption (OPT_SSLSTARTTLS,
267 _("Secure connection with TLS?"));
270 pop_data->use_stls = 1;
272 pop_data->use_stls = 2;
274 if (pop_data->use_stls == 2) {
275 m_strcpy(buf, sizeof(buf), "STLS\r\n");
276 ret = pop_query (pop_data, buf, sizeof (buf));
277 if (ret == PQ_NOT_CONNECTED)
280 mutt_error ("%s", pop_data->err_msg);
283 #if defined (USE_SSL) || defined (USE_GNUTLS)
284 else if (mutt_ssl_starttls (pop_data->conn))
287 mutt_error (_("Could not negotiate TLS connection"));
292 /* recheck capabilities after STLS completes */
293 ret = pop_capabilities (pop_data, 1);
294 if (ret == PQ_NOT_CONNECTED)
304 if (option(OPTSSLFORCETLS) && !pop_data->conn->ssf) {
305 mutt_error _("Encrypted connection unavailable");
311 ret = pop_authenticate (pop_data);
312 if (ret == PQ_NOT_CONNECTED)
314 if (ret == PFD_FUNCT_ERROR)
319 /* recheck capabilities after authentication */
320 ret = pop_capabilities (pop_data, 2);
321 if (ret == PQ_NOT_CONNECTED)
328 /* get total size of mailbox */
329 m_strcpy(buf, sizeof(buf), "STAT\r\n");
330 ret = pop_query (pop_data, buf, sizeof (buf));
331 if (ret == PQ_NOT_CONNECTED)
334 mutt_error ("%s", pop_data->err_msg);
339 sscanf (buf, "+OK %u %u", &n, &size);
340 pop_data->size = size;
344 pop_data->status = POP_DISCONNECTED;
345 mutt_error _("Server closed connection!");
348 return PQ_NOT_CONNECTED;
351 /* logout from POP server */
352 void pop_logout (CONTEXT * ctx)
354 pop_query_status ret = 0;
355 char buf[LONG_STRING];
356 POP_DATA *pop_data = (POP_DATA *) ctx->data;
358 if (pop_data->status == POP_CONNECTED) {
359 mutt_message _("Closing connection to POP server...");
362 m_strcpy(buf, sizeof(buf), "RSET\r\n");
363 ret = pop_query (pop_data, buf, sizeof (buf));
366 if (ret != PQ_NOT_CONNECTED) {
367 m_strcpy(buf, sizeof(buf), "QUIT\r\n");
368 pop_query (pop_data, buf, sizeof (buf));
374 pop_data->status = POP_DISCONNECTED;
379 * Send data from buffer and receive answer to the same buffer
381 * -1 - conection lost,
382 * -2 - invalid command or execution error.
384 pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, const char *msg)
386 int dbg = M_SOCK_LOG_CMD;
389 if (pop_data->status != POP_CONNECTED)
390 return PQ_NOT_CONNECTED;
393 /* print msg instaed of real command */
395 dbg = M_SOCK_LOG_FULL;
396 debug_print (M_SOCK_LOG_CMD, ("> %s", msg));
400 mutt_socket_write_d (pop_data->conn, buf, dbg);
402 c = strpbrk (buf, " \r\n");
404 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
406 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
407 pop_data->status = POP_DISCONNECTED;
408 return PQ_NOT_CONNECTED;
410 if (!m_strncmp(buf, "+OK", 3))
413 pop_error (pop_data, buf);
418 * This function calls funct(*line, *data) for each received line,
419 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
422 * -1 - conection lost,
423 * -2 - invalid command or execution error,
424 * -3 - error in funct(*line, *data)
426 pop_query_status pop_fetch_data (POP_DATA * pop_data, const char *query, progress_t* bar,
427 int (*funct) (char *, void *), void *data)
429 char buf[LONG_STRING];
432 pop_query_status ret;
437 m_strcpy(buf, sizeof(buf), query);
438 ret = pop_query (pop_data, buf, sizeof (buf));
442 inbuf = p_new(char, sizeof(buf));
446 mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
449 pop_data->status = POP_DISCONNECTED;
450 ret = PQ_NOT_CONNECTED;
455 if (!lenbuf && buf[0] == '.') {
461 m_strcpy(inbuf + lenbuf,sizeof(buf), p);
464 if (chunk >= ssizeof(buf)) {
465 lenbuf += strlen (p);
468 mutt_progress_bar (bar, pos);
469 if (ret == 0 && funct (inbuf, data) < 0)
470 ret = PFD_FUNCT_ERROR;
474 p_realloc(&inbuf, lenbuf + sizeof(buf));
481 /* find message with this UIDL and set refno */
482 static int check_uidl (char *line, void *data)
485 CONTEXT *ctx = (CONTEXT *)data;
487 sscanf (line, "%u %s", &idx, line);
488 for (i = 0; i < ctx->msgcount; i++) {
489 if (!m_strcmp(ctx->hdrs[i]->data, line)) {
490 ctx->hdrs[i]->refno = idx;
498 /* reconnect and verify indexes if connection was lost */
499 pop_query_status pop_reconnect (CONTEXT * ctx)
501 pop_query_status ret;
502 POP_DATA *pop_data = (POP_DATA *) ctx->data;
505 if (pop_data->status == POP_CONNECTED)
507 if (pop_data->status == POP_BYE)
508 return PQ_NOT_CONNECTED;
511 mutt_socket_close (pop_data->conn);
513 ret = pop_open_connection (pop_data);
517 bar.msg = _("Verifying message indexes...");
519 mutt_progress_bar (&bar, 0);
521 for (i = 0; i < ctx->msgcount; i++)
522 ctx->hdrs[i]->refno = -1;
524 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
526 mutt_error ("%s", pop_data->err_msg);
536 return PQ_NOT_CONNECTED;
538 if (query_quadoption (OPT_POPRECONNECT,
539 _("Connection lost. Reconnect to POP server?")) !=
541 return PQ_NOT_CONNECTED;