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>
22 #include <sys/types.h>
25 #include "lib-lua_priv.h"
29 static int madmutt_pwd(lua_State *L)
31 char path[_POSIX_PATH_MAX];
32 getcwd(path, sizeof(path));
33 lua_pushstring(L, path);
37 static int madmutt_folder_path(lua_State *L)
39 lua_pushstring(L, CurrentFolder ?: "");
43 static int madmutt_folder_name(lua_State *L)
47 if (!m_strisempty(Maildir)
48 && m_strstart(CurrentFolder, Maildir, &p) && *p) {
53 p = strchr(CurrentFolder ?: "", '/');
54 lua_pushstring(L, p ? p + 1 : (CurrentFolder ?: ""));
59 static int madmutt_assign(lua_State *L)
61 const char *idx = luaL_checkstring(L, 2);
62 const char *val = luaL_checkstring(L, 3);
64 switch (lua_which_token(idx, -1)) {
66 luaL_error(L, "bad subscript to madmutt: %s", val);
74 lua_getmetatable(L, 1);
75 lua_pushstring(L, idx);
76 lua_pushstring(L, val);
82 static const struct luaL_Reg madmutt_module_funcs[] = {
86 ** \fIThis is a read-only system property and, at runtime,
87 ** specifies the current working directory of the madmutt
90 {"folder_path", madmutt_folder_path},
93 ** \fIThis is a read-only system property and, at runtime,
94 ** specifies the full path or URI of the folder currently
97 {"folder_name", madmutt_folder_name},
100 ** \fIThis is a read-only system property and, at runtime,
101 ** specifies the actual name of the folder as far as it could
104 ** For detection, $$$folder is first taken into account
105 ** and simply stripped to form the result when a match is found. For
106 ** example, with $$$folder being \fTimap://host\fP and the folder is
107 ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just
110 ** Second, if the initial portion of a name is not $$$folder,
111 ** the result will be everything after the last ``/''.
113 ** Third and last, the result will be just the name if neither
114 ** $$$folder nor a ``/'' were found in the name.
117 {"__newindex", madmutt_assign},
121 static const struct {
124 } madmutt_module_vars[] = {
125 { "version", VERSION },
128 ** \fIThis is a read-only system property and specifies madmutt's
129 ** version string.\fP
131 { "sysconfdir", SYSCONFDIR },
134 ** \fIThis is a read-only system property and specifies madmutt's
135 ** subversion revision string.\fP
137 { "bindir", BINDIR },
140 ** \fIThis is a read-only system property and specifies the
141 ** directory containing the madmutt binary.\fP
143 { "docdir", PKGDOCDIR },
146 ** \fIThis is a read-only system property and specifies the
147 ** directory containing the madmutt documentation.\fP
150 #if defined(HAVE_QDBM)
151 { "hcache_backend", "qdbm" },
152 #elif defined(HAVE_GDBM)
153 { "hcache_backend", "gdbm" },
154 #elif defined(HAVE_DB4)
155 { "hcache_backend", "db4" },
157 { "hcache_backend", "unknown" },
161 ** \fIThis is a read-only system property and specifies the
162 ** header chaching's database backend.\fP
167 static const char *madmutt_init_shell(void)
169 struct passwd *pw = getpwuid(getuid());
173 return getenv("SHELL") ?: "/bin/sh";
176 static const struct {
178 const char *(*f)(void);
179 } madmutt_module_vars2[] = {
180 { "shell", madmutt_init_shell },
183 ** Command to use when spawning a subshell. By default, the user's login
184 ** shell from \fT/etc/passwd\fP is used.
188 int luaopen_madmutt(lua_State *L)
192 lua_newuserdata(L, sizeof(void*));
193 luaL_newmetatable(L, "madmutt.core");
195 luaL_openlib(L, NULL, madmutt_module_funcs, 0);
197 for (i = 0; i < countof(madmutt_module_vars); i++) {
198 lua_pushstring(L, madmutt_module_vars[i].value);
199 lua_setfield(L, -2, madmutt_module_vars[i].key);
202 for (i = 0; i < countof(madmutt_module_vars2); i++) {
203 lua_pushstring(L, madmutt_module_vars2[i].f());
204 lua_setfield(L, -2, madmutt_module_vars2[i].k);
207 lua_pushstring(L, "__index");
208 lua_pushvalue(L, -2);
211 lua_setmetatable(L, -2);
212 lua_setglobal(L, "madmutt");