diff -r 51f96539f9f3 -r a5582e1a83da src/lua_irc.c --- a/src/lua_irc.c Sun Apr 19 23:53:37 2009 +0300 +++ b/src/lua_irc.c Sun Apr 19 23:56:01 2009 +0300 @@ -4,13 +4,15 @@ #include #include +static struct lua_type lua_chan_type = LUA_TYPE("evirc.chan"); + /** * Create a lua_chan userdata from the given irc_chan and leave it on the stack, returning 1 */ static int lua_chan_create (lua_State *L, struct irc_chan *chan) { // create the new obj - struct lua_chan *lua_chan = lua_obj_create_obj(L, "evirc.chan", sizeof(*lua_chan)); + struct lua_chan *lua_chan = lua_type_create(L, &lua_chan_type, sizeof(*lua_chan)); // initialize lua_chan->chan = chan; @@ -22,9 +24,17 @@ /** * Return the channel name as a lua string */ -static int lua_chan_tostring (lua_State *L) +static struct lua_func lua_chan__tostring_func = LUA_FUNC(&lua_chan_type, "__tostring", + "format using channel name", + + LUA_FUNC_ARG_END + ); + +static int lua_chan__tostring (lua_State *L) { - struct lua_chan *lua_chan = lua_obj_get_obj(L, __func__, "evirc.chan"); + struct lua_chan *lua_chan; + + lua_args_parse(L, &lua_chan__tostring_func, (void *) &lua_chan); lua_pushfstring(L, "", irc_chan_name(lua_chan->chan)); @@ -34,14 +44,22 @@ /** * Send a PRIVMSG to the channel */ +static struct lua_func lua_chan_say_func = LUA_FUNC(&lua_chan_type, "evirc.chan.say", + "send a message to a channel", + + LUA_FUNC_ARG_STRING("message", LUA_ARG_STRING_REQUIRED ) + ); + static int lua_chan_say (lua_State *L) { + struct lua_chan *lua_chan; err_t err; + const char *message; - struct lua_chan *lua_chan = lua_obj_get_obj(L, __func__, "evirc.chan"); - - // the message - const char *message = luaL_checkstring(L, 2); + // parse args + lua_args_parse(L, &lua_chan_say_func, (void *) &lua_chan, + &message + ); // send if ((err = irc_chan_PRIVMSG(lua_chan->chan, message))) @@ -51,19 +69,14 @@ return 0; } -static const struct luaL_Reg lua_chan_methods[] = { - { "__tostring", &lua_chan_tostring }, - { "say", &lua_chan_say }, - { NULL, NULL }, -}; +static struct lua_method lua_chan_methods[] = LUA_METHODS( + LUA_METHOD("__tostring", lua_chan__tostring, &lua_chan__tostring_func ), + LUA_METHOD("say", lua_chan_say, &lua_chan_say_func ) + ); -/** - * Initialize the lua_chan object type - */ -static void lua_chan_init (lua_State *L) -{ - lua_obj_create_type(L, "evirc.chan", lua_chan_methods); -} + + +static struct lua_type lua_net_type = LUA_TYPE("evirc.net"); /** * Create a lua_net userdata from the given irc_net and push it onto the stack, returning 1. @@ -71,7 +84,7 @@ static int lua_net_create (lua_State *L, struct irc_net *net) { // create the new obj - struct lua_net *lua_net = lua_obj_create_obj(L, "evirc.net", sizeof(*lua_net)); + struct lua_net *lua_net = lua_type_create(L, &lua_net_type, sizeof(*lua_net)); // initialize lua_net->net = net; @@ -83,9 +96,17 @@ /** * Return the network name as a lua string */ -static int lua_net_tostring (lua_State *L) +static struct lua_func lua_net__tostring_func = LUA_FUNC(&lua_net_type, "__tostring", + "format using network name", + + LUA_FUNC_ARG_END + ); + +static int lua_net__tostring (lua_State *L) { - struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net"); + struct lua_net *lua_net; + + lua_args_parse(L, &lua_net__tostring_func, (void *) &lua_net); lua_pushfstring(L, "", irc_net_name(lua_net->net)); @@ -95,16 +116,24 @@ /** * Join a new channel, returning the lua_chan */ +static struct lua_func lua_net_join_func = LUA_FUNC(&lua_net_type, "join", + "create a new channel and join it", + + LUA_FUNC_ARG_STRING("channel", LUA_ARG_STRING_REQUIRED) + ); + + static int lua_net_join (lua_State *L) { - struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net"); + struct lua_net *lua_net; struct irc_chan_info chan_info; struct irc_chan *chan; struct error_info err; // the channel name - // XXX: bad! bad! bad! - chan_info.channel = strdup(luaL_checkstring(L, 2)); + lua_args_parse(L, &lua_net_join_func, (void *) &lua_net, + &chan_info.channel + ); // add it if (irc_net_add_chan(lua_net->net, &chan, &chan_info, &err)) @@ -117,13 +146,22 @@ /** * Look up a channel by name, returning the lua_chan */ +static struct lua_func lua_net_get_chan_func = LUA_FUNC(&lua_net_type, "channel", + "look up a channel by name", + + LUA_FUNC_ARG_STRING("channel", LUA_ARG_STRING_REQUIRED) + ); + static int lua_net_get_chan (lua_State *L) { - struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net"); + struct lua_net *lua_net; struct irc_chan *chan; - - // the channel name - const char *channel = luaL_checkstring(L, 2); + const char *channel; + + // parse args + lua_args_parse(L, &lua_net_get_chan_func, (void *) &lua_net, + &channel + ); // lookup the irc_chan if ((chan = irc_net_get_chan(lua_net->net, channel)) == NULL) @@ -133,22 +171,18 @@ return lua_chan_create(L, chan); } -static const struct luaL_Reg lua_net_methods[] = { - { "__tostring", &lua_net_tostring }, - { "join", &lua_net_join }, - { "channel", &lua_net_get_chan }, - { NULL, NULL } -}; +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 ) + ); -/** - * Initialize the lua_net object type - */ -static void lua_net_init (lua_State *L) -{ - lua_obj_create_type(L, "evirc.net", lua_net_methods); -} -static struct lua_func lua_client_set_defaults_func = LUA_FUNC("evirc.client", "set_defaults", + +static struct lua_type lua_client_type = LUA_TYPE("evirc.client"); + + +static struct lua_func lua_client_set_defaults_func = LUA_FUNC(&lua_client_type, "set_defaults", "set the default settings to use for evirc.client.connect", LUA_FUNC_ARG_STRING("nickname", LUA_ARG_STRING_REQUIRED ), @@ -156,7 +190,7 @@ LUA_FUNC_ARG_STRING("realname", LUA_ARG_STRING_REQUIRED ), LUA_FUNC_ARG_STRING("service", IRC_PORT ), LUA_FUNC_ARG_STRING("service_ssl", IRC_SSL_PORT ) -); + ); static int lua_client_set_defaults (lua_State *L) { @@ -165,8 +199,8 @@ // parse args lua_args_parse(L, &lua_client_set_defaults_func, (void *) &lua_client, - &nickname, &username, &realname, &service, &service_ssl - ); + &nickname, &username, &realname, &service, &service_ssl + ); // set struct irc_client_defaults defaults = { @@ -187,7 +221,7 @@ return 0; } -static struct lua_func lua_client_connect_func = LUA_FUNC("evirc.client", "connect", +static struct lua_func lua_client_connect_func = LUA_FUNC(&lua_client_type, "connect", "Create and return a new IRC network", LUA_FUNC_ARG_STRING("network", LUA_ARG_STRING_REQUIRED ), @@ -239,13 +273,22 @@ return lua_net_create(L, net); } +static struct lua_func lua_client_get_network_func = LUA_FUNC(&lua_client_type, "network", + "Lookup an existing network by name", + + LUA_FUNC_ARG_STRING("network", LUA_ARG_STRING_REQUIRED) + ); + static int lua_client_get_network (lua_State *L) { - struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client"); + struct lua_client *lua_client; struct irc_net *net; - - // the network name - const char *network = luaL_checkstring(L, 2); + const char *network; + + // parse args + lua_args_parse(L, &lua_client_get_network_func, (void *) &lua_client, + &network + ); // lookup the irc_net if ((net = irc_client_get_net(lua_client->client, network)) == NULL) @@ -255,13 +298,22 @@ return lua_net_create(L, net); } +static struct lua_func lua_client_quit_func = LUA_FUNC(&lua_client_type, "quit", + "Disconnect from all networks", + + LUA_FUNC_ARG_STRING("message", "Bye") + ); + static int lua_client_quit (lua_State *L) { - struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client"); + struct lua_client *lua_client; err_t err; - - // the message - const char *message = luaL_checkstring(L, 2); + const char *message; + + // parse args + lua_args_parse(L, &lua_client_quit_func, (void *) &lua_client, + &message + ); // execute if ((err = irc_client_quit(lua_client->client, message))) @@ -271,31 +323,23 @@ return 0; } -static const struct luaL_Reg lua_client_methods[] = { - { "set_defaults", &lua_client_set_defaults }, - { "connect", &lua_client_connect }, - { "network", &lua_client_get_network }, - { "quit", &lua_client_quit }, - { NULL, NULL } -}; - -/** - * Initialize the evirc.client type for lua_client, and registers an instance bound to the given irc_client at - * 'client'. - */ -static void lua_client_init (lua_State *L, struct irc_client *client) -{ - // allocate the global "client" object - struct lua_client *lua_client = lua_obj_create_global_type(L, "evirc.client", lua_client_methods, "client", sizeof(*lua_client)); - - // initialize it - lua_client->client = client; -} +static struct lua_method lua_client_methods[] = LUA_METHODS( + 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("quit", lua_client_quit, &lua_client_quit_func ) + ); void lua_irc_init (struct nexus_lua *lua) { - lua_client_init(lua->st, lua->nexus->client); - lua_net_init(lua->st); - lua_chan_init(lua->st); + // register types + lua_type_register(lua->st, &lua_chan_type, lua_chan_methods); + lua_type_register(lua->st, &lua_net_type, lua_net_methods); + + // register the global "client" object + struct lua_client *lua_client = lua_type_register_global(lua->st, &lua_client_type, lua_client_methods, "client", sizeof(*lua_client)); + + // initialize it + lua_client->client = lua->nexus->client; }