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