Nico Golde:
[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         if (SidebarWidth == 0 || !option (OPTMBOXPANE)) {
400           if (STAT_CHECK) {
401             BuffyCount++;
402             tmp->new = tmp->has_new = 1;
403           }
404 #ifdef BUFFY_SIZE
405           else
406           {
407             /* some other program has deleted mail from the folder */
408             tmp->size = (long) sb.st_size;
409           }
410 #endif
411         } else if (SidebarWidth > 0 && option (OPTMBOXPANE) && 
412                    (STAT_CHECK || tmp->msgcount == 0)) {
413           /* sidebar visible */
414           int msg_count = 0, msg_unread = 0, msg_new = 0;
415           BuffyCount++;
416           if ((ctx = mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT, NULL)) != NULL) {
417             msg_count = ctx->msgcount;
418             msg_unread = ctx->unread;
419             msg_new = ctx->new;
420             mx_close_mailbox (ctx, 0);
421           }
422           tmp->msgcount = msg_count;
423           tmp->msg_unread = msg_unread;
424           tmp->new = msg_new;
425           tmp->has_new = msg_new > 0;
426         }
427         if (tmp->newly_created &&
428             (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
429           tmp->newly_created = 0;
430         break;
431
432       case M_MAILDIR:
433
434         snprintf (path, sizeof (path), "%s/new", tmp->path);
435         if ((dirp = opendir (path)) == NULL)
436         {
437           tmp->magic = 0;
438           break;
439         }
440         tmp->msgcount = 0;
441         tmp->msg_unread = 0;
442         tmp->new = 0;
443         while ((de = readdir (dirp)) != NULL)
444         {
445           char *p;
446           if (*de->d_name != '.' && 
447               (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
448           {
449             /* one new and undeleted message is enough */
450             if (tmp->new == 0)
451             {
452                BuffyCount++;
453                tmp->has_new = tmp->new = 1;
454                if (SidebarWidth == 0 || !option (OPTMBOXPANE))
455                  /* if sidebar invisible -> done */
456                  break;
457             }
458             tmp->msgcount++;
459             tmp->msg_unread++;
460             tmp->new++;
461           }
462         }
463         closedir (dirp);
464
465         if (SidebarWidth > 0 && option (OPTMBOXPANE))
466         {
467           /* only count total mail if sidebar visible */
468           snprintf (path, sizeof (path), "%s/cur", tmp->path);
469           if ((dirp = opendir (path)) == NULL)
470           {
471             tmp->magic = 0;
472             break;
473           }
474           while ((de = readdir (dirp)) != NULL)
475           {
476             char *p;
477             if (*de->d_name != '.' && 
478                 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
479             {
480               tmp->msgcount++;
481             }
482           }
483           closedir (dirp);
484         }
485         break;
486
487       case M_MH:
488         if ((tmp->new = mh_buffy (tmp->path)) > 0)
489           BuffyCount++;
490         if (SidebarWidth > 0 && option (OPTMBOXPANE))
491         {
492           DIR *dp;
493           struct dirent *de;
494           if ((dp = opendir (path)) == NULL)
495             break;
496               tmp->msgcount = 0;
497           while ((de = readdir (dp)))
498           {
499             if (mh_valid_message (de->d_name))
500             {
501               tmp->msgcount++;
502               tmp->has_new = tmp->new = 1;
503             }
504           }
505           closedir (dp);
506         }
507         break;
508
509 #ifdef USE_IMAP
510       case M_IMAP:
511         tmp->msgcount = imap_mailbox_check(tmp->path, 0);
512         if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0) {
513           BuffyCount++;
514         }
515         else
516           tmp->new = 0;
517         break;
518 #endif
519
520 #ifdef USE_POP
521       case M_POP:
522         break;
523 #endif
524
525 #ifdef USE_NNTP
526       case M_NNTP:
527         break;
528 #endif
529       }
530     }
531 #ifdef BUFFY_SIZE
532     else if (Context && Context->path)
533       tmp->size = (long) sb.st_size;    /* update the size */
534 #endif
535
536     if (!tmp->new)
537       tmp->notified = 0;
538     else if (!tmp->notified)
539       BuffyNotify++;
540   }
541
542   BuffyDoneTime = BuffyTime;
543   return (BuffyCount);
544 }
545
546 int mutt_buffy_list (void)
547 {
548   BUFFY *tmp;
549   char path[_POSIX_PATH_MAX];
550   char buffylist[160];
551   int pos;
552   int first;
553
554   int have_unnotified = BuffyNotify;
555   
556   pos = 0;
557   first = 1;
558   buffylist[0] = 0;
559   pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
560   for (tmp = Incoming; tmp; tmp = tmp->next)
561   {
562     /* Is there new mail in this mailbox? */
563     if (!tmp->new || (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     {
578       /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos));  __STRNCAT_CHECKED__ */
579       tmp->notified = 1;
580       BuffyNotify--;
581     }
582     pos += strlen (strncat(buffylist + pos, path, sizeof(buffylist)-1-pos)); /* __STRNCAT_CHECKED__ */
583     first = 0;
584   }
585   if (!first && tmp)
586   {
587     strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos); /* __STRNCAT_CHECKED__ */
588   }
589   if (!first)
590   {
591     /* on new mail: redraw sidebar */
592     draw_sidebar (CurrentMenu);
593     mutt_message ("%s", buffylist);
594     return (1);
595   }
596   /* there were no mailboxes needing to be notified, so clean up since 
597    * BuffyNotify has somehow gotten out of sync
598    */
599   BuffyNotify = 0;
600   return (0);
601 }
602
603 int mutt_buffy_notify (void)
604 {
605   if (mutt_buffy_check (0) && BuffyNotify)
606   {
607     return (mutt_buffy_list ());
608   }
609   return (0);
610 }
611
612 /* 
613  * mutt_buffy() -- incoming folders completion routine
614  *
615  * given a folder name, this routine gives the next incoming folder with new
616  * new mail.
617  */
618 void mutt_buffy (char *s, size_t slen)
619 {
620   int count;
621   BUFFY *tmp = Incoming;
622
623   mutt_expand_path (s, _POSIX_PATH_MAX);
624   switch (mutt_buffy_check (0))
625   {
626   case 0:
627
628     *s = '\0';
629     break;
630
631   case 1:
632
633     while (tmp && !tmp->new)
634       tmp = tmp->next;
635     if (!tmp)
636     {
637       *s = '\0';
638       mutt_buffy_check (1); /* buffy was wrong - resync things */
639       break;
640     }
641     strfcpy (s, tmp->path, slen);
642     mutt_pretty_mailbox (s);
643     break;
644
645   default:
646     
647     count = 0;
648     while (count < 3)
649     {
650       if (mutt_strcmp (s, tmp->path) == 0)
651         count++;
652       else if (count && tmp->new)
653         break;
654       tmp = tmp->next;
655       if (!tmp)
656       {
657         tmp = Incoming;
658         count++;
659       }
660     }
661     if (count >= 3)
662     {
663       *s = '\0';
664       mutt_buffy_check (1); /* buffy was wrong - resync things */
665       break;
666     }
667     strfcpy (s, tmp->path, slen);
668     mutt_pretty_mailbox (s);
669     break;
670   }
671 }