b5aa6058d518f1ad78867c847776787e73b3b6d4
[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   char *c;
386
387   if (pop_data->status != POP_CONNECTED)
388     return PQ_NOT_CONNECTED;
389
390   mutt_socket_write(pop_data->conn, buf);
391
392   c = strpbrk (buf, " \r\n");
393   *c = '\0';
394   snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
395
396   if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
397     pop_data->status = POP_DISCONNECTED;
398     return PQ_NOT_CONNECTED;
399   }
400   if (!m_strncmp(buf, "+OK", 3))
401     return PQ_OK;
402
403   pop_error (pop_data, buf);
404   return PQ_ERR;
405 }
406
407 /*
408  * This function calls  funct(*line, *data)  for each received line,
409  * funct(NULL, *data)  if  rewind(*data)  needs, exits when fail or done.
410  * Returned codes:
411  *  0 - successful,
412  * -1 - conection lost,
413  * -2 - invalid command or execution error,
414  * -3 - error in funct(*line, *data)
415  */
416 pop_query_status pop_fetch_data (POP_DATA * pop_data, const char *query, progress_t* bar,
417                     int (*funct) (char *, void *), void *data)
418 {
419   char buf[LONG_STRING];
420   char *inbuf;
421   char *p;
422   pop_query_status ret;
423   int chunk = 0;
424   long pos = 0;
425   size_t lenbuf = 0;
426
427   m_strcpy(buf, sizeof(buf), query);
428   ret = pop_query (pop_data, buf, sizeof (buf));
429   if (ret != PQ_OK)
430     return ret;
431
432   inbuf = p_new(char, sizeof(buf));
433
434   for (;;) {
435     chunk =
436       mutt_socket_readln(buf, sizeof (buf), pop_data->conn);
437     if (chunk < 0) {
438       pop_data->status = POP_DISCONNECTED;
439       ret = PQ_NOT_CONNECTED;
440       break;
441     }
442
443     p = buf;
444     if (!lenbuf && buf[0] == '.') {
445       if (buf[1] != '.')
446         break;
447       p++;
448     }
449
450     m_strcpy(inbuf + lenbuf,sizeof(buf), p);
451     pos += chunk;
452
453     if (chunk >= ssizeof(buf)) {
454       lenbuf += strlen (p);
455     } else {
456       if (bar)
457         mutt_progress_bar (bar, pos);
458       if (ret == 0 && funct (inbuf, data) < 0)
459         ret = PFD_FUNCT_ERROR;
460       lenbuf = 0;
461     }
462
463     p_realloc(&inbuf, lenbuf + sizeof(buf));
464   }
465
466   p_delete(&inbuf);
467   return ret;
468 }
469
470 /* find message with this UIDL and set refno */
471 static int check_uidl (char *line, void *data)
472 {
473   int i, idx;
474   CONTEXT *ctx = (CONTEXT *)data;
475
476   sscanf (line, "%u %s", &idx, line);
477   for (i = 0; i < ctx->msgcount; i++) {
478     if (!m_strcmp(ctx->hdrs[i]->data, line)) {
479       ctx->hdrs[i]->refno = idx;
480       break;
481     }
482   }
483
484   return 0;
485 }
486
487 /* reconnect and verify indexes if connection was lost */
488 pop_query_status pop_reconnect (CONTEXT * ctx)
489 {
490   pop_query_status ret;
491   POP_DATA *pop_data = (POP_DATA *) ctx->data;
492   progress_t bar;
493
494   if (pop_data->status == POP_CONNECTED)
495     return PQ_OK;
496   if (pop_data->status == POP_BYE)
497     return PQ_NOT_CONNECTED;
498
499   for (;;) {
500     mutt_socket_close (pop_data->conn);
501
502     ret = pop_open_connection (pop_data);
503     if (ret == PQ_OK) {
504       int i;
505
506       bar.msg = _("Verifying message indexes...");
507       bar.size = 0;
508       mutt_progress_bar (&bar, 0);
509
510       for (i = 0; i < ctx->msgcount; i++)
511         ctx->hdrs[i]->refno = -1;
512
513       ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
514       if (ret == PQ_ERR) {
515         mutt_error ("%s", pop_data->err_msg);
516         mutt_sleep (2);
517       }
518     }
519     if (ret == PQ_OK)
520       return PQ_OK;
521
522     pop_logout (ctx);
523
524     if (ret == PQ_ERR)
525       return PQ_NOT_CONNECTED;
526
527     if (query_quadoption (OPT_POPRECONNECT,
528                           _("Connection lost. Reconnect to POP server?")) !=
529         M_YES)
530       return PQ_NOT_CONNECTED;
531   }
532 }