wrap irc_net and add lua_net_join
authorTero Marttila <terom@fixme.fi>
Tue, 31 Mar 2009 19:56:18 +0300
changeset 94 05a96b200d7b
parent 93 42ade8285570
child 95 6bb8ef294689
wrap irc_net and add lua_net_join
src/lua_objs.c
--- a/src/lua_objs.c	Tue Mar 31 19:35:51 2009 +0300
+++ b/src/lua_objs.c	Tue Mar 31 19:56:18 2009 +0300
@@ -7,11 +7,77 @@
 /**
  * Our lua wrapper for irc_net
  */
-struct lua_irc_net {
+struct lua_net {
     struct irc_net *net;
 };
 
 /**
+ * Create a lua_net userdata from the given irc_net and push it onto the stack, returning 1.
+ */
+static int lua_net_create (lua_State *L, struct irc_net *net)
+{
+    // create the new userdata on the stack
+    struct lua_net *lua_net = lua_newuserdata(L, sizeof(*lua_net));
+
+    // get the type and set it
+    luaL_getmetatable(L, "evirc.net");
+    lua_setmetatable(L, -2);
+
+    // initialize
+    lua_net->net = net;
+
+    // ok
+    return 1;
+}
+
+/**
+ * Join a new channel, returning the lua_chan
+ */
+static int lua_net_join (lua_State *L)
+{
+    struct lua_net *lua_net;
+    struct irc_chan_info chan_info;
+    struct irc_chan *chan;
+    struct error_info err;
+    
+    // validate the lua_net arg
+    if ((lua_net = luaL_checkudata(L, 1, "evirc.net")) == NULL)
+        return luaL_argerror(L, 1, "`evirc.net` expected");
+
+    // the channel name
+    chan_info.channel = luaL_checkstring(L, 2);
+    
+    // add it
+    if (irc_net_add_chan(lua_net->net, &chan, &chan_info, &err))
+        return luaL_error(L, "irc_net_add_chan: %s: %s", chan_info.channel, error_msg(&err));
+    
+    // XXX: wrap it
+    return 0;
+}
+
+static const struct luaL_Reg lua_net_methods[] = {
+    {   "join",         &lua_net_join       },
+    {   NULL,           NULL                }
+};
+
+/**
+ * Initialize the lua_net object type
+ */
+static err_t lua_net_init (lua_State *L)
+{
+    luaL_newmetatable(L, "evirc.net");
+
+    // set the metatable __index to itself
+    lua_pushvalue(L, -1);
+    lua_setfield(L, -1, "__index");
+
+    // register the methods to the metatable
+    luaL_register(L, NULL, lua_net_methods);
+
+    return SUCCESS;
+}
+
+/**
  * Wrapper for irc_client
  */
 struct lua_client {
@@ -25,7 +91,7 @@
    
     // validate the lua_client arg
     if ((lua_client = luaL_checkudata(L, 1, "evirc.client")) == NULL)
-        return luaL_argerror(L, 1, "`client` expected");
+        return luaL_argerror(L, 1, "`evirc.client` expected");
     
     // the message
     const char *message = luaL_checkstring(L, 2);
@@ -38,9 +104,30 @@
     return 0;
 }
 
-static const struct luaL_Reg lua_client_lib[] = {
-    {   "quit",     &lua_client_quit    },
-    {   NULL,       NULL                    }
+static int lua_client_get_network (lua_State *L)
+{
+    struct lua_client *lua_client;
+    struct irc_net *net;
+    
+    // validate the lua_client arg
+    if ((lua_client = luaL_checkudata(L, 1, "evirc.client")) == NULL)
+        return luaL_argerror(L, 1, "`evirc.client` expected");
+
+    // the network name
+    const char *network = luaL_checkstring(L, 2);
+    
+    // lookup the irc_net
+    if ((net = irc_client_get_net(lua_client->client, network)) == NULL)
+        return luaL_error(L, "irc_client_get_net: no such network: %s", network);
+
+    // wrap it
+    return lua_net_create(L, net);
+}
+
+static const struct luaL_Reg lua_client_methods[] = {
+    {   "quit",         &lua_client_quit        },
+    {   "network",      &lua_client_get_network },
+    {   NULL,           NULL                    }
 };
 
 /**
@@ -51,7 +138,7 @@
 {
     // allocate the global "client" object
     // XXX: mem errors?
-    struct lua_client *lua_client = lua_newuserdata(L, sizeof(*client));
+    struct lua_client *lua_client = lua_newuserdata(L, sizeof(*lua_client));
     
     // push a new metatable to identify the client object
     luaL_newmetatable(L, "evirc.client");
@@ -61,7 +148,7 @@
     lua_setfield(L, -1, "__index");
 
     // register the methods to the metatable
-    luaL_register(L, NULL, lua_client_lib);
+    luaL_register(L, NULL, lua_client_methods);
 
     // set the client userdata's metatable
     lua_setmetatable(L, -2);
@@ -79,5 +166,8 @@
 err_t lua_objs_init (lua_State *L, struct nexus *nexus)
 {
     // init the various bits
-    return lua_client_init(L, nexus->client);
+    lua_client_init(L, nexus->client);
+    lua_net_init(L);
+
+    return SUCCESS;
 }