src/lua_objs.c
changeset 107 5c1eeb45c7f2
parent 106 f00661136ac2
child 108 50ff7ac8a725
--- a/src/lua_objs.c	Wed Apr 01 01:41:08 2009 +0300
+++ b/src/lua_objs.c	Wed Apr 01 17:29:29 2009 +0300
@@ -370,6 +370,31 @@
     return 1;
 }
 
+/**
+ * Parse and apply the given config option value as a raw value to the given module's option
+ */
+static int _lua_module_conf_raw_str (lua_State *L, struct lua_module *lua_module, struct nexus *nexus, 
+        const struct config_option *option, const char *conf_value)
+{
+    struct config_value value;
+    struct error_info err;
+
+    // mutable version of the conf_value
+    char conf_value_buf[strlen(conf_value) + 1];
+    strcpy(conf_value_buf, conf_value);
+
+    // parse it as a raw value
+    if (config_parse(option, nexus, &value, conf_value_buf, &err))
+        return luaL_error(L, "config_parse: %s/%s: %s", option->name, conf_value_buf, error_msg(&err));
+    
+    // apply it while conf_value_buf is still in scope
+    if (module_conf(lua_module->module, option, &value, &err))
+        return luaL_error(L, "module_conf: %s/%s: %s", module_name(lua_module->module), option->name, error_msg(&err));
+    
+    // ok
+    return 0;
+}
+
 static int lua_module_conf (lua_State *L)
 {
     struct lua_module *lua_module = lua_obj_get_obj(L, __func__, "spbot.module");
@@ -383,26 +408,49 @@
     // the config name
     const char *conf_name = luaL_checkstring(L, 2);
 
-    // the config value
-    // XXX: support lua_chan
-    const char *conf_value = luaL_checkstring(L, 3);
-    
-    // mutable version of the conf_value
-    char conf_value_buf[strlen(conf_value) + 1];
-    strcpy(conf_value_buf, conf_value);
-
     // look it up
     if ((option = module_conf_lookup(lua_module->module, conf_name, &err)) == NULL)
         return luaL_error(L, "module_conf_lookup: %s/%s: %s", module_name(lua_module->module), conf_name, error_msg(&err));
+ 
+    // the config value
+    switch (lua_type(L, 3)) {
+        case LUA_TSTRING:
+            // parse+apply it as a raw string
+            return _lua_module_conf_raw_str(L, lua_module, nexus, option, lua_tostring(L, 3));
+
+        case LUA_TUSERDATA:
+            // some kind of userdata, use its metatable to figure out what type it is
+            if (!lua_getmetatable(L, 3))
+                return luaL_error(L, "config value is userdata without metatable");
+
+            // get the target metatable
+            lua_getfield(L, LUA_REGISTRYINDEX, "evirc.chan");
+
+            // is it a chan?
+            if (!lua_rawequal(L, -1, -2))
+                return luaL_error(L, "config value is userdata of unknown type");
+
+            // pop the metatables
+            lua_pop(L, 2);
+
+            // get the irc_chan
+            struct lua_chan *lua_chan = lua_touserdata(L, 3);
+
+            // build the value
+            value.type = CONFIG_IRC_CHAN;
+            value.irc_chan = lua_chan->chan;
+            
+            break;
+        
+        default:
+            return luaL_error(L, "config value is of unknown lua type '%s'", lua_typename(L, 3));
+
+    }
     
-    // parse it
-    if (config_parse(option, nexus, &value, conf_value_buf, &err))
-        return luaL_error(L, "config_parse: %s/%s: %s", option->name, conf_value_buf, error_msg(&err));
-
     // apply it
     if (module_conf(lua_module->module, option, &value, &err))
-        return luaL_error(L, "module_conf: %s/%s/%s: %s", module_name(lua_module->module), option->name, conf_value_buf, error_msg(&err));
-    
+        return luaL_error(L, "module_conf: %s/%s: %s", module_name(lua_module->module), option->name, error_msg(&err));
+   
     // ok
     return 0;
 }