author | Tero Marttila <terom@fixme.fi> |
Thu, 21 May 2009 16:57:56 +0300 | |
changeset 213 | f0e52e026197 |
parent 199 | 8eb839fbabba |
permissions | -rw-r--r-- |
116 | 1 |
#include "lua_irc.h" |
143 | 2 |
#include "lua_func.h" |
116 | 3 |
|
4 |
#include <stdlib.h> |
|
5 |
#include <string.h> |
|
6 |
||
145 | 7 |
static struct lua_type lua_chan_type = LUA_TYPE("evirc.chan"); |
8 |
||
116 | 9 |
/** |
10 |
* Create a lua_chan userdata from the given irc_chan and leave it on the stack, returning 1 |
|
11 |
*/ |
|
12 |
static int lua_chan_create (lua_State *L, struct irc_chan *chan) |
|
13 |
{ |
|
14 |
// create the new obj |
|
145 | 15 |
struct lua_chan *lua_chan = lua_type_create(L, &lua_chan_type, sizeof(*lua_chan)); |
116 | 16 |
|
17 |
// initialize |
|
18 |
lua_chan->chan = chan; |
|
19 |
||
20 |
// ok |
|
21 |
return 1; |
|
22 |
} |
|
23 |
||
24 |
/** |
|
25 |
* Return the channel name as a lua string |
|
26 |
*/ |
|
145 | 27 |
static struct lua_func lua_chan__tostring_func = LUA_FUNC(&lua_chan_type, "__tostring", |
28 |
"format using channel name", |
|
29 |
||
30 |
LUA_FUNC_ARG_END |
|
31 |
); |
|
32 |
||
33 |
static int lua_chan__tostring (lua_State *L) |
|
116 | 34 |
{ |
145 | 35 |
struct lua_chan *lua_chan; |
36 |
||
37 |
lua_args_parse(L, &lua_chan__tostring_func, (void *) &lua_chan); |
|
116 | 38 |
|
39 |
lua_pushfstring(L, "<irc_chan %s>", irc_chan_name(lua_chan->chan)); |
|
40 |
||
41 |
return 1; |
|
42 |
} |
|
43 |
||
44 |
/** |
|
45 |
* Send a PRIVMSG to the channel |
|
46 |
*/ |
|
145 | 47 |
static struct lua_func lua_chan_say_func = LUA_FUNC(&lua_chan_type, "evirc.chan.say", |
48 |
"send a message to a channel", |
|
49 |
||
50 |
LUA_FUNC_ARG_STRING("message", LUA_ARG_STRING_REQUIRED ) |
|
51 |
); |
|
52 |
||
116 | 53 |
static int lua_chan_say (lua_State *L) |
54 |
{ |
|
145 | 55 |
struct lua_chan *lua_chan; |
116 | 56 |
err_t err; |
145 | 57 |
const char *message; |
116 | 58 |
|
145 | 59 |
// parse args |
60 |
lua_args_parse(L, &lua_chan_say_func, (void *) &lua_chan, |
|
61 |
&message |
|
62 |
); |
|
116 | 63 |
|
64 |
// send |
|
65 |
if ((err = irc_chan_PRIVMSG(lua_chan->chan, message))) |
|
66 |
return luaL_error(L, "irc_chan_PRIVMSG: '%s': %s", message, error_name(err)); |
|
67 |
||
68 |
// ok |
|
69 |
return 0; |
|
70 |
} |
|
71 |
||
145 | 72 |
static struct lua_method lua_chan_methods[] = LUA_METHODS( |
73 |
LUA_METHOD("__tostring", lua_chan__tostring, &lua_chan__tostring_func ), |
|
74 |
LUA_METHOD("say", lua_chan_say, &lua_chan_say_func ) |
|
75 |
); |
|
116 | 76 |
|
145 | 77 |
|
78 |
||
79 |
static struct lua_type lua_net_type = LUA_TYPE("evirc.net"); |
|
116 | 80 |
|
81 |
/** |
|
82 |
* Create a lua_net userdata from the given irc_net and push it onto the stack, returning 1. |
|
83 |
*/ |
|
84 |
static int lua_net_create (lua_State *L, struct irc_net *net) |
|
85 |
{ |
|
86 |
// create the new obj |
|
145 | 87 |
struct lua_net *lua_net = lua_type_create(L, &lua_net_type, sizeof(*lua_net)); |
116 | 88 |
|
89 |
// initialize |
|
90 |
lua_net->net = net; |
|
91 |
||
92 |
// ok |
|
93 |
return 1; |
|
94 |
} |
|
95 |
||
96 |
/** |
|
97 |
* Return the network name as a lua string |
|
98 |
*/ |
|
145 | 99 |
static struct lua_func lua_net__tostring_func = LUA_FUNC(&lua_net_type, "__tostring", |
100 |
"format using network name", |
|
101 |
||
102 |
LUA_FUNC_ARG_END |
|
103 |
); |
|
104 |
||
105 |
static int lua_net__tostring (lua_State *L) |
|
116 | 106 |
{ |
145 | 107 |
struct lua_net *lua_net; |
108 |
||
109 |
lua_args_parse(L, &lua_net__tostring_func, (void *) &lua_net); |
|
116 | 110 |
|
111 |
lua_pushfstring(L, "<irc_net %s>", irc_net_name(lua_net->net)); |
|
112 |
||
113 |
return 1; |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Join a new channel, returning the lua_chan |
|
118 |
*/ |
|
145 | 119 |
static struct lua_func lua_net_join_func = LUA_FUNC(&lua_net_type, "join", |
120 |
"create a new channel and join it", |
|
121 |
||
122 |
LUA_FUNC_ARG_STRING("channel", LUA_ARG_STRING_REQUIRED) |
|
123 |
); |
|
124 |
||
125 |
||
116 | 126 |
static int lua_net_join (lua_State *L) |
127 |
{ |
|
145 | 128 |
struct lua_net *lua_net; |
116 | 129 |
struct irc_chan_info chan_info; |
130 |
struct irc_chan *chan; |
|
131 |
struct error_info err; |
|
132 |
||
133 |
// the channel name |
|
145 | 134 |
lua_args_parse(L, &lua_net_join_func, (void *) &lua_net, |
135 |
&chan_info.channel |
|
136 |
); |
|
116 | 137 |
|
138 |
// add it |
|
139 |
if (irc_net_add_chan(lua_net->net, &chan, &chan_info, &err)) |
|
140 |
return luaL_error(L, "irc_net_add_chan: %s: %s", chan_info.channel, error_msg(&err)); |
|
141 |
||
142 |
// return it |
|
143 |
return lua_chan_create(L, chan); |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Look up a channel by name, returning the lua_chan |
|
148 |
*/ |
|
145 | 149 |
static struct lua_func lua_net_get_chan_func = LUA_FUNC(&lua_net_type, "channel", |
150 |
"look up a channel by name", |
|
151 |
||
152 |
LUA_FUNC_ARG_STRING("channel", LUA_ARG_STRING_REQUIRED) |
|
153 |
); |
|
154 |
||
116 | 155 |
static int lua_net_get_chan (lua_State *L) |
156 |
{ |
|
145 | 157 |
struct lua_net *lua_net; |
116 | 158 |
struct irc_chan *chan; |
145 | 159 |
const char *channel; |
160 |
||
161 |
// parse args |
|
162 |
lua_args_parse(L, &lua_net_get_chan_func, (void *) &lua_net, |
|
163 |
&channel |
|
164 |
); |
|
116 | 165 |
|
166 |
// lookup the irc_chan |
|
167 |
if ((chan = irc_net_get_chan(lua_net->net, channel)) == NULL) |
|
168 |
return luaL_error(L, "irc_net_get_chan: no such channel: %s", channel); |
|
169 |
||
170 |
// wrap it |
|
171 |
return lua_chan_create(L, chan); |
|
172 |
} |
|
173 |
||
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
174 |
static struct lua_func lua_net_channels_func = LUA_FUNC(&lua_net_type, "channels", |
199
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
175 |
"Return an iterater over the network's channels", |
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
176 |
|
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
177 |
LUA_FUNC_ARG_END |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
178 |
); |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
179 |
|
199
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
180 |
static int lua_net_channels_iter (lua_State *L) |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
181 |
{ |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
182 |
int nargs = lua_gettop(L); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
183 |
struct lua_net *lua_net = lua_arg_obj(L, nargs, 1, &lua_net_type, false); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
184 |
struct lua_chan *lua_chan = lua_arg_obj(L, nargs, 2, &lua_chan_type, true); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
185 |
struct irc_chan *chan_next; |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
186 |
|
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
187 |
// get next item from current |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
188 |
if (lua_chan) |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
189 |
chan_next = TAILQ_NEXT(lua_chan->chan, net_channels); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
190 |
else |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
191 |
chan_next = TAILQ_FIRST(&lua_net->net->channels); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
192 |
|
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
193 |
// push return value - next item |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
194 |
if (chan_next) |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
195 |
lua_chan_create(L, chan_next); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
196 |
else |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
197 |
lua_pushnil(L); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
198 |
|
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
199 |
return 1; |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
200 |
} |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
201 |
|
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
202 |
static int lua_net_channels (lua_State *L) |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
203 |
{ |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
204 |
struct lua_net *lua_net; |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
205 |
|
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
206 |
// parse args |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
207 |
lua_args_parse(L, &lua_net_channels_func, (void *) &lua_net); |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
208 |
|
199
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
209 |
// push iter func |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
210 |
lua_pushcfunction(L, lua_net_channels_iter); |
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
211 |
|
199
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
212 |
// push invariant state - the lua_net |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
213 |
lua_pushvalue(L, 1); |
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
214 |
|
199
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
215 |
// return iter three-tuple |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
216 |
return 2; |
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
217 |
} |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
218 |
|
145 | 219 |
static struct lua_method lua_net_methods[] = LUA_METHODS( |
220 |
LUA_METHOD("__tostring", lua_net__tostring, &lua_net__tostring_func ), |
|
221 |
LUA_METHOD("join", lua_net_join, &lua_net_join_func ), |
|
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
222 |
LUA_METHOD("channel", lua_net_get_chan, &lua_net_get_chan_func ), |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
223 |
LUA_METHOD("channels", lua_net_channels, &lua_net_channels_func ) |
145 | 224 |
); |
116 | 225 |
|
226 |
||
145 | 227 |
|
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
228 |
|
145 | 229 |
static struct lua_type lua_client_type = LUA_TYPE("evirc.client"); |
230 |
||
231 |
||
232 |
static struct lua_func lua_client_set_defaults_func = LUA_FUNC(&lua_client_type, "set_defaults", |
|
143 | 233 |
"set the default settings to use for evirc.client.connect", |
234 |
||
235 |
LUA_FUNC_ARG_STRING("nickname", LUA_ARG_STRING_REQUIRED ), |
|
236 |
LUA_FUNC_ARG_STRING("username", LUA_ARG_STRING_REQUIRED ), |
|
237 |
LUA_FUNC_ARG_STRING("realname", LUA_ARG_STRING_REQUIRED ), |
|
238 |
LUA_FUNC_ARG_STRING("service", IRC_PORT ), |
|
239 |
LUA_FUNC_ARG_STRING("service_ssl", IRC_SSL_PORT ) |
|
145 | 240 |
); |
143 | 241 |
|
116 | 242 |
static int lua_client_set_defaults (lua_State *L) |
243 |
{ |
|
143 | 244 |
struct lua_client *lua_client; |
245 |
const char *nickname, *username, *realname, *service, *service_ssl; |
|
116 | 246 |
|
143 | 247 |
// parse args |
248 |
lua_args_parse(L, &lua_client_set_defaults_func, (void *) &lua_client, |
|
145 | 249 |
&nickname, &username, &realname, &service, &service_ssl |
250 |
); |
|
116 | 251 |
|
143 | 252 |
// set |
253 |
struct irc_client_defaults defaults = { |
|
254 |
.register_info = { |
|
255 |
.nickname = nickname, |
|
256 |
.username = username, |
|
257 |
.realname = realname |
|
258 |
}, |
|
259 |
.service = service, |
|
260 |
.service_ssl = service_ssl |
|
261 |
}; |
|
116 | 262 |
|
263 |
// invoke |
|
143 | 264 |
// XXX: needs to be copied |
265 |
irc_client_set_defaults(lua_client->client, &defaults); |
|
116 | 266 |
|
267 |
// ok |
|
268 |
return 0; |
|
269 |
} |
|
270 |
||
145 | 271 |
static struct lua_func lua_client_connect_func = LUA_FUNC(&lua_client_type, "connect", |
143 | 272 |
"Create and return a new IRC network", |
273 |
||
274 |
LUA_FUNC_ARG_STRING("network", LUA_ARG_STRING_REQUIRED ), |
|
275 |
LUA_FUNC_ARG_STRING("hostname", LUA_ARG_STRING_REQUIRED ), |
|
276 |
LUA_FUNC_ARG_STRING("service", NULL ), |
|
277 |
LUA_FUNC_ARG_BOOL( "ssl", false ), |
|
278 |
LUA_FUNC_ARG_STRING("ssl_cafile", NULL ), |
|
279 |
LUA_FUNC_ARG_BOOL( "ssl_verify", false ), |
|
280 |
LUA_FUNC_ARG_STRING("ssl_cert", NULL ), |
|
281 |
LUA_FUNC_ARG_STRING("ssl_pkey", NULL ) |
|
282 |
); |
|
283 |
||
116 | 284 |
static int lua_client_connect (lua_State *L) |
285 |
{ |
|
143 | 286 |
struct lua_client *lua_client; |
116 | 287 |
struct irc_net_info net_info; |
141
0b850238c588
implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents:
131
diff
changeset
|
288 |
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
|
289 |
bool use_ssl = false, ssl_verify = false; |
116 | 290 |
struct irc_net *net; |
291 |
struct error_info err; |
|
292 |
||
293 |
// init net_info |
|
294 |
memset(&net_info, 0, sizeof(net_info)); |
|
295 |
||
143 | 296 |
// parse args |
297 |
lua_args_parse(L, &lua_client_connect_func, (void *) &lua_client, |
|
298 |
&net_info.network, &net_info.hostname, &net_info.service, |
|
299 |
&use_ssl, &ssl_cafile, &ssl_verify, &ssl_cert, &ssl_pkey |
|
300 |
); |
|
141
0b850238c588
implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents:
131
diff
changeset
|
301 |
|
0b850238c588
implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents:
131
diff
changeset
|
302 |
// SSL? |
0b850238c588
implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents:
131
diff
changeset
|
303 |
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
|
304 |
// verify |
0b850238c588
implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents:
131
diff
changeset
|
305 |
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
|
306 |
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
|
307 |
|
0b850238c588
implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents:
131
diff
changeset
|
308 |
// create |
180
22967b165692
rename sock_ssl/sock_gnutls -> ssl/ssl_client
Tero Marttila <terom@fixme.fi>
parents:
145
diff
changeset
|
309 |
if (ssl_client_cred_create(&net_info.ssl_cred, ssl_cafile, ssl_verify, ssl_cert, ssl_pkey, &err)) |
141
0b850238c588
implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents:
131
diff
changeset
|
310 |
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
|
311 |
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
|
312 |
); |
0b850238c588
implement SSL stuff for lua_client_connect
Tero Marttila <terom@fixme.fi>
parents:
131
diff
changeset
|
313 |
} |
116 | 314 |
|
315 |
// create it |
|
316 |
if (irc_client_add_net(lua_client->client, &net, &net_info, &err)) |
|
317 |
return luaL_error(L, "irc_client_add_net: %s/%s: %s", net_info.network, net_info.hostname, error_msg(&err)); |
|
318 |
||
319 |
// wrap it |
|
320 |
return lua_net_create(L, net); |
|
321 |
} |
|
322 |
||
145 | 323 |
static struct lua_func lua_client_get_network_func = LUA_FUNC(&lua_client_type, "network", |
324 |
"Lookup an existing network by name", |
|
325 |
||
326 |
LUA_FUNC_ARG_STRING("network", LUA_ARG_STRING_REQUIRED) |
|
327 |
); |
|
328 |
||
116 | 329 |
static int lua_client_get_network (lua_State *L) |
330 |
{ |
|
145 | 331 |
struct lua_client *lua_client; |
116 | 332 |
struct irc_net *net; |
145 | 333 |
const char *network; |
334 |
||
335 |
// parse args |
|
336 |
lua_args_parse(L, &lua_client_get_network_func, (void *) &lua_client, |
|
337 |
&network |
|
338 |
); |
|
116 | 339 |
|
340 |
// lookup the irc_net |
|
341 |
if ((net = irc_client_get_net(lua_client->client, network)) == NULL) |
|
342 |
return luaL_error(L, "irc_client_get_net: no such network: %s", network); |
|
343 |
||
344 |
// wrap it |
|
345 |
return lua_net_create(L, net); |
|
346 |
} |
|
347 |
||
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
348 |
static struct lua_func lua_client_networks_func = LUA_FUNC(&lua_client_type, "channels", |
199
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
349 |
"Return an iterator over the client's networks", |
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
350 |
|
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
351 |
LUA_FUNC_ARG_END |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
352 |
); |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
353 |
|
199
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
354 |
static int lua_client_networks_iter (lua_State *L) |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
355 |
{ |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
356 |
int nargs = lua_gettop(L); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
357 |
struct lua_client *lua_client = lua_arg_obj(L, nargs, 1, &lua_client_type, false); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
358 |
struct lua_net *lua_net = lua_arg_obj(L, nargs, 2, &lua_net_type, true); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
359 |
struct irc_net *net_next; |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
360 |
|
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
361 |
if (lua_net) |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
362 |
// return next |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
363 |
net_next = TAILQ_NEXT(lua_net->net, client_networks); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
364 |
else |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
365 |
// return first |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
366 |
net_next = TAILQ_FIRST(&lua_client->client->networks); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
367 |
|
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
368 |
// build and return next value |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
369 |
if (net_next) |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
370 |
lua_net_create(L, net_next); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
371 |
else |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
372 |
lua_pushnil(L); |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
373 |
|
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
374 |
return 1; |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
375 |
} |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
376 |
|
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
377 |
static int lua_client_networks (lua_State *L) |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
378 |
{ |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
379 |
struct lua_client *lua_client; |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
380 |
|
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
381 |
// parse args |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
382 |
lua_args_parse(L, &lua_client_networks_func, (void *) &lua_client); |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
383 |
|
199
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
384 |
// push iter func |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
385 |
lua_pushcfunction(L, lua_client_networks_iter); |
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
386 |
|
199
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
387 |
// push invariant state - the lua_client |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
388 |
lua_pushvalue(L, 1); |
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
389 |
|
199
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
390 |
// return three-tuple |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
198
diff
changeset
|
391 |
return 2; |
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
392 |
} |
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
393 |
|
145 | 394 |
static struct lua_func lua_client_quit_func = LUA_FUNC(&lua_client_type, "quit", |
395 |
"Disconnect from all networks", |
|
396 |
||
397 |
LUA_FUNC_ARG_STRING("message", "Bye") |
|
398 |
); |
|
399 |
||
116 | 400 |
static int lua_client_quit (lua_State *L) |
401 |
{ |
|
145 | 402 |
struct lua_client *lua_client; |
116 | 403 |
err_t err; |
145 | 404 |
const char *message; |
405 |
||
406 |
// parse args |
|
407 |
lua_args_parse(L, &lua_client_quit_func, (void *) &lua_client, |
|
408 |
&message |
|
409 |
); |
|
116 | 410 |
|
411 |
// execute |
|
412 |
if ((err = irc_client_quit(lua_client->client, message))) |
|
413 |
return luaL_error(L, "irc_client_quit: %s", error_name(err)); |
|
414 |
||
415 |
// ok |
|
416 |
return 0; |
|
417 |
} |
|
418 |
||
145 | 419 |
static struct lua_method lua_client_methods[] = LUA_METHODS( |
420 |
LUA_METHOD("set_defaults", lua_client_set_defaults, &lua_client_set_defaults_func ), |
|
421 |
LUA_METHOD("connect", lua_client_connect, &lua_client_connect_func ), |
|
422 |
LUA_METHOD("network", lua_client_get_network, &lua_client_get_network_func ), |
|
198
b74185e1357a
implement lua_client_networks and lua_network_channels
Tero Marttila <terom@fixme.fi>
parents:
180
diff
changeset
|
423 |
LUA_METHOD("networks", lua_client_networks, &lua_client_networks_func ), |
145 | 424 |
LUA_METHOD("quit", lua_client_quit, &lua_client_quit_func ) |
425 |
); |
|
116 | 426 |
|
427 |
void lua_irc_init (struct nexus_lua *lua) |
|
428 |
{ |
|
145 | 429 |
// register types |
430 |
lua_type_register(lua->st, &lua_chan_type, lua_chan_methods); |
|
431 |
lua_type_register(lua->st, &lua_net_type, lua_net_methods); |
|
432 |
||
433 |
// register the global "client" object |
|
434 |
struct lua_client *lua_client = lua_type_register_global(lua->st, &lua_client_type, lua_client_methods, "client", sizeof(*lua_client)); |
|
435 |
||
436 |
// initialize it |
|
437 |
lua_client->client = lua->nexus->client; |
|
116 | 438 |
} |
439 |