Make buffy a lua package as well.
[apps/madmutt.git] / buffy.cpkg
similarity index 88%
rename from buffy.c
rename to buffy.cpkg
index 6c9ef3e..5ea4188 100644 (file)
--- a/buffy.c
@@ -14,6 +14,7 @@
 #include <dirent.h>
 #include <utime.h>
 
+#include <lib-lua/lib-lua.h>
 #include <lib-ui/curses.h>
 #include <lib-ui/sidebar.h>
 #include <lib-mx/mx.h>
 
 #include "mutt.h"
 #include "buffy.h"
+@import "lib-lua/base.cpkg"
 
 static time_t BuffyTime = 0;      /* last time we started checking for mail */
 static time_t ImapBuffyTime = 0;  /* last time we started checking for mail */
 static short  BuffyCount = 0;     /* how many boxes with new mail */
 static short  BuffyNotify = 0;    /* # of unnotified new boxes */
+buffy_array   Incoming;
 
-/* Return the index number of path in Incoming list */
-int buffy_lookup(const char* path)
-{
-    int i;
+@package Buffy {
+    /*
+     ** .pp
+     ** This variable configures how often (in seconds) Madmutt should look for
+     ** new mail.
+     ** .pp
+     ** \fBNote:\fP This does not apply to IMAP mailboxes, see $$imap_mail_check.
+     */
+    int mail_check = 5;
 
-    if (m_strisempty(path))
-        return -1;
+    void mailboxes(const string_t s) {
+        buffy_do_mailboxes(s, 1);
+        RETURN();
+    };
 
-    for (i = 0; i < Incoming.len; i++) {
-        if (!m_strcmp(Incoming.arr[i]->path, path))
-            return i;
-    }
+    void unmailboxes(const string_t s) {
+        buffy_do_mailboxes(s, 0);
+        RETURN();
+    };
+};
 
-    return -1;
-}
-
-int buffy_parse_mailboxes(BUFFER *path, BUFFER *s, unsigned long data,
-                          BUFFER *err __attribute__ ((unused)))
+void buffy_do_mailboxes(const char *s, int add)
 {
-    BUFFY *tmp;
     char buf[_POSIX_PATH_MAX];
+    BUFFY *tmp;
+    int i;
 
-    while (MoreArgs(s)) {
-        int i;
+    if (!m_strcmp(s, "*")) {
+        buffy_array_wipe(&Incoming);
+        return;
+    }
 
-        mutt_extract_token(path, s, 0);
-        m_strcpy(buf, sizeof(buf), path->data);
+    if (m_strisempty(s))
+        return;
 
-        if (data == M_UNMAILBOXES && !m_strcmp(buf, "*")) {
-            buffy_array_wipe(&Incoming);
-            return 0;
-        }
+    _mutt_expand_path(buf, sizeof(buf), s, 0);
+    i = buffy_lookup(buf);
 
-        /* Skip empty tokens. */
-        if (!*buf)
-            continue;
+    if (!add) {
+        tmp = buffy_array_take(&Incoming, i);
+        buffy_delete(&tmp);
+        return;
+    }
 
-        mutt_expand_path(buf, sizeof (buf));
-        i = buffy_lookup(buf);
+    if (i < 0) {
+        tmp = p_new(BUFFY, 1);
+        tmp->path = m_strdup(buf);
+        buffy_array_append(&Incoming, tmp);
+    } else {
+        tmp = Incoming.arr[i];
+    }
 
-        if (data == M_UNMAILBOXES) {
-            tmp = buffy_array_take(&Incoming, i);
-            buffy_delete(&tmp);
-            continue;
-        }
+    tmp->new = 0;
+    tmp->notified = 1;
+    tmp->newly_created = 0;
+}
 
-        if (i < 0) {
-            tmp = p_new(BUFFY, 1);
-            tmp->path = m_strdup(buf);
-            buffy_array_append(&Incoming, tmp);
-        } else {
-            tmp = Incoming.arr[i];
-        }
+/* Return the index number of path in Incoming list */
+int buffy_lookup(const char* path)
+{
+    int i;
 
-        tmp->new = 0;
-        tmp->notified = 1;
-        tmp->newly_created = 0;
+    if (m_strisempty(path))
+        return -1;
+
+    for (i = 0; i < Incoming.len; i++) {
+        if (!m_strcmp(Incoming.arr[i]->path, path))
+            return i;
     }
 
-    return 0;
+    return -1;
 }
 
 #define STAT_CHECK (sb.st_mtime > sb.st_atime || (tmp->newly_created && sb.st_ctime == sb.st_mtime && sb.st_ctime == sb.st_atime))
@@ -118,12 +132,12 @@ int buffy_check(int force)
         return 0;
 
     now = time (NULL);
-    if (force == 0 && (now - BuffyTime < BuffyTimeout)
+    if (force == 0 && (now - BuffyTime < Buffy.mail_check)
         && (now - ImapBuffyTime < ImapBuffyTimeout))
         return BuffyCount;
 
     last1 = BuffyTime;
-    if (force == 1 || now - BuffyTime >= BuffyTimeout)
+    if (force == 1 || now - BuffyTime >= Buffy.mail_check)
         BuffyTime = now;
     last2 = ImapBuffyTime;
     if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
@@ -161,7 +175,7 @@ int buffy_check(int force)
               case M_MBOX:
               case M_MMDF:
                 /* only check on force or $mail_check reached */
-                if (force == 1 || (now - last1 >= BuffyTimeout)) {
+                if (force == 1 || (now - last1 >= Buffy.mail_check)) {
                     if (!count) {
                         if (STAT_CHECK) {
                             BuffyCount++;
@@ -191,7 +205,7 @@ int buffy_check(int force)
 
               case M_MAILDIR:
                 /* only check on force or $mail_check reached */
-                if (force == 1 || (now - last1 >= BuffyTimeout)) {
+                if (force == 1 || (now - last1 >= Buffy.mail_check)) {
                     snprintf (path, sizeof (path), "%s/new", tmp->path);
                     if ((dirp = opendir (path)) == NULL) {
                         tmp->magic = 0;
@@ -250,7 +264,7 @@ int buffy_check(int force)
 
               case M_MH:
                 /* only check on force or $mail_check reached */
-                if (force == 1 || (now - last1 >= BuffyTimeout)) {
+                if (force == 1 || (now - last1 >= Buffy.mail_check)) {
                     if ((tmp->new = mh_buffy (tmp->path)) > 0)
                         BuffyCount++;
                     if (count) {
@@ -404,3 +418,5 @@ void buffy_next(char *s, size_t slen)
         mutt_pretty_mailbox(s);
     }
 }
+
+/* vim:set ft=c: */