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/macros.h>
22 #if defined (USE_SSL) || defined (USE_GNUTLS)
23 # include "mutt_ssl.h"
26 #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 (!str_ncmp (msg, "-ERR ", 5)) {
78 strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
79 str_skip_trailws (pop_data->err_msg);
82 /* Parse CAPA output */
83 static int fetch_capa (char *line, void *data)
85 POP_DATA *pop_data = (POP_DATA *) data;
88 if (!ascii_strncasecmp (line, "SASL", 4)) {
89 p_delete(&pop_data->auth_list);
92 pop_data->auth_list = str_dup (c);
95 else if (!ascii_strncasecmp (line, "STLS", 4))
96 pop_data->cmd_stls = CMD_AVAILABLE;
98 else if (!ascii_strncasecmp (line, "USER", 4))
99 pop_data->cmd_user = CMD_AVAILABLE;
101 else if (!ascii_strncasecmp (line, "UIDL", 4))
102 pop_data->cmd_uidl = CMD_AVAILABLE;
104 else if (!ascii_strncasecmp (line, "TOP", 3))
105 pop_data->cmd_top = CMD_AVAILABLE;
110 /* Fetch list of the authentication mechanisms */
111 static int fetch_auth (char *line, void *data)
113 POP_DATA *pop_data = (POP_DATA *) data;
115 if (!pop_data->auth_list) {
116 pop_data->auth_list = p_new(char, strlen(line) + 1);
118 p_realloc(&pop_data->auth_list,
119 strlen(pop_data->auth_list) + strlen(line) + 2);
120 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
122 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
130 * -1 - conection lost,
131 * -2 - execution error.
133 static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode)
135 char buf[LONG_STRING];
137 /* don't check capabilities on reconnect */
138 if (pop_data->capabilities)
141 /* init capabilities */
143 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
144 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
145 pop_data->cmd_user = CMD_NOT_AVAILABLE;
146 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
147 pop_data->cmd_top = CMD_NOT_AVAILABLE;
148 pop_data->resp_codes = 0;
149 pop_data->expire = 1;
150 pop_data->login_delay = 0;
151 p_delete(&pop_data->auth_list);
154 /* Execute CAPA command */
155 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
156 strfcpy (buf, "CAPA\r\n", sizeof (buf));
157 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
160 pop_data->cmd_capa = CMD_AVAILABLE;
163 case PFD_FUNCT_ERROR:
166 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
169 case PQ_NOT_CONNECTED:
170 return PQ_NOT_CONNECTED;
174 /* CAPA not supported, use defaults */
175 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
176 pop_data->cmd_user = CMD_UNKNOWN;
177 pop_data->cmd_uidl = CMD_UNKNOWN;
178 pop_data->cmd_top = CMD_UNKNOWN;
180 strfcpy (buf, "AUTH\r\n", sizeof (buf));
181 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
182 return PQ_NOT_CONNECTED;
185 /* Check capabilities */
189 if (!pop_data->expire)
190 msg = _("Unable to leave messages on server.");
191 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
192 msg = _("Command TOP is not supported by server.");
193 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
194 msg = _("Command UIDL is not supported by server.");
195 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
199 pop_data->capabilities = 1;
208 * -1 - conection lost,
209 * -2 - invalid response.
211 pop_query_status pop_connect (POP_DATA * pop_data)
213 char buf[LONG_STRING];
215 pop_data->status = POP_NONE;
216 if (mutt_socket_open (pop_data->conn) < 0 ||
217 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
218 mutt_error (_("Error connecting to server: %s"),
219 pop_data->conn->account.host);
220 return PQ_NOT_CONNECTED;
223 pop_data->status = POP_CONNECTED;
225 if (str_ncmp (buf, "+OK", 3)) {
226 *pop_data->err_msg = '\0';
227 pop_error (pop_data, buf);
228 mutt_error ("%s", pop_data->err_msg);
232 pop_apop_timestamp (pop_data, buf);
238 * Open connection and authenticate
240 * -1 - conection lost,
241 * -2 - invalid command or execution error,
242 * -3 - authentication canceled.
244 pop_query_status pop_open_connection (POP_DATA * pop_data)
246 pop_query_status ret;
247 unsigned int n, size;
248 char buf[LONG_STRING];
250 ret = pop_connect (pop_data);
256 ret = pop_capabilities (pop_data, 0);
257 if (ret == PQ_NOT_CONNECTED)
264 #if (defined(USE_SSL) || defined(USE_GNUTLS))
265 /* Attempt STLS if available and desired. */
266 if (!pop_data->conn->ssf && (pop_data->cmd_stls || option(OPTSSLFORCETLS))) {
267 if (option (OPTSSLFORCETLS))
268 pop_data->use_stls = 2;
269 if (pop_data->use_stls == 0) {
270 ret = query_quadoption (OPT_SSLSTARTTLS,
271 _("Secure connection with TLS?"));
274 pop_data->use_stls = 1;
276 pop_data->use_stls = 2;
278 if (pop_data->use_stls == 2) {
279 strfcpy (buf, "STLS\r\n", sizeof (buf));
280 ret = pop_query (pop_data, buf, sizeof (buf));
281 if (ret == PQ_NOT_CONNECTED)
284 mutt_error ("%s", pop_data->err_msg);
287 #if defined (USE_SSL) || defined (USE_GNUTLS)
288 else if (mutt_ssl_starttls (pop_data->conn))
291 mutt_error (_("Could not negotiate TLS connection"));
296 /* recheck capabilities after STLS completes */
297 ret = pop_capabilities (pop_data, 1);
298 if (ret == PQ_NOT_CONNECTED)
308 if (option(OPTSSLFORCETLS) && !pop_data->conn->ssf) {
309 mutt_error _("Encrypted connection unavailable");
315 ret = pop_authenticate (pop_data);
316 if (ret == PQ_NOT_CONNECTED)
318 if (ret == PFD_FUNCT_ERROR)
323 /* recheck capabilities after authentication */
324 ret = pop_capabilities (pop_data, 2);
325 if (ret == PQ_NOT_CONNECTED)
332 /* get total size of mailbox */
333 strfcpy (buf, "STAT\r\n", sizeof (buf));
334 ret = pop_query (pop_data, buf, sizeof (buf));
335 if (ret == PQ_NOT_CONNECTED)
338 mutt_error ("%s", pop_data->err_msg);
343 sscanf (buf, "+OK %u %u", &n, &size);
344 pop_data->size = size;
348 pop_data->status = POP_DISCONNECTED;
349 mutt_error _("Server closed connection!");
352 return PQ_NOT_CONNECTED;
355 /* logout from POP server */
356 void pop_logout (CONTEXT * ctx)
358 pop_query_status ret = 0;
359 char buf[LONG_STRING];
360 POP_DATA *pop_data = (POP_DATA *) ctx->data;
362 if (pop_data->status == POP_CONNECTED) {
363 mutt_message _("Closing connection to POP server...");
366 strfcpy (buf, "RSET\r\n", sizeof (buf));
367 ret = pop_query (pop_data, buf, sizeof (buf));
370 if (ret != PQ_NOT_CONNECTED) {
371 strfcpy (buf, "QUIT\r\n", sizeof (buf));
372 pop_query (pop_data, buf, sizeof (buf));
378 pop_data->status = POP_DISCONNECTED;
383 * Send data from buffer and receive answer to the same buffer
385 * -1 - conection lost,
386 * -2 - invalid command or execution error.
388 pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, const char *msg)
390 int dbg = M_SOCK_LOG_CMD;
393 if (pop_data->status != POP_CONNECTED)
394 return PQ_NOT_CONNECTED;
397 /* print msg instaed of real command */
399 dbg = M_SOCK_LOG_FULL;
400 debug_print (M_SOCK_LOG_CMD, ("> %s", msg));
404 mutt_socket_write_d (pop_data->conn, buf, dbg);
406 c = strpbrk (buf, " \r\n");
408 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
410 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
411 pop_data->status = POP_DISCONNECTED;
412 return PQ_NOT_CONNECTED;
414 if (!str_ncmp (buf, "+OK", 3))
417 pop_error (pop_data, buf);
422 * This function calls funct(*line, *data) for each received line,
423 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
426 * -1 - conection lost,
427 * -2 - invalid command or execution error,
428 * -3 - error in funct(*line, *data)
430 pop_query_status pop_fetch_data (POP_DATA * pop_data, const char *query, progress_t* bar,
431 int (*funct) (char *, void *), void *data)
433 char buf[LONG_STRING];
436 pop_query_status ret;
441 strfcpy (buf, query, sizeof (buf));
442 ret = pop_query (pop_data, buf, sizeof (buf));
446 inbuf = p_new(char, sizeof(buf));
450 mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
453 pop_data->status = POP_DISCONNECTED;
454 ret = PQ_NOT_CONNECTED;
459 if (!lenbuf && buf[0] == '.') {
465 strfcpy(inbuf + lenbuf, p, sizeof(buf));
468 if (chunk >= sizeof (buf)) {
469 lenbuf += strlen (p);
473 mutt_progress_bar (bar, pos);
474 if (ret == 0 && funct (inbuf, data) < 0)
475 ret = PFD_FUNCT_ERROR;
479 p_realloc(&inbuf, lenbuf + sizeof(buf));
486 /* find message with this UIDL and set refno */
487 static int check_uidl (char *line, void *data)
491 CONTEXT *ctx = (CONTEXT *) data;
493 sscanf (line, "%u %s", &index, line);
494 for (i = 0; i < ctx->msgcount; i++) {
495 if (!str_cmp (ctx->hdrs[i]->data, line)) {
496 ctx->hdrs[i]->refno = index;
504 /* reconnect and verify indexes if connection was lost */
505 pop_query_status pop_reconnect (CONTEXT * ctx)
507 pop_query_status ret;
508 POP_DATA *pop_data = (POP_DATA *) ctx->data;
511 if (pop_data->status == POP_CONNECTED)
513 if (pop_data->status == POP_BYE)
514 return PQ_NOT_CONNECTED;
517 mutt_socket_close (pop_data->conn);
519 ret = pop_open_connection (pop_data);
523 bar.msg = _("Verifying message indexes...");
525 mutt_progress_bar (&bar, 0);
527 for (i = 0; i < ctx->msgcount; i++)
528 ctx->hdrs[i]->refno = -1;
530 ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
532 mutt_error ("%s", pop_data->err_msg);
542 return PQ_NOT_CONNECTED;
544 if (query_quadoption (OPT_POPRECONNECT,
545 _("Connection lost. Reconnect to POP server?")) !=
547 return PQ_NOT_CONNECTED;