X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=lib-lua%2Fmadmutt.c;h=cc6cb8f9396a93d4dc09a084bc54a3ef383ed72c;hb=418e55040ec4a95698a8f1f128f3afbfd6aba48b;hp=351ecb5256122cc5b199d449e72f72e8bce76baa;hpb=6729a8ab646a00a661858b7ac7ee21f770bff3d3;p=apps%2Fmadmutt.git diff --git a/lib-lua/madmutt.c b/lib-lua/madmutt.c index 351ecb5..cc6cb8f 100644 --- a/lib-lua/madmutt.c +++ b/lib-lua/madmutt.c @@ -18,6 +18,10 @@ */ #include + +#include +#include + #include "lib-lua_priv.h" #include "../mutt.h" @@ -52,6 +56,32 @@ static int madmutt_folder_name(lua_State *L) return 1; } +static int madmutt_assign(lua_State *L) +{ + const char *idx = luaL_checkstring(L, 2); + const char *val = luaL_checkstring(L, 3); + char buf[STRING]; + + switch (lua_which_token(idx, -1)) { + default: + luaL_error(L, "bad subscript to madmutt: %s", val); + return 0; + + case LTK_SENDMAIL: + case LTK_SHELL: + _mutt_expand_path(buf, sizeof(buf), val, 0); + val = buf; + break; + } + + lua_getmetatable(L, 1); + lua_pushstring(L, idx); + lua_pushstring(L, val); + lua_rawset(L, -3); + + return 0; +} + static const struct luaL_Reg madmutt_module_funcs[] = { {"pwd", madmutt_pwd}, /* @@ -86,6 +116,8 @@ static const struct luaL_Reg madmutt_module_funcs[] = { ** Third and last, the result will be just the name if neither ** $$$folder nor a ``/'' were found in the name. */ + + {"__newindex", madmutt_assign}, {NULL, NULL} }; @@ -119,13 +151,13 @@ static const struct { */ #ifdef USE_HCACHE #if defined(HAVE_QDBM) - {"hcache_backend", "qdbm" }, + { "hcache_backend", "qdbm" }, #elif defined(HAVE_GDBM) - {"hcache_backend", "gdbm" }, + { "hcache_backend", "gdbm" }, #elif defined(HAVE_DB4) - {"hcache_backend", "db4" }, + { "hcache_backend", "db4" }, #else - {"hcache_backend", "unknown" }, + { "hcache_backend", "unknown" }, #endif /* ** .pp @@ -135,14 +167,57 @@ static const struct { #endif }; +static void madmutt_init_shell(char *buf, ssize_t len) +{ + struct passwd *pw = getpwuid(getuid()); + + if (pw) { + m_strcpy(buf, len, pw->pw_shell); + _mutt_expand_path(buf, len, pw->pw_shell, 0); + } else { + m_strcpy(buf, len, getenv("SHELL") ?: "/bin/sh"); + } +} + +static const struct { + const char *key; + void (*fun)(char *buf, ssize_t len); +} madmutt_module_vars2[] = { + { "shell", madmutt_init_shell }, + /* + ** .pp + ** Command to use when spawning a subshell. By default, the user's login + ** shell from \fT/etc/passwd\fP is used. + */ +}; + int luaopen_madmutt(lua_State *L) { int i; - luaL_register(L, LUA_MADMUTT, madmutt_module_funcs); + + lua_newuserdata(L, sizeof(void*)); + luaL_newmetatable(L, "madmutt.core"); + + luaL_openlib(L, NULL, madmutt_module_funcs, 0); for (i = 0; i < countof(madmutt_module_vars); i++) { lua_pushstring(L, madmutt_module_vars[i].value); lua_setfield(L, -2, madmutt_module_vars[i].key); } + + for (i = 0; i < countof(madmutt_module_vars2); i++) { + char buf[STRING]; + (madmutt_module_vars2[i].fun)(buf, sizeof(buf)); + lua_pushstring(L, buf); + lua_setfield(L, -2, madmutt_module_vars2[i].key); + } + + lua_pushstring(L, "__index"); + lua_pushvalue(L, -2); + lua_settable(L, -3); + + lua_setmetatable(L, -2); + lua_setglobal(L, "madmutt"); + return 1; }