bye bye DT_SYS variables.
[apps/madmutt.git] / lib-lua / madmutt.c
diff --git a/lib-lua/madmutt.c b/lib-lua/madmutt.c
new file mode 100644 (file)
index 0000000..351ecb5
--- /dev/null
@@ -0,0 +1,148 @@
+/*
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or (at
+ *  your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful, but
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *  MA 02110-1301, USA.
+ *
+ *  Copyright © 2007 Pierre Habouzit
+ */
+
+#include <lib-lib/lib-lib.h>
+#include "lib-lua_priv.h"
+
+#include "../mutt.h"
+
+static int madmutt_pwd(lua_State *L)
+{
+    char path[_POSIX_PATH_MAX];
+    getcwd(path, sizeof(path));
+    lua_pushstring(L, path);
+    return 1;
+}
+
+static int madmutt_folder_path(lua_State *L)
+{
+    lua_pushstring(L, CurrentFolder ?: "");
+    return 1;
+}
+
+static int madmutt_folder_name(lua_State *L)
+{
+    const char *p;
+
+    if (!m_strisempty(Maildir)
+    && m_strstart(CurrentFolder, Maildir, &p) && *p) {
+        while (*p == '/')
+            p++;
+        lua_pushstring(L, p);
+    } else {
+        p = strchr(CurrentFolder ?: "", '/');
+        lua_pushstring(L, p ? p + 1 : (CurrentFolder ?: ""));
+    }
+    return 1;
+}
+
+static const struct luaL_Reg madmutt_module_funcs[] = {
+    {"pwd",         madmutt_pwd},
+    /*
+     ** .pp
+     ** \fIThis is a read-only system property and, at runtime,
+     ** specifies the current working directory of the madmutt
+     ** binary.\fP
+     */
+    {"folder_path", madmutt_folder_path},
+    /*
+     ** .pp
+     ** \fIThis is a read-only system property and, at runtime,
+     ** specifies the full path or URI of the folder currently
+     ** open (if any).\fP
+     */
+    {"folder_name", madmutt_folder_name},
+    /*
+     ** .pp
+     ** \fIThis is a read-only system property and, at runtime,
+     ** specifies the actual name of the folder as far as it could
+     ** be detected.\fP
+     ** .pp
+     ** For detection, $$$folder is first taken into account
+     ** and simply stripped to form the result when a match is found. For
+     ** example, with $$$folder being \fTimap://host\fP and the folder is
+     ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just
+     ** \fTINBOX/foo\fP.)
+     ** .pp
+     ** Second, if the initial portion of a name is not $$$folder,
+     ** the result will be everything after the last ``/''.
+     ** .pp
+     ** Third and last, the result will be just the name if neither
+     ** $$$folder nor a ``/'' were found in the name.
+     */
+    {NULL, NULL}
+};
+
+static const struct {
+    const char *key;
+    const char *value;
+} madmutt_module_vars[] = {
+    { "version",    VERSION },
+    /*
+     ** .pp
+     ** \fIThis is a read-only system property and specifies madmutt's
+     ** version string.\fP
+     */
+    { "sysconfdir", SYSCONFDIR },
+    /*
+     ** .pp
+     ** \fIThis is a read-only system property and specifies madmutt's
+     ** subversion revision string.\fP
+     */
+    { "bindir",     BINDIR },
+    /*
+     ** .pp
+     ** \fIThis is a read-only system property and specifies the
+     ** directory containing the madmutt binary.\fP
+     */
+    { "docdir",     PKGDOCDIR },
+    /*
+     ** .pp
+     ** \fIThis is a read-only system property and specifies the
+     ** directory containing the madmutt documentation.\fP
+     */
+#ifdef USE_HCACHE
+#if defined(HAVE_QDBM)
+    {"hcache_backend", "qdbm" },
+#elif defined(HAVE_GDBM)
+    {"hcache_backend", "gdbm" },
+#elif defined(HAVE_DB4)
+    {"hcache_backend", "db4" },
+#else
+    {"hcache_backend", "unknown" },
+#endif
+    /*
+     ** .pp
+     ** \fIThis is a read-only system property and specifies the
+     ** header chaching's database backend.\fP
+     */
+#endif
+};
+
+int luaopen_madmutt(lua_State *L)
+{
+    int i;
+    luaL_register(L, LUA_MADMUTT, madmutt_module_funcs);
+
+    for (i = 0; i < countof(madmutt_module_vars); i++) {
+        lua_pushstring(L, madmutt_module_vars[i].value);
+        lua_setfield(L, -2, madmutt_module_vars[i].key);
+    }
+    return 1;
+}