src/lua_irc.c
author Tero Marttila <terom@fixme.fi>
Sun, 19 Apr 2009 04:52:12 +0300
changeset 142 dc2bb09d412c
parent 141 0b850238c588
child 143 1edab39c88a8
permissions -rw-r--r--
implement table-as-argument for lua_arg_*
#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
    // XXX: bad! bad! bad!
    chan_info.channel = strdup(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)
{
    int nargs = lua_gettop(L);
    struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");

    // required args
    lua_client->defaults.register_info.nickname = lua_arg_string(L, nargs, 2, "nickname", LUA_ARG_STRING_REQUIRED);
    lua_client->defaults.register_info.username = lua_arg_string(L, nargs, 3, "username", LUA_ARG_STRING_REQUIRED);
    lua_client->defaults.register_info.realname = lua_arg_string(L, nargs, 4, "realname", LUA_ARG_STRING_REQUIRED);

    // optional args
    lua_client->defaults.service        = lua_arg_string(L, nargs, 5, "service", IRC_PORT);
    lua_client->defaults.service_ssl    = lua_arg_string(L, nargs, 6, "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)
{
    int nargs = lua_gettop(L);
    struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
    struct irc_net_info net_info;
    const char *ssl_cafile = NULL, *ssl_cert = NULL, *ssl_pkey = NULL;
    bool use_ssl = false, ssl_verify = false;
    struct irc_net *net;
    struct error_info err;

    // init net_info
    memset(&net_info, 0, sizeof(net_info));

    // required args
    net_info.network    = lua_arg_string(L, nargs, 2, "network",    LUA_ARG_STRING_REQUIRED);
    net_info.hostname   = lua_arg_string(L, nargs, 3, "hostname",   LUA_ARG_STRING_REQUIRED);

    // optional args
    net_info.service    = lua_arg_string(L, nargs, 4, "service",    NULL);

    // ssl stuff
    use_ssl             = lua_arg_bool  (L, nargs, 5, "use_ssl",    false);
    ssl_cafile          = lua_arg_string(L, nargs, 6, "ssl_cafile", NULL);
    ssl_verify          = lua_arg_bool  (L, nargs, 7, "ssl_verify", false);
    ssl_cert            = lua_arg_string(L, nargs, 8, "ssl_cert",   NULL);
    ssl_pkey            = lua_arg_string(L, nargs, 9, "ssl_pkey",   NULL);

    // SSL?
    if (use_ssl || ssl_cafile || ssl_verify || ssl_cert || ssl_pkey) {
        // verify
        if ((ssl_cert || ssl_pkey) && !(ssl_cert && ssl_pkey))
            return luaL_error(L, "must give both ssl_cert and ssl_pkey");
        
        // create
        if (sock_ssl_client_cred_create(&net_info.ssl_cred, ssl_cafile, ssl_verify, ssl_cert, ssl_pkey, &err))
            return luaL_error(L, "sock_ssl_client_cred_create(ssl_cafile=%s, ssl_verify=%b, ssl_cert=%s, ssl_pkey=%s): %s", 
                    ssl_cafile, ssl_verify, ssl_cert, ssl_pkey, error_msg(&err)
                );
    }

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