always build mutt with pgp + smime support.
[apps/madmutt.git] / pop / pop.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2000-2002 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/file.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/macros.h>
18
19 #include "mutt.h"
20 #include "mx.h"
21 #include "pop.h"
22 #include "mutt_crypt.h"
23 #include "mutt_curses.h"
24
25 #include "lib/debug.h"
26
27 #include <string.h>
28 #include <unistd.h>
29
30 /* write line to file */
31 static int fetch_message (char *line, void *file)
32 {
33   FILE *f = (FILE *) file;
34
35   fputs (line, f);
36   if (fputc ('\n', f) == EOF)
37     return -1;
38
39   return 0;
40 }
41
42 /*
43  * Read header
44  * returns:
45  *  0 on success
46  * -1 - conection lost,
47  * -2 - invalid command or execution error,
48  * -3 - error writing to tempfile
49  */
50 static pop_query_status pop_read_header (POP_DATA * pop_data, HEADER * h)
51 {
52   FILE *f;
53   int index;
54   pop_query_status ret;
55   long length;
56   char buf[LONG_STRING];
57   char tempfile[_POSIX_PATH_MAX];
58
59   mutt_mktemp (tempfile);
60   if (!(f = safe_fopen (tempfile, "w+"))) {
61     mutt_perror (tempfile);
62     return PFD_FUNCT_ERROR;
63   }
64
65   snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno);
66   ret = pop_query (pop_data, buf, sizeof (buf));
67   if (ret == PQ_OK) {
68     sscanf (buf, "+OK %d %ld", &index, &length);
69
70     snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno);
71     ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f);
72
73     if (pop_data->cmd_top == CMD_UNKNOWN) {
74       if (ret == PQ_OK) {
75         pop_data->cmd_top = CMD_AVAILABLE;
76
77         debug_print (1, ("set TOP capability\n"));
78       }
79
80       if (ret == PQ_ERR) {
81         pop_data->cmd_top = CMD_NOT_AVAILABLE;
82
83         debug_print (1, ("unset TOP capability\n"));
84         snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
85                   _("Command TOP is not supported by server."));
86       }
87     }
88   }
89
90   switch (ret) {
91   case PQ_OK:
92     {
93       rewind (f);
94       h->env = mutt_read_rfc822_header (f, h, 0, 0);
95       h->content->length = length - h->content->offset + 1;
96       rewind (f);
97       while (!feof (f)) {
98         h->content->length--;
99         fgets (buf, sizeof (buf), f);
100       }
101       break;
102     }
103   case PQ_ERR:
104     {
105       mutt_error ("%s", pop_data->err_msg);
106       break;
107     }
108   case PFD_FUNCT_ERROR:
109     {
110       mutt_error _("Can't write header to temporary file!");
111
112       break;
113     }
114   case PQ_NOT_CONNECTED:
115     {
116       mutt_error _("Can't fetch header: Not connected!");
117       break;
118     }
119   }
120
121   fclose (f);
122   unlink (tempfile);
123   return ret;
124 }
125
126 /* parse UIDL */
127 static int fetch_uidl (char *line, void *data)
128 {
129   int i, index;
130   CONTEXT *ctx = (CONTEXT *) data;
131   POP_DATA *pop_data = (POP_DATA *) ctx->data;
132
133   sscanf (line, "%d %s", &index, line);
134   for (i = 0; i < ctx->msgcount; i++)
135     if (!m_strcmp(line, ctx->hdrs[i]->data))
136       break;
137
138   if (i == ctx->msgcount) {
139     debug_print (1, ("new header %d %s\n", index, line));
140
141     if (i >= ctx->hdrmax)
142       mx_alloc_memory (ctx);
143
144     ctx->msgcount++;
145     ctx->hdrs[i] = mutt_new_header ();
146     ctx->hdrs[i]->data = m_strdup(line);
147   }
148   else if (ctx->hdrs[i]->index != index - 1)
149     pop_data->clear_cache = 1;
150
151   ctx->hdrs[i]->refno = index;
152   ctx->hdrs[i]->index = index - 1;
153
154   return 0;
155 }
156
157 /*
158  * Read headers
159  * returns:
160  *  0 on success
161  * -1 - conection lost,
162  * -2 - invalid command or execution error,
163  * -3 - error writing to tempfile
164  */
165 static int pop_fetch_headers (CONTEXT * ctx)
166 {
167   int i, old_count, new_count;
168   pop_query_status ret;
169   POP_DATA *pop_data = (POP_DATA *) ctx->data;
170
171   time (&pop_data->check_time);
172   pop_data->clear_cache = 0;
173
174   for (i = 0; i < ctx->msgcount; i++)
175     ctx->hdrs[i]->refno = -1;
176
177   old_count = ctx->msgcount;
178   ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx);
179   new_count = ctx->msgcount;
180   ctx->msgcount = old_count;
181
182   if (pop_data->cmd_uidl == CMD_UNKNOWN) {
183     if (ret == PQ_OK) {
184       pop_data->cmd_uidl = CMD_AVAILABLE;
185
186       debug_print (1, ("set UIDL capability\n"));
187     }
188
189     if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) {
190       pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
191
192       debug_print (1, ("unset UIDL capability\n"));
193       snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
194                 _("Command UIDL is not supported by server."));
195     }
196   }
197
198   if (ret == PQ_OK) {
199     for (i = 0; i < old_count; i++)
200       if (ctx->hdrs[i]->refno == -1)
201         ctx->hdrs[i]->deleted = 1;
202
203     for (i = old_count; i < new_count; i++) {
204       mutt_message (_("Fetching message headers... [%d/%d]"),
205                     i + 1 - old_count, new_count - old_count);
206
207       ret = pop_read_header (pop_data, ctx->hdrs[i]);
208       if (ret != PQ_OK)
209         break;
210
211       ctx->msgcount++;
212     }
213
214     if (i > old_count)
215       mx_update_context (ctx, i - old_count);
216   }
217
218   if (ret != PQ_OK) {
219     for (i = ctx->msgcount; i < new_count; i++)
220       mutt_free_header (&ctx->hdrs[i]);
221     return ret;
222   }
223
224   mutt_clear_error ();
225   return (new_count - old_count);
226 }
227
228 /* open POP mailbox - fetch only headers */
229 int pop_open_mailbox (CONTEXT * ctx)
230 {
231   int ret;
232   char buf[LONG_STRING];
233   CONNECTION *conn;
234   ACCOUNT acct;
235   POP_DATA *pop_data;
236   ciss_url_t url;
237
238   if (pop_parse_path (ctx->path, &acct)) {
239     mutt_error (_("%s is an invalid POP path"), ctx->path);
240     mutt_sleep (2);
241     return -1;
242   }
243
244   mutt_account_tourl (&acct, &url);
245   url.path = NULL;
246   url_ciss_tostring (&url, buf, sizeof (buf), 0);
247   conn = mutt_conn_find (NULL, &acct);
248   if (!conn)
249     return -1;
250
251   p_delete(&ctx->path);
252   ctx->path = m_strdup(buf);
253
254   pop_data = p_new(POP_DATA, 1);
255   pop_data->conn = conn;
256   ctx->data = pop_data;
257
258   if (pop_open_connection (pop_data) < 0)
259     return -1;
260
261   conn->data = pop_data;
262
263   for (;;) {
264     if (pop_reconnect (ctx) != PQ_OK)
265       return -1;
266
267     ctx->size = pop_data->size;
268
269     mutt_message _("Fetching list of messages...");
270
271     ret = pop_fetch_headers (ctx);
272
273     if (ret >= 0)
274       return 0;
275
276     if (ret < -1) {
277       mutt_sleep (2);
278       return -1;
279     }
280   }
281 }
282
283 /* delete all cached messages */
284 static void pop_clear_cache (POP_DATA * pop_data)
285 {
286   int i;
287
288   if (!pop_data->clear_cache)
289     return;
290
291   debug_print (1, ("delete cached messages\n"));
292
293   for (i = 0; i < POP_CACHE_LEN; i++) {
294     if (pop_data->cache[i].path) {
295       unlink (pop_data->cache[i].path);
296       p_delete(&pop_data->cache[i].path);
297     }
298   }
299 }
300
301 /* close POP mailbox */
302 void pop_close_mailbox (CONTEXT * ctx)
303 {
304   POP_DATA *pop_data = (POP_DATA *) ctx->data;
305
306   if (!pop_data)
307     return;
308
309   pop_logout (ctx);
310
311   if (pop_data->status != POP_NONE)
312     mutt_socket_close (pop_data->conn);
313
314   pop_data->status = POP_NONE;
315
316   pop_data->clear_cache = 1;
317   pop_clear_cache (pop_data);
318
319   if (!pop_data->conn->data)
320     mutt_socket_free (pop_data->conn);
321
322   return;
323 }
324
325 /* fetch message from POP server */
326 int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
327 {
328   int ret;
329   void *uidl;
330   char buf[LONG_STRING];
331   char path[_POSIX_PATH_MAX];
332   progress_t bar;
333   POP_DATA *pop_data = (POP_DATA *) ctx->data;
334   POP_CACHE *cache;
335   HEADER *h = ctx->hdrs[msgno];
336
337   /* see if we already have the message in our cache */
338   cache = &pop_data->cache[h->index % POP_CACHE_LEN];
339
340   if (cache->path) {
341     if (cache->index == h->index) {
342       /* yes, so just return a pointer to the message */
343       msg->fp = fopen (cache->path, "r");
344       if (msg->fp)
345         return 0;
346
347       mutt_perror (cache->path);
348       mutt_sleep (2);
349       return -1;
350     }
351     else {
352       /* clear the previous entry */
353       unlink (cache->path);
354       p_delete(&cache->path);
355     }
356   }
357
358   for (;;) {
359     if (pop_reconnect (ctx) != PQ_OK)
360       return -1;
361
362     /* verify that massage index is correct */
363     if (h->refno < 0) {
364       mutt_error
365         _("The message index is incorrect. Try reopening the mailbox.");
366       mutt_sleep (2);
367       return -1;
368     }
369
370     bar.size = h->content->length + h->content->offset - 1;
371     bar.msg = _("Fetching message...");
372     mutt_progress_bar (&bar, 0);
373
374     mutt_mktemp (path);
375     msg->fp = safe_fopen (path, "w+");
376     if (!msg->fp) {
377       mutt_perror (path);
378       mutt_sleep (2);
379       return -1;
380     }
381
382     snprintf (buf, sizeof (buf), "RETR %d\r\n", h->refno);
383
384     ret = pop_fetch_data (pop_data, buf, &bar, fetch_message, msg->fp);
385     if (ret == PQ_OK)
386       break;
387
388     safe_fclose (&msg->fp);
389     unlink (path);
390
391     if (ret == PQ_ERR) {
392       mutt_error ("%s", pop_data->err_msg);
393       mutt_sleep (2);
394       return -1;
395     }
396
397     if (ret == PFD_FUNCT_ERROR) {
398       mutt_error _("Can't write message to temporary file!");
399
400       mutt_sleep (2);
401       return -1;
402     }
403   }
404
405   /* Update the header information.  Previously, we only downloaded a
406    * portion of the headers, those required for the main display.
407    */
408   cache->index = h->index;
409   cache->path = m_strdup(path);
410   rewind (msg->fp);
411   uidl = h->data;
412   mutt_free_envelope (&h->env);
413   h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
414   h->data = uidl;
415   h->lines = 0;
416   fgets (buf, sizeof (buf), msg->fp);
417   while (!feof (msg->fp)) {
418     ctx->hdrs[msgno]->lines++;
419     fgets (buf, sizeof (buf), msg->fp);
420   }
421
422   h->content->length = ftello (msg->fp) - h->content->offset;
423
424   /* This needs to be done in case this is a multipart message */
425   h->security = crypt_query (h->content);
426
427   mutt_clear_error ();
428   rewind (msg->fp);
429
430   return 0;
431 }
432
433 /* update POP mailbox - delete messages from server */
434 pop_query_status pop_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
435 {
436   int i;
437   pop_query_status ret;
438   char buf[LONG_STRING];
439   POP_DATA *pop_data = (POP_DATA *) ctx->data;
440
441   pop_data->check_time = 0;
442
443   for (;;) {
444     if (pop_reconnect (ctx) != PQ_OK)
445       return PQ_NOT_CONNECTED;
446
447     mutt_message (_("Marking %d messages deleted..."), ctx->deleted);
448
449     for (i = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++) {
450       if (ctx->hdrs[i]->deleted) {
451         snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
452         ret = pop_query (pop_data, buf, sizeof (buf));
453       }
454     }
455
456     if (ret == PQ_OK) {
457       m_strcpy(buf, sizeof(buf), "QUIT\r\n");
458       ret = pop_query (pop_data, buf, sizeof (buf));
459     }
460
461     if (ret == PQ_OK) {
462       pop_data->clear_cache = 1;
463       pop_clear_cache (pop_data);
464       pop_data->status = POP_DISCONNECTED;
465       return PQ_OK;
466     }
467
468     if (ret == PQ_ERR) {
469       mutt_error ("%s", pop_data->err_msg);
470       mutt_sleep (2);
471       return PQ_NOT_CONNECTED;
472     }
473   }
474 }
475
476 /* Check for new messages and fetch headers */
477 int pop_check_mailbox (CONTEXT * ctx, int *index_hint, int unused)
478 {
479   int ret;
480   POP_DATA *pop_data = (POP_DATA *) ctx->data;
481
482   if ((pop_data->check_time + PopCheckTimeout) > time (NULL))
483     return 0;
484
485   pop_logout (ctx);
486
487   mutt_socket_close (pop_data->conn);
488
489   if (pop_open_connection (pop_data) < 0)
490     return -1;
491
492   ctx->size = pop_data->size;
493
494   mutt_message _("Checking for new messages...");
495
496   ret = pop_fetch_headers (ctx);
497   pop_clear_cache (pop_data);
498
499   if (ret < 0)
500     return -1;
501
502   if (ret > 0)
503     return M_NEW_MAIL;
504
505   return 0;
506 }
507
508 /* Fetch messages and save them in $spoolfile */
509 void pop_fetch_mail (void)
510 {
511   char buffer[LONG_STRING];
512   char msgbuf[SHORT_STRING];
513   char *url, *p;
514   int i, delanswer, last = 0, msgs, bytes, rset = 0;
515   pop_query_status ret;
516   CONNECTION *conn;
517   CONTEXT ctx;
518   MESSAGE *msg = NULL;
519   ACCOUNT acct;
520   POP_DATA *pop_data;
521
522   if (!PopHost) {
523     mutt_error _("POP host is not defined.");
524
525     return;
526   }
527
528   url = p = p_new(char, strlen (PopHost) + 7);
529   if (url_check_scheme (PopHost) == U_UNKNOWN) {
530     strcpy (url, "pop://");     /* __STRCPY_CHECKED__ */
531     p = strchr (url, '\0');
532   }
533   strcpy (p, PopHost);          /* __STRCPY_CHECKED__ */
534
535   ret = pop_parse_path (url, &acct);
536   p_delete(&url);
537   if (ret) {
538     mutt_error (_("%s is an invalid POP path"), PopHost);
539     return;
540   }
541
542   conn = mutt_conn_find (NULL, &acct);
543   if (!conn)
544     return;
545
546   pop_data = p_new(POP_DATA, 1);
547   pop_data->conn = conn;
548
549   if (pop_open_connection (pop_data) < 0) {
550     mutt_socket_free (pop_data->conn);
551     p_delete(&pop_data);
552     return;
553   }
554
555   conn->data = pop_data;
556
557   mutt_message _("Checking for new messages...");
558
559   /* find out how many messages are in the mailbox. */
560   m_strcpy(buffer, sizeof(buffer), "STAT\r\n");
561   ret = pop_query (pop_data, buffer, sizeof (buffer));
562   if (ret == PQ_NOT_CONNECTED)
563     goto fail;
564   if (ret == PQ_ERR) {
565     mutt_error ("%s", pop_data->err_msg);
566     goto finish;
567   }
568
569   sscanf (buffer, "+OK %d %d", &msgs, &bytes);
570
571   /* only get unread messages */
572   if (msgs > 0 && option (OPTPOPLAST)) {
573     m_strcpy(buffer, sizeof(buffer), "LAST\r\n");
574     ret = pop_query (pop_data, buffer, sizeof (buffer));
575     if (ret == PQ_NOT_CONNECTED)
576       goto fail;
577     if (ret == PQ_OK)
578       sscanf (buffer, "+OK %d", &last);
579   }
580
581   if (msgs <= last) {
582     mutt_message _("No new mail in POP mailbox.");
583
584     goto finish;
585   }
586
587   if (mx_open_mailbox (NONULL (Spoolfile), M_APPEND, &ctx) == NULL)
588     goto finish;
589
590   delanswer =
591     query_quadoption (OPT_POPDELETE, _("Delete messages from server?"));
592
593   snprintf (msgbuf, sizeof (msgbuf), _("Reading new messages (%d bytes)..."),
594             bytes);
595   mutt_message ("%s", msgbuf);
596
597   for (i = last + 1; i <= msgs; i++) {
598     if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL)
599       ret = -3;
600     else {
601       snprintf (buffer, sizeof (buffer), "RETR %d\r\n", i);
602       ret = pop_fetch_data (pop_data, buffer, NULL, fetch_message, msg->fp);
603       if (ret == PFD_FUNCT_ERROR)
604         rset = 1;
605
606       if (ret == PQ_OK && mx_commit_message (msg, &ctx) != 0) {
607         rset = 1;
608         ret = PFD_FUNCT_ERROR;
609       }
610
611       mx_close_message (&msg);
612     }
613
614     if (ret == PQ_OK && delanswer == M_YES) {
615       /* delete the message on the server */
616       snprintf (buffer, sizeof (buffer), "DELE %d\r\n", i);
617       ret = pop_query (pop_data, buffer, sizeof (buffer));
618     }
619
620     if (ret == PQ_NOT_CONNECTED) {
621       mx_close_mailbox (&ctx, NULL);
622       goto fail;
623     }
624     if (ret == PQ_ERR) {
625       mutt_error ("%s", pop_data->err_msg);
626       break;
627     }
628     if (ret == -3) { /* this is -3 when ret != 0, because it will keep the value from before *gna* */
629       mutt_error _("Error while writing mailbox!");
630
631       break;
632     }
633
634     mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last,
635                   msgs - last);
636   }
637
638   mx_close_mailbox (&ctx, NULL);
639
640   if (rset) {
641     /* make sure no messages get deleted */
642     m_strcpy(buffer, sizeof(buffer), "RSET\r\n");
643     if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
644       goto fail;
645   }
646
647 finish:
648   /* exit gracefully */
649   m_strcpy(buffer, sizeof(buffer), "QUIT\r\n");
650   if (pop_query (pop_data, buffer, sizeof (buffer)) == PQ_NOT_CONNECTED)
651     goto fail;
652   mutt_socket_close (conn);
653   p_delete(&pop_data);
654   return;
655
656 fail:
657   mutt_error _("Server closed connection!");
658   mutt_socket_close (conn);
659   p_delete(&pop_data);
660 }