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