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/url.h>
27 #if defined (USE_SSL) || defined (USE_GNUTLS)
28 # include <lib-sys/mutt_ssl.h>
31 /* given an POP mailbox name, return host, port, username and password */
32 int pop_parse_path (const char *path, ACCOUNT * act)
41 act->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 act->flags |= M_ACCT_SSL;
49 act->port = POP_SSL_PORT;
52 if ((!url.path || !*url.path) && mutt_account_fromurl (act, &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 (!m_strncmp(msg, "-ERR ", 5)) {
69 c2 = vskipspaces(msg + 5);
74 m_strcpy(t, sizeof(pop_data->err_msg) - strlen(pop_data->err_msg), c);
75 m_strrtrim(pop_data->err_msg);
78 /* Parse CAPA output */
79 static int fetch_capa (char *line, void *data)
81 POP_DATA *pop_data = (POP_DATA *) data;
84 if (!ascii_strncasecmp (line, "SASL", 4)) {
85 p_delete(&pop_data->auth_list);
86 c = vskipspaces(line + 4);
87 pop_data->auth_list = m_strdup(c);
90 else if (!ascii_strncasecmp (line, "STLS", 4))
91 pop_data->cmd_stls = CMD_AVAILABLE;
93 else if (!ascii_strncasecmp (line, "USER", 4))
94 pop_data->cmd_user = CMD_AVAILABLE;
96 else if (!ascii_strncasecmp (line, "UIDL", 4))
97 pop_data->cmd_uidl = CMD_AVAILABLE;
99 else if (!ascii_strncasecmp (line, "TOP", 3))
100 pop_data->cmd_top = CMD_AVAILABLE;
105 /* Fetch list of the authentication mechanisms */
106 static int fetch_auth (char *line, void *data)
108 POP_DATA *pop_data = (POP_DATA *) data;
110 if (!pop_data->auth_list) {
111 pop_data->auth_list = p_new(char, strlen(line) + 1);
113 p_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 p_delete(&pop_data->auth_list);
149 /* Execute CAPA command */
150 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
151 m_strcpy(buf, sizeof(buf), "CAPA\r\n");
152 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
155 pop_data->cmd_capa = CMD_AVAILABLE;
158 case PFD_FUNCT_ERROR:
161 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
164 case PQ_NOT_CONNECTED:
165 return PQ_NOT_CONNECTED;
169 /* CAPA not supported, use defaults */
170 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
171 pop_data->cmd_user = CMD_UNKNOWN;
172 pop_data->cmd_uidl = CMD_UNKNOWN;
173 pop_data->cmd_top = CMD_UNKNOWN;
175 m_strcpy(buf, sizeof(buf), "AUTH\r\n");
176 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
177 return PQ_NOT_CONNECTED;
180 /* Check capabilities */
184 if (!pop_data->expire)
185 msg = _("Unable to leave messages on server.");
186 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
187 msg = _("Command TOP is not supported by server.");
188 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
189 msg = _("Command UIDL is not supported by server.");
190 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
194 pop_data->capabilities = 1;
203 * -1 - conection lost,
204 * -2 - invalid response.
206 pop_query_status pop_connect (POP_DATA * pop_data)
208 char buf[LONG_STRING];
210 pop_data->status = POP_NONE;
211 if (mutt_socket_open (pop_data->conn) < 0 ||
212 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
213 mutt_error (_("Error connecting to server: %s"),
214 pop_data->conn->account.host);
215 return PQ_NOT_CONNECTED;
218 pop_data->status = POP_CONNECTED;
220 if (m_strncmp(buf, "+OK", 3)) {
221 *pop_data->err_msg = '\0';
222 pop_error (pop_data, buf);
223 mutt_error ("%s", pop_data->err_msg);
227 pop_apop_timestamp (pop_data, buf);
233 * Open connection and authenticate
235 * -1 - conection lost,
236 * -2 - invalid command or execution error,
237 * -3 - authentication canceled.
239 pop_query_status pop_open_connection (POP_DATA * pop_data)
241 pop_query_status ret;
242 unsigned int n, size;
243 char buf[LONG_STRING];
245 ret = pop_connect (pop_data);
251 ret = pop_capabilities (pop_data, 0);
252 if (ret == PQ_NOT_CONNECTED)
259 #if (defined(USE_SSL) || defined(USE_GNUTLS))
260 /* Attempt STLS if available and desired. */
261 if (!pop_data->conn->ssf && (pop_data->cmd_stls || option(OPTSSLFORCETLS))) {
262 if (option (OPTSSLFORCETLS))
263 pop_data->use_stls = 2;
264 if (pop_data->use_stls == 0) {
265 ret = query_quadoption (OPT_SSLSTARTTLS,
266 _("Secure connection with TLS?"));
269 pop_data->use_stls = 1;
271 pop_data->use_stls = 2;
273 if (pop_data->use_stls == 2) {
274 m_strcpy(buf, sizeof(buf), "STLS\r\n");
275 ret = pop_query (pop_data, buf, sizeof (buf));
276 if (ret == PQ_NOT_CONNECTED)
279 mutt_error ("%s", pop_data->err_msg);
282 #if defined (USE_SSL) || defined (USE_GNUTLS)
283 else if (mutt_ssl_starttls (pop_data->conn))
286 mutt_error (_("Could not negotiate TLS connection"));
291 /* recheck capabilities after STLS completes */
292 ret = pop_capabilities (pop_data, 1);
293 if (ret == PQ_NOT_CONNECTED)
303 if (option(OPTSSLFORCETLS) && !pop_data->conn->ssf) {
304 mutt_error _("Encrypted connection unavailable");
310 ret = pop_authenticate (pop_data);
311 if (ret == PQ_NOT_CONNECTED)
313 if (ret == PFD_FUNCT_ERROR)
318 /* recheck capabilities after authentication */
319 ret = pop_capabilities (pop_data, 2);
320 if (ret == PQ_NOT_CONNECTED)
327 /* get total size of mailbox */
328 m_strcpy(buf, sizeof(buf), "STAT\r\n");
329 ret = pop_query (pop_data, buf, sizeof (buf));
330 if (ret == PQ_NOT_CONNECTED)
333 mutt_error ("%s", pop_data->err_msg);
338 sscanf (buf, "+OK %u %u", &n, &size);
339 pop_data->size = size;
343 pop_data->status = POP_DISCONNECTED;
344 mutt_error _("Server closed connection!");
347 return PQ_NOT_CONNECTED;
350 /* logout from POP server */
351 void pop_logout (CONTEXT * ctx)
353 pop_query_status ret = 0;
354 char buf[LONG_STRING];
355 POP_DATA *pop_data = (POP_DATA *) ctx->data;
357 if (pop_data->status == POP_CONNECTED) {
358 mutt_message _("Closing connection to POP server...");
361 m_strcpy(buf, sizeof(buf), "RSET\r\n");
362 ret = pop_query (pop_data, buf, sizeof (buf));
365 if (ret != PQ_NOT_CONNECTED) {
366 m_strcpy(buf, sizeof(buf), "QUIT\r\n");
367 pop_query (pop_data, buf, sizeof (buf));
373 pop_data->status = POP_DISCONNECTED;
378 * Send data from buffer and receive answer to the same buffer
380 * -1 - conection lost,
381 * -2 - invalid command or execution error.
383 pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, const char *msg)
387 if (pop_data->status != POP_CONNECTED)
388 return PQ_NOT_CONNECTED;
390 mutt_socket_write(pop_data->conn, buf);
392 c = strpbrk (buf, " \r\n");
394 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
396 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
397 pop_data->status = POP_DISCONNECTED;
398 return PQ_NOT_CONNECTED;
400 if (!m_strncmp(buf, "+OK", 3))
403 pop_error (pop_data, buf);
408 * This function calls funct(*line, *data) for each received line,
409 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
412 * -1 - conection lost,
413 * -2 - invalid command or execution error,
414 * -3 - error in funct(*line, *data)
416 pop_query_status pop_fetch_data (POP_DATA * pop_data, const char *query, progress_t* bar,
417 int (*funct) (char *, void *), void *data)
419 char buf[LONG_STRING];
422 pop_query_status ret;
427 m_strcpy(buf, sizeof(buf), query);
428 ret = pop_query (pop_data, buf, sizeof (buf));
432 inbuf = p_new(char, sizeof(buf));
436 mutt_socket_readln(buf, sizeof (buf), pop_data->conn);
438 pop_data->status = POP_DISCONNECTED;
439 ret = PQ_NOT_CONNECTED;
444 if (!lenbuf && buf[0] == '.') {
450 m_strcpy(inbuf + lenbuf,sizeof(buf), p);
453 if (chunk >= ssizeof(buf)) {
454 lenbuf += strlen (p);
457 mutt_progress_bar (bar, pos);
458 if (ret == 0 && funct (inbuf, data) < 0)
459 ret = PFD_FUNCT_ERROR;
463 p_realloc(&inbuf, lenbuf + sizeof(buf));
470 /* find message with this UIDL and set refno */
471 static int check_uidl (char *line, void *data)
474 CONTEXT *ctx = (CONTEXT *)data;
476 sscanf (line, "%u %s", &idx, line);
477 for (i = 0; i < ctx->msgcount; i++) {
478 if (!m_strcmp(ctx->hdrs[i]->data, line)) {
479 ctx->hdrs[i]->refno = idx;
487 /* reconnect and verify indexes if connection was lost */
488 pop_query_status pop_reconnect (CONTEXT * ctx)
490 pop_query_status ret;
491 POP_DATA *pop_data = (POP_DATA *) ctx->data;
494 if (pop_data->status == POP_CONNECTED)
496 if (pop_data->status == POP_BYE)
497 return PQ_NOT_CONNECTED;
500 mutt_socket_close (pop_data->conn);
502 ret = pop_open_connection (pop_data);
506 bar.msg = _("Verifying message indexes...");
508 mutt_progress_bar (&bar, 0);
510 for (i = 0; i < ctx->msgcount; i++)
511 ctx->hdrs[i]->refno = -1;
513 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
515 mutt_error ("%s", pop_data->err_msg);
525 return PQ_NOT_CONNECTED;
527 if (query_quadoption (OPT_POPRECONNECT,
528 _("Connection lost. Reconnect to POP server?")) !=
530 return PQ_NOT_CONNECTED;