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