Remove the time module alltogether.
[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/lib-ui.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;
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     if (!Context || !Context->path || 
149         (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
150         /* check device ID and serial number instead of comparing paths */
151         contex_sb.st_dev = 0;
152         contex_sb.st_ino = 0;
153     }
154
155     for (i = 0; i < Incoming.len; i++) {
156         BUFFY *tmp = Incoming.arr[i];
157         tmp->magic = mx_get_magic (tmp->path);
158         local = mx_is_local (tmp->magic-1);
159         if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
160             /* if the mailbox still doesn't exist, set the newly created flag to
161              * be ready for when it does. */
162             tmp->newly_created = 1;
163             tmp->magic = -1;
164             continue;
165         }
166
167         /* check to see if the folder is the currently selected folder
168          * before polling */
169         if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
170                                                     sb.st_ino != contex_sb.st_ino) : 
171                                            m_strcmp(tmp->path, Context->path))) {
172             switch (tmp->magic) {
173               case M_MBOX:
174                 /* only check on force or $mail_check reached */
175                 if (force == 1 || (now - last1 >= mod_buffy.mail_check)) {
176                     if (!sidebar_w) {
177                         if (STAT_CHECK) {
178                             BuffyCount++;
179                             tmp->new = 1;
180                         }
181                     }
182                     else if (STAT_CHECK || tmp->msgcount == 0) {
183                         /* sidebar visible */
184                         BuffyCount++;
185                         if ((ctx =
186                              mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
187                                               NULL)) != NULL) {
188                             tmp->msgcount = ctx->msgcount;
189                             tmp->new = ctx->new;
190                             tmp->msg_unread = ctx->new;       /* for sidebar, wtf? */
191                             tmp->msg_flagged = ctx->flagged;
192                             mx_close_mailbox (ctx, 0);
193                         }
194                     }
195                     if (tmp->newly_created &&
196                         (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
197                         tmp->newly_created = 0;
198                 }
199                 else if (tmp->new > 0)
200                     BuffyCount++;
201                 break;
202
203               case M_MAILDIR:
204                 /* only check on force or $mail_check reached */
205                 if (force == 1 || (now - last1 >= mod_buffy.mail_check)) {
206                     snprintf (path, sizeof (path), "%s/new", tmp->path);
207                     if ((dirp = opendir (path)) == NULL) {
208                         tmp->magic = 0;
209                         break;
210                     }
211                     tmp->new = 0;
212                     tmp->msg_unread = 0;
213                     tmp->msgcount = 0;
214                     while ((de = readdir (dirp)) != NULL) {
215                         char *p;
216
217                         if (*de->d_name != '.' &&
218                             (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
219                             /* one new and undeleted message is enough */
220                             if (tmp->new == 0) {
221                                 BuffyCount++;
222                                 if (!sidebar_w) {
223                                     /* if sidebar invisible -> done */
224                                     tmp->new = 1;
225                                     break;
226                                 }
227                             }
228                             tmp->msgcount++;
229                             tmp->msg_unread++;
230                             tmp->new++;
231                         }
232                     }
233                     closedir (dirp);
234
235                     if (sidebar_w) {
236                         /* only count total mail if sidebar visible */
237                         snprintf (path, sizeof (path), "%s/cur", tmp->path);
238                         if ((dirp = opendir (path)) == NULL) {
239                             tmp->magic = 0;
240                             break;
241                         }
242                         tmp->msg_flagged = 0;
243                         while ((de = readdir (dirp)) != NULL) {
244                             char *p;
245
246                             if (*de->d_name != '.'
247                                 && (p = strstr (de->d_name, ":2,")) != NULL) {
248                                 if (!strchr (p + 3, 'T'))
249                                     tmp->msgcount++;
250                                 if (strchr (p + 3, 'F'))
251                                     tmp->msg_flagged++;
252                             }
253                         }
254                         closedir (dirp);
255                     }
256                 }
257                 else if (tmp->new > 0)
258                     /* keep current stats if !force and !$mail_check reached */
259                     BuffyCount++;
260                 break;
261
262               case M_MH:
263                 /* only check on force or $mail_check reached */
264                 if (force == 1 || (now - last1 >= mod_buffy.mail_check)) {
265                     if ((tmp->new = mh_buffy (tmp->path)) > 0)
266                         BuffyCount++;
267                     if (sidebar_w) {
268                         DIR *dp;
269
270                         if ((dp = opendir (path)) == NULL)
271                             break;
272                         tmp->new = 0;
273                         tmp->msgcount = 0;
274                         tmp->msg_unread = 0;
275                         while ((de = readdir (dp))) {
276                             if (mh_valid_message (de->d_name)) {
277                                 tmp->msgcount++;
278                                 tmp->msg_unread++;
279                                 tmp->new++;
280                             }
281                         }
282                         closedir (dp);
283                     }
284                 }
285                 else if (tmp->new > 0)
286                     /* keep current stats if !force and !$mail_check reached */
287                     BuffyCount++;
288                 break;
289
290               case M_IMAP:
291                 /* only check on force or $imap_mail_check reached */
292                 if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
293                     tmp->msgcount = imap_mailbox_check (tmp->path, 0);
294                     tmp->new = imap_mailbox_check (tmp->path, 1);
295                     tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
296                     if (tmp->new > 0)
297                         BuffyCount++;
298                     else
299                         tmp->new = 0;
300                     if (tmp->msg_unread < 0)
301                         tmp->msg_unread = 0;
302                 }
303                 else if (tmp->new > 0)
304                     /* keep current stats if !force and !$imap_mail_check reached */
305                     BuffyCount++;
306                 break;
307
308             }
309         }
310
311         if (tmp->new <= 0)
312             tmp->notified = 0;
313         else if (!tmp->notified)
314             BuffyNotify++;
315         tmp->has_new = tmp->new > 0;
316     }
317     if (BuffyCount > 0 && force != 2)
318         sidebar_draw ();
319     return (BuffyCount);
320 }
321
322 int buffy_list (void)
323 {
324     char buffylist[LONG_STRING];
325     int have_unnotified = BuffyNotify;
326     int pos = 0, first = 1, i;
327
328     pos = m_strcpy(buffylist, sizeof(buffylist), _("New mail in "));
329
330     for (i = 0; i < Incoming.len; i++) {
331         char path[_POSIX_PATH_MAX];
332         BUFFY *tmp = Incoming.arr[i];
333
334         /* Is there new mail in this mailbox? */
335         if (tmp->new <= 0 || (have_unnotified && tmp->notified))
336             continue;
337
338         m_strcpy(path, sizeof(path), tmp->path);
339         mutt_pretty_mailbox(path);
340
341         if (!first) {
342             if (pos + m_strlen(path) >= COLS - 7)
343                 break;
344             pos += m_strcpy(buffylist + pos, sizeof(buffylist) - pos,  ", ");
345         }
346
347         tmp->notified = 1;
348         BuffyNotify--;
349
350         pos += m_strcpy(buffylist + pos, sizeof(buffylist) - pos, path);
351         first = 0;
352     }
353
354     if (!first) {
355         /* on new mail: redraw sidebar */
356         sidebar_draw();
357
358         if (i < Incoming.len) {
359             mutt_message("%s, ...", buffylist);
360         } else {
361             mutt_message("%s", buffylist);
362         }
363         return 1;
364     }
365
366     /* there were no mailboxes needing to be notified, so clean up since
367        BuffyNotify has somehow gotten out of sync */
368     BuffyNotify = 0;
369     return 0;
370 }
371
372 int buffy_notify(void)
373 {
374     return buffy_check(0) && BuffyNotify ? buffy_list() : 0;
375 }
376
377 /*
378  * buffy_next() -- incoming folders completion routine
379  *
380  * given a folder name, this routine gives the next incoming folder with new
381  * new mail.
382  */
383 void buffy_next(char *s, size_t slen)
384 {
385     int l = 0;
386     int c = 0, i = 0;
387
388     mutt_expand_path(s, _POSIX_PATH_MAX);
389     if (!buffy_check(0)) {
390         *s = '\0';
391         return;
392     }
393
394     /*
395      * buffy_lookup returns the index,
396      * or -1 if not found (-1..Incoming.len-1);
397      * plus one --> (0..Incoming.len).
398      * Modulo mapps it into the correct range.
399      */
400     i = 1 + buffy_lookup(s);
401     for (l = 0; l < Incoming.len; l++) {
402         c = (l+i) % Incoming.len;
403
404         if (m_strcmp(Incoming.arr[c]->path, s) && Incoming.arr[c]->new > 0)
405             break;
406     }
407
408     if (l >= Incoming.len) {
409         *s = '\0';
410         /* something went wrong since we're here when buffy_check
411          * reported new mail */
412         buffy_check(1);
413     } else {
414         m_strcpy(s, slen, Incoming.arr[c]->path);
415         mutt_pretty_mailbox(s);
416     }
417 }
418
419 /* vim:set ft=c: */