src/lua_objs.c
author Tero Marttila <terom@fixme.fi>
Wed, 08 Apr 2009 01:28:46 +0300
changeset 120 576bab0a1c5a
parent 116 92e71129074d
child 122 52ffbdb6bba1
permissions -rw-r--r--
modify config to support options with multiple params/values
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"
104
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
     3
#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
     4
108
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
     5
#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
     6
#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
     7
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
     8
void lua_obj_create_type (lua_State *L, const char *name, const struct luaL_Reg methods[])
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
     9
{
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    10
    luaL_newmetatable(L, name);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    11
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    12
    // set the metatable __index to itself
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    13
    lua_pushvalue(L, -1);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    14
    lua_setfield(L, -1, "__index");
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
    // register the methods to the metatable
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    17
    luaL_register(L, NULL, methods);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    18
}
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    19
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    20
void* lua_obj_create_obj (lua_State *L, const char *name, size_t size)
96
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
    // 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
    23
    void *ud = lua_newuserdata(L, size);
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
    // get the type and set it
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    26
    luaL_getmetatable(L, name);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    27
    lua_setmetatable(L, -2);
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    28
    
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    29
    // ok
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    30
    return ud;
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
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    33
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)
103
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
    // allocate the global object
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    36
    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
    37
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    38
    // create the type metatable
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    39
    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
    40
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    41
    // 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
    42
    lua_setmetatable(L, -2);
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    43
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    44
    // 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
    45
    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
    46
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    47
    // ok
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    48
    return obj;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    49
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    50
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    51
void* lua_obj_get_obj (lua_State *L, const char *func, const char *name)
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    52
{
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    53
    void *ud;
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    54
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    55
    // validate the userdata arg
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    56
    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
    57
        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
    58
        
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    59
        // 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
    60
        return NULL;
96
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    61
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    62
    } else {
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    63
        // ok
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    64
        return ud;
a07d917adec1 add irc_chan and unify type/obj creation
Tero Marttila <terom@fixme.fi>
parents: 94
diff changeset
    65
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
}
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
/**
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    70
 * Wrapper for module
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    71
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    72
struct lua_module {
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    73
    struct module *module;
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
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    76
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    77
 * 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
    78
 *
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    79
 * 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
    80
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    81
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
    82
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    83
    // create the new obj
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    84
    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
    85
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    86
    // initialize
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    87
    lua_module->module = module;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    88
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    89
    // ok
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    90
    return 1;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    91
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
    92
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
    93
/**
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    94
 * 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
    95
 */
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    96
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
    97
{
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    98
    struct lua_module *lua_module = lua_obj_get_obj(L, __func__, "spbot.module");
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
    99
    
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   100
    // put it
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   101
    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
   102
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   103
    return 0;
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   104
}
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   105
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   106
/**
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   107
 * 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
   108
 */
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   109
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
   110
        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
   111
{
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   112
    struct config_value value;
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   113
    struct error_info err;
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   114
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   115
    // mutable version of the conf_value
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   116
    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
   117
    strcpy(conf_value_buf, conf_value);
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   118
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   119
    // parse it as a raw value
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   120
    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
   121
        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
   122
    
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   123
    // 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
   124
    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
   125
        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
   126
    
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   127
    // ok
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   128
    return 0;
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   129
}
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   130
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   131
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
   132
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   133
    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
   134
    const struct config_option *option;
