src/lua_objs.c
author Tero Marttila <terom@fixme.fi>
Wed, 01 Apr 2009 19:31:11 +0300
changeset 109 bfe9b9a8fe5b
parent 108 50ff7ac8a725
child 113 477d1cb3d87c
permissions -rw-r--r--
fix some more valgrind errors
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "lua_objs.h"
104
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
     2
#include "log.h"
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
108
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
     4
#include <stdlib.h>
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
     5
#include <string.h>
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
     6
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
#include <lua5.1/lua.h>
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
#include <lua5.1/lualib.h>
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
#include <lua5.1/lauxlib.h>
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
/**
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    12
 * Convenience function to register a new metatable for a type, this leaves the metatable on the stack
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    13
 */
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    14
static void lua_obj_create_type (lua_State *L, const char *name, const struct luaL_Reg methods[])
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    15
{
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    16
    luaL_newmetatable(L, name);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    17
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    18
    // set the metatable __index to itself
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    19
    lua_pushvalue(L, -1);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    20
    lua_setfield(L, -1, "__index");
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    21
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    22
    // register the methods to the metatable
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    23
    luaL_register(L, NULL, methods);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    24
}
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    25
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    26
/**
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    27
 * Convenience function to create a new userdata with the given type metatable name, return the pointer, and keep it
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    28
 * on the stack
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    29
 */
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    30
static void* lua_obj_create_obj (lua_State *L, const char *name, size_t size)
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    31
{
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    32
    // create the new userdata on the stack
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    33
    void *ud = lua_newuserdata(L, size);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    34
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    35
    // get the type and set it
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    36
    luaL_getmetatable(L, name);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    37
    lua_setmetatable(L, -2);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    38
    
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    39
    // ok
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    40
    return ud;
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    41
}
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    42
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    43
/**
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    44
 * Convenience function to create a new metatable for a type, a userdata for that type, and register it as a global
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    45
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    46
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)
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    47
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    48
    // allocate the global object
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    49
    void *obj = lua_newuserdata(L, size);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    50
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    51
    // create the type metatable
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    52
    lua_obj_create_type(L, type_name, methods);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    53
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    54
    // set the userdata's metatable
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    55
    lua_setmetatable(L, -2);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    56
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    57
    // store it as a global
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    58
    lua_setglobal(L, global_name);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    59
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    60
    // ok
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    61
    return obj;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    62
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    63
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    64
/**
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    65
 * Convenience function to get a userdata with the given type metatable name as the first argument for a function
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    66
 */
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    67
static void* lua_obj_get_obj (lua_State *L, const char *func, const char *name)
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    68
{
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    69
    void *ud;
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    70
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    71
    // validate the userdata arg
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    72
    if ((ud = luaL_checkudata(L, 1, name)) == NULL) {
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    73
        luaL_error(L, "bad type argument to %s: `%s` expected", func, name);
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    74
        
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    75
        // XXX: needs a __noreturn__ attribute
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    76
        return NULL;
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    77
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    78
    } else {
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    79
        // ok
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    80
        return ud;
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    81
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    82
    }
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    83
}
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    84
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    85
/**
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    86
 * Our lua wrapper for irc_chan
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    87
 */
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    88
struct lua_chan {
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    89
    struct irc_chan *chan;
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    90
};
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    91
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    92
/**
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    93
 * Create a lua_chan userdata from the given irc_chan and leave it on the stack, returning 1
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    94
 */
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    95
static int lua_chan_create (lua_State *L, struct irc_chan *chan)
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    96
{
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    97
    // create the new obj
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    98
    struct lua_chan *lua_chan = lua_obj_create_obj(L, "evirc.chan", sizeof(*lua_chan));
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    99
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   100
    // initialize
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   101
    lua_chan->chan = chan;
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   102
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   103
    // ok
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   104
    return 1;
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   105
}
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   106
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   107
/**
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   108
 * Return the channel name as a lua string
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   109
 */
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   110
static int lua_chan_tostring (lua_State *L)
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   111
{
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   112
    struct lua_chan *lua_chan = lua_obj_get_obj(L, __func__, "evirc.chan");
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   113
    
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   114
    lua_pushfstring(L, "<irc_chan %s>", irc_chan_name(lua_chan->chan));
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   115
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   116
    return 1;
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   117
}
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   118
97
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   119
/**
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   120
 * Send a PRIVMSG to the channel
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   121
 */
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   122
static int lua_chan_say (lua_State *L)
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   123
{
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   124
    err_t err;
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   125
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   126
    struct lua_chan *lua_chan = lua_obj_get_obj(L, __func__, "evirc.chan");
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   127
    
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   128
    // the message
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   129
    const char *message = luaL_checkstring(L, 2);
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   130
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   131
    // send
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   132
    if ((err = irc_chan_PRIVMSG(lua_chan->chan, message)))
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   133
        return luaL_error(L, "irc_chan_PRIVMSG: '%s': %s", message, error_name(err));
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   134
    
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   135
    // ok
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   136
    return 0;
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   137
}
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   138
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   139
static const struct luaL_Reg lua_chan_methods[] = {
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   140
    {   "__tostring",       &lua_chan_tostring  },
97
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 96
diff changeset
   141
    {   "say",              &lua_chan_say       },
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   142
    {   NULL,               NULL                },
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   143
};
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   144
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   145
/**
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   146
 * Initialize the lua_chan object type
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   147
 */
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   148
static void lua_chan_init (lua_State *L)
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   149
{
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   150
    lua_obj_create_type(L, "evirc.chan", lua_chan_methods);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   151
}
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   152
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   153
/**
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
 * Our lua wrapper for irc_net
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   155
 */
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   156
struct lua_net {
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   157
    struct irc_net *net;
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   158
};
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   159
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   160
/**
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   161
 * Create a lua_net userdata from the given irc_net and push it onto the stack, returning 1.
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   162
 */
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   163
static int lua_net_create (lua_State *L, struct irc_net *net)
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   164
{
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   165
    // create the new obj
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   166
    struct lua_net *lua_net = lua_obj_create_obj(L, "evirc.net", sizeof(*lua_net));
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   167
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   168
    // initialize
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   169
    lua_net->net = net;
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   170
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   171
    // ok
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   172
    return 1;
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   173
}
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   174
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   175
/**
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   176
 * Return the network name as a lua string
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   177
 */
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   178
static int lua_net_tostring (lua_State *L)
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   179
{
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   180
    struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   181
    
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   182
    lua_pushfstring(L, "<irc_net %s>", irc_net_name(lua_net->net));
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   183
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   184
    return 1;
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   185
}
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   186
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   187
/**
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   188
 * Join a new channel, returning the lua_chan
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   189
 */
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   190
static int lua_net_join (lua_State *L)
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   191
{
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   192
    struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   193
    struct irc_chan_info chan_info;
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   194
    struct irc_chan *chan;
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   195
    struct error_info err;
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   196
    
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   197
    // the channel name
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   198
    chan_info.channel = luaL_checkstring(L, 2);
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   199
    
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   200
    // add it
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   201
    if (irc_net_add_chan(lua_net->net, &chan, &chan_info, &err))
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   202
        return luaL_error(L, "irc_net_add_chan: %s: %s", chan_info.channel, error_msg(&err));
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   203
    
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   204
    // return it
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   205
    return lua_chan_create(L, chan);    
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   206
}
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   207
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   208
/**
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   209
 * Look up a channel by name, returning the lua_chan
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   210
 */
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   211
static int lua_net_get_chan (lua_State *L)
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   212
{
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   213
    struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   214
    struct irc_chan *chan;
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   215
    
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   216
    // the channel name
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   217
    const char *channel = luaL_checkstring(L, 2);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   218
    
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   219
    // lookup the irc_chan
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   220
    if ((chan = irc_net_get_chan(lua_net->net, channel)) == NULL)
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   221
        return luaL_error(L, "irc_net_get_chan: no such channel: %s", channel);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   222
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   223
    // wrap it
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   224
    return lua_chan_create(L, chan);
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   225
}
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   226
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   227
static const struct luaL_Reg lua_net_methods[] = {
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   228
    {   "__tostring",   &lua_net_tostring   },
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   229
    {   "join",         &lua_net_join       },
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   230
    {   "channel",      &lua_net_get_chan   },
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   231
    {   NULL,           NULL                }
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   232
};
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   233
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   234
/**
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   235
 * Initialize the lua_net object type
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   236
 */
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   237
static void lua_net_init (lua_State *L)
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   238
{
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   239
    lua_obj_create_type(L, "evirc.net", lua_net_methods);
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   240
}
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   241
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   242
/**
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   243
 * Wrapper for irc_client
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   244
 */
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   245
struct lua_client {
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   246
    struct irc_client *client;
99
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   247
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   248
    struct irc_client_defaults defaults;
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   249
};
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   250
99
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   251
static int lua_client_set_defaults (lua_State *L)
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   252
{
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   253
    struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   254
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   255
    // read the args
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   256
    // XXX: need to copy these, really
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   257
    lua_client->defaults.register_info.nickname = luaL_checkstring(L, 2);
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   258
    lua_client->defaults.register_info.username = luaL_checkstring(L, 3);
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   259
    lua_client->defaults.register_info.realname = luaL_checkstring(L, 4);
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   260
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   261
    // set the non-args
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   262
    lua_client->defaults.service = IRC_PORT;
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   263
    lua_client->defaults.service_ssl = IRC_SSL_PORT;
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   264
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   265
    // invoke
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   266
    irc_client_set_defaults(lua_client->client, &lua_client->defaults);
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   267
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   268
    // ok
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   269
    return 0;
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   270
}
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   271
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   272
static int lua_client_connect (lua_State *L)
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   273
{
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   274
    struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   275
    struct irc_net_info net_info;
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   276
    struct irc_net *net;
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   277
    struct error_info err;
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   278
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   279
    // init net_info
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   280
    memset(&net_info, 0, sizeof(net_info));
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   281
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   282
    // the network name
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   283
    net_info.network = luaL_checkstring(L, 2);
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   284
    
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   285
    // the hostname
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   286
    net_info.hostname = luaL_checkstring(L, 3);
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   287
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   288
    // service remains default
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   289
    net_info.service = "6667";
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   290
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   291
    // create it
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   292
    if (irc_client_add_net(lua_client->client, &net, &net_info, &err))
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   293
        return luaL_error(L, "irc_client_add_net: %s/%s: %s", net_info.network, net_info.hostname, error_msg(&err));
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   294
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   295
    // wrap it
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   296
    return lua_net_create(L, net);
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   297
}
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   298
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   299
static int lua_client_get_network (lua_State *L)
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   300
{
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   301
    struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   302
    struct irc_net *net;
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   303
    
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   304
    // the network name
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   305
    const char *network = luaL_checkstring(L, 2);
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   306
    
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   307
    // lookup the irc_net
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   308
    if ((net = irc_client_get_net(lua_client->client, network)) == NULL)
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   309
        return luaL_error(L, "irc_client_get_net: no such network: %s", network);
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   310
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   311
    // wrap it
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   312
    return lua_net_create(L, net);
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   313
}
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   314
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   315
static int lua_client_quit (lua_State *L)
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   316
{
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   317
    struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   318
    err_t err;
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   319
   
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   320
    // the message
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   321
    const char *message = luaL_checkstring(L, 2);
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   322
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   323
    // execute
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   324
    if ((err = irc_client_quit(lua_client->client, message)))
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   325
        return luaL_error(L, "irc_client_quit: %s", error_name(err));
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   326
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   327
    // ok
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   328
    return 0;
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   329
}
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   330
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   331
static const struct luaL_Reg lua_client_methods[] = {
99
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   332
    {   "set_defaults", &lua_client_set_defaults    },
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   333
    {   "connect",      &lua_client_connect         },
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   334
    {   "network",      &lua_client_get_network     },
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   335
    {   "quit",         &lua_client_quit            },
155a6c7d3886 implement lua_client_set_defaults
Tero Marttila <terom@fixme.fi>
parents: 98
diff changeset
   336
    {   NULL,           NULL                        }
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   337
};
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   338
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   339
/**
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   340
 * Initialize the evirc.client type for lua_client, and registers an instance bound to the given irc_client at
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   341
 * 'client'.
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   342
 */
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   343
static void lua_client_init (lua_State *L, struct irc_client *client)
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   344
{
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   345
    // allocate the global "client" object
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   346
    struct lua_client *lua_client = lua_obj_create_global_type(L, "evirc.client", lua_client_methods, "client", sizeof(*lua_client));
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   347
    
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   348
    // initialize it
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   349
    lua_client->client = client;
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   350
}
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   351
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   352
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   353
 * Wrapper for module
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   354
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   355
struct lua_module {
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   356
    struct module *module;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   357
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   358
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   359
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   360
 * Create a lua_module userdata from the given module and push it onto the stack, returning 1.
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   361
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   362
static int lua_module_create (lua_State *L, struct module *module)
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   363
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   364
    // create the new obj
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   365
    struct lua_module *lua_module = lua_obj_create_obj(L, "spbot.module", sizeof(*lua_module));
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   366
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   367
    // initialize
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   368
    lua_module->module = module;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   369
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   370
    // ok
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   371
    return 1;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   372
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   373
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   374
/**
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   375
 * Parse and apply the given config option value as a raw value to the given module's option
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   376
 */
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   377
static int _lua_module_conf_raw_str (lua_State *L, struct lua_module *lua_module, struct nexus *nexus, 
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   378
        const struct config_option *option, const char *conf_value)
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   379
{
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   380
    struct config_value value;
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   381
    struct error_info err;
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   382
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   383
    // mutable version of the conf_value
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   384
    char conf_value_buf[strlen(conf_value) + 1];
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   385
    strcpy(conf_value_buf, conf_value);
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   386
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   387
    // parse it as a raw value
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   388
    if (config_parse(option, nexus, &value, conf_value_buf, &err))
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   389
        return luaL_error(L, "config_parse: %s/%s: %s", option->name, conf_value_buf, error_msg(&err));
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   390
    
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   391
    // apply it while conf_value_buf is still in scope
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   392
    if (module_conf(lua_module->module, option, &value, &err))
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   393
        return luaL_error(L, "module_conf: %s/%s: %s", module_name(lua_module->module), option->name, error_msg(&err));
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   394
    
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   395
    // ok
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   396
    return 0;
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   397
}
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   398
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   399
static int lua_module_conf (lua_State *L)
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   400
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   401
    struct lua_module *lua_module = lua_obj_get_obj(L, __func__, "spbot.module");
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   402
    const struct config_option *option;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   403
    struct config_value value;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   404
    struct error_info err;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   405
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   406
    // XXX: come up with some better way...
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   407
    struct nexus *nexus = lua_module->module->modules->nexus;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   408
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   409
    // the config name
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   410
    const char *conf_name = luaL_checkstring(L, 2);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   411
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   412
    // look it up
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   413
    if ((option = module_conf_lookup(lua_module->module, conf_name, &err)) == NULL)
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   414
        return luaL_error(L, "module_conf_lookup: %s/%s: %s", module_name(lua_module->module), conf_name, error_msg(&err));
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   415
 
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   416
    // the config value
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   417
    switch (lua_type(L, 3)) {
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   418
        case LUA_TSTRING:
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   419
            // parse+apply it as a raw string
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   420
            return _lua_module_conf_raw_str(L, lua_module, nexus, option, lua_tostring(L, 3));
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   421
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   422
        case LUA_TUSERDATA:
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   423
            // some kind of userdata, use its metatable to figure out what type it is
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   424
            if (!lua_getmetatable(L, 3))
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   425
                return luaL_error(L, "config value is userdata without metatable");
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   426
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   427
            // get the target metatable
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   428
            lua_getfield(L, LUA_REGISTRYINDEX, "evirc.chan");
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   429
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   430
            // is it a chan?
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   431
            if (!lua_rawequal(L, -1, -2))
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   432
                return luaL_error(L, "config value is userdata of unknown type");
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   433
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   434
            // pop the metatables
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   435
            lua_pop(L, 2);
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   436
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   437
            // get the irc_chan
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   438
            struct lua_chan *lua_chan = lua_touserdata(L, 3);
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   439
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   440
            // build the value
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   441
            value.type = CONFIG_IRC_CHAN;
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   442
            value.irc_chan = lua_chan->chan;
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   443
            
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   444
            break;
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   445
        
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   446
        default:
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   447
            return luaL_error(L, "config value is of unknown lua type '%s'", lua_typename(L, 3));
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   448
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   449
    }
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   450
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   451
    // apply it
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   452
    if (module_conf(lua_module->module, option, &value, &err))
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   453
        return luaL_error(L, "module_conf: %s/%s: %s", module_name(lua_module->module), option->name, error_msg(&err));
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   454
   
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   455
    // ok
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   456
    return 0;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   457
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   458
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   459
static const struct luaL_Reg lua_module_methods[] = {
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   460
    {   "conf",         &lua_module_conf            },
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   461
    {   NULL,           NULL                        }
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   462
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   463
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   464
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   465
 * Initialize the lua_module object type
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   466
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   467
static void lua_module_init (lua_State *L)
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   468
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   469
    lua_obj_create_type(L, "spbot.module", lua_module_methods);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   470
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   471
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   472
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   473
 * Wrapper for modules
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   474
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   475
struct lua_modules {
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   476
    struct modules *modules;
108
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   477
    
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   478
    // strdup'd path for module_path
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   479
    // XXX: remove when gc'd
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   480
    char *path;
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   481
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   482
108
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   483
static int lua_modules_path (lua_State *L)
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   484
{
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   485
    struct lua_modules *lua_modules = lua_obj_get_obj(L, __func__, "spbot.modules");
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   486
    char *path = NULL;
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   487
    const char *old_path;
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   488
    
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   489
    if (!lua_isnoneornil(L, 2)) {
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   490
        // the new path
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   491
        if ((path = strdup(luaL_checkstring(L, 2))) == NULL)
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   492
            return luaL_error(L, "strdup");
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   493
    }
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   494
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   495
    // set or get
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   496
    old_path = modules_path(lua_modules->modules, path);
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   497
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   498
    // return the old path
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   499
    if (old_path)
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   500
        lua_pushstring(L, old_path);
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   501
    else
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   502
        lua_pushnil(L);
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   503
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   504
    if (path) {
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   505
        // replace the old path
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   506
        free(lua_modules->path); 
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   507
        lua_modules->path = path;
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   508
    }
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   509
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   510
    // ok
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   511
    return 1;
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   512
}
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   513
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   514
static int lua_modules_load (lua_State *L)
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   515
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   516
    struct lua_modules *lua_modules = lua_obj_get_obj(L, __func__, "spbot.modules");
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   517
    struct module *module;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   518
    struct module_info info;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   519
    struct error_info err;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   520
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   521
    // the module name/path
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   522
    info.name = luaL_checkstring(L, 2);
108
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   523
    info.path = lua_isnoneornil(L, 3) ? NULL : luaL_checkstring(L, 3);
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   524
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   525
    // load
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   526
    if (module_load(lua_modules->modules, &module, &info, &err))
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   527
        return luaL_error(L, "module_load: %s/%s: %s", info.name, info.path, error_msg(&err));
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   528
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   529
    // wrap
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   530
    return lua_module_create(L, module);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   531
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   532
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   533
static int lua_modules_module (lua_State *L)
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   534
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   535
    struct lua_modules *lua_modules = lua_obj_get_obj(L, __func__, "spbot.modules");
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   536
    struct module *module;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   537
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   538
    // the module name
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   539
    const char *name = luaL_checkstring(L, 2);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   540
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   541
    // look it up
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   542
    if ((module = module_get(lua_modules->modules, name)) == NULL)
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   543
        return luaL_error(L, "module_get: %s: no such module", name);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   544
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   545
    // wrap
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   546
    return lua_module_create(L, module);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   547
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   548
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   549
static const struct luaL_Reg lua_modules_methods[] = {
108
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   550
    {   "path",         &lua_modules_path           },
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   551
    {   "load",         &lua_modules_load           },
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   552
    {   "module",       &lua_modules_module         },
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   553
    {   NULL,           NULL                        }
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   554
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   555
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   556
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   557
 * Initialize the spbot.modules type for lua_modules, and registers an instance bound to the given modules list at
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   558
 * 'modules'.
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   559
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   560
static void lua_modules_init (lua_State *L, struct modules *modules)
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   561
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   562
    // allocate the global "modules" object
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   563
    struct lua_modules *lua_modules = lua_obj_create_global_type(L, "spbot.modules", lua_modules_methods, "modules", sizeof(*lua_modules));
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   564
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   565
    // initialize it
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   566
    lua_modules->modules = modules;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   567
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   568
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   569
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   570
 * Wrapper for nexus
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   571
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   572
struct lua_nexus {
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   573
    struct nexus *nexus;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   574
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   575
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   576
static int lua_nexus_shutdown (lua_State *L)
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   577
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   578
    struct lua_nexus *lua_nexus = lua_obj_get_obj(L, __func__, "spbot.nexus");
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   579
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   580
    // just shut it down
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   581
    nexus_shutdown(lua_nexus->nexus);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   582
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   583
    return 0;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   584
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   585
109
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   586
static int lua_nexus_load_config (lua_State *L)
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   587
{
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   588
    struct lua_nexus *lua_nexus = lua_obj_get_obj(L, __func__, "spbot.nexus");
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   589
    struct error_info err;
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   590
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   591
    const char *path = luaL_checkstring(L, 2);
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   592
    
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   593
    // just load it
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   594
    if (nexus_load_config(lua_nexus->nexus, path, &err))
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   595
        return luaL_error(L, "nexus_load_config(%s): %s", path, error_msg(&err));
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   596
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   597
    return 0;
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   598
}
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   599
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   600
static const struct luaL_Reg lua_nexus_methods[] = {
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   601
    {   "shutdown",     &lua_nexus_shutdown         },
109
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   602
    {   "load_config",  &lua_nexus_load_config      },
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   603
    {   NULL,           NULL                        }
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   604
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   605
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   606
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   607
 * Initialize the spbot.nexus type for lua_nexus, and registers an instance bound to the given nexus list at
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   608
 * 'nexus'.
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   609
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   610
static void lua_nexus_init (lua_State *L, struct nexus *nexus)
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   611
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   612
    // allocate the global "nexus" object
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   613
    struct lua_nexus *lua_nexus = lua_obj_create_global_type(L, "spbot.nexus", lua_nexus_methods, "nexus", sizeof(*lua_nexus));
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   614
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   615
    // initialize it
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   616
    lua_nexus->nexus = nexus;
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   617
}
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   618
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   619
/**
104
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   620
 * Global functions
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   621
 */
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   622
static int lua_log_level (lua_State *L)
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   623
{
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   624
    // log level as a string
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   625
    enum log_level new_level = luaL_checkoption(L, 1, NULL, log_level_names);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   626
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   627
    // set it
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   628
    set_log_level(new_level);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   629
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   630
    // ok
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   631
    return 0;
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   632
}
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   633
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   634
static int lua_log (lua_State *L)
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   635
{
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   636
    // log level as a string
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   637
    enum log_level level = luaL_checkoption(L, 1, NULL, log_level_names);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   638
    
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   639
    // log message
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   640
    const char *msg = luaL_checkstring(L, 2);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   641
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   642
    // log it
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   643
    log_msg(level, "lua", "%s", msg);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   644
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   645
    // ok
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   646
    return 0;
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   647
}
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   648
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   649
static const struct luaL_Reg lua_global_functions[] = {
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   650
    {   "log_level",    &lua_log_level              },
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   651
    {   "log",          &lua_log                    },
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   652
    {   NULL,           NULL                        }
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   653
};
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   654
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   655
static void lua_global_init (lua_State *L)
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   656
{
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   657
    const struct luaL_Reg *reg;
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   658
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   659
    for (reg = lua_global_functions; reg->name && reg->func; reg++) {
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   660
        // put the function on the stack
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   661
        lua_pushcfunction(L, reg->func);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   662
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   663
        // set the global
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   664
        lua_setglobal(L, reg->name);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   665
    }
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   666
}
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   667
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   668
/**
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   669
 * Set up the lua state in protected mode
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   670
 */
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   671
static int _lua_objs_init (lua_State *L)
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   672
{
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   673
    struct nexus *nexus;
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   674
    
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   675
    // read the nexus off the stack
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   676
    if ((nexus = lua_touserdata(L, 1)) == NULL)
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   677
        luaL_error(L, "lua_touserdata: NULL");
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   678
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   679
    // init the various bits
104
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   680
    lua_global_init(L);
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   681
    lua_nexus_init(L, nexus);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   682
    lua_modules_init(L, nexus->modules);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   683
    lua_module_init(L);
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   684
    lua_client_init(L, nexus->client);
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   685
    lua_net_init(L);
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
   686
    lua_chan_init(L);
94
05a96b200d7b wrap irc_net and add lua_net_join
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
   687
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   688
    // nothing
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   689
    return 0;
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   690
}
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   691
106
f00661136ac2 add nexus_lua_error for unified LUA_ERR* -> ERR_LUA_* mapping, and lua configuration support
Tero Marttila <terom@fixme.fi>
parents: 105
diff changeset
   692
err_t lua_objs_init (struct nexus_lua *lua, struct error_info *err)
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   693
{
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   694
    // call in protected mode
106
f00661136ac2 add nexus_lua_error for unified LUA_ERR* -> ERR_LUA_* mapping, and lua configuration support
Tero Marttila <terom@fixme.fi>
parents: 105
diff changeset
   695
    return nexus_lua_error(lua->st, lua_cpcall(lua->st, &_lua_objs_init, lua->nexus), err);
98
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   696
}
f357f835f0d5 add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
   697