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);
78 mlua_regsets(tk, buf);
83 static int madmutt_get(lua_State *L)
85 const char *idx = luaL_checkstring(L, 2);
88 switch ((tk = lua_which_token(idx, -1))) {
90 lua_getmetatable(L, 1);
98 lua_pushstring(L, mlua_reggets(tk));
105 static const struct luaL_Reg madmutt_module_funcs[] = {
106 { "pwd", madmutt_pwd },
109 ** \fIThis is a read-only system property and, at runtime,
110 ** specifies the current working directory of the madmutt
113 { "folder_path", madmutt_folder_path },
116 ** \fIThis is a read-only system property and, at runtime,
117 ** specifies the full path or URI of the folder currently
120 { "folder_name", madmutt_folder_name },
123 ** \fIThis is a read-only system property and, at runtime,
124 ** specifies the actual name of the folder as far as it could
127 ** For detection, $$$folder is first taken into account
128 ** and simply stripped to form the result when a match is found. For
129 ** example, with $$$folder being \fTimap://host\fP and the folder is
130 ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just
133 ** Second, if the initial portion of a name is not $$$folder,
134 ** the result will be everything after the last ``/''.
136 ** Third and last, the result will be just the name if neither
137 ** $$$folder nor a ``/'' were found in the name.
140 { "__newindex", madmutt_assign },
141 { "__index", madmutt_get },
147 /* {{{ read-only properties */
149 static const struct {
152 } madmutt_module_vars[] = {
153 { "version", VERSION },
156 ** \fIThis is a read-only system property and specifies madmutt's
157 ** version string.\fP
159 { "sysconfdir", SYSCONFDIR },
162 ** \fIThis is a read-only system property and specifies madmutt's
163 ** subversion revision string.\fP
165 { "bindir", BINDIR },
168 ** \fIThis is a read-only system property and specifies the
169 ** directory containing the madmutt binary.\fP
171 { "docdir", PKGDOCDIR },
174 ** \fIThis is a read-only system property and specifies the
175 ** directory containing the madmutt documentation.\fP
178 #if defined(HAVE_QDBM)
179 { "hcache_backend", "qdbm" },
180 #elif defined(HAVE_GDBM)
181 { "hcache_backend", "gdbm" },
182 #elif defined(HAVE_DB4)
183 { "hcache_backend", "db4" },
185 { "hcache_backend", "unknown" },
189 ** \fIThis is a read-only system property and specifies the
190 ** header chaching's database backend.\fP
197 /* {{{ madmutt magic properties */
199 static void madmutt_init_shell(char *buf, ssize_t len)
201 struct passwd *pw = getpwuid(getuid());
204 m_strcpy(buf, len, pw->pw_shell);
205 _mutt_expand_path(buf, len, pw->pw_shell, 0);
207 m_strcpy(buf, len, getenv("SHELL") ?: "/bin/sh");
211 static const struct {
213 void (*fun)(char *buf, ssize_t len);
215 } madmutt_module_vars2[] = {
216 { "dotlock", NULL, BINDIR "/mutt_dotlock" },
219 ** Contains the path of the \fTmadmutt_dotlock(1)\fP binary to be used by
222 { "sendmail", NULL, SENDMAIL " -oem -oi" },
225 ** Specifies the program and arguments used to deliver mail sent by Madmutt.
226 ** Madmutt expects that the specified program interprets additional
227 ** arguments as recipient addresses.
229 { "shell", madmutt_init_shell, NULL },
232 ** Command to use when spawning a subshell. By default, the user's login
233 ** shell from \fT/etc/passwd\fP is used.
239 int luaopen_madmutt(lua_State *L)
243 lua_newuserdata(L, sizeof(void*));
244 luaL_newmetatable(L, "madmutt.core");
246 luaL_openlib(L, NULL, madmutt_module_funcs, 0);
248 for (i = 0; i < countof(madmutt_module_vars); i++) {
249 lua_pushstring(L, madmutt_module_vars[i].value);
250 lua_setfield(L, -2, madmutt_module_vars[i].key);
253 lua_setmetatable(L, -2);
255 for (i = 0; i < countof(madmutt_module_vars2); i++) {
256 if (madmutt_module_vars2[i].fun) {
258 (madmutt_module_vars2[i].fun)(buf, sizeof(buf));
259 lua_pushstring(L, buf);
261 lua_pushstring(L, madmutt_module_vars2[i].val);
263 lua_setfield(L, -2, madmutt_module_vars2[i].key);
266 lua_setglobal(L, "madmutt");