implement lua_client_networks and lua_network_channels
authorTero Marttila <terom@fixme.fi>
Thu, 14 May 2009 21:45:43 +0300
changeset 198 b74185e1357a
parent 197 3f9175ead92a
child 199 8eb839fbabba
implement lua_client_networks and lua_network_channels
src/lua_irc.c
--- a/src/lua_irc.c	Thu May 14 21:45:28 2009 +0300
+++ b/src/lua_irc.c	Thu May 14 21:45:43 2009 +0300
@@ -171,14 +171,50 @@
     return lua_chan_create(L, chan);
 }
 
+static struct lua_func lua_net_channels_func = LUA_FUNC(&lua_net_type, "channels",
+        "return a list of channel objects",
+        
+        LUA_FUNC_ARG_END
+    );
+
+static int lua_net_channels (lua_State *L)
+{
+    struct lua_net *lua_net;
+    struct irc_chan *chan;
+    int i = 1;
+
+    // parse args
+    lua_args_parse(L, &lua_net_channels_func, (void *) &lua_net);
+
+    // create table to return
+    lua_newtable(L);
+
+    // iter
+    TAILQ_FOREACH(chan, &lua_net->net->channels, net_channels) {
+        // push index
+        lua_pushinteger(L, i);
+
+        // push value as new lua_chan
+        lua_chan_create(L, chan);
+
+        // store in table
+        lua_settable(L, -3);
+    }
+
+    // return table
+    return 1;
+}
+
 static struct lua_method lua_net_methods[] = LUA_METHODS(
         LUA_METHOD("__tostring",    lua_net__tostring,  &lua_net__tostring_func ),
         LUA_METHOD("join",          lua_net_join,       &lua_net_join_func      ),
-        LUA_METHOD("channel",       lua_net_get_chan,   &lua_net_get_chan_func  )
+        LUA_METHOD("channel",       lua_net_get_chan,   &lua_net_get_chan_func  ),
+        LUA_METHOD("channels",      lua_net_channels,   &lua_net_channels_func  )
     );
 
 
 
+
 static struct lua_type lua_client_type = LUA_TYPE("evirc.client");
 
 
@@ -298,6 +334,40 @@
     return lua_net_create(L, net);
 }
 
+static struct lua_func lua_client_networks_func = LUA_FUNC(&lua_client_type, "channels",
+        "Build list of client's networks",
+
+        LUA_FUNC_ARG_END
+    );
+
+static int lua_client_networks (lua_State *L)
+{
+    struct lua_client *lua_client;
+    struct irc_net *net;
+    int i = 1;
+
+    // parse args
+    lua_args_parse(L, &lua_client_networks_func, (void *) &lua_client);
+
+    // create new table
+    lua_newtable(L);
+
+    // append each channel
+    TAILQ_FOREACH(net, &lua_client->client->networks, client_networks) {
+        // index
+        lua_pushinteger(L, i);
+
+        // push new lua_net
+        lua_net_create(L, net);
+
+        // store
+        lua_settable(L, -3);
+    }
+
+    // ok, return the table
+    return 1;
+}
+
 static struct lua_func lua_client_quit_func = LUA_FUNC(&lua_client_type, "quit",
         "Disconnect from all networks",
 
@@ -327,6 +397,7 @@
         LUA_METHOD("set_defaults",  lua_client_set_defaults,    &lua_client_set_defaults_func   ),
         LUA_METHOD("connect",       lua_client_connect,         &lua_client_connect_func        ),
         LUA_METHOD("network",       lua_client_get_network,     &lua_client_get_network_func    ),
+        LUA_METHOD("networks",      lua_client_networks,        &lua_client_networks_func       ),
         LUA_METHOD("quit",          lua_client_quit,            &lua_client_quit_func           )
     );