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