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 int madmutt_assign(lua_State *L)
63 const char *idx = luaL_checkstring(L, 2);
64 const char *val = luaL_checkstring(L, 3);
67 switch ((tk = lua_which_token(idx, -1))) {
71 luaL_error(L, "read-only or inexistant property '%s'", idx, tk);
77 _mutt_expand_path(buf, sizeof(buf), val, 0);
82 mlua_regsets(tk, val);
87 static int madmutt_get(lua_State *L)
89 const char *idx = luaL_checkstring(L, 2);
92 switch ((tk = lua_which_token(idx, -1))) {
94 lua_getmetatable(L, 1);
102 lua_pushstring(L, mlua_reggets(tk));
109 static const struct luaL_Reg madmutt_module_funcs[] = {
110 { "pwd", madmutt_pwd },
113 ** \fIThis is a read-only system property and, at runtime,
114 ** specifies the current working directory of the madmutt
117 { "folder_path", madmutt_folder_path },
120 ** \fIThis is a read-only system property and, at runtime,
121 ** specifies the full path or URI of the folder currently
124 { "folder_name", madmutt_folder_name },
127 ** \fIThis is a read-only system property and, at runtime,
128 ** specifies the actual name of the folder as far as it could
131 ** For detection, $$$folder is first taken into account
132 ** and simply stripped to form the result when a match is found. For
133 ** example, with $$$folder being \fTimap://host\fP and the folder is
134 ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just
137 ** Second, if the initial portion of a name is not $$$folder,
138 ** the result will be everything after the last ``/''.
140 ** Third and last, the result will be just the name if neither
141 ** $$$folder nor a ``/'' were found in the name.
144 { "__newindex", madmutt_assign },
145 { "__index", madmutt_get },
151 /* {{{ read-only properties */
153 static const struct {
156 } madmutt_module_vars[] = {
157 { "version", VERSION },
160 ** \fIThis is a read-only system property and specifies madmutt's
161 ** version string.\fP
163 { "sysconfdir", SYSCONFDIR },
166 ** \fIThis is a read-only system property and specifies madmutt's
167 ** subversion revision string.\fP
169 { "bindir", BINDIR },
172 ** \fIThis is a read-only system property and specifies the
173 ** directory containing the madmutt binary.\fP
175 { "docdir", PKGDOCDIR },
178 ** \fIThis is a read-only system property and specifies the
179 ** directory containing the madmutt documentation.\fP
182 #if defined(HAVE_QDBM)
183 { "hcache_backend", "qdbm" },
184 #elif defined(HAVE_GDBM)
185 { "hcache_backend", "gdbm" },
186 #elif defined(HAVE_DB4)
187 { "hcache_backend", "db4" },
189 { "hcache_backend", "unknown" },
193 ** \fIThis is a read-only system property and specifies the
194 ** header chaching's database backend.\fP
201 /* {{{ madmutt magic properties */
203 static void madmutt_init_editor(char *buf, ssize_t len)
205 m_strcpy(buf, len, getenv("VISUAL") ?: getenv("EDITOR") ?: "vi");
206 fprintf("%s\n", buf);
209 static void madmutt_init_shell(char *buf, ssize_t len)
211 struct passwd *pw = getpwuid(getuid());
214 m_strcpy(buf, len, pw->pw_shell);
215 _mutt_expand_path(buf, len, pw->pw_shell, 0);
217 m_strcpy(buf, len, getenv("SHELL") ?: "/bin/sh");
221 static const struct {
223 void (*fun)(char *buf, ssize_t len);
225 } madmutt_module_vars2[] = {
226 { "dotlock", NULL, BINDIR "/mutt_dotlock" },
229 ** Contains the path of the \fTmadmutt_dotlock(1)\fP binary to be used by
232 {"editor", madmutt_init_editor, NULL },
235 ** This variable specifies which editor is used by Madmutt.
236 ** It defaults to the value of the \fT$$$VISUAL\fP, or \fT$$$EDITOR\fP, environment
237 ** variable, or to the string "\fTvi\fP" if neither of those are set.
239 { "sendmail", NULL, SENDMAIL " -oem -oi" },
242 ** Specifies the program and arguments used to deliver mail sent by Madmutt.
243 ** Madmutt expects that the specified program interprets additional
244 ** arguments as recipient addresses.
246 { "shell", madmutt_init_shell, NULL },
249 ** Command to use when spawning a subshell. By default, the user's login
250 ** shell from \fT/etc/passwd\fP is used.
256 int luaopen_madmutt(lua_State *L)
260 lua_newuserdata(L, sizeof(void*));
261 luaL_newmetatable(L, "madmutt.core");
263 luaL_openlib(L, NULL, madmutt_module_funcs, 0);
265 for (i = 0; i < countof(madmutt_module_vars); i++) {
266 lua_pushstring(L, madmutt_module_vars[i].value);
267 lua_setfield(L, -2, madmutt_module_vars[i].key);
270 lua_setmetatable(L, -2);
272 for (i = 0; i < countof(madmutt_module_vars2); i++) {
273 if (madmutt_module_vars2[i].fun) {
275 (madmutt_module_vars2[i].fun)(buf, sizeof(buf));
276 lua_pushstring(L, buf);
278 lua_pushstring(L, madmutt_module_vars2[i].val);
280 lua_setfield(L, -2, madmutt_module_vars2[i].key);
283 lua_setglobal(L, "madmutt");