src/lua_objs.c
author Tero Marttila <terom@fixme.fi>
Wed, 01 Apr 2009 18:30:47 +0300
changeset 108 50ff7ac8a725
parent 107 5c1eeb45c7f2
child 109 bfe9b9a8fe5b
permissions -rw-r--r--
implement modules_path + module_load with NULL path
#include "lua_objs.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[])
{
    luaL_newmetatable(L, name);

    // set the metatable __index to itself
    lua_pushvalue(L, -1);
    lua_setfield(L, -1, "__index");

    // register the methods to the metatable
    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)
{
    // create the new userdata on the stack
    void *ud = lua_newuserdata(L, size);

    // get the type and set it
    luaL_getmetatable(L, name);
    lua_setmetatable(L, -2);
    
    // ok
    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)
{
    // allocate the global object
    void *obj = lua_newuserdata(L, size);
    
    // create the type metatable
    lua_obj_create_type(L, type_name, methods);

    // set the userdata's metatable
    lua_setmetatable(L, -2);

    // store it as a global
    lua_setglobal(L, global_name);
    
    // ok
    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 *ud;

    // validate the userdata arg
    if ((ud = luaL_checkudata(L, 1, name)) == NULL) {
        luaL_error(L, "bad type argument to %s: `%s` expected", func, name);
        
        // XXX: needs a __noreturn__ attribute
        return NULL;

    } else {
        // ok
        return ud;

    }
}

/**
 * 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 {
    struct module *module;
};

/**
 * Create a lua_module userdata from the given module and push it onto the stack, returning 1.
 */
static int lua_module_create (lua_State *L, struct module *module)
{
    // create the new obj
    struct lua_module *lua_module = lua_obj_create_obj(L, "spbot.module", sizeof(*lua_module));

    // initialize
    lua_module->module = module;

    // ok
    return 1;
}

/**
 * Parse and apply the given config option value as a raw value to the given module's option
 */
static int _lua_module_conf_raw_str (lua_State *L, struct lua_module *lua_module, struct nexus *nexus, 
        const struct config_option *option, const char *conf_value)
{
    struct config_value value;
    struct error_info err;

    // mutable version of the conf_value
    char conf_value_buf[strlen(conf_value) + 1];
    strcpy(conf_value_buf, conf_value);

    // parse it as a raw value
    if (config_parse(option, nexus, &value, conf_value_buf, &err))
        return luaL_error(L, "config_parse: %s/%s: %s", option->name, conf_value_buf, error_msg(&err));
    
    // apply it while conf_value_buf is still in scope
    if (module_conf(lua_module->module, option, &value, &err))
        return luaL_error(L, "module_conf: %s/%s: %s", module_name(lua_module->module), option->name, error_msg(&err));
    
    // ok
    return 0;
}

static int lua_module_conf (lua_State *L)
{
    struct lua_module *lua_module = lua_obj_get_obj(L, __func__, "spbot.module");
    const struct config_option *option;
    struct config_value value;
    struct error_info err;

    // XXX: come up with some better way...
    struct nexus *nexus = lua_module->module->modules->nexus;

    // the config name
    const char *conf_name = luaL_checkstring(L, 2);

    // look it up
    if ((option = module_conf_lookup(lua_module->module, conf_name, &err)) == NULL)
        return luaL_error(L, "module_conf_lookup: %s/%s: %s", module_name(lua_module->module), conf_name, error_msg(&err));
 
    // the config value
    switch (lua_type(L, 3)) {
        case LUA_TSTRING:
            // parse+apply it as a raw string
            return _lua_module_conf_raw_str(L, lua_module, nexus, option, lua_tostring(L, 3));

        case LUA_TUSERDATA:
            // some kind of userdata, use its metatable to figure out what type it is
            if (!lua_getmetatable(L, 3))
                return luaL_error(L, "config value is userdata without metatable");

            // get the target metatable
            lua_getfield(L, LUA_REGISTRYINDEX, "evirc.chan");

            // is it a chan?
            if (!lua_rawequal(L, -1, -2))
                return luaL_error(L, "config value is userdata of unknown type");

            // pop the metatables
            lua_pop(L, 2);

            // get the irc_chan
            struct lua_chan *lua_chan = lua_touserdata(L, 3);

            // build the value
            value.type = CONFIG_IRC_CHAN;
            value.irc_chan = lua_chan->chan;
            
            break;
        
        default:
            return luaL_error(L, "config value is of unknown lua type '%s'", lua_typename(L, 3));

    }
    
    // apply it
    if (module_conf(lua_module->module, option, &value, &err))
        return luaL_error(L, "module_conf: %s/%s: %s", module_name(lua_module->module), option->name, error_msg(&err));
   
    // ok
    return 0;
}

static const struct luaL_Reg lua_module_methods[] = {
    {   "conf",         &lua_module_conf            },
    {   NULL,           NULL                        }
};

/**
 * Initialize the lua_module object type
 */
