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 /* {{{ madmutt functions */
31 static int madmutt_pwd(lua_State *L)
33 char path[_POSIX_PATH_MAX];
34 getcwd(path, sizeof(path));
35 lua_pushstring(L, path);
39 static int madmutt_folder_path(lua_State *L)
41 lua_pushstring(L, CurrentFolder ?: "");
45 static int madmutt_folder_name(lua_State *L)
49 if (!m_strisempty(Maildir)
50 && m_strstart(CurrentFolder, Maildir, &p) && *p) {
55 p = strchr(CurrentFolder ?: "", '/');
56 lua_pushstring(L, p ? p + 1 : (CurrentFolder ?: ""));
61 static quadopt_t quadopt_parse(const char *s)
63 if (!m_strcasecmp("yes", s))
65 if (!m_strcasecmp("no", s))
67 if (!m_strcasecmp("ask-yes", s))
69 if (!m_strcasecmp("ask-no", s))
74 static int madmutt_assign(lua_State *L)
76 const char *idx = luaL_checkstring(L, 2);
77 const char *val = luaL_checkstring(L, 3);
80 switch ((tk = lua_which_token(idx, -1))) {
85 return luaL_error(L, "read-only or inexistant property '%s'", idx, tk);
91 _mutt_expand_path(buf, sizeof(buf), val, 0);
95 mlua_regsets(tk, val);
99 i = quadopt_parse(val);
101 return luaL_error(L, "invalid quad option value: '%s'", val);
107 static int madmutt_get(lua_State *L)
109 const char *idx = luaL_checkstring(L, 2);
112 switch ((tk = lua_which_token(idx, -1))) {
114 lua_getmetatable(L, 1);
122 lua_pushstring(L, mlua_reggets(tk));
129 static const struct luaL_Reg madmutt_module_funcs[] = {
130 { "pwd", madmutt_pwd },
133 ** \fIThis is a read-only system property and, at runtime,
134 ** specifies the current working directory of the madmutt
137 { "folder_path", madmutt_folder_path },
140 ** \fIThis is a read-only system property and, at runtime,
141 ** specifies the full path or URI of the folder currently
144 { "folder_name", madmutt_folder_name },
147 ** \fIThis is a read-only system property and, at runtime,
148 ** specifies the actual name of the folder as far as it could
151 ** For detection, $$$folder is first taken into account
152 ** and simply stripped to form the result when a match is found. For
153 ** example, with $$$folder being \fTimap://host\fP and the folder is
154 ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just
157 ** Second, if the initial portion of a name is not $$$folder,
158 ** the result will be everything after the last ``/''.
160 ** Third and last, the result will be just the name if neither
161 ** $$$folder nor a ``/'' were found in the name.
164 { "__newindex", madmutt_assign },
165 { "__index", madmutt_get },
171 /* {{{ read-only properties */
173 static const struct {
176 } madmutt_module_vars[] = {
177 { "version", VERSION },
180 ** \fIThis is a read-only system property and specifies madmutt's
181 ** version string.\fP
183 { "sysconfdir", SYSCONFDIR },
186 ** \fIThis is a read-only system property and specifies madmutt's
187 ** subversion revision string.\fP
189 { "bindir", BINDIR },
192 ** \fIThis is a read-only system property and specifies the
193 ** directory containing the madmutt binary.\fP
195 { "docdir", PKGDOCDIR },
198 ** \fIThis is a read-only system property and specifies the
199 ** directory containing the madmutt documentation.\fP
202 #if defined(HAVE_QDBM)
203 { "hcache_backend", "qdbm" },
204 #elif defined(HAVE_GDBM)
205 { "hcache_backend", "gdbm" },
206 #elif defined(HAVE_DB4)
207 { "hcache_backend", "db4" },
209 { "hcache_backend", "unknown" },
213 ** \fIThis is a read-only system property and specifies the
214 ** header chaching's database backend.\fP
221 /* {{{ madmutt magic properties */
223 static void madmutt_init_editor(char *buf, ssize_t len)
225 m_strcpy(buf, len, getenv("VISUAL") ?: getenv("EDITOR") ?: "vi");
228 static void madmutt_init_shell(char *buf, ssize_t len)
230 struct passwd *pw = getpwuid(getuid());
233 m_strcpy(buf, len, pw->pw_shell);
234 _mutt_expand_path(buf, len, pw->pw_shell, 0);
236 m_strcpy(buf, len, getenv("SHELL") ?: "/bin/sh");
240 static const struct {
242 void (*fun)(char *buf, ssize_t len);
244 } madmutt_module_vars2[] = {
245 { "dotlock", NULL, BINDIR "/mutt_dotlock" },
248 ** Contains the path of the \fTmadmutt_dotlock(1)\fP binary to be used by
251 {"editor", madmutt_init_editor, NULL },
254 ** This variable specifies which editor is used by Madmutt.
255 ** It defaults to the value of the \fT$$$VISUAL\fP, or \fT$$$EDITOR\fP, environment
256 ** variable, or to the string "\fTvi\fP" if neither of those are set.
258 { "sendmail", NULL, SENDMAIL " -oem -oi" },
261 ** Specifies the program and arguments used to deliver mail sent by Madmutt.
262 ** Madmutt expects that the specified program interprets additional
263 ** arguments as recipient addresses.
265 { "shell", madmutt_init_shell, NULL },
268 ** Command to use when spawning a subshell. By default, the user's login
269 ** shell from \fT/etc/passwd\fP is used.
271 {"quit", NULL, "yes" }
274 ** This variable controls whether ``quit'' and ``exit'' actually quit
275 ** from Madmutt. If it set to \fIyes\fP, they do quit, if it is set to \fIno\fP, they
276 ** have no effect, and if it is set to \fIask-yes\fP or \fIask-no\fP, you are
277 ** prompted for confirmation when you try to quit.
283 int luaopen_madmutt(lua_State *L)
287 lua_newuserdata(L, sizeof(void*));
288 luaL_newmetatable(L, "madmutt.core");
290 luaL_openlib(L, NULL, madmutt_module_funcs, 0);
292 for (i = 0; i < countof(madmutt_module_vars); i++) {
293 lua_pushstring(L, madmutt_module_vars[i].value);
294 lua_setfield(L, -2, madmutt_module_vars[i].key);
297 lua_setmetatable(L, -2);
299 for (i = 0; i < countof(madmutt_module_vars2); i++) {
300 if (madmutt_module_vars2[i].fun) {
302 (madmutt_module_vars2[i].fun)(buf, sizeof(buf));
303 lua_pushstring(L, buf);
305 lua_pushstring(L, madmutt_module_vars2[i].val);
307 lua_setfield(L, -2, madmutt_module_vars2[i].key);
310 lua_setglobal(L, "madmutt");