[lua] More madmutt package upgrades:
[apps/madmutt.git] / lib-lua / madmutt.c
index 351ecb5..31ee28a 100644 (file)
@@ -52,6 +52,29 @@ static int madmutt_folder_name(lua_State *L)
     return 1;
 }
 
     return 1;
 }
 
+static int madmutt_assign(lua_State *L)
+{
+    const char *idx = luaL_checkstring(L, 2);
+    const char *val = luaL_checkstring(L, 3);
+
+    switch (lua_which_token(idx, -1)) {
+      default:
+        luaL_error(L, "bad subscript to madmutt: %s", val);
+        return 0;
+
+      case LTK_SENDMAIL:
+      case LTK_SHELL:
+        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},
     /*
 static const struct luaL_Reg madmutt_module_funcs[] = {
     {"pwd",         madmutt_pwd},
     /*
@@ -86,6 +109,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.
      */
      ** 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}
 };
 
     {NULL, NULL}
 };
 
@@ -138,11 +163,23 @@ static const struct {
 int luaopen_madmutt(lua_State *L)
 {
     int i;
 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_vars); i++) {
         lua_pushstring(L, madmutt_module_vars[i].value);
         lua_setfield(L, -2, madmutt_module_vars[i].key);
     }
+
+    lua_pushstring(L, "__index");
+    lua_pushvalue(L, -2);
+    lua_settable(L, -3);
+
+    lua_setmetatable(L, -2);
+    lua_setglobal(L, "madmutt");
+
     return 1;
 }
     return 1;
 }