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