120
576bab0a1c5a modify config to support options with multiple params/values
Tero Marttila <terom@fixme.fi>
parents: 116
diff changeset
   135
    struct config_value values[2] = {
576bab0a1c5a modify config to support options with multiple params/values
Tero Marttila <terom@fixme.fi>
parents: 116
diff changeset
   136
        CONFIG_VALUE_END,
576bab0a1c5a modify config to support options with multiple params/values
Tero Marttila <terom@fixme.fi>
parents: 116
diff changeset
   137
        CONFIG_VALUE_END
576bab0a1c5a modify config to support options with multiple params/values
Tero Marttila <terom@fixme.fi>
parents: 116
diff changeset
   138
    }, *value = &values[0];
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   139
    struct error_info err;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   140
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   141
    // 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
   142
    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
   143
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   144
    // the config name
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   145
    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
   146
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   147
    // look it up
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   148
    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
   149
        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
   150
 
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   151
    // the config value
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   152
    switch (lua_type(L, 3)) {
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   153
        case LUA_TSTRING:
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   154
            // 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
   155
            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
   156
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   157
        case LUA_TUSERDATA:
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   158
            // 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
   159
            if (!lua_getmetatable(L, 3))
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   160
                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
   161
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   162
            // get the target metatable
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   163
            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
   164
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   165
            // is it a chan?
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   166
            if (!lua_rawequal(L, -1, -2))
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   167
                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
   168
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   169
            // pop the metatables
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   170
            lua_pop(L, 2);
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   171
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   172
            // get the irc_chan
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   173
            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
   174
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   175
            // build the value
120
576bab0a1c5a modify config to support options with multiple params/values
Tero Marttila <terom@fixme.fi>
parents: 116
diff changeset
   176
            value->type = CONFIG_IRC_CHAN;
576bab0a1c5a modify config to support options with multiple params/values
Tero Marttila <terom@fixme.fi>
parents: 116
diff changeset
   177
            value->irc_chan = lua_chan->chan;
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   178
            
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   179
            break;
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   180
        
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   181
        default:
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   182
            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
   183
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   184
    }
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   185
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   186
    // apply it
120
576bab0a1c5a modify config to support options with multiple params/values
Tero Marttila <terom@fixme.fi>
parents: 116
diff changeset
   187
    if (module_conf(lua_module->module, option, values, &err))