static void lua_module_init (lua_State *L)
{
    lua_obj_create_type(L, "spbot.module", lua_module_methods);
}

/**
 * Wrapper for modules
 */
struct lua_modules {
    struct modules *modules;
    
    // strdup'd path for module_path
    // XXX: remove when gc'd
    char *path;
};

static int lua_modules_path (lua_State *L)
{
    struct lua_modules *lua_modules = lua_obj_get_obj(L, __func__, "spbot.modules");
    char *path = NULL;
    const char *old_path;
    
    if (!lua_isnoneornil(L, 2)) {
        // the new path
        if ((path = strdup(luaL_checkstring(L, 2))) == NULL)
            return luaL_error(L, "strdup");
    }

    // set or get
    old_path = modules_path(lua_modules->modules, path);

    // return the old path
    if (old_path)
        lua_pushstring(L, old_path);
    else
        lua_pushnil(L);

    if (path) {
        // replace the old path
        free(lua_modules->path); 
        lua_modules->path = path;
    }

    // ok
    return 1;
}

static int lua_modules_load (lua_State *L)
{
    struct lua_modules *lua_modules = lua_obj_get_obj(L, __func__, "spbot.modules");
    struct module *module;
    struct module_info info;
    struct error_info err;

    // the module name/path
    info.name = luaL_checkstring(L, 2);
    info.path = lua_isnoneornil(L, 3) ? NULL : luaL_checkstring(L, 3);

    // load
    if (module_load(lua_modules->modules, &module, &info, &err))
        return luaL_error(L, "module_load: %s/%s: %s", info.name, info.path, error_msg(&err));

    // wrap
    return lua_module_create(L, module);
}

static int lua_modules_module (lua_State *L)
{
    struct lua_modules *lua_modules = lua_obj_get_obj(L, __func__, "spbot.modules");
    struct module *module;
    
    // the module name
    const char *name = luaL_checkstring(L, 2);

    // look it up
    if ((module = module_get(lua_modules->modules, name)) == NULL)
        return luaL_error(L, "module_get: %s: no such module", name);

    // wrap
    return lua_module_create(L, module);
}

static const struct luaL_Reg lua_modules_methods[] = {
    {   "path",         &lua_modules_path           },
    {   "load",         &lua_modules_load           },
    {   "module",       &lua_modules_module         },
    {   NULL,           NULL                        }
};

/**
 * Initialize the spbot.modules type for lua_modules, and registers an instance bound to the given modules list at
 * 'modules'.
 */
static void lua_modules_init (lua_State *L, struct modules *modules)
{
    // allocate the global "modules" object
    struct lua_modules *lua_modules = lua_obj_create_global_type(L, "spbot.modules", lua_modules_methods, "modules", sizeof(*lua_modules));
    
    // initialize it
    lua_modules->modules = modules;
}

/**
 * Wrapper for nexus
 */
struct lua_nexus {
    struct nexus *nexus;
};

static int lua_nexus_shutdown (lua_State *L)
{
    struct lua_nexus *lua_nexus = lua_obj_get_obj(L, __func__, "spbot.nexus");

    // just shut it down
    nexus_shutdown(lua_nexus->nexus);

    return 0;
}

static const struct luaL_Reg lua_nexus_methods[] = {
    {   "shutdown",     &lua_nexus_shutdown         },
    {   NULL,           NULL                        }
};

/**
 * Initialize the spbot.nexus type for lua_nexus, and registers an instance bound to the given nexus list at
 * 'nexus'.
 */
static void lua_nexus_init (lua_State *L, struct nexus *nexus)
{
    // allocate the global "nexus" object
    struct lua_nexus *lua_nexus = lua_obj_create_global_type(L, "spbot.nexus", lua_nexus_methods, "nexus", sizeof(*lua_nexus));
    
    // initialize it
    lua_nexus->nexus = nexus;
}

/**
 * Global functions
 */
static int lua_log_level (lua_State *L)
{
    // log level as a string
    enum log_level new_level = luaL_checkoption(L, 1, NULL, log_level_names);

    // set it
    set_log_level(new_level);

    // ok
    return 0;
}

static int lua_log (lua_State *L)
{
    // log level as a string
    enum log_level level = luaL_checkoption(L, 1, NULL, log_level_names);
    
    // log message
    const char *msg = luaL_checkstring(L, 2);

    // log it
    log_msg(level, "lua", "%s", msg);

    // ok
    return 0;
}

static const struct luaL_Reg lua_global_functions[] = {
    {   "log_level",    &lua_log_level              },
    {   "log",          &lua_log                    },
    {   NULL,           NULL                        }
};

static void lua_global_init (lua_State *L)
{
    const struct luaL_Reg *reg;

    for (reg = lua_global_functions; reg->name && reg->func; reg++) {
        // put the function on the stack
        lua_pushcfunction(L, reg->func);

        // set the global
        lua_setglobal(L, reg->name);
    }
}

/**
 * Set up the lua state in protected mode
 */
static int _lua_objs_init (lua_State *L)
{
    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;
}

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);
}