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