2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * Copyright © 2007 Pierre Habouzit
20 #include <lib-lib/lib-lib.h>
21 #include "lib-lua_priv.h"
25 static int madmutt_pwd(lua_State *L)
27 char path[_POSIX_PATH_MAX];
28 getcwd(path, sizeof(path));
29 lua_pushstring(L, path);
33 static int madmutt_folder_path(lua_State *L)
35 lua_pushstring(L, CurrentFolder ?: "");
39 static int madmutt_folder_name(lua_State *L)
43 if (!m_strisempty(Maildir)
44 && m_strstart(CurrentFolder, Maildir, &p) && *p) {
49 p = strchr(CurrentFolder ?: "", '/');
50 lua_pushstring(L, p ? p + 1 : (CurrentFolder ?: ""));
55 static int madmutt_assign(lua_State *L)
57 const char *idx = luaL_checkstring(L, 2);
58 const char *val = luaL_checkstring(L, 3);
60 switch (lua_which_token(idx, -1)) {
62 luaL_error(L, "bad subscript to madmutt: %s", val);
70 lua_getmetatable(L, 1);
71 lua_pushstring(L, idx);
72 lua_pushstring(L, val);
78 static const struct luaL_Reg madmutt_module_funcs[] = {
82 ** \fIThis is a read-only system property and, at runtime,
83 ** specifies the current working directory of the madmutt
86 {"folder_path", madmutt_folder_path},
89 ** \fIThis is a read-only system property and, at runtime,
90 ** specifies the full path or URI of the folder currently
93 {"folder_name", madmutt_folder_name},
96 ** \fIThis is a read-only system property and, at runtime,
97 ** specifies the actual name of the folder as far as it could
100 ** For detection, $$$folder is first taken into account
101 ** and simply stripped to form the result when a match is found. For
102 ** example, with $$$folder being \fTimap://host\fP and the folder is
103 ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just
106 ** Second, if the initial portion of a name is not $$$folder,
107 ** the result will be everything after the last ``/''.
109 ** Third and last, the result will be just the name if neither
110 ** $$$folder nor a ``/'' were found in the name.
113 {"__newindex", madmutt_assign},
117 static const struct {
120 } madmutt_module_vars[] = {
121 { "version", VERSION },
124 ** \fIThis is a read-only system property and specifies madmutt's
125 ** version string.\fP
127 { "sysconfdir", SYSCONFDIR },
130 ** \fIThis is a read-only system property and specifies madmutt's
131 ** subversion revision string.\fP
133 { "bindir", BINDIR },
136 ** \fIThis is a read-only system property and specifies the
137 ** directory containing the madmutt binary.\fP
139 { "docdir", PKGDOCDIR },
142 ** \fIThis is a read-only system property and specifies the
143 ** directory containing the madmutt documentation.\fP
146 #if defined(HAVE_QDBM)
147 {"hcache_backend", "qdbm" },
148 #elif defined(HAVE_GDBM)
149 {"hcache_backend", "gdbm" },
150 #elif defined(HAVE_DB4)
151 {"hcache_backend", "db4" },
153 {"hcache_backend", "unknown" },
157 ** \fIThis is a read-only system property and specifies the
158 ** header chaching's database backend.\fP
163 int luaopen_madmutt(lua_State *L)
167 lua_newuserdata(L, sizeof(void*));
168 luaL_newmetatable(L, "madmutt.core");
170 luaL_openlib(L, NULL, madmutt_module_funcs, 0);
172 for (i = 0; i < countof(madmutt_module_vars); i++) {
173 lua_pushstring(L, madmutt_module_vars[i].value);
174 lua_setfield(L, -2, madmutt_module_vars[i].key);
177 lua_pushstring(L, "__index");
178 lua_pushvalue(L, -2);
181 lua_setmetatable(L, -2);
182 lua_setglobal(L, "madmutt");