src/lua_irc.c
changeset 199 8eb839fbabba
parent 198 b74185e1357a
equal deleted inserted replaced
198:b74185e1357a 199:8eb839fbabba
   170     // wrap it
   170     // wrap it
   171     return lua_chan_create(L, chan);
   171     return lua_chan_create(L, chan);
   172 }
   172 }
   173 
   173 
   174 static struct lua_func lua_net_channels_func = LUA_FUNC(&lua_net_type, "channels",
   174 static struct lua_func lua_net_channels_func = LUA_FUNC(&lua_net_type, "channels",
   175         "return a list of channel objects",
   175         "Return an iterater over the network's channels",
   176         
   176         
   177         LUA_FUNC_ARG_END
   177         LUA_FUNC_ARG_END
   178     );
   178     );
   179 
   179 
       
   180 static int lua_net_channels_iter (lua_State *L)
       
   181 {
       
   182     int nargs = lua_gettop(L);
       
   183     struct lua_net *lua_net = lua_arg_obj(L, nargs, 1, &lua_net_type, false);
       
   184     struct lua_chan *lua_chan = lua_arg_obj(L, nargs, 2, &lua_chan_type, true);
       
   185     struct irc_chan *chan_next;
       
   186 
       
   187     // get next item from current
       
   188     if (lua_chan)
       
   189         chan_next = TAILQ_NEXT(lua_chan->chan, net_channels);
       
   190     else
       
   191         chan_next = TAILQ_FIRST(&lua_net->net->channels);
       
   192 
       
   193     // push return value - next item
       
   194     if (chan_next)
       
   195         lua_chan_create(L, chan_next);
       
   196     else
       
   197         lua_pushnil(L);
       
   198 
       
   199     return 1;
       
   200 }
       
   201 
   180 static int lua_net_channels (lua_State *L)
   202 static int lua_net_channels (lua_State *L)
   181 {
   203 {
   182     struct lua_net *lua_net;
   204     struct lua_net *lua_net;
   183     struct irc_chan *chan;
       
   184     int i = 1;
       
   185 
   205 
   186     // parse args
   206     // parse args
   187     lua_args_parse(L, &lua_net_channels_func, (void *) &lua_net);
   207     lua_args_parse(L, &lua_net_channels_func, (void *) &lua_net);
   188 
   208 
   189     // create table to return
   209     // push iter func
   190     lua_newtable(L);
   210     lua_pushcfunction(L, lua_net_channels_iter);
   191 
   211 
   192     // iter
   212     // push invariant state - the lua_net
   193     TAILQ_FOREACH(chan, &lua_net->net->channels, net_channels) {
   213     lua_pushvalue(L, 1);
   194         // push index
   214 
   195         lua_pushinteger(L, i);
   215     // return iter three-tuple
   196 
   216     return 2;
   197         // push value as new lua_chan
       
   198         lua_chan_create(L, chan);
       
   199 
       
   200         // store in table
       
   201         lua_settable(L, -3);
       
   202     }
       
   203 
       
   204     // return table
       
   205     return 1;
       
   206 }
   217 }
   207 
   218 
   208 static struct lua_method lua_net_methods[] = LUA_METHODS(
   219 static struct lua_method lua_net_methods[] = LUA_METHODS(
   209         LUA_METHOD("__tostring",    lua_net__tostring,  &lua_net__tostring_func ),
   220         LUA_METHOD("__tostring",    lua_net__tostring,  &lua_net__tostring_func ),
   210         LUA_METHOD("join",          lua_net_join,       &lua_net_join_func      ),
   221         LUA_METHOD("join",          lua_net_join,       &lua_net_join_func      ),
   333     // wrap it
   344     // wrap it
   334     return lua_net_create(L, net);
   345     return lua_net_create(L, net);
   335 }
   346 }
   336 
   347 
   337 static struct lua_func lua_client_networks_func = LUA_FUNC(&lua_client_type, "channels",
   348 static struct lua_func lua_client_networks_func = LUA_FUNC(&lua_client_type, "channels",
   338         "Build list of client's networks",
   349         "Return an iterator over the client's networks",
   339 
   350 
   340         LUA_FUNC_ARG_END
   351         LUA_FUNC_ARG_END
   341     );
   352     );
   342 
   353 
       
   354 static int lua_client_networks_iter (lua_State *L)
       
   355 {
       
   356     int nargs = lua_gettop(L);
       
   357     struct lua_client *lua_client = lua_arg_obj(L, nargs, 1, &lua_client_type, false);
       
   358     struct lua_net *lua_net = lua_arg_obj(L, nargs, 2, &lua_net_type, true);
       
   359     struct irc_net *net_next;
       
   360 
       
   361     if (lua_net)
       
   362         // return next
       
   363         net_next = TAILQ_NEXT(lua_net->net, client_networks);
       
   364     else
       
   365         // return first
       
   366         net_next = TAILQ_FIRST(&lua_client->client->networks);
       
   367     
       
   368     // build and return next value
       
   369     if (net_next)
       
   370         lua_net_create(L, net_next);
       
   371     else
       
   372         lua_pushnil(L);
       
   373     
       
   374     return 1;
       
   375 }
       
   376 
   343 static int lua_client_networks (lua_State *L)
   377 static int lua_client_networks (lua_State *L)
   344 {
   378 {
   345     struct lua_client *lua_client;
   379     struct lua_client *lua_client;
   346     struct irc_net *net;
       
   347     int i = 1;
       
   348 
   380 
   349     // parse args
   381     // parse args
   350     lua_args_parse(L, &lua_client_networks_func, (void *) &lua_client);
   382     lua_args_parse(L, &lua_client_networks_func, (void *) &lua_client);
   351 
   383 
   352     // create new table
   384     // push iter func
   353     lua_newtable(L);
   385     lua_pushcfunction(L, lua_client_networks_iter);
   354 
   386 
   355     // append each channel
   387     // push invariant state - the lua_client
   356     TAILQ_FOREACH(net, &lua_client->client->networks, client_networks) {
   388     lua_pushvalue(L, 1);
   357         // index
   389 
   358         lua_pushinteger(L, i);
   390     // return three-tuple
   359 
   391     return 2;
   360         // push new lua_net
       
   361         lua_net_create(L, net);
       
   362 
       
   363         // store
       
   364         lua_settable(L, -3);
       
   365     }
       
   366 
       
   367     // ok, return the table
       
   368     return 1;
       
   369 }
   392 }
   370 
   393 
   371 static struct lua_func lua_client_quit_func = LUA_FUNC(&lua_client_type, "quit",
   394 static struct lua_func lua_client_quit_func = LUA_FUNC(&lua_client_type, "quit",
   372         "Disconnect from all networks",
   395         "Disconnect from all networks",
   373 
   396