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