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