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