src/lua_irc.c
author Tero Marttila <terom@fixme.fi>
Sun, 19 Apr 2009 06:20:59 +0300
changeset 143 1edab39c88a8
parent 142 dc2bb09d412c
child 145 a5582e1a83da
permissions -rw-r--r--
implement lua_args_parse
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "lua_irc.h"
143
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
     2
#include "lua_func.h"
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
#include <stdlib.h>
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include <string.h>
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
/**
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
 * Create a lua_chan userdata from the given irc_chan and leave it on the stack, returning 1
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
static int lua_chan_create (lua_State *L, struct irc_chan *chan)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    // create the new obj
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    struct lua_chan *lua_chan = lua_obj_create_obj(L, "evirc.chan", sizeof(*lua_chan));
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    // initialize
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    lua_chan->chan = chan;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    // ok
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    return 1;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
/**
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
 * Return the channel name as a lua string
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
static int lua_chan_tostring (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    struct lua_chan *lua_chan = lua_obj_get_obj(L, __func__, "evirc.chan");
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
    lua_pushfstring(L, "<irc_chan %s>", irc_chan_name(lua_chan->chan));
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    return 1;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
/**
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
 * Send a PRIVMSG to the channel
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
static int lua_chan_say (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
    err_t err;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
    struct lua_chan *lua_chan = lua_obj_get_obj(L, __func__, "evirc.chan");
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
    // the message
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
    const char *message = luaL_checkstring(L, 2);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    // send
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
    if ((err = irc_chan_PRIVMSG(lua_chan->chan, message)))
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
        return luaL_error(L, "irc_chan_PRIVMSG: '%s': %s", message, error_name(err));
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
    // ok
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
    return 0;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
static const struct luaL_Reg lua_chan_methods[] = {
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
    {   "__tostring",       &lua_chan_tostring  },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
    {   "say",              &lua_chan_say       },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
    {   NULL,               NULL                },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
};
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
/**
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
 * Initialize the lua_chan object type
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
static void lua_chan_init (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
    lua_obj_create_type(L, "evirc.chan", lua_chan_methods);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
/**
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
 * Create a lua_net userdata from the given irc_net and push it onto the stack, returning 1.
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
static int lua_net_create (lua_State *L, struct irc_net *net)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
    // create the new obj
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    struct lua_net *lua_net = lua_obj_create_obj(L, "evirc.net", sizeof(*lua_net));
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    // initialize
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
    lua_net->net = net;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
    // ok
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
    return 1;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
/**
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
 * Return the network name as a lua string
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
static int lua_net_tostring (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
    struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
    lua_pushfstring(L, "<irc_net %s>", irc_net_name(lua_net->net));
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
    return 1;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
/**
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
 * Join a new channel, returning the lua_chan
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
static int lua_net_join (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
    struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
    struct irc_chan_info chan_info;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
    struct irc_chan *chan;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
    struct error_info err;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
    // the channel name
131
df949b399491 lua_irc is broken, as irc_chan_info::channel is set to a lua-stack allocated string...
Tero Marttila <terom@fixme.fi>
parents: 116
diff changeset
   106
    // XXX: bad! bad! bad!
df949b399491 lua_irc is broken, as irc_chan_info::channel is set to a lua-stack allocated string...
Tero Marttila <terom@fixme.fi>
parents: 116
diff changeset
   107
    chan_info.channel = strdup(luaL_checkstring(L, 2));
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
    // add it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
    if (irc_net_add_chan(lua_net->net, &chan, &chan_info, &err))
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
        return luaL_error(L, "irc_net_add_chan: %s: %s", chan_info.channel, error_msg(&err));
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
    // return it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
    return lua_chan_create(L, chan);    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
/**
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
 * Look up a channel by name, returning the lua_chan
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
static int lua_net_get_chan (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
    struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
    struct irc_chan *chan;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
    // the channel name
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
    const char *channel = luaL_checkstring(L, 2);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
    // lookup the irc_chan
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
    if ((chan = irc_net_get_chan(lua_net->net, channel)) == NULL)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
        return luaL_error(L, "irc_net_get_chan: no such channel: %s", channel);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
    // wrap it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
    return lua_chan_create(L, chan);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
static const struct luaL_Reg lua_net_methods[] = {
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
    {   "__tostring",   &lua_net_tostring   },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
    {   "join",         &lua_net_join       },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
    {   "channel",      &lua_net_get_chan   },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
    {   NULL,           NULL                }
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   141
};
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
/**
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
 * Initialize the lua_net object type
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   146
static void lua_net_init (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148
    lua_obj_create_type(L, "evirc.net", lua_net_methods);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
143
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   151
static struct lua_func lua_client_set_defaults_func = LUA_FUNC("evirc.client", "set_defaults", 
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   152
        "set the default settings to use for evirc.client.connect",
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   153
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   154
        LUA_FUNC_ARG_STRING("nickname",     LUA_ARG_STRING_REQUIRED ),
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   155
        LUA_FUNC_ARG_STRING("username",     LUA_ARG_STRING_REQUIRED ),
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   156
        LUA_FUNC_ARG_STRING("realname",     LUA_ARG_STRING_REQUIRED ),
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   157
        LUA_FUNC_ARG_STRING("service",      IRC_PORT                ),
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   158
        LUA_FUNC_ARG_STRING("service_ssl",  IRC_SSL_PORT            )
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   159
);
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   160
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   161
static int lua_client_set_defaults (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   162
{
143
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   163
    struct lua_client *lua_client;
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   164
    const char *nickname, *username, *realname, *service, *service_ssl;
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   165
143
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   166
    // parse args
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   167
    lua_args_parse(L, &lua_client_set_defaults_func, (void *) &lua_client,
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   168
        &nickname, &username, &realname, &service, &service_ssl
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   169
    );
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   170
143
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   171
    // set
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   172
    struct irc_client_defaults defaults = {
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   173
        .register_info = {
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   174
            .nickname   = nickname,
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   175
            .username   = username,
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   176
            .realname   = realname
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   177
        },
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   178
        .service        = service,
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   179
        .service_ssl    = service_ssl
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   180
    };
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   181
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   182
    // invoke
143
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   183
    // XXX: needs to be copied
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   184
    irc_client_set_defaults(lua_client->client, &defaults);
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   185
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   186
    // ok
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   187
    return 0;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   188
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   189
143
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   190
static struct lua_func lua_client_connect_func = LUA_FUNC("evirc.client", "connect",
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   191
        "Create and return a new IRC network",
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   192
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   193
        LUA_FUNC_ARG_STRING("network",      LUA_ARG_STRING_REQUIRED ),
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   194
        LUA_FUNC_ARG_STRING("hostname",     LUA_ARG_STRING_REQUIRED ),
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   195
        LUA_FUNC_ARG_STRING("service",      NULL                    ),
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   196
        LUA_FUNC_ARG_BOOL(  "ssl",          false                   ),
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   197
        LUA_FUNC_ARG_STRING("ssl_cafile",   NULL                    ),
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   198
        LUA_FUNC_ARG_BOOL(  "ssl_verify",   false                   ),
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   199
        LUA_FUNC_ARG_STRING("ssl_cert",     NULL                    ),
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   200
        LUA_FUNC_ARG_STRING("ssl_pkey",     NULL                    )
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   201
);
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   202
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   203
static int lua_client_connect (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   204
{
143
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   205
    struct lua_client *lua_client;
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   206
    struct irc_net_info net_info;
141
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   207
    const char *ssl_cafile = NULL, *ssl_cert = NULL, *ssl_pkey = NULL;
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   208
    bool use_ssl = false, ssl_verify = false;
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   209
    struct irc_net *net;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   210
    struct error_info err;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   211
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   212
    // init net_info
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   213
    memset(&net_info, 0, sizeof(net_info));
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   214
143
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   215
    // parse args
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   216
    lua_args_parse(L, &lua_client_connect_func, (void *) &lua_client,
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   217
        &net_info.network, &net_info.hostname, &net_info.service,
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   218
        &use_ssl, &ssl_cafile, &ssl_verify, &ssl_cert, &ssl_pkey
1edab39c88a8 implement lua_args_parse
Tero Marttila <terom@fixme.fi>
parents: 142
diff changeset
   219
    );
141
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   220
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   221
    // SSL?
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   222
    if (use_ssl || ssl_cafile || ssl_verify || ssl_cert || ssl_pkey) {
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   223
        // verify
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   224
        if ((ssl_cert || ssl_pkey) && !(ssl_cert && ssl_pkey))
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   225
            return luaL_error(L, "must give both ssl_cert and ssl_pkey");
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   226
        
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   227
        // create
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   228
        if (sock_ssl_client_cred_create(&net_info.ssl_cred, ssl_cafile, ssl_verify, ssl_cert, ssl_pkey, &err))
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   229
            return luaL_error(L, "sock_ssl_client_cred_create(ssl_cafile=%s, ssl_verify=%b, ssl_cert=%s, ssl_pkey=%s): %s", 
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   230
                    ssl_cafile, ssl_verify, ssl_cert, ssl_pkey, error_msg(&err)
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   231
                );
0b850238c588 implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents: 131
diff changeset
   232
    }
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   233
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   234
    // create it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   235
    if (irc_client_add_net(lua_client->client, &net, &net_info, &err))
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   236
        return luaL_error(L, "irc_client_add_net: %s/%s: %s", net_info.network, net_info.hostname, error_msg(&err));
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   237
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   238
    // wrap it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   239
    return lua_net_create(L, net);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   240
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   241
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   242
static int lua_client_get_network (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   243
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   244
    struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   245
    struct irc_net *net;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   246
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   247
    // the network name
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   248
    const char *network = luaL_checkstring(L, 2);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   249
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   250
    // lookup the irc_net
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   251
    if ((net = irc_client_get_net(lua_client->client, network)) == NULL)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   252
        return luaL_error(L, "irc_client_get_net: no such network: %s", network);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   253
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   254
    // wrap it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   255
    return lua_net_create(L, net);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   256
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   257
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   258
static int lua_client_quit (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   259
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   260
    struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   261
    err_t err;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   262
   
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   263
    // the message
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   264
    const char *message = luaL_checkstring(L, 2);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   265
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   266
    // execute
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   267
    if ((err = irc_client_quit(lua_client->client, message)))
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   268
        return luaL_error(L, "irc_client_quit: %s", error_name(err));
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   269
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   270
    // ok
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   271
    return 0;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   272
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   273
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   274
static const struct luaL_Reg lua_client_methods[] = {
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   275
    {   "set_defaults", &lua_client_set_defaults    },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   276
    {   "connect",      &lua_client_connect         },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   277
    {   "network",      &lua_client_get_network     },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   278
    {   "quit",         &lua_client_quit            },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   279
    {   NULL,           NULL                        }
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   280
};
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   281
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   282
/**
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   283
 * Initialize the evirc.client type for lua_client, and registers an instance bound to the given irc_client at
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   284
 * 'client'.
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   285
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   286
static void lua_client_init (lua_State *L, struct irc_client *client)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   287
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   288
    // allocate the global "client" object
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   289
    struct lua_client *lua_client = lua_obj_create_global_type(L, "evirc.client", lua_client_methods, "client", sizeof(*lua_client));
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   290
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   291
    // initialize it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   292
    lua_client->client = client;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   293
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   294
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   295
void lua_irc_init (struct nexus_lua *lua)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   296
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   297
    lua_client_init(lua->st, lua->nexus->client);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   298
    lua_net_init(lua->st);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   299
    lua_chan_init(lua->st);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   300
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   301