I don't care about systems where atimes are broken.
[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 #include <lib-lib/lib-lib.h>
14 #include <dirent.h>
15 #include <utime.h>
16
17 #include <lib-ui/curses.h>
18 #include <lib-ui/sidebar.h>
19
20 #include <imap/imap.h>
21
22 #include "mutt.h"
23 #include "buffy.h"
24 #include "mx.h"
25 #include "mh.h"
26
27 static time_t BuffyTime = 0;    /* last time we started checking for mail */
28
29 static time_t ImapBuffyTime = 0;        /* last time we started checking for mail */
30 static short BuffyCount = 0;    /* how many boxes with new mail */
31 static short BuffyNotify = 0;   /* # of unnotified new boxes */
32
33 /* func to free buffy for list_del() */
34 static inline void buffy_free (BUFFY** p) {
35   p_delete(&(*p)->path);
36   p_delete(p);
37 }
38
39 /* Return the index number of path in Incoming list */
40 int buffy_lookup (const char* path) {
41   int i = 0;
42
43   if (list_empty(Incoming) || !path || !*path)
44     return (-1);
45
46   for (i = 0; i < Incoming->length; i++) {
47     if (!m_strcmp(((BUFFY *)Incoming->data[i])->path, path))
48       return (i);
49   }
50
51   return (-1);
52 }
53
54 int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data,
55                           BUFFER * err __attribute__ ((unused)))
56 {
57   BUFFY* tmp;
58   char buf[_POSIX_PATH_MAX];
59   int i = 0;
60
61   while (MoreArgs (s)) {
62     mutt_extract_token (path, s, 0);
63     m_strcpy(buf, sizeof(buf), path->data);
64
65     if (data == M_UNMAILBOXES && !strcmp(buf, "*")) {
66       list_del (&Incoming, (list_del_t*) buffy_free);
67       return 0;
68     }
69
70     /* Skip empty tokens. */
71     if (!*buf)
72       continue;
73
74     mutt_expand_path (buf, sizeof (buf));
75     i = buffy_lookup (buf);
76
77     if (data == M_UNMAILBOXES) {
78       if (i >= 0) {
79         tmp = (BUFFY*) list_pop_idx (Incoming, i);
80         buffy_free (&tmp);
81       }
82       continue;
83     }
84
85     if (i < 0) {
86       tmp = p_new(BUFFY, 1);
87       tmp->path = m_strdup(buf);
88       tmp->magic = 0;
89       list_push_back (&Incoming, tmp);
90       i = Incoming->length-1;
91     } else
92       tmp = (BUFFY*) Incoming->data[i];
93
94     tmp->new = 0;
95     tmp->notified = 1;
96     tmp->newly_created = 0;
97
98   }
99   return 0;
100 }
101
102 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
103
104 /* values for force:
105  * 0    don't force any checks + update sidebar
106  * 1    force all checks + update sidebar
107  * 2    don't force any checks + _don't_ update sidebar
108  */
109 int buffy_check (int force)
110 {
111   BUFFY *tmp;
112   struct stat sb;
113   struct dirent *de;
114   DIR *dirp;
115   char path[_POSIX_PATH_MAX];
116   struct stat contex_sb;
117   time_t now, last1;
118   CONTEXT *ctx;
119   int i = 0;
120   int local = 0, count = 0;
121   time_t last2;
122
123   /* update postponed count as well, on force */
124   if (force == 1)
125     mutt_update_num_postponed ();
126
127   /* fastest return if there are no mailboxes */
128   if (list_empty(Incoming))
129     return 0;
130   now = time (NULL);
131   if (force == 0 && (now - BuffyTime < BuffyTimeout)
132       && (now - ImapBuffyTime < ImapBuffyTimeout))
133     return BuffyCount;
134
135   last1 = BuffyTime;
136   if (force == 1 || now - BuffyTime >= BuffyTimeout)
137     BuffyTime = now;
138   last2 = ImapBuffyTime;
139   if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
140     ImapBuffyTime = now;
141   BuffyCount = 0;
142   BuffyNotify = 0;
143
144   count = sidebar_need_count ();
145
146   if (!Context || !Context->path || 
147       (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
148     /* check device ID and serial number instead of comparing paths */
149     contex_sb.st_dev = 0;
150     contex_sb.st_ino = 0;
151   }
152
153   for (i = 0; i < Incoming->length; i++) {
154     tmp = (BUFFY*) Incoming->data[i];
155     tmp->magic = mx_get_magic (tmp->path);
156     local = mx_is_local (tmp->magic-1);
157     if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
158       /* if the mailbox still doesn't exist, set the newly created flag to
159        * be ready for when it does. */
160       tmp->newly_created = 1;
161       tmp->magic = -1;
162       continue;
163     }
164
165     /* check to see if the folder is the currently selected folder
166      * before polling */
167     if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
168                                                 sb.st_ino != contex_sb.st_ino) : 
169                                        m_strcmp(tmp->path, Context->path))) {
170       switch (tmp->magic) {
171       case M_MBOX:
172       case M_MMDF:
173         /* only check on force or $mail_check reached */
174         if (force == 1 || (now - last1 >= BuffyTimeout)) {
175           if (!count) {
176             if (STAT_CHECK) {
177               BuffyCount++;
178               tmp->new = 1;
179             }
180           }
181           else if (STAT_CHECK || tmp->msgcount == 0) {
182             /* sidebar visible */
183             BuffyCount++;
184             if ((ctx =
185                  mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
186                                   NULL)) != NULL) {
187               tmp->msgcount = ctx->msgcount;
188               tmp->new = ctx->new;
189               tmp->msg_unread = ctx->new;       /* for sidebar, wtf? */
190               tmp->msg_flagged = ctx->flagged;
191               mx_close_mailbox (ctx, 0);
192             }
193           }
194           if (tmp->newly_created &&
195               (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
196             tmp->newly_created = 0;
197         }
198         else if (tmp->new > 0)
199           BuffyCount++;
200         break;
201
202       case M_MAILDIR:
203         /* only check on force or $mail_check reached */
204         if (force == 1 || (now - last1 >= BuffyTimeout)) {
205           snprintf (path, sizeof (path), "%s/new", tmp->path);
206           if ((dirp = opendir (path)) == NULL) {
207             tmp->magic = 0;
208             break;
209           }
210           tmp->new = 0;
211           tmp->msg_unread = 0;
212           tmp->msgcount = 0;
213           while ((de = readdir (dirp)) != NULL) {
214             char *p;
215
216             if (*de->d_name != '.' &&
217                 (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
218               /* one new and undeleted message is enough */
219               if (tmp->new == 0) {
220                 BuffyCount++;
221                 if (!count) {
222                   /* if sidebar invisible -> done */
223                   tmp->new = 1;
224                   break;
225                 }
226               }
227               tmp->msgcount++;
228               tmp->msg_unread++;
229               tmp->new++;
230             }
231           }
232           closedir (dirp);
233
234           if (count) {
235             /* only count total mail if sidebar visible */
236             snprintf (path, sizeof (path), "%s/cur", tmp->path);
237             if ((dirp = opendir (path)) == NULL) {
238               tmp->magic = 0;
239               break;
240             }
241             tmp->msg_flagged = 0;
242             while ((de = readdir (dirp)) != NULL) {
243               char *p;
244
245               if (*de->d_name != '.'
246                   && (p = strstr (de->d_name, ":2,")) != NULL) {
247                 if (!strchr (p + 3, 'T'))
248                   tmp->msgcount++;
249                 if (strchr (p + 3, 'F'))
250                   tmp->msg_flagged++;
251               }
252             }
253             closedir (dirp);
254           }
255         }
256         else if (tmp->new > 0)
257           /* keep current stats if !force and !$mail_check reached */
258           BuffyCount++;
259         break;
260
261       case M_MH:
262         /* only check on force or $mail_check reached */
263         if (force == 1 || (now - last1 >= BuffyTimeout)) {
264           if ((tmp->new = mh_buffy (tmp->path)) > 0)
265             BuffyCount++;
266           if (count) {
267             DIR *dp;
268
269             if ((dp = opendir (path)) == NULL)
270               break;
271             tmp->new = 0;
272             tmp->msgcount = 0;
273             tmp->msg_unread = 0;
274             while ((de = readdir (dp))) {
275               if (mh_valid_message (de->d_name)) {
276                 tmp->msgcount++;
277                 tmp->msg_unread++;
278                 tmp->new++;
279               }
280             }
281             closedir (dp);
282           }
283         }
284         else if (tmp->new > 0)
285           /* keep current stats if !force and !$mail_check reached */
286           BuffyCount++;
287         break;
288
289       case M_IMAP:
290         /* only check on force or $imap_mail_check reached */
291         if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
292           tmp->msgcount = imap_mailbox_check (tmp->path, 0);
293           tmp->new = imap_mailbox_check (tmp->path, 1);
294           tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
295           if (tmp->new > 0)
296             BuffyCount++;
297           else
298             tmp->new = 0;
299           if (tmp->msg_unread < 0)
300             tmp->msg_unread = 0;
301         }
302         else if (tmp->new > 0)
303           /* keep current stats if !force and !$imap_mail_check reached */
304           BuffyCount++;
305         break;
306
307       }
308     }
309
310     if (tmp->new <= 0)
311       tmp->notified = 0;
312     else if (!tmp->notified)
313       BuffyNotify++;
314     tmp->has_new = tmp->new > 0;
315   }
316   if (BuffyCount > 0 && force != 2)
317     sidebar_draw ();
318   return (BuffyCount);
319 }
320
321 int buffy_list (void)
322 {
323   BUFFY *tmp;
324   char path[_POSIX_PATH_MAX];
325   char buffylist[160];
326   int pos;
327   int first;
328   int have_unnotified = BuffyNotify;
329   int i = 0;
330
331   pos = 0;
332   first = 1;
333   buffylist[0] = 0;
334   pos += m_strlen(strncat (buffylist, _("New mail in "), sizeof (buffylist) - 1 - pos)); /* __STRNCAT_CHECKED__ */
335   if (Incoming) {
336     for (i = 0; i < Incoming->length; i++) {
337       tmp = Incoming->data[i];
338       /* Is there new mail in this mailbox? */
339       if (tmp->new <= 0 || (have_unnotified && tmp->notified))
340         continue;
341
342       m_strcpy(path, sizeof(path), tmp->path);
343       mutt_pretty_mailbox (path);
344
345       if (!first && pos + m_strlen(path) >= COLS - 7)
346         break;
347
348       if (!first)
349         pos += m_strlen(strncat (buffylist + pos, ", ", sizeof (buffylist) - 1 - pos));    /* __STRNCAT_CHECKED__ */
350
351       tmp->notified = 1;
352       BuffyNotify--;
353
354       pos += m_strlen(strncat (buffylist + pos, path, sizeof (buffylist) - 1 - pos));      /* __STRNCAT_CHECKED__ */
355       first = 0;
356     }
357   }
358   if (!first && i < Incoming->length) {
359     strncat (buffylist + pos, ", ...", sizeof (buffylist) - 1 - pos);   /* __STRNCAT_CHECKED__ */
360   }
361   if (!first) {
362     /* on new mail: redraw sidebar */
363     sidebar_draw ();
364     mutt_message ("%s", buffylist);
365     return (1);
366   }
367   /* there were no mailboxes needing to be notified, so clean up since 
368    * BuffyNotify has somehow gotten out of sync
369    */
370   BuffyNotify = 0;
371   return (0);
372 }
373
374 int buffy_notify (void)
375 {
376   if (buffy_check (0) && BuffyNotify) {
377     return (buffy_list ());
378   }
379   return (0);
380 }
381
382 /* 
383  * buffy_next() -- incoming folders completion routine
384  *
385  * given a folder name, this routine gives the next incoming folder with new
386  * new mail.
387  */
388 void buffy_next (char *s, size_t slen)
389 {
390   int l = 0;
391   int c = 0, i = 0;
392
393   mutt_expand_path (s, _POSIX_PATH_MAX);
394   if (!buffy_check (0)) {
395       *s = '\0';
396       return;
397     }
398
399   /*
400    * buffy_lookup returns the index,
401    * or -1 if not found (-1..Incoming->length-1);
402    * plus one --> (0..Incoming->length).
403    * Modulo mapps it into the correct range.
404    */
405   i = 1 + buffy_lookup (s);
406   for (l = 0; l < Incoming->length; l++) {
407     c = (l+i) % Incoming->length;
408
409     if (m_strcmp(((BUFFY*) Incoming->data[c])->path, s) &&
410         ((BUFFY*) Incoming->data[c])->new > 0)
411       break;
412   }
413   if (l >= Incoming->length) {
414     *s = '\0';
415     /* something went wrong since we're here when buffy_check
416      * reported new mail */
417     buffy_check (1);
418   } else {
419     m_strcpy(s, slen, ((BUFFY*)Incoming->data[c])->path);
420     mutt_pretty_mailbox (s);
421   }
422 }