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