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