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);
65 switch (lua_which_token(idx, -1)) {
67 luaL_error(L, "bad subscript to madmutt: %s", val);
72 _mutt_expand_path(buf, sizeof(buf), val, 0);
77 lua_getmetatable(L, 1);
78 lua_pushstring(L, idx);
79 lua_pushstring(L, val);
85 static const struct luaL_Reg madmutt_module_funcs[] = {
89 ** \fIThis is a read-only system property and, at runtime,
90 ** specifies the current working directory of the madmutt
93 {"folder_path", madmutt_folder_path},
96 ** \fIThis is a read-only system property and, at runtime,
97 ** specifies the full path or URI of the folder currently
100 {"folder_name", madmutt_folder_name},
103 ** \fIThis is a read-only system property and, at runtime,
104 ** specifies the actual name of the folder as far as it could
107 ** For detection, $$$folder is first taken into account
108 ** and simply stripped to form the result when a match is found. For
109 ** example, with $$$folder being \fTimap://host\fP and the folder is
110 ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just
113 ** Second, if the initial portion of a name is not $$$folder,
114 ** the result will be everything after the last ``/''.
116 ** Third and last, the result will be just the name if neither
117 ** $$$folder nor a ``/'' were found in the name.
120 {"__newindex", madmutt_assign},
124 static const struct {
127 } madmutt_module_vars[] = {
128 { "version", VERSION },
131 ** \fIThis is a read-only system property and specifies madmutt's
132 ** version string.\fP
134 { "sysconfdir", SYSCONFDIR },
137 ** \fIThis is a read-only system property and specifies madmutt's
138 ** subversion revision string.\fP
140 { "bindir", BINDIR },
143 ** \fIThis is a read-only system property and specifies the
144 ** directory containing the madmutt binary.\fP
146 { "docdir", PKGDOCDIR },
149 ** \fIThis is a read-only system property and specifies the
150 ** directory containing the madmutt documentation.\fP
153 #if defined(HAVE_QDBM)
154 { "hcache_backend", "qdbm" },
155 #elif defined(HAVE_GDBM)
156 { "hcache_backend", "gdbm" },
157 #elif defined(HAVE_DB4)
158 { "hcache_backend", "db4" },
160 { "hcache_backend", "unknown" },
164 ** \fIThis is a read-only system property and specifies the
165 ** header chaching's database backend.\fP
170 static void madmutt_init_shell(char *buf, ssize_t len)
172 struct passwd *pw = getpwuid(getuid());
175 m_strcpy(buf, len, pw->pw_shell);
176 _mutt_expand_path(buf, len, pw->pw_shell, 0);
178 m_strcpy(buf, len, getenv("SHELL") ?: "/bin/sh");
182 static const struct {
184 void (*fun)(char *buf, ssize_t len);
185 } madmutt_module_vars2[] = {
186 { "shell", madmutt_init_shell },
189 ** Command to use when spawning a subshell. By default, the user's login
190 ** shell from \fT/etc/passwd\fP is used.
194 int luaopen_madmutt(lua_State *L)
198 lua_newuserdata(L, sizeof(void*));
199 luaL_newmetatable(L, "madmutt.core");
201 luaL_openlib(L, NULL, madmutt_module_funcs, 0);
203 for (i = 0; i < countof(madmutt_module_vars); i++) {
204 lua_pushstring(L, madmutt_module_vars[i].value);
205 lua_setfield(L, -2, madmutt_module_vars[i].key);
208 for (i = 0; i < countof(madmutt_module_vars2); i++) {
210 (madmutt_module_vars2[i].fun)(buf, sizeof(buf));
211 lua_pushstring(L, buf);
212 lua_setfield(L, -2, madmutt_module_vars2[i].key);
215 lua_pushstring(L, "__index");
216 lua_pushvalue(L, -2);
219 lua_setmetatable(L, -2);
220 lua_setglobal(L, "madmutt");