src/lua_objs.c
changeset 98 f357f835f0d5
parent 97 d3bc82ee76cb
child 99 155a6c7d3886
--- a/src/lua_objs.c	Tue Mar 31 20:57:07 2009 +0300
+++ b/src/lua_objs.c	Tue Mar 31 22:09:53 2009 +0300
@@ -1,5 +1,7 @@
 #include "lua_objs.h"
 
+#include <string.h>
+
 #include <lua5.1/lua.h>
 #include <lua5.1/lualib.h>
 #include <lua5.1/lauxlib.h>
@@ -218,20 +220,31 @@
     struct irc_client *client;
 };
 
-static int lua_client_quit (lua_State *L)
+static int lua_client_connect (lua_State *L)
 {
     struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
-    err_t err;
-   
-    // the message
-    const char *message = luaL_checkstring(L, 2);
+    struct irc_net_info net_info;
+    struct irc_net *net;
+    struct error_info err;
 
-    // execute
-    if ((err = irc_client_quit(lua_client->client, message)))
-        return luaL_error(L, "irc_client_quit: %s", error_name(err));
+    // init net_info
+    memset(&net_info, 0, sizeof(net_info));
 
-    // ok
-    return 0;
+    // the network name
+    net_info.network = luaL_checkstring(L, 2);
+    
+    // the hostname
+    net_info.hostname = luaL_checkstring(L, 3);
+
+    // service remains default
+    net_info.service = "6667";
+
+    // create it
+    if (irc_client_add_net(lua_client->client, &net, &net_info, &err))
+        return luaL_error(L, "irc_client_add_net: %s/%s: %s", net_info.network, net_info.hostname, error_msg(&err));
+
+    // wrap it
+    return lua_net_create(L, net);
 }
 
 static int lua_client_get_network (lua_State *L)
@@ -250,9 +263,26 @@
     return lua_net_create(L, net);
 }
 
+static int lua_client_quit (lua_State *L)
+{
+    struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
+    err_t err;
+   
+    // the message
+    const char *message = luaL_checkstring(L, 2);
+
+    // execute
+    if ((err = irc_client_quit(lua_client->client, message)))
+        return luaL_error(L, "irc_client_quit: %s", error_name(err));
+
+    // ok
+    return 0;
+}
+
 static const struct luaL_Reg lua_client_methods[] = {
+    {   "connect",      &lua_client_connect     },
+    {   "network",      &lua_client_get_network },
     {   "quit",         &lua_client_quit        },
-    {   "network",      &lua_client_get_network },
     {   NULL,           NULL                    }
 };
 
@@ -263,7 +293,6 @@
 static void lua_client_init (lua_State *L, struct irc_client *client)
 {
     // allocate the global "client" object
-    // XXX: mem errors?
     struct lua_client *lua_client = lua_newuserdata(L, sizeof(*lua_client));
     
     // create the type metatable
@@ -279,12 +308,35 @@
     lua_setglobal(L, "client");
 }
 
-err_t lua_objs_init (lua_State *L, struct nexus *nexus)
+/**
+ * Set up the lua state in protected mode
+ */
+static int _lua_objs_init (lua_State *L)
 {
+    struct nexus *nexus;
+    
+    // read the nexus off the stack
+    if ((nexus = lua_touserdata(L, 1)) == NULL)
+        luaL_error(L, "lua_touserdata: NULL");
+
     // init the various bits
     lua_client_init(L, nexus->client);
     lua_net_init(L);
     lua_chan_init(L);
 
-    return SUCCESS;
+    // nothing
+    return 0;
 }
+
+err_t lua_objs_init (lua_State *L, struct nexus *nexus)
+{
+    // call in protected mode
+    switch (lua_cpcall(L, &_lua_objs_init, nexus)) {
+        case 0:             return SUCCESS;
+        case LUA_ERRRUN:    return ERR_LUA_RUN;
+        case LUA_ERRMEM:    return ERR_LUA_MEM;
+        case LUA_ERRERR:    return ERR_LUA_ERR;
+        default:            return ERR_UNKNOWN;
+    }
+}
+