Nico Golde:
[apps/madmutt.git] / pop / pop_lib.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2000-2003 Vsevolod Volkov <vvv@mutt.org.ua>
4  *
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.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include "mutt.h"
15 #include "mx.h"
16 #include "url.h"
17 #include "pop.h"
18 #ifdef USE_SSL
19 # include "mutt_ssl.h"
20 #endif
21
22 #include "lib/mem.h"
23 #include "lib/intl.h"
24 #include "lib/debug.h"
25 #include "lib/str.h"
26
27 #include <string.h>
28 #include <unistd.h>
29 #include <ctype.h>
30
31 /* given an POP mailbox name, return host, port, username and password */
32 int pop_parse_path (const char *path, ACCOUNT * acct)
33 {
34   ciss_url_t url;
35   char *c;
36   int ret = -1;
37
38   /* Defaults */
39   acct->flags = 0;
40   acct->port = POP_PORT;
41   acct->type = M_ACCT_TYPE_POP;
42
43   c = safe_strdup (path);
44   url_parse_ciss (&url, c);
45
46   if (url.scheme == U_POP || url.scheme == U_POPS) {
47     if (url.scheme == U_POPS) {
48       acct->flags |= M_ACCT_SSL;
49       acct->port = POP_SSL_PORT;
50     }
51
52     if ((!url.path || !*url.path) && mutt_account_fromurl (acct, &url) == 0)
53       ret = 0;
54   }
55
56   FREE (&c);
57   return ret;
58 }
59
60 /* Copy error message to err_msg buffer */
61 void pop_error (POP_DATA * pop_data, char *msg)
62 {
63   char *t, *c, *c2;
64
65   t = strchr (pop_data->err_msg, '\0');
66   c = msg;
67
68   if (!safe_strncmp (msg, "-ERR ", 5)) {
69     c2 = msg + 5;
70     SKIPWS (c2);
71
72     if (*c2)
73       c = c2;
74   }
75
76   strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
77   str_skip_trailws (pop_data->err_msg);
78 }
79
80 /* Parse CAPA output */
81 static int fetch_capa (char *line, void *data)
82 {
83   POP_DATA *pop_data = (POP_DATA *) data;
84   char *c;
85
86   if (!ascii_strncasecmp (line, "SASL", 4)) {
87     FREE (&pop_data->auth_list);
88     c = line + 4;
89     SKIPWS (c);
90     pop_data->auth_list = safe_strdup (c);
91   }
92
93   else if (!ascii_strncasecmp (line, "STLS", 4))
94     pop_data->cmd_stls = CMD_AVAILABLE;
95
96   else if (!ascii_strncasecmp (line, "USER", 4))
97     pop_data->cmd_user = CMD_AVAILABLE;
98
99   else if (!ascii_strncasecmp (line, "UIDL", 4))
100     pop_data->cmd_uidl = CMD_AVAILABLE;
101
102   else if (!ascii_strncasecmp (line, "TOP", 3))
103     pop_data->cmd_top = CMD_AVAILABLE;
104
105   return 0;
106 }
107
108 /* Fetch list of the authentication mechanisms */
109 static int fetch_auth (char *line, void *data)
110 {
111   POP_DATA *pop_data = (POP_DATA *) data;
112
113   if (!pop_data->auth_list) {
114     pop_data->auth_list = safe_malloc (strlen (line) + 1);
115     *pop_data->auth_list = '\0';
116   }
117   else {
118     safe_realloc (&pop_data->auth_list,
119                   strlen (pop_data->auth_list) + strlen (line) + 2);
120     strcat (pop_data->auth_list, " ");  /* __STRCAT_CHECKED__ */
121   }
122   strcat (pop_data->auth_list, line);   /* __STRCAT_CHECKED__ */
123
124   return 0;
125 }
126
127 /*
128  * Get capabilities
129  *  0 - successful,
130  * -1 - conection lost,
131  * -2 - execution error.
132 */
133 static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode)
134 {
135   char buf[LONG_STRING];
136
137   /* don't check capabilities on reconnect */
138   if (pop_data->capabilities)
139     return 0;
140
141   /* init capabilities */
142   if (mode == 0) {
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     FREE (&pop_data->auth_list);
152   }
153
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)) {
158     case PQ_OK:
159       {
160         pop_data->cmd_capa = CMD_AVAILABLE;
161         break;
162       }
163     case PQ_ERR:
164       {
165         pop_data->cmd_capa = CMD_NOT_AVAILABLE;
166         break;
167       }
168     case PQ_NOT_CONNECTED:
169       return PQ_NOT_CONNECTED;
170     }
171   }
172
173   /* CAPA not supported, use defaults */
174   if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
175     pop_data->cmd_user = CMD_UNKNOWN;
176     pop_data->cmd_uidl = CMD_UNKNOWN;
177     pop_data->cmd_top = CMD_UNKNOWN;
178
179     strfcpy (buf, "AUTH\r\n", sizeof (buf));
180     if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
181       return PQ_NOT_CONNECTED;
182   }
183
184   /* Check capabilities */
185   if (mode == 2) {
186     char *msg = NULL;
187
188     if (!pop_data->expire)
189       msg = _("Unable to leave messages on server.");
190     if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
191       msg = _("Command TOP is not supported by server.");
192     if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
193       msg = _("Command UIDL is not supported by server.");
194     if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
195       mutt_error (msg);
196       return PQ_ERR;
197     }
198     pop_data->capabilities = 1;
199   }
200
201   return PQ_OK;
202 }
203
204 /*
205  * Open connection
206  *  0 - successful,
207  * -1 - conection lost,
208  * -2 - invalid response.
209 */
210 pop_query_status pop_connect (POP_DATA * pop_data)
211 {
212   char buf[LONG_STRING];
213
214   pop_data->status = POP_NONE;
215   if (mutt_socket_open (pop_data->conn) < 0 ||
216       mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
217     mutt_error (_("Error connecting to server: %s"),
218                 pop_data->conn->account.host);
219     return PQ_NOT_CONNECTED;
220   }
221
222   pop_data->status = POP_CONNECTED;
223
224   if (safe_strncmp (buf, "+OK", 3)) {
225     *pop_data->err_msg = '\0';
226     pop_error (pop_data, buf);
227     mutt_error ("%s", pop_data->err_msg);
228     return PQ_ERR;
229   }
230
231   pop_apop_timestamp (pop_data, buf);
232
233   return PQ_OK;
234 }
235
236 /*
237  * Open connection and authenticate
238  *  0 - successful,
239  * -1 - conection lost,
240  * -2 - invalid command or execution error,
241  * -3 - authentication canceled.
242 */
243 pop_query_status pop_open_connection (POP_DATA * pop_data)
244 {
245   pop_query_status ret;
246   unsigned int n, size;
247   char buf[LONG_STRING];
248
249   ret = pop_connect (pop_data);
250   if (ret != PQ_OK) {
251     mutt_sleep (2);
252     return ret;
253   }
254
255   ret = pop_capabilities (pop_data, 0);
256   if (ret == PQ_NOT_CONNECTED)
257     goto err_conn;
258   if (ret == PQ_ERR) {
259     mutt_sleep (2);
260     return PQ_ERR;
261   }
262
263 #if (defined(USE_SSL) || defined(USE_GNUTLS)) && !defined(USE_NSS)
264   /* Attempt STLS if available and desired. */
265   if (pop_data->cmd_stls && !pop_data->conn->ssf) {
266     if (pop_data->use_stls == 0) {
267       ret = query_quadoption (OPT_SSLSTARTTLS,
268                               _("Secure connection with TLS?"));
269       if (ret == -1)
270         return PQ_ERR;
271       pop_data->use_stls = 1;
272       if (ret == M_YES)
273         pop_data->use_stls = 2;
274     }
275     if (pop_data->use_stls == 2) {
276       strfcpy (buf, "STLS\r\n", sizeof (buf));
277       ret = pop_query (pop_data, buf, sizeof (buf));
278       if (ret == PQ_NOT_CONNECTED)
279         goto err_conn;
280       if (ret != PQ_OK) {
281         mutt_error ("%s", pop_data->err_msg);
282         mutt_sleep (2);
283       }
284 #ifdef USE_SSL
285       else if (mutt_ssl_starttls (pop_data->conn))
286 #elif USE_GNUTLS
287       else if (mutt_gnutls_starttls (pop_data->conn))
288 #endif
289       {
290         mutt_error (_("Could not negotiate TLS connection"));
291         mutt_sleep (2);
292         return PQ_ERR;
293       }
294       else {
295         /* recheck capabilities after STLS completes */
296         ret = pop_capabilities (pop_data, 1);
297         if (ret == PQ_NOT_CONNECTED)
298           goto err_conn;
299         if (ret == PQ_ERR) {
300           mutt_sleep (2);
301           return PQ_ERR;
302         }
303       }
304     }
305   }
306 #endif
307
308   ret = pop_authenticate (pop_data);
309   if (ret == PQ_NOT_CONNECTED)
310     goto err_conn;
311   if (ret == PFD_FUNCT_ERROR)
312     mutt_clear_error ();
313   if (ret != PQ_OK)
314     return ret;
315
316   /* recheck capabilities after authentication */
317   ret = pop_capabilities (pop_data, 2);
318   if (ret == PQ_NOT_CONNECTED)
319     goto err_conn;
320   if (ret == PQ_ERR) {
321     mutt_sleep (2);
322     return PQ_ERR;
323   }
324
325   /* get total size of mailbox */
326   strfcpy (buf, "STAT\r\n", sizeof (buf));
327   ret = pop_query (pop_data, buf, sizeof (buf));
328   if (ret == PQ_NOT_CONNECTED)
329     goto err_conn;
330   if (ret == PQ_ERR) {
331     mutt_error ("%s", pop_data->err_msg);
332     mutt_sleep (2);
333     return ret;
334   }
335
336   sscanf (buf, "+OK %u %u", &n, &size);
337   pop_data->size = size;
338   return PQ_OK;
339
340 err_conn:
341   pop_data->status = POP_DISCONNECTED;
342   mutt_error _("Server closed connection!");
343
344   mutt_sleep (2);
345   return PQ_NOT_CONNECTED;
346 }
347
348 /* logout from POP server */
349 void pop_logout (CONTEXT * ctx)
350 {
351   pop_query_status ret = 0;
352   char buf[LONG_STRING];
353   POP_DATA *pop_data = (POP_DATA *) ctx->data;
354
355   if (pop_data->status == POP_CONNECTED) {
356     mutt_message _("Closing connection to POP server...");
357
358     if (ctx->readonly) {
359       strfcpy (buf, "RSET\r\n", sizeof (buf));
360       ret = pop_query (pop_data, buf, sizeof (buf));
361     }
362
363     if (ret != PQ_NOT_CONNECTED) {
364       strfcpy (buf, "QUIT\r\n", sizeof (buf));
365       pop_query (pop_data, buf, sizeof (buf));
366     }
367
368     mutt_clear_error ();
369   }
370
371   pop_data->status = POP_DISCONNECTED;
372   return;
373 }
374
375 /*
376  * Send data from buffer and receive answer to the same buffer
377  *  0 - successful,
378  * -1 - conection lost,
379  * -2 - invalid command or execution error.
380 */
381 pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, char *msg)
382 {
383   int dbg = M_SOCK_LOG_CMD;
384   char *c;
385
386   if (pop_data->status != POP_CONNECTED)
387     return PQ_NOT_CONNECTED;
388
389 #ifdef DEBUG
390   /* print msg instaed of real command */
391   if (msg) {
392     dbg = M_SOCK_LOG_FULL;
393     debug_print (M_SOCK_LOG_CMD, ("> %s", msg));
394   }
395 #endif
396
397   mutt_socket_write_d (pop_data->conn, buf, dbg);
398
399   c = strpbrk (buf, " \r\n");
400   *c = '\0';
401   snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
402
403   if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
404     pop_data->status = POP_DISCONNECTED;
405     return PQ_NOT_CONNECTED;
406   }
407   if (!safe_strncmp (buf, "+OK", 3))
408     return PQ_OK;
409
410   pop_error (pop_data, buf);
411   return PQ_ERR;
412 }
413
414 /*
415  * This function calls  funct(*line, *data)  for each received line,
416  * funct(NULL, *data)  if  rewind(*data)  needs, exits when fail or done.
417  * Returned codes:
418  *  0 - successful,
419  * -1 - conection lost,
420  * -2 - invalid command or execution error,
421  * -3 - error in funct(*line, *data)
422  */
423 pop_query_status pop_fetch_data (POP_DATA * pop_data, char *query, char *msg,
424                     int (*funct) (char *, void *), void *data)
425 {
426   char buf[LONG_STRING];
427   char *inbuf;
428   char *p;
429   pop_query_status ret;
430   int chunk, line = 0;
431   size_t lenbuf = 0;
432
433   strfcpy (buf, query, sizeof (buf));
434   ret = pop_query (pop_data, buf, sizeof (buf));
435   if (ret != PQ_OK)
436     return ret;
437
438   inbuf = safe_malloc (sizeof (buf));
439
440   FOREVER {
441     chunk =
442       mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
443                             M_SOCK_LOG_HDR);
444     if (chunk < 0) {
445       pop_data->status = POP_DISCONNECTED;
446       ret = PQ_NOT_CONNECTED;
447       break;
448     }
449
450     p = buf;
451     if (!lenbuf && buf[0] == '.') {
452       if (buf[1] != '.')
453         break;
454       p++;
455     }
456
457     strfcpy (inbuf + lenbuf, p, sizeof (buf));
458
459     if (chunk >= sizeof (buf)) {
460       lenbuf += strlen (p);
461     }
462     else {
463       line++;
464       if (msg && ReadInc && (line % ReadInc == 0))
465         mutt_message ("%s %d", msg, line);
466       if (ret == 0 && funct (inbuf, data) < 0)
467         ret = PFD_FUNCT_ERROR;
468       lenbuf = 0;
469     }
470
471     safe_realloc (&inbuf, lenbuf + sizeof (buf));
472   }
473
474   FREE (&inbuf);
475   return ret;
476 }
477
478 /* find message with this UIDL and set refno */
479 static int check_uidl (char *line, void *data)
480 {
481   int i;
482   unsigned int index;
483   CONTEXT *ctx = (CONTEXT *) data;
484
485   sscanf (line, "%u %s", &index, line);
486   for (i = 0; i < ctx->msgcount; i++) {
487     if (!mutt_strcmp (ctx->hdrs[i]->data, line)) {
488       ctx->hdrs[i]->refno = index;
489       break;
490     }
491   }
492
493   return 0;
494 }
495
496 /* reconnect and verify indexes if connection was lost */
497 pop_query_status pop_reconnect (CONTEXT * ctx)
498 {
499   pop_query_status ret;
500   POP_DATA *pop_data = (POP_DATA *) ctx->data;
501
502   if (pop_data->status == POP_CONNECTED)
503     return PQ_OK;
504   if (pop_data->status == POP_BYE)
505     return PQ_NOT_CONNECTED;
506
507   FOREVER {
508     mutt_socket_close (pop_data->conn);
509
510     ret = pop_open_connection (pop_data);
511     if (ret == PQ_OK) {
512       char *msg = _("Verifying message indexes...");
513       int i;
514
515       for (i = 0; i < ctx->msgcount; i++)
516         ctx->hdrs[i]->refno = -1;
517
518       mutt_message (msg);
519
520       ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
521       if (ret == PQ_ERR) {
522         mutt_error ("%s", pop_data->err_msg);
523         mutt_sleep (2);
524       }
525     }
526     if (ret == PQ_OK)
527       return PQ_OK;
528
529     pop_logout (ctx);
530
531     if (ret == PQ_ERR)
532       return PQ_NOT_CONNECTED;
533
534     if (query_quadoption (OPT_POPRECONNECT,
535                           _("Connection lost. Reconnect to POP server?")) !=
536         M_YES)
537       return PQ_NOT_CONNECTED;
538   }
539 }