Changes of bugtracker info and names in fleang manual
[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 time_t BuffyDoneTime = 0;       /* last time we knew for sure how much mail there was. */
46 static short BuffyCount = 0;    /* how many boxes with new mail */
47 static short BuffyNotify = 0;   /* # of unnotified new boxes */
48
49 #ifdef BUFFY_SIZE
50
51 /* Find the last message in the file. 
52  * upon success return 0. If no message found - return -1 */
53
54 int fseek_last_message (FILE * f)
55 {
56   long int pos;
57   char buffer[BUFSIZ + 9];      /* 7 for "\n\nFrom " */
58   int bytes_read;
59   int i;                        /* Index into `buffer' for scanning.  */
60
61   memset (buffer, 0, sizeof(buffer));
62   fseek (f, 0, SEEK_END);
63   pos = ftell (f);
64
65   /* Set `bytes_read' to the size of the last, probably partial, buffer; 0 <
66    * `bytes_read' <= `BUFSIZ'.  */
67   bytes_read = pos % BUFSIZ;
68   if (bytes_read == 0)
69     bytes_read = BUFSIZ;
70   /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
71    * reads will be on block boundaries, which might increase efficiency.  */
72   while ((pos -= bytes_read) >= 0)
73   {
74     /* we save in the buffer at the end the first 7 chars from the last read */
75     strncpy (buffer + BUFSIZ, buffer, 5+2); /* 2 == 2 * mutt_strlen(CRLF) */
76     fseek (f, pos, SEEK_SET);
77     bytes_read = fread (buffer, sizeof (char), bytes_read, f);
78     if (bytes_read == -1)
79       return -1;
80     for (i = bytes_read; --i >= 0;)
81       if (!mutt_strncmp (buffer + i, "\n\nFrom ", mutt_strlen ("\n\nFrom ")))
82       {                         /* found it - go to the beginning of the From */
83         fseek (f, pos + i + 2, SEEK_SET);
84         return 0;
85       }
86     bytes_read = BUFSIZ;
87   }
88
89   /* here we are at the beginning of the file */
90   if (!mutt_strncmp ("From ", buffer, 5))
91   {
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   {
133     rc = test_last_status_new (f);
134     fclose (f);
135   }
136
137   return rc;
138 }
139
140 BUFFY *mutt_find_mailbox (const char *path)
141 {
142   BUFFY *tmp = NULL;
143   struct stat sb;
144   struct stat tmp_sb;
145   
146   if (stat (path,&sb) != 0)
147     return NULL;
148
149   for (tmp = Incoming; tmp; tmp = tmp->next)
150   {
151     if (stat (tmp->path,&tmp_sb) ==0 && 
152         sb.st_dev == tmp_sb.st_dev && sb.st_ino == tmp_sb.st_ino)
153       break;
154   }
155   return tmp;
156 }
157
158 void mutt_update_mailbox (BUFFY * b)
159 {
160   struct stat sb;
161
162   if (!b)
163     return;
164
165   if (stat (b->path, &sb) == 0)
166     b->size = (long) sb.st_size;
167   else
168     b->size = 0;
169   return;
170 }
171 #endif
172
173 int mutt_parse_mailboxes (BUFFER *path, BUFFER *s, unsigned long data, BUFFER *err)
174 {
175   BUFFY **tmp,*tmp1,*last;
176   char buf[_POSIX_PATH_MAX];
177   int dup = 0;
178 #ifdef BUFFY_SIZE
179   struct stat sb;
180 #endif /* BUFFY_SIZE */
181
182   /*
183    * FIXME
184    * to get rid of correcting the ->prev pointers in sidebar.c,
185    * correct them right here
186    */
187
188   /*
189    * FIXME
190    * if we really want to make the sort order of the sidebar
191    * configurable, this has to go right here
192    */
193   
194   while (MoreArgs (s))
195   {
196     mutt_extract_token (path, s, 0);
197     strfcpy (buf, path->data, sizeof (buf));
198
199     if(data == M_UNMAILBOXES && mutt_strcmp(buf,"*") == 0)
200     {
201       for (tmp = &Incoming; *tmp;)
202       {
203         FREE (&((*tmp)->path));
204         tmp1=(*tmp)->next;
205         FREE (tmp);
206         *tmp=tmp1;
207       }
208       return 0;
209     }
210
211     mutt_expand_path (buf, sizeof (buf));
212
213     /* Skip empty tokens. */
214     if(!*buf) continue;
215
216     /* simple check to avoid duplicates */
217     dup = 0;
218     for (tmp = &Incoming; *tmp && dup == 0; tmp = &((*tmp)->next))
219     {
220       if (mutt_strcmp (buf, (*tmp)->path) == 0) {
221         dup = 1;
222         break;
223       }
224     }
225
226     if (dup == 1)
227       continue;
228     tmp = &Incoming;
229
230     if(data == M_UNMAILBOXES)
231     {
232       if(*tmp)
233       {
234         FREE (&((*tmp)->path));
235         tmp1=(*tmp)->next;
236         FREE (tmp);
237         *tmp=tmp1;
238       }
239       continue;
240     }
241
242     /* loop over list while it's sorted */
243     tmp1 = NULL;
244     last = NULL;
245     for (tmp = &Incoming; *tmp ; tmp = &((*tmp)->next)) {
246       /*
247        * FIXME
248        * change this to get whatever sorting order
249        */
250       if (mutt_strcmp (buf, (*tmp)->path) < 0) {
251         tmp1 = (*tmp);
252         break;
253       }
254       last = (*tmp);
255     }
256
257     /* we want: last -> tmp -> tmp1 */
258     *tmp = (BUFFY *) safe_calloc (1, sizeof (BUFFY));
259     (*tmp)->path = safe_strdup (buf);
260     (*tmp)->magic = 0;
261
262     /* correct pointers */
263     (*tmp)->next = tmp1;
264     if (last)
265       last->next = (*tmp);
266
267     /* left as-is */
268     (*tmp)->new = 0;
269     (*tmp)->notified = 1;
270     (*tmp)->newly_created = 0;
271
272 #ifdef BUFFY_SIZE
273     /* for buffy_size, it is important that if the folder is new (tested by
274      * reading it), the size is set to 0 so that later when we check we see
275      * that it increased .  without buffy_size we probably don't care.
276      */
277     if (stat ((*tmp)->path, &sb) == 0 && !test_new_folder ((*tmp)->path))
278     {
279       /* some systems out there don't have an off_t type */
280       (*tmp)->size = (long) sb.st_size;
281     }
282     else
283       (*tmp)->size = 0;
284 #endif /* BUFFY_SIZE */
285   }
286   return 0;
287 }
288
289 #ifdef BUFFY_SIZE
290 /* people use buffy_size on systems where modified time attributes are BADLY
291  * broken. Ignore them.
292  */
293 #define STAT_CHECK (sb.st_size > tmp->size)
294 #else
295 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
296 #endif /* BUFFY_SIZE */
297
298 int mutt_buffy_check (int force)
299 {
300   BUFFY *tmp;
301   struct stat sb;
302   struct dirent *de;
303   DIR *dirp;
304   char path[_POSIX_PATH_MAX];
305   struct stat contex_sb;
306   time_t t;
307   CONTEXT *ctx;
308 #ifdef USE_IMAP
309   /* update postponed count as well, on force */
310   if (force)
311     mutt_update_num_postponed ();
312 #endif
313
314   /* fastest return if there are no mailboxes */
315   if (!Incoming)
316     return 0;
317   t = time (NULL);
318   if (!force && (t - BuffyTime < BuffyTimeout))
319     return BuffyCount;
320  
321   BuffyTime = t;
322   BuffyCount = 0;
323   BuffyNotify = 0;
324
325 #ifdef USE_IMAP
326   if (!Context || Context->magic != M_IMAP)
327 #endif
328 #ifdef USE_POP
329   if (!Context || Context->magic != M_POP)
330 #endif
331 #ifdef USE_NNTP
332   if (!Context || Context->magic != M_NNTP)
333 #endif
334   /* check device ID and serial number instead of comparing paths */
335   if (!Context || !Context->path || stat (Context->path, &contex_sb) != 0)
336   {
337     contex_sb.st_dev=0;
338     contex_sb.st_ino=0;
339   }
340   
341   for (tmp = Incoming; tmp; tmp = tmp->next)
342   {
343     if ( tmp->new == 1 )
344       tmp->has_new = 1;
345     tmp->new = 0;
346
347 #ifdef USE_IMAP
348     if (mx_is_imap (tmp->path))
349       tmp->magic = M_IMAP;
350     else
351 #endif
352 #ifdef USE_POP
353     if (mx_is_pop (tmp->path))
354       tmp->magic = M_POP;
355     else
356 #endif
357 #ifdef USE_NNTP
358     if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
359       tmp->magic = M_NNTP;
360     else
361 #endif
362     if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
363        (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
364     {
365       /* if the mailbox still doesn't exist, set the newly created flag to
366        * be ready for when it does. */
367       tmp->newly_created = 1;
368       tmp->magic = 0;
369 #ifdef BUFFY_SIZE
370       tmp->size = 0;
371 #endif
372       continue;
373     }
374
375     /* check to see if the folder is the currently selected folder
376      * before polling */
377     if (!Context || !Context->path ||
378          (
379            (0
380 #ifdef USE_IMAP
381             || tmp->magic == M_IMAP
382 #endif
383 #ifdef USE_POP
384             || tmp->magic == M_POP
385 #endif
386 #ifdef USE_NNTP
387             || tmp->magic == M_NNTP
388 #endif
389            ) ? mutt_strcmp (tmp->path, Context->path) :
390                (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
391          )
392        )
393     {
394       switch (tmp->magic)
395       {
396       case M_MBOX:
397       case M_MMDF:
398
399     {
400         if (STAT_CHECK || tmp->msgcount == 0)
401         {
402           BuffyCount++;
403           /* only do complete count if sidebar visible */
404           if (SidebarWidth > 0)
405           {
406             BUFFY b = *tmp;
407             int msgcount = 0;
408             int msg_unread = 0;
409             BuffyCount++;
410             /* parse the mailbox, to see how much mail there is */
411             ctx = mx_open_mailbox( tmp->path, M_READONLY | M_QUIET | M_NOSORT,
412               NULL);
413             if(ctx)
414             {
415                 msgcount = ctx->msgcount;
416                 msg_unread = ctx->unread;
417                 mx_close_mailbox(ctx, 0);
418             }
419             *tmp = b;
420             tmp->msgcount = msgcount;
421             tmp->msg_unread = msg_unread;
422             if(STAT_CHECK)
423                 tmp->has_new = tmp->new = 1;
424           }
425           else
426           {
427             /* sidebar invisible -> done */
428             tmp->new = 1;
429           }
430         }
431 #ifdef BUFFY_SIZE
432           else
433           {
434             /* some other program has deleted mail from the folder */
435             tmp->size = (long) sb.st_size;
436           }
437 #endif
438           if (tmp->newly_created &&
439               (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
440             tmp->newly_created = 0;
441           }
442         break;
443
444       case M_MAILDIR:
445
446         snprintf (path, sizeof (path), "%s/new", tmp->path);
447         if ((dirp = opendir (path)) == NULL)
448         {
449           tmp->magic = 0;
450           break;
451         }
452         tmp->msgcount = 0;
453         tmp->msg_unread = 0;
454         while ((de = readdir (dirp)) != NULL)
455         {
456           char *p;
457           if (*de->d_name != '.' && 
458               (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
459           {
460             /* one new and undeleted message is enough */
461             if (tmp->new != 1)
462             {
463                BuffyCount++;
464                tmp->has_new = tmp->new = 1;
465                if (SidebarWidth == 0)
466                {
467                  /* if sidebar invisible -> done */
468                  break;
469                }
470             }
471             tmp->msgcount++;
472             tmp->msg_unread++;
473           }
474         }
475         closedir (dirp);
476
477         if (SidebarWidth > 0)
478         {
479           /* only count total mail if sidebar visible */
480           snprintf (path, sizeof (path), "%s/cur", tmp->path);
481           if ((dirp = opendir (path)) == NULL)
482           {
483             tmp->magic = 0;
484             break;
485           }
486           while ((de = readdir (dirp)) != NULL)
487           {
488             char *p;
489             if (*de->d_name != '.' && 
490                 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
491             {
492               tmp->msgcount++;
493             }
494           }
495           closedir (dirp);
496         }
497         break;
498
499       case M_MH:
500         if ((tmp->new = mh_buffy (tmp->path)) > 0)
501           BuffyCount++;
502         if (SidebarWidth > 0)
503         {
504           DIR *dp;
505           struct dirent *de;
506           if ((dp = opendir (path)) == NULL)
507             break;
508               tmp->msgcount = 0;
509           while ((de = readdir (dp)))
510           {
511             if (mh_valid_message (de->d_name))
512             {
513               tmp->msgcount++;
514               tmp->has_new = tmp->new = 1;
515             }
516           }
517           closedir (dp);
518         }
519         break;
520
521 #ifdef USE_IMAP
522       case M_IMAP:
523         tmp->msgcount = imap_mailbox_check(tmp->path, 0);
524         if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
525           BuffyCount++;
526         }
527         else
528           tmp->new = 0;
529         break;
530 #endif
531
532 #ifdef USE_POP
533       case M_POP:
534         break;
535 #endif
536
537 #ifdef USE_NNTP
538       case M_NNTP:
539         break;
540 #endif
541       }
542     }
543 #ifdef BUFFY_SIZE
544     else if (Context && Context->path)
545       tmp->size = (long) sb.st_size;    /* update the size */
546 #endif
547
548     if (!tmp->new)
549       tmp->notified = 0;
550     else if (!tmp->notified)
551       BuffyNotify++;
552   }
553
554   BuffyDoneTime = BuffyTime;
555   return (BuffyCount);
556 }
557
558 int mutt_buffy_list (void)
559 {
560   BUFFY *tmp;
561   char path[_POSIX_PATH_MAX];
562   char buffylist[160];
563   int pos;
564   int first;
565
566   int have_unnotified = BuffyNotify;
567   
568   pos = 0;
569   first = 1;
570   buffylist[0] = 0;
571   pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
572   for (tmp = Incoming; tmp; tmp = tmp->next)
573   {
574     /* Is there new mail in this mailbox? */
575     if (!tmp->new || (have_unnotified && tmp->notified))
576       continue;
577
578     strfcpy (path, tmp->path, sizeof (path));
579     mutt_pretty_mailbox (path);
580     
581     if (!first && pos + strlen (path) >= COLS - 7)
582       break;
583     
584     if (!first)
585       pos += strlen (strncat(buffylist + pos, ", ", sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
586
587     /* Prepend an asterisk to mailboxes not already notified */
588     if (!tmp->notified)
589     {
590       /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos));  __STRNCAT_CHECKED__ */
591       tmp->notified = 1;
592       BuffyNotify--;
593     }
594     pos += strlen (strncat(buffylist + pos, path, sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
595     first = 0;
596   }
597   if (!first && tmp)
598   {
599     strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
600   }
601   if (!first)
602   {
603     /* on new mail: redraw sidebar */
604     draw_sidebar (CurrentMenu);
605     mutt_message ("%s", buffylist);
606     return (1);
607   }
608   /* there were no mailboxes needing to be notified, so clean up since 
609    * BuffyNotify has somehow gotten out of sync
610    */
611   BuffyNotify = 0;
612   return (0);
613 }
614
615 int mutt_buffy_notify (void)
616 {
617   if (mutt_buffy_check (0) && BuffyNotify)
618   {
619     return (mutt_buffy_list ());
620   }
621   return (0);
622 }
623
624 /* 
625  * mutt_buffy() -- incoming folders completion routine
626  *
627  * given a folder name, this routine gives the next incoming folder with new
628  * new mail.
629  */
630 void mutt_buffy (char *s, size_t slen)
631 {
632   int count;
633   BUFFY *tmp = Incoming;
634
635   mutt_expand_path (s, _POSIX_PATH_MAX);
636   switch (mutt_buffy_check (0))
637   {
638   case 0:
639
640     *s = '\0';
641     break;
642
643   case 1:
644
645     while (tmp && !tmp->new)
646       tmp = tmp->next;
647     if (!tmp)
648     {
649       *s = '\0';
650       mutt_buffy_check (1); /* buffy was wrong - resync things */
651       break;
652     }
653     strfcpy (s, tmp->path, slen);
654     mutt_pretty_mailbox (s);
655     break;
656
657   default:
658     
659     count = 0;
660     while (count < 3)
661     {
662       if (mutt_strcmp (s, tmp->path) == 0)
663         count++;
664       else if (count && tmp->new)
665         break;
666       tmp = tmp->next;
667       if (!tmp)
668       {
669         tmp = Incoming;
670         count++;
671       }
672     }
673     if (count >= 3)
674     {
675       *s = '\0';
676       mutt_buffy_check (1); /* buffy was wrong - resync things */
677       break;
678     }
679     strfcpy (s, tmp->path, slen);
680     mutt_pretty_mailbox (s);
681     break;
682   }
683 }