Oops, forgot to remove that before moving mutt-ng to SVN.
[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
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     tmp->new = 0;
303
304 #ifdef USE_IMAP
305     if (mx_is_imap (tmp->path))
306       tmp->magic = M_IMAP;
307     else
308 #endif
309 #ifdef USE_POP
310     if (mx_is_pop (tmp->path))
311       tmp->magic = M_POP;
312     else
313 #endif
314 #ifdef USE_NNTP
315     if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
316       tmp->magic = M_NNTP;
317     else
318 #endif
319     if (stat (tmp->path, &sb) != 0 || sb.st_size == 0 ||
320         (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
321     {
322       /* if the mailbox still doesn't exist, set the newly created flag to
323        * be ready for when it does. */
324       tmp->newly_created = 1;
325       tmp->magic = 0;
326 #ifdef BUFFY_SIZE
327       tmp->size = 0;
328 #endif
329       continue;
330     }
331
332     /* check to see if the folder is the currently selected folder
333      * before polling */
334     if (!Context || !Context->path ||
335          (
336            (0
337 #ifdef USE_IMAP
338             || tmp->magic == M_IMAP
339 #endif
340 #ifdef USE_POP
341             || tmp->magic == M_POP
342 #endif
343 #ifdef USE_NNTP
344             || tmp->magic == M_NNTP
345 #endif
346            ) ? mutt_strcmp (tmp->path, Context->path) :
347                (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
348          )
349        )
350     {
351       switch (tmp->magic)
352       {
353       case M_MBOX:
354       case M_MMDF:
355
356         if (STAT_CHECK)
357         {
358           BuffyCount++;
359           tmp->new = 1;
360         }
361 #ifdef BUFFY_SIZE
362         else
363         {
364           /* some other program has deleted mail from the folder */
365           tmp->size = (long) sb.st_size;
366         }
367 #endif
368         if (tmp->newly_created &&
369             (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
370           tmp->newly_created = 0;
371
372         break;
373
374       case M_MAILDIR:
375
376         snprintf (path, sizeof (path), "%s/new", tmp->path);
377         if ((dirp = opendir (path)) == NULL)
378         {
379           tmp->magic = 0;
380           break;
381         }
382         while ((de = readdir (dirp)) != NULL)
383         {
384           char *p;
385           if (*de->d_name != '.' && 
386               (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
387           {
388             /* one new and undeleted message is enough */
389             BuffyCount++;
390             tmp->new = 1;
391             break;
392           }
393         }
394         closedir (dirp);
395         break;
396
397       case M_MH:
398         if ((tmp->new = mh_buffy (tmp->path)) > 0)
399           BuffyCount++;
400         break;
401         
402 #ifdef USE_IMAP
403       case M_IMAP:
404         if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0)
405           BuffyCount++;
406         else
407           tmp->new = 0;
408
409         break;
410 #endif
411
412 #ifdef USE_POP
413       case M_POP:
414         break;
415 #endif
416
417 #ifdef USE_NNTP
418       case M_NNTP:
419         break;
420 #endif
421       }
422     }
423 #ifdef BUFFY_SIZE
424     else if (Context && Context->path)
425       tmp->size = (long) sb.st_size;    /* update the size */
426 #endif
427
428     if (!tmp->new)
429       tmp->notified = 0;
430     else if (!tmp->notified)
431       BuffyNotify++;
432   }
433
434   BuffyDoneTime = BuffyTime;
435   return (BuffyCount);
436 }
437
438 int mutt_buffy_list (void)
439 {
440   BUFFY *tmp;
441   char path[_POSIX_PATH_MAX];
442   char buffylist[160];
443   int pos;
444   int first;
445
446   int have_unnotified = BuffyNotify;
447   
448   pos = 0;
449   first = 1;
450   buffylist[0] = 0;
451   pos += strlen (strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos));
452   for (tmp = Incoming; tmp; tmp = tmp->next)
453   {
454     /* Is there new mail in this mailbox? */
455     if (!tmp->new || (have_unnotified && tmp->notified))
456       continue;
457
458     strfcpy (path, tmp->path, sizeof (path));
459     mutt_pretty_mailbox (path);
460     
461     if (!first && pos + strlen (path) >= COLS - 7)
462       break;
463     
464     if (!first)
465       pos += strlen (strncat(buffylist + pos, ", ", sizeof(buffylist)-1-pos));
466
467     /* Prepend an asterisk to mailboxes not already notified */
468     if (!tmp->notified)
469     {
470       /* pos += strlen (strncat(buffylist + pos, "*", sizeof(buffylist)-1-pos)); */
471       tmp->notified = 1;
472       BuffyNotify--;
473     }
474     pos += strlen (strncat(buffylist + pos, path, sizeof(buffylist)-1-pos));
475     first = 0;
476   }
477   if (!first && tmp)
478   {
479     strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos);
480   }
481   if (!first)
482   {
483     mutt_message ("%s", buffylist);
484     return (1);
485   }
486   /* there were no mailboxes needing to be notified, so clean up since 
487    * BuffyNotify has somehow gotten out of sync
488    */
489   BuffyNotify = 0;
490   return (0);
491 }
492
493 int mutt_buffy_notify (void)
494 {
495   if (mutt_buffy_check (0) && BuffyNotify)
496   {
497     return (mutt_buffy_list ());
498   }
499   return (0);
500 }
501
502 /* 
503  * mutt_buffy() -- incoming folders completion routine
504  *
505  * given a folder name, this routine gives the next incoming folder with new
506  * new mail.
507  */
508 void mutt_buffy (char *s, size_t slen)
509 {
510   int count;
511   BUFFY *tmp = Incoming;
512
513   mutt_expand_path (s, _POSIX_PATH_MAX);
514   switch (mutt_buffy_check (0))
515   {
516   case 0:
517
518     *s = '\0';
519     break;
520
521   case 1:
522
523     while (tmp && !tmp->new)
524       tmp = tmp->next;
525     if (!tmp)
526     {
527       *s = '\0';
528       mutt_buffy_check (1); /* buffy was wrong - resync things */
529       break;
530     }
531     strfcpy (s, tmp->path, slen);
532     mutt_pretty_mailbox (s);
533     break;
534
535   default:
536     
537     count = 0;
538     while (count < 3)
539     {
540       if (mutt_strcmp (s, tmp->path) == 0)
541         count++;
542       else if (count && tmp->new)
543         break;
544       tmp = tmp->next;
545       if (!tmp)
546       {
547         tmp = Incoming;
548         count++;
549       }
550     }
551     if (count >= 3)
552     {
553       *s = '\0';
554       mutt_buffy_check (1); /* buffy was wrong - resync things */
555       break;
556     }
557     strfcpy (s, tmp->path, slen);
558     mutt_pretty_mailbox (s);
559     break;
560   }
561 }