Andreas Krennmair:
[apps/madmutt.git] / buffy.c
1 /* 
2  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3  * 
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "mutt.h"
24 #include "buffy.h"
25 #include "mailbox.h"
26 #include "mx.h"
27 #include "sidebar.h"
28
29 #include "mutt_curses.h"
30
31 #ifdef USE_IMAP
32 #include "imap.h"
33 #endif
34
35 #include <string.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
38 #include <utime.h>
39 #include <ctype.h>
40 #include <unistd.h>
41
42 #include <stdio.h>
43
44 static time_t BuffyTime = 0;    /* last time we started checking for mail */
45
46 #ifdef USE_IMAP
47 static time_t ImapBuffyTime = 0;        /* last time we started checking for mail */
48 #endif
49 static short BuffyCount = 0;    /* how many boxes with new mail */
50 static short BuffyNotify = 0;   /* # of unnotified new boxes */
51
52 #ifdef BUFFY_SIZE
53
54 /* Find the last message in the file. 
55  * upon success return 0. If no message found - return -1 */
56
57 int fseek_last_message (FILE * f)
58 {
59   long int pos;
60   char buffer[BUFSIZ + 9];      /* 7 for "\n\nFrom " */
61   int bytes_read;
62   int i;                        /* Index into `buffer' for scanning.  */
63
64   memset (buffer, 0, sizeof (buffer));
65   fseek (f, 0, SEEK_END);
66   pos = ftell (f);
67
68   /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
69    * `bytes_read' <= `BUFSIZ'.  */
70   bytes_read = pos % BUFSIZ;
71   if (bytes_read == 0)
72     bytes_read = BUFSIZ;
73   /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
74    * reads will be on block boundaries, which might increase efficiency.  */
75   while ((pos -= bytes_read) >= 0) {
76     /* we save in the buffer at the end the first 7 chars from the last read */
77     strncpy (buffer + BUFSIZ, buffer, 5 + 2);   /* 2 == 2 * mutt_strlen(CRLF) */
78     fseek (f, pos, SEEK_SET);
79     bytes_read = fread (buffer, sizeof (char), bytes_read, f);
80     if (bytes_read == -1)
81       return -1;
82     for (i = bytes_read; --i >= 0;)
83       if (!mutt_strncmp (buffer + i, "\n\nFrom ", mutt_strlen ("\n\nFrom "))) { /* found it - go to the beginning of the From */
84         fseek (f, pos + i + 2, SEEK_SET);
85         return 0;
86       }
87     bytes_read = BUFSIZ;
88   }
89
90   /* here we are at the beginning of the file */
91   if (!mutt_strncmp ("From ", buffer, 5)) {
92     fseek (f, 0, 0);
93     return (0);
94   }
95
96   return (-1);
97 }
98
99 /* Return 1 if the last message is new */
100 int test_last_status_new (FILE * f)
101 {
102   HEADER *hdr;
103   ENVELOPE *tmp_envelope;
104   int result = 0;
105
106   if (fseek_last_message (f) == -1)
107     return (0);
108
109   hdr = mutt_new_header ();
110   tmp_envelope = mutt_read_rfc822_header (f, hdr, 0, 0);
111   if (!(hdr->read || hdr->old))
112     result = 1;
113
114   mutt_free_envelope (&tmp_envelope);
115   mutt_free_header (&hdr);
116
117   return result;
118 }
119
120 int test_new_folder (const char *path)
121 {
122   FILE *f;
123   int rc = 0;
124   int typ;
125
126   typ = mx_get_magic (path);
127
128   if (typ != M_MBOX && typ != M_MMDF)
129     return 0;
130
131   if ((f = fopen (path, "rb"))) {
132     rc = test_last_status_new (f);
133     fclose (f);
134   }
135
136   return rc;
137 }
138
139 BUFFY *mutt_find_mailbox (const char *path)
140 {
141   BUFFY *tmp = NULL;
142   struct stat sb;
143   struct stat tmp_sb;
144
145   if (stat (path, &sb) != 0)
146     return NULL;
147
148   for (tmp = Incoming; tmp; tmp = tmp->next) {
149     if (stat (tmp->path, &tmp_sb) == 0 &&
150         sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
151       break;
152   }
153   return tmp;
154 }
155
156 void mutt_update_mailbox (BUFFY * b)
157 {
158   struct stat sb;
159
160   if (!b)
161     return;
162
163   if (stat (b->path, &sb) == 0)
164     b->size = (long) sb.st_size;
165   else
166     b->size = 0;
167   return;
168 }
169 #endif
170
171 int mutt_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
172                           BUFFER * err)
173 {
174   BUFFY **tmp, *tmp1;
175   char buf[_POSIX_PATH_MAX];
176
177 #ifdef BUFFY_SIZE
178   struct stat sb;
179 #endif /* BUFFY_SIZE */
180
181   while (MoreArgs (s)) {
182     mutt_extract_token (path, s, 0);
183     strfcpy (buf, path->data, sizeof (buf));
184
185     if (data == M_UNMAILBOXES && mutt_strcmp (buf, "*") == 0) {
186       for (tmp = &Incoming; *tmp;) {
187         FREE (&((*tmp)->path));
188         tmp1 = (*tmp)->next;
189         FREE (tmp);
190         *tmp = tmp1;
191       }
192       return 0;
193     }
194
195     mutt_expand_path (buf, sizeof (buf));
196
197     /* Skip empty tokens. */
198     if (!*buf)
199       continue;
200
201     /* simple check to avoid duplicates */
202     for (tmp = &Incoming; *tmp; tmp = &((*tmp)->next)) {
203       if (mutt_strcmp (buf, (*tmp)->path) == 0)
204         break;
205     }
206
207     if (data == M_UNMAILBOXES) {
208       if (*tmp) {
209         FREE (&((*tmp)->path));
210         tmp1 = (*tmp)->next;
211         FREE (tmp);
212         *tmp = tmp1;
213       }
214       continue;
215     }
216
217     if (!*tmp) {
218       *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
219       (*tmp)->path = safe_strdup (buf);
220       (*tmp)->next = NULL;
221       /* it is tempting to set magic right here */
222       (*tmp)->magic = 0;
223
224     }
225
226     (*tmp)->new = 0;
227     (*tmp)->notified = 1;
228     (*tmp)->newly_created = 0;
229
230 #ifdef BUFFY_SIZE
231     /* for buffy_size, it is important that if the folder is new (tested by
232      * reading it), the size is set to 0 so that later when we check we see
233      * that it increased .  without buffy_size we probably don't care.
234      */
235     if (stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path)) {
236       /* some systems out there don't have an off_t type */
237       (*tmp)->size = (long) sb.st_size;
238     }
239     else
240       (*tmp)->size = 0;
241 #endif /* BUFFY_SIZE */
242   }
243   return 0;
244 }
245
246 #ifdef BUFFY_SIZE
247 /* people use buffy_size on systems where modified time attributes are BADLY
248  * broken. Ignore them.
249  */
250 #define STAT_CHECK (sb.st_size > tmp->size)
251 #else
252 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
253 #endif /* BUFFY_SIZE */
254
255 /* values for force:
256  * 0    don't force any checks + update sidebar
257  * 1    force all checks + update sidebar
258  * 2    force all checks + _don't_ update sidebar
259  */
260 int mutt_buffy_check (int force)
261 {
262   BUFFY *tmp;
263   struct stat sb;
264   struct dirent *de;
265   DIR *dirp;
266   char path[_POSIX_PATH_MAX];
267   struct stat contex_sb;
268   time_t now, last1;
269   CONTEXT *ctx;
270
271 #ifdef USE_IMAP
272   time_t last2;
273
274   /* update postponed count as well, on force */
275   if (force != 0)
276     mutt_update_num_postponed ();
277 #endif
278
279   /* fastest return if there are no mailboxes */
280   if (!Incoming)
281     return 0;
282   now = time (NULL);
283   if (force == 0 && (now - BuffyTime < BuffyTimeout)
284 #ifdef USE_IMAP
285       && (now - ImapBuffyTime < ImapBuffyTimeout))
286 #else
287     )
288 #endif
289     return BuffyCount;
290
291   last1 = BuffyTime;
292   if (force != 0 || now - BuffyTime >= BuffyTimeout)
293     BuffyTime = now;
294 #ifdef USE_IMAP
295   last2 = ImapBuffyTime;
296   if (force != 0 || now - ImapBuffyTime >= ImapBuffyTimeout)
297     ImapBuffyTime = now;
298 #endif
299   BuffyCount = 0;
300   BuffyNotify = 0;
301
302 #ifdef USE_IMAP
303   if (!Context || Context->magic != M_IMAP)
304 #endif
305 #ifdef USE_POP
306     if (!Context || Context->magic != M_POP)
307 #endif
308 #ifdef USE_NNTP
309       if (!Context || Context->magic != M_NNTP)
310 #endif
311         /* check device ID and serial number instead of comparing paths */
312         if (!Context || !Context->path
313             || stat (Context->path, &contex_sb) != 0) {
314           contex_sb.st_dev = 0;
315           contex_sb.st_ino = 0;
316         }
317
318   for (tmp = Incoming; tmp; tmp = tmp->next) {
319 #ifdef USE_IMAP
320     if (mx_is_imap (tmp->path))
321       tmp->magic = M_IMAP;
322     else
323 #endif
324 #ifdef USE_POP
325     if (mx_is_pop (tmp->path))
326       tmp->magic = M_POP;
327     else
328 #endif
329 #ifdef USE_NNTP
330     if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
331       tmp->magic = M_NNTP;
332     else
333 #endif
334     if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
335           (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0)) {
336       /* if the mailbox still doesn't exist, set the newly created flag to
337        * be ready for when it does. */
338       tmp->newly_created = 1;
339       tmp->magic = 0;
340 #ifdef BUFFY_SIZE
341       tmp->size = 0;
342 #endif
343       continue;
344     }
345
346     /* check to see if the folder is the currently selected folder
347      * before polling */
348     if (!Context || !Context->path || ((0
349 #ifdef USE_IMAP
350                                         || tmp->magic == M_IMAP
351 #endif
352 #ifdef USE_POP
353                                         || tmp->magic == M_POP
354 #endif
355 #ifdef USE_NNTP
356                                         || tmp->magic == M_NNTP
357 #endif
358                                        )? mutt_strcmp (tmp->path,
359                                                        Context->path) : (sb.
360                                                                          st_dev
361                                                                          !=
362                                                                          contex_sb.
363                                                                          st_dev
364                                                                          ||
365                                                                          sb.
366                                                                          st_ino
367                                                                          !=
368                                                                          contex_sb.
369                                                                          st_ino)
370         )
371       ) {
372       switch (tmp->magic) {
373       case M_MBOX:
374       case M_MMDF:
375         /* only check on force or $mail_check reached */
376         if (force != 0 || (now - last1 >= BuffyTimeout)) {
377           if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
378             if (STAT_CHECK) {
379               BuffyCount++;
380               tmp->new = 1;
381             }
382 #ifdef BUFFY_SIZE
383             else {
384               /* some other program has deleted mail from the folder */
385               tmp->size = (long) sb.st_size;
386             }
387 #endif
388           }
389           else if (SidebarWidth > 0 && option (OPTMBOXPANE) &&
390                    (STAT_CHECK || tmp->msgcount == 0)) {
391             /* sidebar visible */
392             BuffyCount++;
393             if ((ctx =
394                  mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT,
395                                   NULL)) != NULL) {
396               tmp->msgcount = ctx->msgcount;
397               tmp->new = ctx->new;
398               tmp->msg_unread = ctx->new;       /* for sidebar, wtf? */
399               tmp->msg_flagged = ctx->flagged;
400               mx_close_mailbox (ctx, 0);
401             }
402           }
403           if (tmp->newly_created &&
404               (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
405             tmp->newly_created = 0;
406         }
407         else if (tmp->new > 0)
408           BuffyCount++;
409         break;
410
411       case M_MAILDIR:
412         /* only check on force or $mail_check reached */
413         if (force != 0 || (now - last1 >= BuffyTimeout)) {
414           snprintf (path, sizeof (path), "%s/new", tmp->path);
415           if ((dirp = opendir (path)) == NULL) {
416             tmp->magic = 0;
417             break;
418           }
419           tmp->new = 0;
420           tmp->msg_unread = 0;
421           tmp->msgcount = 0;
422           while ((de = readdir (dirp)) != NULL) {
423             char *p;
424
425             if (*de->d_name != '.' &&
426                 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
427               /* one new and undeleted message is enough */
428               if (tmp->new == 0) {
429                 BuffyCount++;
430                 tmp->new = 1;
431                 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
432                   /* if sidebar invisible -> done */
433                   break;
434               }
435               tmp->msgcount++;
436               tmp->msg_unread++;
437               tmp->new++;
438             }
439           }
440           closedir (dirp);
441
442           if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
443             /* only count total mail if sidebar visible */
444             snprintf (path, sizeof (path), "%s/cur", tmp->path);
445             if ((dirp = opendir (path)) == NULL) {
446               tmp->magic = 0;
447               break;
448             }
449             tmp->msg_flagged = 0;
450             while ((de = readdir (dirp)) != NULL) {
451               char *p;
452
453               if (*de->d_name != '.'
454                   && (p = strstr (de->d_name, ":2,")) != NULL) {
455                 if (!strchr (p + 3, 'T'))
456                   tmp->msgcount++;
457                 if (strchr (p + 3, 'F'))
458                   tmp->msg_flagged++;
459               }
460             }
461             closedir (dirp);
462           }
463         }
464         else if (tmp->new > 0)
465           /* keep current stats if !force and !$mail_check reached */
466           BuffyCount++;
467         break;
468
469       case M_MH:
470         /* only check on force or $mail_check reached */
471         if (force != 0 || (now - last1 >= BuffyTimeout)) {
472           if ((tmp->new = mh_buffy (tmp->path)) > 0)
473             BuffyCount++;
474           if (SidebarWidth > 0 && option (OPTMBOXPANE)) {
475             DIR *dp;
476             struct dirent *de;
477
478             if ((dp = opendir (path)) == NULL)
479               break;
480             tmp->new = 0;
481             tmp->msgcount = 0;
482             tmp->msg_unread = 0;
483             while ((de = readdir (dp))) {
484               if (mh_valid_message (de->d_name)) {
485                 tmp->msgcount++;
486                 tmp->msg_unread++;
487                 tmp->new++;
488               }
489             }
490             closedir (dp);
491           }
492         }
493         else if (tmp->new > 0)
494           /* keep current stats if !force and !$mail_check reached */
495           BuffyCount++;
496         break;
497
498 #ifdef USE_IMAP
499       case M_IMAP:
500         /* only check on force or $imap_mail_check reached */
501         if (force != 0 || (now - last2 >= ImapBuffyTimeout)) {
502           tmp->msgcount = imap_mailbox_check (tmp->path, 0);
503           if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
504             BuffyCount++;
505             tmp->msg_unread = tmp->new; /* for sidebar; wtf? */
506           }
507           else {
508             tmp->new = 0;
509             tmp->msg_unread = 0;
510           }
511         }
512         else if (tmp->new > 0)
513           /* keep current stats if !force and !$imap_mail_check reached */
514           BuffyCount++;
515         break;
516 #endif
517
518 #ifdef USE_POP
519       case M_POP:
520         break;
521 #endif
522
523 #ifdef USE_NNTP
524       case M_NNTP:
525         break;
526 #endif
527       }
528     }
529 #ifdef BUFFY_SIZE
530     else if (Context && Context->path)
531       tmp->size = (long) sb.st_size;    /* update the size */
532 #endif
533
534     if (tmp->new <= 0)
535       tmp->notified = 0;
536     else if (!tmp->notified)
537       BuffyNotify++;
538     tmp->has_new = tmp->new > 0;
539   }
540   if (BuffyCount > 0 && force != 2)
541     draw_sidebar (CurrentMenu);
542   return (BuffyCount);
543 }
544
545 int mutt_buffy_list (void)
546 {
547   BUFFY *tmp;
548   char path[_POSIX_PATH_MAX];
549   char buffylist[160];
550   int pos;
551   int first;
552   int have_unnotified = BuffyNotify;
553
554   if (option (OPTFORCEBUFFYCHECK))
555     mutt_buffy_check (1);
556
557   pos = 0;
558   first = 1;
559   buffylist[0] = 0;
560   pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
561   for (tmp = Incoming; tmp; tmp = tmp->next) {
562     /* Is there new mail in this mailbox? */
563     if (tmp->new <= 0 || (have_unnotified && tmp->notified))
564       continue;
565
566     strfcpy (path, tmp->path, sizeof (path));
567     mutt_pretty_mailbox (path);
568
569     if (!first && pos + strlen (path) >= COLS - 7)
570       break;
571
572     if (!first)
573       pos += strlen (strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos));    /* __STRNCAT_CHECKED__ */
574
575     /* Prepend an asterisk to mailboxes not already notified */
576     if (!tmp->notified) {
577       /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos));  __STRNCAT_CHECKED__ */
578       tmp->notified = 1;
579       BuffyNotify--;
580     }
581     pos += strlen (strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos));      /* __STRNCAT_CHECKED__ */
582     first = 0;
583   }
584   if (!first && tmp) {
585     strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos);   /* __STRNCAT_CHECKED__ */
586   }
587   if (!first) {
588     /* on new mail: redraw sidebar */
589     draw_sidebar (CurrentMenu);
590     mutt_message ("%s", buffylist);
591     return (1);
592   }
593   /* there were no mailboxes needing to be notified, so clean up since 
594    * BuffyNotify has somehow gotten out of sync
595    */
596   BuffyNotify = 0;
597   return (0);
598 }
599
600 int mutt_buffy_notify (void)
601 {
602   if (mutt_buffy_check (0) && BuffyNotify) {
603     return (mutt_buffy_list ());
604   }
605   return (0);
606 }
607
608 /* 
609  * mutt_buffy() -- incoming folders completion routine
610  *
611  * given a folder name, this routine gives the next incoming folder with new
612  * new mail.
613  */
614 void mutt_buffy (char *s, size_t slen)
615 {
616   int count;
617   BUFFY *tmp = Incoming;
618
619   mutt_expand_path (s, _POSIX_PATH_MAX);
620   switch (mutt_buffy_check (0)) {
621   case 0:
622
623     *s = '\0';
624     break;
625
626   case 1:
627
628     while (tmp && tmp->new <= 0)
629       tmp = tmp->next;
630     if (!tmp) {
631       *s = '\0';
632       mutt_buffy_check (1);     /* buffy was wrong - resync things */
633       break;
634     }
635     strfcpy (s, tmp->path, slen);
636     mutt_pretty_mailbox (s);
637     break;
638
639   default:
640
641     count = 0;
642     while (count < 3) {
643       if (mutt_strcmp (s, tmp->path) == 0)
644         count++;
645       else if (count && tmp->new > 0)
646         break;
647       tmp = tmp->next;
648       if (!tmp) {
649         tmp = Incoming;
650         count++;
651       }
652     }
653     if (count >= 3) {
654       *s = '\0';
655       mutt_buffy_check (1);     /* buffy was wrong - resync things */
656       break;
657     }
658     strfcpy (s, tmp->path, slen);
659     mutt_pretty_mailbox (s);
660     break;
661   }
662 }