src/lua_irc.c
changeset 198 b74185e1357a
parent 180 22967b165692
child 199 8eb839fbabba
equal deleted inserted replaced
197:3f9175ead92a 198:b74185e1357a
   169 
   169 
   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",
       
   175         "return a list of channel objects",
       
   176         
       
   177         LUA_FUNC_ARG_END
       
   178     );
       
   179 
       
   180 static int lua_net_channels (lua_State *L)
       
   181 {
       
   182     struct lua_net *lua_net;
       
   183     struct irc_chan *chan;
       
   184     int i = 1;
       
   185 
       
   186     // parse args
       
   187     lua_args_parse(L, &lua_net_channels_func, (void *) &lua_net);
       
   188 
       
   189     // create table to return
       
   190     lua_newtable(L);
       
   191 
       
   192     // iter
       
   193     TAILQ_FOREACH(chan, &lua_net->net->channels, net_channels) {
       
   194         // push index
       
   195         lua_pushinteger(L, i);
       
   196 
       
   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 }
       
   207 
   174 static struct lua_method lua_net_methods[] = LUA_METHODS(
   208 static struct lua_method lua_net_methods[] = LUA_METHODS(
   175         LUA_METHOD("__tostring",    lua_net__tostring,  &lua_net__tostring_func ),
   209         LUA_METHOD("__tostring",    lua_net__tostring,  &lua_net__tostring_func ),
   176         LUA_METHOD("join",          lua_net_join,       &lua_net_join_func      ),
   210         LUA_METHOD("join",          lua_net_join,       &lua_net_join_func      ),
   177         LUA_METHOD("channel",       lua_net_get_chan,   &lua_net_get_chan_func  )
   211         LUA_METHOD("channel",       lua_net_get_chan,   &lua_net_get_chan_func  ),
   178     );
   212         LUA_METHOD("channels",      lua_net_channels,   &lua_net_channels_func  )
       
   213     );
       
   214 
   179 
   215 
   180 
   216 
   181 
   217 
   182 static struct lua_type lua_client_type = LUA_TYPE("evirc.client");
   218 static struct lua_type lua_client_type = LUA_TYPE("evirc.client");
   183 
   219 
   296 
   332 
   297     // wrap it
   333     // wrap it
   298     return lua_net_create(L, net);
   334     return lua_net_create(L, net);
   299 }
   335 }
   300 
   336 
       
   337 static struct lua_func lua_client_networks_func = LUA_FUNC(&lua_client_type, "channels",
       
   338         "Build list of client's networks",
       
   339 
       
   340         LUA_FUNC_ARG_END
       
   341     );
       
   342 
       
   343 static int lua_client_networks (lua_State *L)
       
   344 {
       
   345     struct lua_client *lua_client;
       
   346     struct irc_net *net;
       
   347     int i = 1;
       
   348 
       
   349     // parse args
       
   350     lua_args_parse(L, &lua_client_networks_func, (void *) &lua_client);
       
   351 
       
   352     // create new table
       
   353     lua_newtable(L);
       
   354 
       
   355     // append each channel
       
   356     TAILQ_FOREACH(net, &lua_client->client->networks, client_networks) {
       
   357         // index
       
   358         lua_pushinteger(L, i);
       
   359 
       
   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 }
       
   370 
   301 static struct lua_func lua_client_quit_func = LUA_FUNC(&lua_client_type, "quit",
   371 static struct lua_func lua_client_quit_func = LUA_FUNC(&lua_client_type, "quit",
   302         "Disconnect from all networks",
   372         "Disconnect from all networks",
   303 
   373 
   304         LUA_FUNC_ARG_STRING("message",  "Bye")
   374         LUA_FUNC_ARG_STRING("message",  "Bye")
   305     );
   375     );
   325 
   395 
   326 static struct lua_method lua_client_methods[] = LUA_METHODS(
   396 static struct lua_method lua_client_methods[] = LUA_METHODS(
   327         LUA_METHOD("set_defaults",  lua_client_set_defaults,    &lua_client_set_defaults_func   ),
   397         LUA_METHOD("set_defaults",  lua_client_set_defaults,    &lua_client_set_defaults_func   ),
   328         LUA_METHOD("connect",       lua_client_connect,         &lua_client_connect_func        ),
   398         LUA_METHOD("connect",       lua_client_connect,         &lua_client_connect_func        ),
   329         LUA_METHOD("network",       lua_client_get_network,     &lua_client_get_network_func    ),
   399         LUA_METHOD("network",       lua_client_get_network,     &lua_client_get_network_func    ),
       
   400         LUA_METHOD("networks",      lua_client_networks,        &lua_client_networks_func       ),
   330         LUA_METHOD("quit",          lua_client_quit,            &lua_client_quit_func           )
   401         LUA_METHOD("quit",          lua_client_quit,            &lua_client_quit_func           )
   331     );
   402     );
   332 
   403 
   333 void lua_irc_init (struct nexus_lua *lua)
   404 void lua_irc_init (struct nexus_lua *lua)
   334 {
   405 {