116
|
1 |
#include "lua_irc.h"
|
|
2 |
|
|
3 |
#include <stdlib.h>
|
|
4 |
#include <string.h>
|
|
5 |
|
|
6 |
/**
|
|
7 |
* Create a lua_chan userdata from the given irc_chan and leave it on the stack, returning 1
|
|
8 |
*/
|
|
9 |
static int lua_chan_create (lua_State *L, struct irc_chan *chan)
|
|
10 |
{
|
|
11 |
// create the new obj
|
|
12 |
struct lua_chan *lua_chan = lua_obj_create_obj(L, "evirc.chan", sizeof(*lua_chan));
|
|
13 |
|
|
14 |
// initialize
|
|
15 |
lua_chan->chan = chan;
|
|
16 |
|
|
17 |
// ok
|
|
18 |
return 1;
|
|
19 |
}
|
|
20 |
|
|
21 |
/**
|
|
22 |
* Return the channel name as a lua string
|
|
23 |
*/
|
|
24 |
static int lua_chan_tostring (lua_State *L)
|
|
25 |
{
|
|
26 |
struct lua_chan *lua_chan = lua_obj_get_obj(L, __func__, "evirc.chan");
|
|
27 |
|
|
28 |
lua_pushfstring(L, "<irc_chan %s>", irc_chan_name(lua_chan->chan));
|
|
29 |
|
|
30 |
return 1;
|
|
31 |
}
|
|
32 |
|
|
33 |
/**
|
|
34 |
* Send a PRIVMSG to the channel
|
|
35 |
*/
|
|
36 |
static int lua_chan_say (lua_State *L)
|
|
37 |
{
|
|
38 |
err_t err;
|
|
39 |
|
|
40 |
struct lua_chan *lua_chan = lua_obj_get_obj(L, __func__, "evirc.chan");
|
|
41 |
|
|
42 |
// the message
|
|
43 |
const char *message = luaL_checkstring(L, 2);
|
|
44 |
|
|
45 |
// send
|
|
46 |
if ((err = irc_chan_PRIVMSG(lua_chan->chan, message)))
|
|
47 |
return luaL_error(L, "irc_chan_PRIVMSG: '%s': %s", message, error_name(err));
|
|
48 |
|
|
49 |
// ok
|
|
50 |
return 0;
|
|
51 |
}
|
|
52 |
|
|
53 |
static const struct luaL_Reg lua_chan_methods[] = {
|
|
54 |
{ "__tostring", &lua_chan_tostring },
|
|
55 |
{ "say", &lua_chan_say },
|
|
56 |
{ NULL, NULL },
|
|
57 |
};
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Initialize the lua_chan object type
|
|
61 |
*/
|
|
62 |
static void lua_chan_init (lua_State *L)
|
|
63 |
{
|
|
64 |
lua_obj_create_type(L, "evirc.chan", lua_chan_methods);
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Create a lua_net userdata from the given irc_net and push it onto the stack, returning 1.
|
|
69 |
*/
|
|
70 |
static int lua_net_create (lua_State *L, struct irc_net *net)
|
|
71 |
{
|
|
72 |
// create the new obj
|
|
73 |
struct lua_net *lua_net = lua_obj_create_obj(L, "evirc.net", sizeof(*lua_net));
|
|
74 |
|
|
75 |
// initialize
|
|
76 |
lua_net->net = net;
|
|
77 |
|
|
78 |
// ok
|
|
79 |
return 1;
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Return the network name as a lua string
|
|
84 |
*/
|
|
85 |
static int lua_net_tostring (lua_State *L)
|
|
86 |
{
|
|
87 |
struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
|
|
88 |
|
|
89 |
lua_pushfstring(L, "<irc_net %s>", irc_net_name(lua_net->net));
|
|
90 |
|
|
91 |
return 1;
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Join a new channel, returning the lua_chan
|
|
96 |
*/
|
|
97 |
static int lua_net_join (lua_State *L)
|
|
98 |
{
|
|
99 |
struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
|
|
100 |
struct irc_chan_info chan_info;
|
|
101 |
struct irc_chan *chan;
|
|
102 |
struct error_info err;
|
|
103 |
|
|
104 |
// the channel name
|
|
105 |
chan_info.channel = luaL_checkstring(L, 2);
|
|
106 |
|
|
107 |
// add it
|
|
108 |
if (irc_net_add_chan(lua_net->net, &chan, &chan_info, &err))
|
|
109 |
return luaL_error(L, "irc_net_add_chan: %s: %s", chan_info.channel, error_msg(&err));
|
|
110 |
|
|
111 |
// return it
|
|
112 |
return lua_chan_create(L, chan);
|
|
113 |
}
|
|
114 |
|
|
115 |
/**
|
|
116 |
* Look up a channel by name, returning the lua_chan
|
|
117 |
*/
|
|
118 |
static int lua_net_get_chan (lua_State *L)
|
|
119 |
{
|
|
120 |
struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net");
|
|
121 |
struct irc_chan *chan;
|
|
122 |
|
|
123 |
// the channel name
|
|
124 |
const char *channel = luaL_checkstring(L, 2);
|
|
125 |
|
|
126 |
// lookup the irc_chan
|
|
127 |
if ((chan = irc_net_get_chan(lua_net->net, channel)) == NULL)
|
|
128 |
return luaL_error(L, "irc_net_get_chan: no such channel: %s", channel);
|
|
129 |
|
|
130 |
// wrap it
|
|
131 |
return lua_chan_create(L, chan);
|
|
132 |
}
|
|
133 |
|
|
134 |
static const struct luaL_Reg lua_net_methods[] = {
|
|
135 |
{ "__tostring", &lua_net_tostring },
|
|
136 |
{ "join", &lua_net_join },
|
|
137 |
{ "channel", &lua_net_get_chan },
|
|
138 |
{ NULL, NULL }
|
|
139 |
};
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Initialize the lua_net object type
|
|
143 |
*/
|
|
144 |
static void lua_net_init (lua_State *L)
|
|
145 |
{
|
|
146 |
lua_obj_create_type(L, "evirc.net", lua_net_methods);
|
|
147 |
}
|
|
148 |
|
|
149 |
static int lua_client_set_defaults (lua_State *L)
|
|
150 |
{
|
|
151 |
struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
|
|
152 |
|
|
153 |
// read the args
|
|
154 |
// XXX: need to copy these, really
|
|
155 |
lua_client->defaults.register_info.nickname = luaL_checkstring(L, 2);
|
|
156 |
lua_client->defaults.register_info.username = luaL_checkstring(L, 3);
|
|
157 |
lua_client->defaults.register_info.realname = luaL_checkstring(L, 4);
|
|
158 |
|
|
159 |
// set the non-args
|
|
160 |
lua_client->defaults.service = IRC_PORT;
|
|
161 |
lua_client->defaults.service_ssl = IRC_SSL_PORT;
|
|
162 |
|
|
163 |
// invoke
|
|
164 |
irc_client_set_defaults(lua_client->client, &lua_client->defaults);
|
|
165 |
|
|
166 |
// ok
|
|
167 |
return 0;
|
|
168 |
}
|
|
169 |
|
|
170 |
static int lua_client_connect (lua_State *L)
|
|
171 |
{
|
|
172 |
struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
|
|
173 |
struct irc_net_info net_info;
|
|
174 |
struct irc_net *net;
|
|
175 |
struct error_info err;
|
|
176 |
|
|
177 |
// init net_info
|
|
178 |
memset(&net_info, 0, sizeof(net_info));
|
|
179 |
|
|
180 |
// the network name
|
|
181 |
net_info.network = luaL_checkstring(L, 2);
|
|
182 |
|
|
183 |
// the hostname
|
|
184 |
net_info.hostname = luaL_checkstring(L, 3);
|
|
185 |
|
|
186 |
// service remains default
|
|
187 |
net_info.service = "6667";
|
|
188 |
|
|
189 |
// create it
|
|
190 |
if (irc_client_add_net(lua_client->client, &net, &net_info, &err))
|
|
191 |
return luaL_error(L, "irc_client_add_net: %s/%s: %s", net_info.network, net_info.hostname, error_msg(&err));
|
|
192 |
|
|
193 |
// wrap it
|
|
194 |
return lua_net_create(L, net);
|
|
195 |
}
|
|
196 |
|
|
197 |
static int lua_client_get_network (lua_State *L)
|
|
198 |
{
|
|
199 |
struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
|
|
200 |
struct irc_net *net;
|
|
201 |
|
|
202 |
// the network name
|
|
203 |
const char *network = luaL_checkstring(L, 2);
|
|
204 |
|
|
205 |
// lookup the irc_net
|
|
206 |
if ((net = irc_client_get_net(lua_client->client, network)) == NULL)
|
|
207 |
return luaL_error(L, "irc_client_get_net: no such network: %s", network);
|
|
208 |
|
|
209 |
// wrap it
|
|
210 |
return lua_net_create(L, net);
|
|
211 |
}
|
|
212 |
|
|
213 |
static int lua_client_quit (lua_State *L)
|
|
214 |
{
|
|
215 |
struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
|
|
216 |
err_t err;
|
|
217 |
|
|
218 |
// the message
|
|
219 |
const char *message = luaL_checkstring(L, 2);
|
|
220 |
|
|
221 |
// execute
|
|
222 |
if ((err = irc_client_quit(lua_client->client, message)))
|
|
223 |
return luaL_error(L, "irc_client_quit: %s", error_name(err));
|
|
224 |
|
|
225 |
// ok
|
|
226 |
return 0;
|
|
227 |
}
|
|
228 |
|
|
229 |
static const struct luaL_Reg lua_client_methods[] = {
|
|
230 |
{ "set_defaults", &lua_client_set_defaults },
|
|
231 |
{ "connect", &lua_client_connect },
|
|
232 |
{ "network", &lua_client_get_network },
|
|
233 |
{ "quit", &lua_client_quit },
|
|
234 |
{ NULL, NULL }
|
|
235 |
};
|
|
236 |
|
|
237 |
/**
|
|
238 |
* Initialize the evirc.client type for lua_client, and registers an instance bound to the given irc_client at
|
|
239 |
* 'client'.
|
|
240 |
*/
|
|
241 |
static void lua_client_init (lua_State *L, struct irc_client *client)
|
|
242 |
{
|
|
243 |
// allocate the global "client" object
|
|
244 |
struct lua_client *lua_client = lua_obj_create_global_type(L, "evirc.client", lua_client_methods, "client", sizeof(*lua_client));
|
|
245 |
|
|
246 |
// initialize it
|
|
247 |
lua_client->client = client;
|
|
248 |
}
|
|
249 |
|
|
250 |
void lua_irc_init (struct nexus_lua *lua)
|
|
251 |
{
|
|
252 |
lua_client_init(lua->st, lua->nexus->client);
|
|
253 |
lua_net_init(lua->st);
|
|
254 |
lua_chan_init(lua->st);
|
|
255 |
}
|
|
256 |
|