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