107
5c1eeb45c7f2 support irc_chan type for lua_module_conf
Tero Marttila <terom@fixme.fi>
parents: 106
diff changeset
   188
        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
   189
   
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   190
    // ok
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   191
    return 0;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   192
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   193
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   194
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
   195
{
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   196
    struct lua_module *lua_module = lua_obj_get_obj(L, __func__, "spbot.module");
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   197
    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
   198
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   199
    // just unload it
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   200
    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
   201
        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
   202
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   203
    // ok
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   204
    return 0;
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   205
}
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   206
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   207
static const struct luaL_Reg lua_module_methods[] = {
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   208
    {   "__gc",         &lua_module__gc             },
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   209
    {   "conf",         &lua_module_conf            },
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   210
    {   "unload",       &lua_module_unload          },
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   211
    {   NULL,           NULL                        }
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   212
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   213
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   214
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   215
 * 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
   216
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   217
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
   218
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   219
    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
   220
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   221
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   222
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   223
 * Wrapper for modules
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   224
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   225
struct lua_modules {
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   226
    struct modules *modules;
108
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
    // strdup'd path for module_path
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   229
    // XXX: remove when gc'd
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   230
    char *path;
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   231
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   232
114
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   233
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
   234
{
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   235
    struct lua_modules *lua_modules = lua_obj_get_obj(L, __func__, "spbot.modules");
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   236
    
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   237
    // 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
   238
    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
   239
        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
   240
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   241
    // 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
   242
    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
   243
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   244
    // ok
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   245
    return 0;
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   246
}
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   247
108
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   248
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
   249
{
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   250
    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
   251
    char *path = NULL;
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   252
    const char *old_path;
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   253
    
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   254
    if (!lua_isnoneornil(L, 2)) {
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   255
        // the new path
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   256
        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
   257
            return luaL_error(L, "strdup");
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   258
    }
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   259
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   260
    // set or get
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   261
    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
   262
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   263
    // return the old path
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   264
    if (old_path)
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   265
        lua_pushstring(L, old_path);
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   266
    else
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   267
        lua_pushnil(L);
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   268
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   269
    if (path) {
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   270
        // replace the old path
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   271
        free(lua_modules->path); 
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   272
        lua_modules->path = path;
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   273
    }
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   274
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   275
    // ok
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   276
    return 1;
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   277
}
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   278
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   279
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
   280
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   281
    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
   282
    struct module *module;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   283
    struct module_info info;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   284
    struct error_info err;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   285
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   286
    // the module name/path
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   287
    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
   288
    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
   289
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   290
    // 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
   291
    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
   292
        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
   293
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   294
    // wrap
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   295
    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
   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
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
   299
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   300
    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
   301
    struct module *module;
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
    // the module name
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   304
    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
   305
113
477d1cb3d87c implement lua_module_unload and lua_module_gc to release references
Tero Marttila <terom@fixme.fi>
parents: 109
diff changeset
   306
    // look it up, as a new reference
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   307
    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
   308
        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
   309
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   310
    // wrap
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   311
    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
   312
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   313
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   314
static const struct luaL_Reg lua_modules_methods[] = {
114
6de0490408f4 implement lua_modules__gc in case the modules global gets lost
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   315
    {   "__gc",         &lua_modules__gc            },
108
50ff7ac8a725 implement modules_path + module_load with NULL path
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   316
    {   "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
   317
    {   "load",         &lua_modules_load           },
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   318
    {   "module",       &lua_modules_module         },
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   319
    {   NULL,           NULL                        }
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   320
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   321
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
 * 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
   324
 * 'modules'.
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
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
   327
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   328
    // 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
   329
    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
   330
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   331
    // initialize it
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   332
    lua_modules->modules = modules;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   333
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   334
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   335
/**
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   336
 * Wrapper for nexus
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   337
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   338
struct lua_nexus {
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   339
    struct nexus *nexus;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   340
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   341
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   342
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
   343
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   344
    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
   345
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   346
    // just shut it down
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   347
    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
   348
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   349
    return 0;
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   350
}
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   351
109
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   352
static int lua_nexus_load_config (lua_State *L)
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   353
{
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   354
    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
   355
    struct error_info err;
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   356
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   357
    const char *path = luaL_checkstring(L, 2);
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   358
    
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   359
    // just load it
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   360
    if (nexus_load_config(lua_nexus->nexus, path, &err))
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   361
        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
   362
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   363
    return 0;
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   364
}
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   365
103
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   366
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
   367
    {   "shutdown",     &lua_nexus_shutdown         },
109
bfe9b9a8fe5b fix some more valgrind errors
Tero Marttila <terom@fixme.fi>
parents: 108
diff changeset
   368
    {   "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
   369
    {   NULL,           NULL                        }
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   370
};
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   371
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
 * 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
   374
 * 'nexus'.
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   375
 */
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   376
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
   377
{
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   378
    // 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
   379
    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
   380
    
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   381
    // initialize it
454aea1e4f11 implement nexus_shutdown, lua_modules/lua_module, and lua_nexus
Tero Marttila <terom@fixme.fi>
parents: 99
diff changeset
   382
    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
   383
}
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
   384
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
   385
/**
104
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   386
 * Global functions
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   387
 */
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   388
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
   389
{
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   390
    // log level as a string
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   391
    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
   392
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   393
    // set it
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   394
    set_log_level(new_level);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   395
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   396
    // ok
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   397
    return 0;
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   398
}
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   399
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   400
static int lua_log (lua_State *L)
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   401
{
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   402
    // log level as a string
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   403
    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
   404
    
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   405
    // log message
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   406
    const char *msg = luaL_checkstring(L, 2);
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
    // log it
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   409
    log_msg(level, "lua", "%s", msg);
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 const struct luaL_Reg lua_global_functions[] = {
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   416
    {   "log_level",    &lua_log_level              },
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   417
    {   "log",          &lua_log                    },
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   418
    {   NULL,           NULL                        }
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
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   421
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
   422
{
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   423
    const struct luaL_Reg *reg;
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   424
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   425
    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
   426
        // put the function on the stack
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   427
        lua_pushcfunction(L, reg->func);
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
        // set the global
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   430
        lua_setglobal(L, reg->name);
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   431
    }
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   432
}
fc196bb4bcc2 implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents: 103
diff changeset
   433
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
   434
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
   435
{
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
   436
    // init the various bits
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
   437
    lua_global_init(lua->st);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
   438
    lua_nexus_init(lua->st, lua->nexus);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
   439
    lua_modules_init(lua->st, lua->nexus->modules);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
   440
    lua_module_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
   441
}
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
   442