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