src/lua_objs.c
author Tero Marttila <terom@fixme.fi>
Thu, 21 May 2009 16:57:00 +0300
changeset 211 b460d958a685
parent 207 3fa22abb5421
permissions -rw-r--r--
fix lua_nexus_sleep to use lua_thread_yield_state
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"
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
     2
#include "lua_irc.h"
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
     3
#include "lua_func.h"
211
b460d958a685 fix lua_nexus_sleep to use lua_thread_yield_state
Tero Marttila <terom@fixme.fi>
parents: 207
diff changeset
     4
#include "lua_thread.h"
104
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
     5
#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
     6
108
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
     7
#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
     8
#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
     9
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    10
/**
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    11
 * Wrapper for module
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    12
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    13
struct lua_module {
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    14
    struct module *module;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    15
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    16
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
    17
static struct lua_type lua_module_type = LUA_TYPE("spbot.module");
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
    18
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    19
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    20
 * Create a lua_module userdata from the given module and push it onto the stack, returning 1.
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    21
 *
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    22
 * The given module should be a reference of its own right.
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    23
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    24
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
    25
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    26
    // create the new obj
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
    27
    struct lua_module *lua_module = lua_type_create(L, &lua_module_type, sizeof(*lua_module));
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    28
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    29
    // initialize
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    30
    lua_module->module = module;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    31
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    32
    // ok
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    33
    return 1;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    34
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    35
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
    36
/**
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    37
 * module_put() our module reference
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    38
 */
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    39
static int lua_module__gc (lua_State *L)
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    40
{
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
    41
    struct lua_module *lua_module = lua_type_get(L, &lua_module_type, 1);
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    42
    
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    43
    // put it
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    44
    module_put(lua_module->module);
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    45
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    46
    return 0;
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    47
}
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    48
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    49
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
    50
{
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
    51
    struct lua_module *lua_module = lua_type_get(L, &lua_module_type, 1);
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    52
    const struct config_option *option;
124
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    53
    struct error_info err;
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    54
    bool is_err = true;
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    55
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    56
    // the list of given config values, and temporary storage for string values
122
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    57
    struct config_value values[CONFIG_VALUES_MAX], *value = values;
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    58
    char *value_bufs[CONFIG_VALUES_MAX], **value_buf = value_bufs;
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    59
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    60
    // number of arguments given
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    61
    int nargs = lua_gettop(L), argidx = 2;
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    62
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    63
    // init to zero
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    64
    memset(values, 0, sizeof(values));
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    65
    memset(value_bufs, 0, sizeof(value_bufs));
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    66
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    67
    // 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
    68
    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
    69
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    70
    // the config name
122
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    71
    const char *conf_name = luaL_checkstring(L, argidx++);
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    72
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    73
    // look it up
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    74
    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
    75
        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
    76
122
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    77
    // maximum number of arguments accepted
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    78
    int maxargs = config_params_count(option);
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
    79
122
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    80
    // too many arguments?
124
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    81
    if (nargs - argidx > maxargs)
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    82
        return luaL_error(L, "lua_module_conf: too many arguments (>%d) given (%d)", maxargs, nargs - argidx);
122
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    83
    
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    84
    // the current param
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    85
    const struct config_param *param = option->params;
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
    86
124
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    87
    // apply each given argument to the correct param, storing it in value
122
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    88
    for (; argidx <= nargs; argidx++, value++, param++) {
124
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    89
        // the given config value
122
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    90
        switch (lua_type(L, argidx)) {
124
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    91
            case LUA_TNONE:
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    92
            case LUA_TNIL:
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    93
                // no value
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    94
                value->type = CONFIG_NULL;
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    95
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    96
                break;
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 122
diff changeset
    97
122
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    98
            case LUA_TSTRING: {
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
    99
                // string arg
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   100
                const char *arg_str = lua_tostring(L, argidx);
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   101
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   102
                // copy it as a mutable string buffer
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   103
                if ((*value_buf = strdup(arg_str)) == NULL) {
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   104
                    lua_pushfstring(L, "strdup");
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   105
                    goto error;
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   106
                }
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   107
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   108
                // parse it as a raw value
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   109
                if (config_parse_param(param, nexus, value, *value_buf, &err)) {
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   110
                    lua_pushfstring(L, "config_parse: %s/%s: %s", option->name, *value_buf, error_msg(&err));
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   111
                    goto error;
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   112
                }
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   113
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   114
                // seek to next value_buf
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   115
                value_buf++;
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   116
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   117
            } break;
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   118
    
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   119
            case LUA_TUSERDATA:
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   120
                // some kind of userdata, use its metatable to figure out what type it is
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   121
                if (!lua_getmetatable(L, argidx)) {
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   122
                    lua_pushfstring(L, "config value is userdata without metatable");
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   123
                    goto error;
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   124
                }
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   125
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   126
                // get the target metatable
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   127
                lua_getfield(L, LUA_REGISTRYINDEX, "evirc.chan");
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   128
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   129
                // is it a chan?
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   130
                if (!lua_rawequal(L, -1, -2)) {
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   131
                    lua_pushfstring(L, "config value is userdata of unknown type");
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   132
                    goto error;
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   133
                }
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   134
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   135
                // pop the metatables
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   136
                lua_pop(L, 2);
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   137
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   138
                // get the irc_chan
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   139
                struct lua_chan *lua_chan = lua_touserdata(L, argidx);
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   140
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   141
                // build the value
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   142
                value->type = CONFIG_IRC_CHAN;
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   143
                value->irc_chan = lua_chan->chan;
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   144
                
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   145
                break;
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   146
            
122
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   147
            default:
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   148
                lua_pushfstring(L, "config value is of unknown lua type '%s'", lua_typename(L, argidx));
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   149
                goto error;
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   150
122
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   151
        }
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   152
    }
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   153
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   154
    // apply it
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   155
    if (module_conf(lua_module->module, option, values, &err)) {
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   156
        lua_pushfstring(L, "module_conf: %s/%s: %s", module_name(lua_module->module), option->name, error_msg(&err));
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   157
        goto error;
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   158
    }
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   159
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   160
    // ok
122
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   161
    is_err = false;
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   162
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   163
error:
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   164
    // release any allocated strings
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   165
    for (value_buf = value_bufs; value_buf <= value_bufs + CONFIG_VALUES_MAX && *value_buf; value_buf++)
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   166
        free(*value_buf);
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   167
    
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   168
    // either error or successful return
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   169
    if (is_err)
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   170
        return lua_error(L);
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   171
    else
52ffbdb6bba1 start implementing multiple args for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 120
diff changeset
   172
        return 0;
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   173
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   174
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   175
static int lua_module_unload (lua_State *L)
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   176
{
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   177
    struct lua_module *lua_module = lua_type_get(L, &lua_module_type, 1);
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   178
    struct error_info err;
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   179
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   180
    // just unload it
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   181
    if ((ERROR_CODE(&err) = module_unload(lua_module->module)))
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   182
        return luaL_error(L, "module_unload: %s: %s", module_name(lua_module->module), error_msg(&err));
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   183
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   184
    // ok
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   185
    return 0;
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   186
}
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   187
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   188
static struct lua_method lua_module_methods[] = LUA_METHODS(
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   189
        LUA_METHOD("__gc",      lua_module__gc,     NULL    ),
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   190
        LUA_METHOD("conf",      lua_module_conf,    NULL    ),
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   191
        LUA_METHOD("unload",    lua_module_unload,  NULL    )
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   192
);
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   193
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   194
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   195
 * Wrapper for modules
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   196
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   197
struct lua_modules {
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   198
    struct modules *modules;
108
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   199
    
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   200
    // strdup'd path for module_path
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   201
    // XXX: remove when gc'd
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   202
    char *path;
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   203
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   204
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   205
static struct lua_type lua_modules_type = LUA_TYPE("spbot.modules");
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   206
114
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   207
static int lua_modules__gc (lua_State *L)
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   208
{
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   209
    struct lua_modules *lua_modules = lua_type_get(L, &lua_modules_type, 1);
114
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   210
    
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   211
    // remove the modules path if it was set by us
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   212
    if (lua_modules->path && modules_path(lua_modules->modules, NULL) == lua_modules->path)
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   213
        modules_path(lua_modules->modules, "");
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   214
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   215
    // release any strdup'd path
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   216
    free(lua_modules->path);
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   217
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   218
    // ok
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   219
    return 0;
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   220
}
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   221
108
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   222
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
   223
{
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   224
    struct lua_modules *lua_modules = lua_type_get(L, &lua_modules_type, 1);
108
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   225
    char *path = NULL;
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   226
    const char *old_path;
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   227
    
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   228
    if (!lua_isnoneornil(L, 2)) {
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   229
        // the new path
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   230
        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
   231
            return luaL_error(L, "strdup");
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   232
    }
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   233
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   234
    // set or get
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   235
    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
   236
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   237
    // return the old path
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   238
    if (old_path)
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   239
        lua_pushstring(L, old_path);
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   240
    else
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   241
        lua_pushnil(L);
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   242
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   243
    if (path) {
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   244
        // replace the old path
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   245
        free(lua_modules->path); 
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   246
        lua_modules->path = path;
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   247
    }
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   248
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   249
    // ok
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   250
    return 1;
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   251
}
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   252
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   253
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
   254
{
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   255
    struct lua_modules *lua_modules = lua_type_get(L, &lua_modules_type, 1);
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   256
    struct module *module;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   257
    struct module_info info;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   258
    struct error_info err;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   259
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   260
    // the module name/path
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   261
    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
   262
    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
   263
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   264
    // load and get a new reference
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   265
    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
   266
        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
   267
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   268
    // wrap
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   269
    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
   270
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   271
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   272
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
   273
{
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   274
    struct lua_modules *lua_modules = lua_type_get(L, &lua_modules_type, 1);
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   275
    struct module *module;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   276
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   277
    // the module name
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   278
    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
   279
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   280
    // look it up, as a new reference
134
978041c1c04d update TODO, partially update error.c, rename module_get to modules_get, update config.lua
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   281
    if ((module = modules_get(lua_modules->modules, name)) == NULL)
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   282
        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
   283
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   284
    // wrap
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   285
    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
   286
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   287
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   288
static struct lua_method lua_modules_methods[] = LUA_METHODS(
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   289
        LUA_METHOD("__gc",      lua_modules__gc,    NULL    ),
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   290
        LUA_METHOD("path",      lua_modules_path,   NULL    ),
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   291
        LUA_METHOD("load",      lua_modules_load,   NULL    ),
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   292
        LUA_METHOD("module",    lua_modules_module, NULL    )
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   293
    );
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   294
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   295
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   296
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   297
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   298
 * 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
   299
 * 'modules'.
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   300
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   301
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
   302
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   303
    // allocate the global "modules" object
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   304
    struct lua_modules *lua_modules = lua_type_register_global(L, &lua_modules_type, lua_modules_methods, "modules", sizeof(*lua_modules));
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   305
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   306
    // initialize it
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   307
    lua_modules->modules = modules;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   308
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   309
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   310
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   311
 * Wrapper for nexus
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   312
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   313
struct lua_nexus {
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   314
    struct nexus *nexus;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   315
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   316
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   317
static struct lua_type lua_nexus_type = LUA_TYPE("spbot.nexus");
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   318
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   319
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
   320
{
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   321
    struct lua_nexus *lua_nexus = lua_type_get(L, &lua_nexus_type, 1);
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   322
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   323
    // just shut it down
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   324
    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
   325
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   326
    return 0;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   327
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   328
109
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   329
static int lua_nexus_load_config (lua_State *L)
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   330
{
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   331
    struct lua_nexus *lua_nexus = lua_type_get(L, &lua_nexus_type, 1);
109
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   332
    struct error_info err;
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   333
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   334
    const char *path = luaL_checkstring(L, 2);
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   335
    
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   336
    // just load it
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   337
    if (nexus_load_config(lua_nexus->nexus, path, &err))
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   338
        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
   339
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   340
    return 0;
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   341
}
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   342
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   343
static struct lua_func lua_nexus_sleep_func = LUA_FUNC(&lua_nexus_type, "sleep",
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   344
        "Schedules itself to resume after the given delay (in seconds) and yields",
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   345
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   346
        LUA_FUNC_ARG_INT("tv_sec",  LUA_ARG_REQUIRED)
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   347
    );
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   348
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   349
static void lua_nexus_sleep_wakeup (evutil_socket_t fd, short what, void *arg)
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   350
{
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   351
    lua_State *L = arg;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   352
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   353
    (void) fd;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   354
    (void) what;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   355
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   356
    // resume the thread that called lua_nexus_sleep
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   357
    lua_thread_resume_state(L);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   358
}
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   359
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   360
static int lua_nexus_sleep (lua_State *L)
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   361
{
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   362
    struct lua_nexus *lua_nexus;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   363
    long tv_sec;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   364
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   365
    // parse args
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   366
    lua_args_parse(L, &lua_nexus_sleep_func, (void *) &lua_nexus, &tv_sec);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   367
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   368
    // build tv
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   369
    struct timeval tv = { tv_sec, 0 };
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   370
    
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   371
    // schedule wakeup
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   372
    // use a pure-timeout event
207
3fa22abb5421 fix lua_nexus_sleep to use EV_TIMEOUT + misc
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   373
    if (event_base_once(lua_nexus->nexus->ev_base, -1, EV_TIMEOUT, lua_nexus_sleep_wakeup, L, &tv))
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   374
        return luaL_error(L, "event_base_once");
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   375
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   376
    // yield
211
b460d958a685 fix lua_nexus_sleep to use lua_thread_yield_state
Tero Marttila <terom@fixme.fi>
parents: 207
diff changeset
   377
    return lua_thread_yield_state(L);
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   378
}
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   379
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   380
static struct lua_method lua_nexus_methods[] = LUA_METHODS(
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   381
        LUA_METHOD("shutdown",      lua_nexus_shutdown,     NULL                    ),
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   382
        LUA_METHOD("load_config",   lua_nexus_load_config,  NULL                    ),
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   383
        LUA_METHOD("sleep",         lua_nexus_sleep,        &lua_nexus_sleep_func   )
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   384
    );
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   385
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   386
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   387
 * 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
   388
 * 'nexus'.
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   389
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   390
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
   391
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   392
    // allocate the global "nexus" object
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   393
    struct lua_nexus *lua_nexus = lua_type_register_global(L, &lua_nexus_type, lua_nexus_methods, "nexus", sizeof(*lua_nexus));
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   394
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   395
    // initialize it
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   396
    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
   397
}
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
   398
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   399
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
   400
/**
104
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   401
 * Global functions
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   402
 */
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   403
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
   404
{
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   405
    // log level as a string
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   406
    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
   407
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   408
    // set it
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   409
    set_log_level(new_level);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   410
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   411
    // ok
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   412
    return 0;
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   413
}
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   414
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   415
static int lua_log (lua_State *L)
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   416
{
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   417
    // log level as a string
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   418
    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
   419
    
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   420
    // log message
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   421
    const char *msg = luaL_checkstring(L, 2);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   422
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   423
    // log it
195
42aedce3e2eb rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents: 145
diff changeset
   424
    _log_msg(level, "lua", "%s", msg);
104
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   425
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   426
    // ok
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   427
    return 0;
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   428
}
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   429
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   430
static const struct luaL_Reg lua_global_functions[] = {
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   431
    {   "log_level",    lua_log_level               },
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents: 195
diff changeset
   432
    {   "log",          lua_log                     },
104
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   433
    {   NULL,           NULL                        }
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   434
};
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   435
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   436
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
   437
{
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   438
    const struct luaL_Reg *reg;
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   439
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   440
    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
   441
        // put the function on the stack
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   442
        lua_pushcfunction(L, reg->func);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   443
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   444
        // set the global
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   445
        lua_setglobal(L, reg->name);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   446
    }
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   447
}
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   448
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
   449
void lua_objs_init (struct nexus_lua *lua)
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
   450
{
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   451
    // register types
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   452
    lua_type_register(lua->st, &lua_module_type, lua_module_methods);
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   453
    
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   454
    // globals
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
   455
    lua_nexus_init(lua->st, lua->nexus);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
   456
    lua_modules_init(lua->st, lua->nexus->modules);
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   457
    
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   458
    // global functions
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   459
    lua_global_init(lua->st);
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
   460
}
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
   461
145
a5582e1a83da implement lua_type
Tero Marttila <terom@fixme.fi>
parents: 143
diff changeset
   462