c81b44e7f58984b5f0055b7c53e9e8696a90f00b
[apps/madmutt.git] / pop_lib.c
1 /*
2  * Copyright (C) 2000-2003 Vsevolod Volkov <vvv@mutt.org.ua>
3  * 
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "mutt.h"
24 #include "mx.h"
25 #include "url.h"
26 #include "pop.h"
27 #ifdef USE_SSL
28 # include "mutt_ssl.h"
29 #endif
30
31 #include <string.h>
32 #include <unistd.h>
33 #include <ctype.h>
34
35 /* given an POP mailbox name, return host, port, username and password */
36 int pop_parse_path (const char *path, ACCOUNT * acct)
37 {
38   ciss_url_t url;
39   char *c;
40   int ret = -1;
41
42   /* Defaults */
43   acct->flags = 0;
44   acct->port = POP_PORT;
45   acct->type = M_ACCT_TYPE_POP;
46
47   c = safe_strdup (path);
48   url_parse_ciss (&url, c);
49
50   if (url.scheme == U_POP || url.scheme == U_POPS) {
51     if (url.scheme == U_POPS) {
52       acct->flags |= M_ACCT_SSL;
53       acct->port = POP_SSL_PORT;
54     }
55
56     if ((!url.path || !*url.path) && mutt_account_fromurl (acct, &url) == 0)
57       ret = 0;
58   }
59
60   FREE (&c);
61   return ret;
62 }
63
64 /* Copy error message to err_msg buffer */
65 void pop_error (POP_DATA * pop_data, char *msg)
66 {
67   char *t, *c, *c2;
68
69   t = strchr (pop_data->err_msg, '\0');
70   c = msg;
71
72   if (!mutt_strncmp (msg, "-ERR ", 5)) {
73     c2 = msg + 5;
74     SKIPWS (c2);
75
76     if (*c2)
77       c = c2;
78   }
79
80   strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
81   mutt_remove_trailing_ws (pop_data->err_msg);
82 }
83
84 /* Parse CAPA output */
85 static int fetch_capa (char *line, void *data)
86 {
87   POP_DATA *pop_data = (POP_DATA *) data;
88   char *c;
89
90   if (!ascii_strncasecmp (line, "SASL", 4)) {
91     FREE (&pop_data->auth_list);
92     c = line + 4;
93     SKIPWS (c);
94     pop_data->auth_list = safe_strdup (c);
95   }
96
97   else if (!ascii_strncasecmp (line, "STLS", 4))
98     pop_data->cmd_stls = 1;
99
100   else if (!ascii_strncasecmp (line, "USER", 4))
101     pop_data->cmd_user = 1;
102
103   else if (!ascii_strncasecmp (line, "UIDL", 4))
104     pop_data->cmd_uidl = 1;
105
106   else if (!ascii_strncasecmp (line, "TOP", 3))
107     pop_data->cmd_top = 1;
108
109   return 0;
110 }
111
112 /* Fetch list of the authentication mechanisms */
113 static int fetch_auth (char *line, void *data)
114 {
115   POP_DATA *pop_data = (POP_DATA *) data;
116
117   if (!pop_data->auth_list) {
118     pop_data->auth_list = safe_malloc (strlen (line) + 1);
119     *pop_data->auth_list = '\0';
120   }
121   else {
122     safe_realloc (&pop_data->auth_list,
123                   strlen (pop_data->auth_list) + strlen (line) + 2);
124     strcat (pop_data->auth_list, " ");  /* __STRCAT_CHECKED__ */
125   }
126   strcat (pop_data->auth_list, line);   /* __STRCAT_CHECKED__ */
127
128   return 0;
129 }
130
131 /*
132  * Get capabilities
133  *  0 - successful,
134  * -1 - conection lost,
135  * -2 - execution error.
136 */
137 static int pop_capabilities (POP_DATA * pop_data, int mode)
138 {
139   char buf[LONG_STRING];
140
141   /* don't check capabilities on reconnect */
142   if (pop_data->capabilities)
143     return 0;
144
145   /* init capabilities */
146   if (mode == 0) {
147     pop_data->cmd_capa = 0;
148     pop_data->cmd_stls = 0;
149     pop_data->cmd_user = 0;
150     pop_data->cmd_uidl = 0;
151     pop_data->cmd_top = 0;
152     pop_data->resp_codes = 0;
153     pop_data->expire = 1;
154     pop_data->login_delay = 0;
155     FREE (&pop_data->auth_list);
156   }
157
158   /* Execute CAPA command */
159   if (mode == 0 || pop_data->cmd_capa) {
160     strfcpy (buf, "CAPA\r\n", sizeof (buf));
161     switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
162     case 0:
163       {
164         pop_data->cmd_capa = 1;
165         break;
166       }
167     case -1:
168       return -1;
169     }
170   }
171
172   /* CAPA not supported, use defaults */
173   if (mode == 0 && !pop_data->cmd_capa) {
174     pop_data->cmd_user = 2;
175     pop_data->cmd_uidl = 2;
176     pop_data->cmd_top = 2;
177
178     strfcpy (buf, "AUTH\r\n", sizeof (buf));
179     if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == -1)
180       return -1;
181   }
182
183   /* Check capabilities */
184   if (mode == 2) {
185     char *msg = NULL;
186
187     if (!pop_data->expire)
188       msg = _("Unable to leave messages on server.");
189     if (!pop_data->cmd_top)
190       msg = _("Command TOP is not supported by server.");
191     if (!pop_data->cmd_uidl)
192       msg = _("Command UIDL is not supported by server.");
193     if (msg && pop_data->cmd_capa) {
194       mutt_error (msg);
195       return -2;
196     }
197     pop_data->capabilities = 1;
198   }
199
200   return 0;
201 }
202
203 /*
204  * Open connection
205  *  0 - successful,
206  * -1 - conection lost,
207  * -2 - invalid response.
208 */
209 int pop_connect (POP_DATA * pop_data)
210 {
211   char buf[LONG_STRING];
212
213   pop_data->status = POP_NONE;
214   if (mutt_socket_open (pop_data->conn) < 0 ||
215       mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
216     mutt_error (_("Error connecting to server: %s"),
217                 pop_data->conn->account.host);
218     return -1;
219   }
220
221   pop_data->status = POP_CONNECTED;
222
223   if (mutt_strncmp (buf, "+OK", 3)) {
224     *pop_data->err_msg = '\0';
225     pop_error (pop_data, buf);
226     mutt_error ("%s", pop_data->err_msg);
227     return -2;
228   }
229
230   pop_apop_timestamp (pop_data, buf);
231
232   return 0;
233 }
234
235 /*
236  * Open connection and authenticate
237  *  0 - successful,
238  * -1 - conection lost,
239  * -2 - invalid command or execution error,
240  * -3 - authentication canceled.
241 */
242 int pop_open_connection (POP_DATA * pop_data)
243 {
244   int ret;
245   unsigned int n, size;
246   char buf[LONG_STRING];
247
248   ret = pop_connect (pop_data);
249   if (ret < 0) {
250     mutt_sleep (2);
251     return ret;
252   }
253
254   ret = pop_capabilities (pop_data, 0);
255   if (ret == -1)
256     goto err_conn;
257   if (ret == -2) {
258     mutt_sleep (2);
259     return -2;
260   }
261
262 #if (defined(USE_SSL) || defined(USE_GNUTLS)) && !defined(USE_NSS)
263   /* Attempt STLS if available and desired. */
264   if (pop_data->cmd_stls && !pop_data->conn->ssf) {
265     if (pop_data->use_stls == 0) {
266       ret = query_quadoption (OPT_SSLSTARTTLS,
267                               _("Secure connection with TLS?"));
268       if (ret == -1)
269         return -2;
270       pop_data->use_stls = 1;
271       if (ret == M_YES)
272         pop_data->use_stls = 2;
273     }
274     if (pop_data->use_stls == 2) {
275       strfcpy (buf, "STLS\r\n", sizeof (buf));
276       ret = pop_query (pop_data, buf, sizeof (buf));
277       if (ret == -1)
278         goto err_conn;
279       if (ret != 0) {
280         mutt_error ("%s", pop_data->err_msg);
281         mutt_sleep (2);
282       }
283 #ifdef USE_SSL
284       else if (mutt_ssl_starttls (pop_data->conn))
285 #elif USE_GNUTLS
286       else if (mutt_gnutls_starttls (pop_data->conn))
287 #endif
288       {
289         mutt_error (_("Could not negotiate TLS connection"));
290         mutt_sleep (2);
291         return -2;
292       }
293       else {
294         /* recheck capabilities after STLS completes */
295         ret = pop_capabilities (pop_data, 1);
296         if (ret == -1)
297           goto err_conn;
298         if (ret == -2) {
299           mutt_sleep (2);
300           return -2;
301         }
302       }
303     }
304   }
305 #endif
306
307   ret = pop_authenticate (pop_data);
308   if (ret == -1)
309     goto err_conn;
310   if (ret == -3)
311     mutt_clear_error ();
312   if (ret != 0)
313     return ret;
314
315   /* recheck capabilities after authentication */
316   ret = pop_capabilities (pop_data, 2);
317   if (ret == -1)
318     goto err_conn;
319   if (ret == -2) {
320     mutt_sleep (2);
321     return -2;
322   }
323
324   /* get total size of mailbox */
325   strfcpy (buf, "STAT\r\n", sizeof (buf));
326   ret = pop_query (pop_data, buf, sizeof (buf));
327   if (ret == -1)
328     goto err_conn;
329   if (ret == -2) {
330     mutt_error ("%s", pop_data->err_msg);
331     mutt_sleep (2);
332     return ret;
333   }
334
335   sscanf (buf, "+OK %u %u", &n, &size);
336   pop_data->size = size;
337   return 0;
338
339 err_conn:
340   pop_data->status = POP_DISCONNECTED;
341   mutt_error _("Server closed connection!");
342
343   mutt_sleep (2);
344   return -1;
345 }
346
347 /* logout from POP server */
348 void pop_logout (CONTEXT * ctx)
349 {
350   int ret = 0;
351   char buf[LONG_STRING];
352   POP_DATA *pop_data = (POP_DATA *) ctx->data;
353
354   if (pop_data->status == POP_CONNECTED) {
355     mutt_message _("Closing connection to POP server...");
356
357     if (ctx->readonly) {
358       strfcpy (buf, "RSET\r\n", sizeof (buf));
359       ret = pop_query (pop_data, buf, sizeof (buf));
360     }
361
362     if (ret != -1) {
363       strfcpy (buf, "QUIT\r\n", sizeof (buf));
364       pop_query (pop_data, buf, sizeof (buf));
365     }
366
367     mutt_clear_error ();
368   }
369
370   pop_data->status = POP_DISCONNECTED;
371   return;
372 }
373
374 /*
375  * Send data from buffer and receive answer to the same buffer
376  *  0 - successful,
377  * -1 - conection lost,
378  * -2 - invalid command or execution error.
379 */
380 int pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, char *msg)
381 {
382   int dbg = M_SOCK_LOG_CMD;
383   char *c;
384
385   if (pop_data->status != POP_CONNECTED)
386     return -1;
387
388 #ifdef DEBUG
389   /* print msg instaed of real command */
390   if (msg) {
391     dbg = M_SOCK_LOG_FULL;
392     dprint (M_SOCK_LOG_CMD, (debugfile, "> %s", msg));
393   }
394 #endif
395
396   mutt_socket_write_d (pop_data->conn, buf, dbg);
397
398   c = strpbrk (buf, " \r\n");
399   *c = '\0';
400   snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
401
402   if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
403     pop_data->status = POP_DISCONNECTED;
404     return -1;
405   }
406   if (!mutt_strncmp (buf, "+OK", 3))
407     return 0;
408
409   pop_error (pop_data, buf);
410   return -2;
411 }
412
413 /*
414  * This function calls  funct(*line, *data)  for each received line,
415  * funct(NULL, *data)  if  rewind(*data)  needs, exits when fail or done.
416  * Returned codes:
417  *  0 - successful,
418  * -1 - conection lost,
419  * -2 - invalid command or execution error,
420  * -3 - error in funct(*line, *data)
421  */
422 int pop_fetch_data (POP_DATA * pop_data, char *query, char *msg,
423                     int (*funct) (char *, void *), void *data)
424 {
425   char buf[LONG_STRING];
426   char *inbuf;
427   char *p;
428   int ret, 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 < 0)
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 = -1;
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 = -3;
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 idnexes if connection was lost */
495 int pop_reconnect (CONTEXT * ctx)
496 {
497   int ret;
498   POP_DATA *pop_data = (POP_DATA *) ctx->data;
499
500   if (pop_data->status == POP_CONNECTED)
501     return 0;
502   if (pop_data->status == POP_BYE)
503     return -1;
504
505   FOREVER {
506     mutt_socket_close (pop_data->conn);
507
508     ret = pop_open_connection (pop_data);
509     if (ret == 0) {
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 == -2) {
520         mutt_error ("%s", pop_data->err_msg);
521         mutt_sleep (2);
522       }
523     }
524     if (ret == 0)
525       return 0;
526
527     pop_logout (ctx);
528
529     if (ret < -1)
530       return -1;
531
532     if (query_quadoption (OPT_POPRECONNECT,
533                           _("Connection lost. Reconnect to POP server?")) !=
534         M_YES)
535       return -1;
536   }
537 }