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