--- a/src/CMakeLists.txt Thu Apr 02 03:02:54 2009 +0300
+++ b/src/CMakeLists.txt Thu Apr 02 03:19:44 2009 +0300
@@ -12,7 +12,7 @@
set (CORE_SOURCES error.c log.c)
set (SOCK_SOURCES sock.c sock_tcp.c sock_gnutls.c sock_test.c line_proto.c)
set (IRC_SOURCES irc_line.c irc_conn.c irc_net.c irc_chan.c chain.c irc_cmd.c irc_proto.c irc_client.c irc_user.c irc_queue.c)
-set (LUA_SOURCES nexus_lua.c lua_objs.c lua_config.c)
+set (LUA_SOURCES nexus_lua.c lua_objs.c lua_config.c lua_irc.c)
set (CONSOLE_SOURCES console.c lua_console.c)
set (NEXUS_SOURCES nexus.c ${CORE_SOURCES} ${SOCK_SOURCES} ${IRC_SOURCES} ${LUA_SOURCES} ${CONSOLE_SOURCES} signals.c module.c config.c)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lua_irc.c Thu Apr 02 03:19:44 2009 +0300
@@ -0,0 +1,256 @@
+#include "lua_irc.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * 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));
+
+ // initialize
+ lua_chan->chan = chan;
+
+ // ok
+ return 1;
+}
+
+/**
+ * Return the channel name as a lua string
+ */
+static int lua_chan_tostring (lua_State *L)
+{
+ struct lua_chan *lua_chan = lua_obj_get_obj(L, __func__, "evirc.chan");
+
+ lua_pushfstring(L, "<irc_chan %s>", irc_chan_name(lua_chan->chan));
+
+ return 1;
+}
+
+/**
+ * Send a PRIVMSG to the channel
+ */
+static int lua_chan_say (lua_State *L)
+{
+ err_t err;
+
+ struct lua_chan *lua_chan = lua_obj_get_obj(L, __func__, "evirc.chan");
+
+ // the message
+ const char *message = luaL_checkstring(L, 2);
+
+ // send
+ if ((err = irc_chan_PRIVMSG(lua_chan->chan, message)))
+ return luaL_error(L, "irc_chan_PRIVMSG: '%s': %s", message, error_name(err));
+
+ // ok
+ return 0;
+}
+
+static const struct luaL_Reg lua_chan_methods[] = {
+ { "__tostring", &lua_chan_tostring },
+ { "say", &lua_chan_say },
+ { NULL, NULL },
+};
+
+/**
+ * Initialize the lua_chan object type
+ */
+static void lua_chan_init (lua_State *L)
+{
+ lua_obj_create_type(L, "evirc.chan", lua_chan_methods);
+}
+
+/**
+ * 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 obj
+ struct lua_net *lua_net = lua_obj_create_obj(L, "evirc.net", sizeof(*lua_net));
+
+ // initialize
+ lua_net->net = net;
+
+ // ok
+ return 1;
+}
+
+/**
+ * Return the network name as a lua string
+ */
+static int lua_net_tostring (lua_State *L)
+{
+ struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
+
+ lua_pushfstring(L, "<irc_net %s>", irc_net_name(lua_net->net));
+
+ return 1;
+}
+
+/**
+ * Join a new channel, returning the lua_chan
+ */
+static int lua_net_join (lua_State *L)
+{
+ struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
+ struct irc_chan_info chan_info;
+ struct irc_chan *chan;
+ struct error_info err;
+
+ // 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));
+
+ // return it
+ return lua_chan_create(L, chan);
+}
+
+/**
+ * Look up a channel by name, returning the lua_chan
+ */
+static int lua_net_get_chan (lua_State *L)
+{
+ struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
+ struct irc_chan *chan;
+
+ // the channel name
+ const char *channel = luaL_checkstring(L, 2);
+
+ // lookup the irc_chan
+ if ((chan = irc_net_get_chan(lua_net->net, channel)) == NULL)
+ return luaL_error(L, "irc_net_get_chan: no such channel: %s", channel);
+
+ // wrap it
+ 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 }
+};
+
+/**
+ * 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 int lua_client_set_defaults (lua_State *L)
+{
+ struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
+
+ // read the args
+ // XXX: need to copy these, really
+ lua_client->defaults.register_info.nickname = luaL_checkstring(L, 2);
+ lua_client->defaults.register_info.username = luaL_checkstring(L, 3);
+ lua_client->defaults.register_info.realname = luaL_checkstring(L, 4);
+
+ // set the non-args
+ lua_client->defaults.service = IRC_PORT;
+ lua_client->defaults.service_ssl = IRC_SSL_PORT;
+
+ // invoke
+ irc_client_set_defaults(lua_client->client, &lua_client->defaults);
+
+ // ok
+ return 0;
+}
+
+static int lua_client_connect (lua_State *L)
+{
+ struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
+ struct irc_net_info net_info;
+ struct irc_net *net;
+ struct error_info err;
+
+ // init net_info
+ memset(&net_info, 0, sizeof(net_info));
+
+ // the network name
+ net_info.network = luaL_checkstring(L, 2);
+
+ // the hostname
+ net_info.hostname = luaL_checkstring(L, 3);
+
+ // service remains default
+ net_info.service = "6667";
+
+ // create it
+ if (irc_client_add_net(lua_client->client, &net, &net_info, &err))
+ return luaL_error(L, "irc_client_add_net: %s/%s: %s", net_info.network, net_info.hostname, error_msg(&err));
+
+ // wrap it
+ return lua_net_create(L, net);
+}
+
+static int lua_client_get_network (lua_State *L)
+{
+ struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
+ struct irc_net *net;
+
+ // 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 int lua_client_quit (lua_State *L)
+{
+ struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
+ err_t err;
+
+ // the message
+ const char *message = luaL_checkstring(L, 2);
+
+ // execute
+ if ((err = irc_client_quit(lua_client->client, message)))
+ return luaL_error(L, "irc_client_quit: %s", error_name(err));
+
+ // ok
+ 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;
+}
+
+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);
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lua_irc.h Thu Apr 02 03:19:44 2009 +0300
@@ -0,0 +1,42 @@
+#ifndef LUA_IRC_H
+#define LUA_IRC_H
+
+/**
+ * Defines lua wrappers for the irc_* objects
+ */
+#include "nexus_lua.h"
+#include "lua_objs.h"
+#include "irc_client.h"
+
+/**
+ * Our lua wrapper for irc_chan
+ */
+struct lua_chan {
+ struct irc_chan *chan;
+};
+
+/**
+ * Our lua wrapper for irc_net
+ */
+struct lua_net {
+ struct irc_net *net;
+};
+
+/**
+ * Our lua wrapper for irc_client
+ */
+struct lua_client {
+ struct irc_client *client;
+
+ /** Any defaults set using lua_client_set_defaults */
+ struct irc_client_defaults defaults;
+};
+
+/**
+ * Initialize the various lua_irc object types for the given lua state.
+ *
+ * Call in protected mode.
+ */
+void lua_irc_init (struct nexus_lua *lua);
+
+#endif
--- a/src/lua_objs.c Thu Apr 02 03:02:54 2009 +0300
+++ b/src/lua_objs.c Thu Apr 02 03:19:44 2009 +0300
@@ -1,17 +1,11 @@
#include "lua_objs.h"
+#include "lua_irc.h"
#include "log.h"
#include <stdlib.h>
#include <string.h>
-#include <lua5.1/lua.h>
-#include <lua5.1/lualib.h>
-#include <lua5.1/lauxlib.h>
-
-/**
- * Convenience function to register a new metatable for a type, this leaves the metatable on the stack
- */
-static void lua_obj_create_type (lua_State *L, const char *name, const struct luaL_Reg methods[])
+void lua_obj_create_type (lua_State *L, const char *name, const struct luaL_Reg methods[])
{
luaL_newmetatable(L, name);
@@ -23,11 +17,7 @@
luaL_register(L, NULL, methods);
}
-/**
- * Convenience function to create a new userdata with the given type metatable name, return the pointer, and keep it
- * on the stack
- */
-static void* lua_obj_create_obj (lua_State *L, const char *name, size_t size)
+void* lua_obj_create_obj (lua_State *L, const char *name, size_t size)
{
// create the new userdata on the stack
void *ud = lua_newuserdata(L, size);
@@ -40,10 +30,7 @@
return ud;
}
-/**
- * Convenience function to create a new metatable for a type, a userdata for that type, and register it as a global
- */
-static void* lua_obj_create_global_type (lua_State *L, const char *type_name, const struct luaL_Reg methods[], const char *global_name, size_t size)
+void* lua_obj_create_global_type (lua_State *L, const char *type_name, const struct luaL_Reg methods[], const char *global_name, size_t size)
{
// allocate the global object
void *obj = lua_newuserdata(L, size);
@@ -61,10 +48,7 @@
return obj;
}
-/**
- * Convenience function to get a userdata with the given type metatable name as the first argument for a function
- */
-static void* lua_obj_get_obj (lua_State *L, const char *func, const char *name)
+void* lua_obj_get_obj (lua_State *L, const char *func, const char *name)
{
void *ud;
@@ -83,273 +67,6 @@
}
/**
- * Our lua wrapper for irc_chan
- */
-struct lua_chan {
- struct irc_chan *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));
-
- // initialize
- lua_chan->chan = chan;
-
- // ok
- return 1;
-}
-
-/**
- * Return the channel name as a lua string
- */
-static int lua_chan_tostring (lua_State *L)
-{
- struct lua_chan *lua_chan = lua_obj_get_obj(L, __func__, "evirc.chan");
-
- lua_pushfstring(L, "<irc_chan %s>", irc_chan_name(lua_chan->chan));
-
- return 1;
-}
-
-/**
- * Send a PRIVMSG to the channel
- */
-static int lua_chan_say (lua_State *L)
-{
- err_t err;
-
- struct lua_chan *lua_chan = lua_obj_get_obj(L, __func__, "evirc.chan");
-
- // the message
- const char *message = luaL_checkstring(L, 2);
-
- // send
- if ((err = irc_chan_PRIVMSG(lua_chan->chan, message)))
- return luaL_error(L, "irc_chan_PRIVMSG: '%s': %s", message, error_name(err));
-
- // ok
- return 0;
-}
-
-static const struct luaL_Reg lua_chan_methods[] = {
- { "__tostring", &lua_chan_tostring },
- { "say", &lua_chan_say },
- { NULL, NULL },
-};
-
-/**
- * Initialize the lua_chan object type
- */
-static void lua_chan_init (lua_State *L)
-{
- lua_obj_create_type(L, "evirc.chan", lua_chan_methods);
-}
-
-/**
- * Our lua wrapper for 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 obj
- struct lua_net *lua_net = lua_obj_create_obj(L, "evirc.net", sizeof(*lua_net));
-
- // initialize
- lua_net->net = net;
-
- // ok
- return 1;
-}
-
-/**
- * Return the network name as a lua string
- */
-static int lua_net_tostring (lua_State *L)
-{
- struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
-
- lua_pushfstring(L, "<irc_net %s>", irc_net_name(lua_net->net));
-
- return 1;
-}
-
-/**
- * Join a new channel, returning the lua_chan
- */
-static int lua_net_join (lua_State *L)
-{
- struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
- struct irc_chan_info chan_info;
- struct irc_chan *chan;
- struct error_info err;
-
- // 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));
-
- // return it
- return lua_chan_create(L, chan);
-}
-
-/**
- * Look up a channel by name, returning the lua_chan
- */
-static int lua_net_get_chan (lua_State *L)
-{
- struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
- struct irc_chan *chan;
-
- // the channel name
- const char *channel = luaL_checkstring(L, 2);
-
- // lookup the irc_chan
- if ((chan = irc_net_get_chan(lua_net->net, channel)) == NULL)
- return luaL_error(L, "irc_net_get_chan: no such channel: %s", channel);
-
- // wrap it
- 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 }
-};
-
-/**
- * Initialize the lua_net object type
- */
-static void lua_net_init (lua_State *L)
-{
- lua_obj_create_type(L, "evirc.net", lua_net_methods);
-}
-
-/**
- * Wrapper for irc_client
- */
-struct lua_client {
- struct irc_client *client;
-
- struct irc_client_defaults defaults;
-};
-
-static int lua_client_set_defaults (lua_State *L)
-{
- struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
-
- // read the args
- // XXX: need to copy these, really
- lua_client->defaults.register_info.nickname = luaL_checkstring(L, 2);
- lua_client->defaults.register_info.username = luaL_checkstring(L, 3);
- lua_client->defaults.register_info.realname = luaL_checkstring(L, 4);
-
- // set the non-args
- lua_client->defaults.service = IRC_PORT;
- lua_client->defaults.service_ssl = IRC_SSL_PORT;
-
- // invoke
- irc_client_set_defaults(lua_client->client, &lua_client->defaults);
-
- // ok
- return 0;
-}
-
-static int lua_client_connect (lua_State *L)
-{
- struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
- struct irc_net_info net_info;
- struct irc_net *net;
- struct error_info err;
-
- // init net_info
- memset(&net_info, 0, sizeof(net_info));
-
- // the network name
- net_info.network = luaL_checkstring(L, 2);
-
- // the hostname
- net_info.hostname = luaL_checkstring(L, 3);
-
- // service remains default
- net_info.service = "6667";
-
- // create it
- if (irc_client_add_net(lua_client->client, &net, &net_info, &err))
- return luaL_error(L, "irc_client_add_net: %s/%s: %s", net_info.network, net_info.hostname, error_msg(&err));
-
- // wrap it
- return lua_net_create(L, net);
-}
-
-static int lua_client_get_network (lua_State *L)
-{
- struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
- struct irc_net *net;
-
- // 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 int lua_client_quit (lua_State *L)
-{
- struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
- err_t err;
-
- // the message
- const char *message = luaL_checkstring(L, 2);
-
- // execute
- if ((err = irc_client_quit(lua_client->client, message)))
- return luaL_error(L, "irc_client_quit: %s", error_name(err));
-
- // ok
- 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;
-}
-
-/**
* Wrapper for module
*/
struct lua_module {
@@ -711,33 +428,12 @@
}
}
-/**
- * Set up the lua state in protected mode
- */
-static int _lua_objs_init (lua_State *L)
+void lua_objs_init (struct nexus_lua *lua)
{
- struct nexus *nexus;
-
- // read the nexus off the stack
- if ((nexus = lua_touserdata(L, 1)) == NULL)
- luaL_error(L, "lua_touserdata: NULL");
-
// init the various bits
- lua_global_init(L);
- lua_nexus_init(L, nexus);
- lua_modules_init(L, nexus->modules);
- lua_module_init(L);
- lua_client_init(L, nexus->client);
- lua_net_init(L);
- lua_chan_init(L);
-
- // nothing
- return 0;
+ lua_global_init(lua->st);
+ lua_nexus_init(lua->st, lua->nexus);
+ lua_modules_init(lua->st, lua->nexus->modules);
+ lua_module_init(lua->st);
}
-err_t lua_objs_init (struct nexus_lua *lua, struct error_info *err)
-{
- // call in protected mode
- return nexus_lua_error(lua->st, lua_cpcall(lua->st, &_lua_objs_init, lua->nexus), err);
-}
-
--- a/src/lua_objs.h Thu Apr 02 03:02:54 2009 +0300
+++ b/src/lua_objs.h Thu Apr 02 03:19:44 2009 +0300
@@ -8,9 +8,34 @@
*/
#include "nexus_lua.h"
+#include <lua5.1/lua.h>
+#include <lua5.1/lauxlib.h>
+
/**
- * Registers our lua runtime objects into the given lua state
+ * Register a new metatable for a named type, this leaves the metatable on the stack.
*/
-err_t lua_objs_init (struct nexus_lua *lua, struct error_info *err);
+void lua_obj_create_type (lua_State *L, const char *name, const struct luaL_Reg methods[]);
+
+/**
+ * Create a new userdata with the given type metatable name, return the pointer, and keep it on the stack.
+ */
+void* lua_obj_create_obj (lua_State *L, const char *name, size_t size);
+
+/**
+ * Create a new metatable for a type, a userdata for that type, and register it as a global
+ */
+void* lua_obj_create_global_type (lua_State *L, const char *type_name, const struct luaL_Reg methods[], const char *global_name, size_t size);
+
+/**
+ * Get a userdata with the given type metatable name as the first argument for a function.
+ */
+void* lua_obj_get_obj (lua_State *L, const char *func, const char *name);
+
+/**
+ * Registers our lua runtime objects into the given lua state.
+ *
+ * Call in protected mode.
+ */
+void lua_objs_init (struct nexus_lua *lua);
#endif /* LUA_OBJS_H */
--- a/src/nexus_lua.c Thu Apr 02 03:02:54 2009 +0300
+++ b/src/nexus_lua.c Thu Apr 02 03:19:44 2009 +0300
@@ -1,11 +1,36 @@
#include "nexus_lua.h"
#include "lua_objs.h"
+#include "lua_irc.h"
#include <stdlib.h>
#include <lua5.1/lualib.h>
#include <lua5.1/lauxlib.h>
+/**
+ * Initialize the lua state, in protected mode
+ */
+int nexus_lua_init (lua_State *L)
+{
+ struct nexus_lua *lua;
+
+ // read the nexus_lua off the stack
+ if ((lua = lua_touserdata(L, 1)) == NULL)
+ luaL_error(L, "lua_touserdata: NULL");
+
+ // open the standard libraries
+ luaL_openlibs(L);
+
+ // open our core lua api
+ lua_objs_init(lua);
+
+ // and the irc_* stuff
+ lua_irc_init(lua);
+
+ // ok
+ return 0;
+}
+
err_t nexus_lua_create (struct nexus_lua **lua_ptr, struct nexus *nexus, struct error_info *err)
{
struct nexus_lua *lua;
@@ -21,13 +46,8 @@
if ((lua->st = luaL_newstate()) == NULL)
JUMP_SET_ERROR(err, ERR_LUA_MEM);
- // we can then load the core libs
- // XXX: we don't need all of these
- // XXX: errors?
- luaL_openlibs(lua->st);
-
- // then our own things
- if (lua_objs_init(lua, err))
+ // init in protected mode
+ if (nexus_lua_error(lua->st, lua_cpcall(lua->st, &nexus_lua_init, lua), err))
goto error;
// ok
--- a/src/nexus_lua.h Thu Apr 02 03:02:54 2009 +0300
+++ b/src/nexus_lua.h Thu Apr 02 03:19:44 2009 +0300
@@ -6,6 +6,8 @@
*
* Defines the lua environment for use with nexus
*/
+struct nexus_lua;
+
#include "nexus.h"
#include <lua5.1/lua.h>