src/lua_irc.c
author Tero Marttila <terom@fixme.fi>
Sun, 19 Apr 2009 04:04:42 +0300
changeset 140 aa390e52eda8
parent 131 df949b399491
child 141 0b850238c588
permissions -rw-r--r--
implement ssl_cafile/verify/cert/pkey for x509 credentials
116
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "lua_irc.h"
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
#include <stdlib.h>
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
#include <string.h>
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
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
 * 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
     8
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
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
    10
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
    // create the new obj
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    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
    13
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    // initialize
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    lua_chan->chan = chan;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    // ok
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    return 1;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
}
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
 * Return the channel name as a lua string
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
static int lua_chan_tostring (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
    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
    27
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    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
    29
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    return 1;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
}
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
 * Send a PRIVMSG to the channel
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
static int lua_chan_say (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
    err_t err;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
    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
    41
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    // the message
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
    const char *message = luaL_checkstring(L, 2);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
    // send
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    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
    47
        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
    48
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    // ok
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
    return 0;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
}
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
static const struct luaL_Reg lua_chan_methods[] = {
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
    {   "__tostring",       &lua_chan_tostring  },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
    {   "say",              &lua_chan_say       },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
    {   NULL,               NULL                },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
};
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
 * Initialize the lua_chan object type
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
static void lua_chan_init (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    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
    65
}
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
 * 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
    69
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
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
    71
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
    // create the new obj
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
    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
    74
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
    // initialize
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    lua_net->net = net;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
    // ok
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
    return 1;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
}
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
 * Return the network name as a lua string
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
static int lua_net_tostring (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
    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
    88
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
    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
    90
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    return 1;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
}
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
 * Join a new channel, returning the lua_chan
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
static int lua_net_join (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
    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
   100
    struct irc_chan_info chan_info;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
    struct irc_chan *chan;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
    struct error_info err;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
    // 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
   105
    // 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
   106
    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
   107
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    // add it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
    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
   110
        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
   111
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    // return it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
    return lua_chan_create(L, chan);    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
}
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
 * 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
   118
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
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
   120
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
    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
   122
    struct irc_chan *chan;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
    // the channel name
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
    const char *channel = luaL_checkstring(L, 2);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
    // lookup the irc_chan
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
    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
   129
        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
   130
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
    // wrap it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
    return lua_chan_create(L, chan);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
}
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
static const struct luaL_Reg lua_net_methods[] = {
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
    {   "__tostring",   &lua_net_tostring   },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
    {   "join",         &lua_net_join       },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
    {   "channel",      &lua_net_get_chan   },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
    {   NULL,           NULL                }
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
};
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
 * Initialize the lua_net object type
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
 */
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
static void lua_net_init (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   146
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
    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
   148
}
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
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
   151
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152
    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
   153
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
    // read the args
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   155
    // XXX: need to copy these, really
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   156
    lua_client->defaults.register_info.nickname = luaL_checkstring(L, 2);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   157
    lua_client->defaults.register_info.username = luaL_checkstring(L, 3);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   158
    lua_client->defaults.register_info.realname = luaL_checkstring(L, 4);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   159
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   160
    // set the non-args
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   161
    lua_client->defaults.service = IRC_PORT;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   162
    lua_client->defaults.service_ssl = IRC_SSL_PORT;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   163
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   164
    // invoke
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   165
    irc_client_set_defaults(lua_client->client, &lua_client->defaults);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   166
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   167
    // ok
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   168
    return 0;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   169
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   170
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   171
static int lua_client_connect (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   172
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
    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
   174
    struct irc_net_info net_info;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   175
    struct irc_net *net;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   176
    struct error_info err;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   177
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   178
    // init net_info
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   179
    memset(&net_info, 0, sizeof(net_info));
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   180
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   181
    // the network name
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   182
    net_info.network = luaL_checkstring(L, 2);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   183
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   184
    // the hostname
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   185
    net_info.hostname = luaL_checkstring(L, 3);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   186
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   187
    // service remains default
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   188
    net_info.service = "6667";
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   189
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   190
    // create it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   191
    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
   192
        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
   193
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   194
    // wrap it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   195
    return lua_net_create(L, net);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   196
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   197
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   198
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
   199
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   200
    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
   201
    struct irc_net *net;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   202
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   203
    // the network name
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   204
    const char *network = luaL_checkstring(L, 2);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   205
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   206
    // lookup the irc_net
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   207
    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
   208
        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
   209
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   210
    // wrap it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   211
    return lua_net_create(L, net);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   212
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   213
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   214
static int lua_client_quit (lua_State *L)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   215
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   216
    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
   217
    err_t err;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   218
   
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   219
    // the message
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   220
    const char *message = luaL_checkstring(L, 2);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   221
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   222
    // execute
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   223
    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
   224
        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
   225
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   226
    // ok
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   227
    return 0;
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   228
}
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   229
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   230
static const struct luaL_Reg lua_client_methods[] = {
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   231
    {   "set_defaults", &lua_client_set_defaults    },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   232
    {   "connect",      &lua_client_connect         },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   233
    {   "network",      &lua_client_get_network     },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   234
    {   "quit",         &lua_client_quit            },
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   235
    {   NULL,           NULL                        }
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   236
};
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
/**
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   239
 * 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
   240
 * 'client'.
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 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
   243
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   244
    // allocate the global "client" object
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   245
    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
   246
    
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   247
    // initialize it
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   248
    lua_client->client = client;
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
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   251
void lua_irc_init (struct nexus_lua *lua)
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   252
{
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   253
    lua_client_init(lua->st, lua->nexus->client);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   254
    lua_net_init(lua->st);
92e71129074d split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   255
    lua_chan_init(lua->st);
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