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