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