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-- |
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 |
|
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 | 107 |
|
108 |
// add it |
|
109 |
if (irc_net_add_chan(lua_net->net, &chan, &chan_info, &err)) |
|
110 |
return luaL_error(L, "irc_net_add_chan: %s: %s", chan_info.channel, error_msg(&err)); |
|
111 |
||
112 |
// return it |
|
113 |
return lua_chan_create(L, chan); |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Look up a channel by name, returning the lua_chan |
|
118 |
*/ |
|
119 |
static int lua_net_get_chan (lua_State *L) |
|
120 |
{ |
|
121 |
struct lua_net *lua_net = lua_obj_get_obj(L, __func__, "evirc.net"); |
|
122 |
struct irc_chan *chan; |
|
123 |
||
124 |
// the channel name |
|
125 |
const char *channel = luaL_checkstring(L, 2); |
|
126 |
||
127 |
// lookup the irc_chan |
|
128 |
if ((chan = irc_net_get_chan(lua_net->net, channel)) == NULL) |
|
129 |
return luaL_error(L, "irc_net_get_chan: no such channel: %s", channel); |
|
130 |
||
131 |
// wrap it |
|
132 |
return lua_chan_create(L, chan); |
|
133 |
} |
|
134 |
||
135 |
static const struct luaL_Reg lua_net_methods[] = { |
|
136 |
{ "__tostring", &lua_net_tostring }, |
|
137 |
{ "join", &lua_net_join }, |
|
138 |
{ "channel", &lua_net_get_chan }, |
|
139 |
{ NULL, NULL } |
|
140 |
}; |
|
141 |
||
142 |
/** |
|
143 |
* Initialize the lua_net object type |
|
144 |
*/ |
|
145 |
static void lua_net_init (lua_State *L) |
|
146 |
{ |
|
147 |
lua_obj_create_type(L, "evirc.net", lua_net_methods); |
|
148 |
} |
|
149 |
||
150 |
static int lua_client_set_defaults (lua_State *L) |
|
151 |
{ |
|
152 |
struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client"); |
|
153 |
||
154 |
// read the args |
|
155 |
// XXX: need to copy these, really |
|
156 |
lua_client->defaults.register_info.nickname = luaL_checkstring(L, 2); |
|
157 |
lua_client->defaults.register_info.username = luaL_checkstring(L, 3); |
|
158 |
lua_client->defaults.register_info.realname = luaL_checkstring(L, 4); |
|
159 |
||
160 |
// set the non-args |
|
161 |
lua_client->defaults.service = IRC_PORT; |
|
162 |
lua_client->defaults.service_ssl = IRC_SSL_PORT; |
|
163 |
||
164 |
// invoke |
|
165 |
irc_client_set_defaults(lua_client->client, &lua_client->defaults); |
|
166 |
||
167 |
// ok |
|
168 |
return 0; |
|
169 |
} |
|
170 |
||
171 |
static int lua_client_connect (lua_State *L) |
|
172 |
{ |
|
173 |
struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client"); |
|
174 |
struct irc_net_info net_info; |
|
175 |
struct irc_net *net; |
|
176 |
struct error_info err; |
|
177 |
||
178 |
// init net_info |
|
179 |
memset(&net_info, 0, sizeof(net_info)); |
|
180 |
||
181 |
// the network name |
|
182 |
net_info.network = luaL_checkstring(L, 2); |
|
183 |
||
184 |
// the hostname |
|
185 |
net_info.hostname = luaL_checkstring(L, 3); |
|
186 |
||
187 |
// service remains default |
|
188 |
net_info.service = "6667"; |
|
189 |
||
190 |
// create it |
|
191 |
if (irc_client_add_net(lua_client->client, &net, &net_info, &err)) |
|
192 |
return luaL_error(L, "irc_client_add_net: %s/%s: %s", net_info.network, net_info.hostname, error_msg(&err)); |
|
193 |
||
194 |
// wrap it |
|
195 |
return lua_net_create(L, net); |
|
196 |
} |
|
197 |
||
198 |
static int lua_client_get_network (lua_State *L) |
|
199 |
{ |
|
200 |
struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client"); |
|
201 |
struct irc_net *net; |
|
202 |
||
203 |
// the network name |
|
204 |
const char *network = luaL_checkstring(L, 2); |
|
205 |
||
206 |
// lookup the irc_net |
|
207 |
if ((net = irc_client_get_net(lua_client->client, network)) == NULL) |
|
208 |
return luaL_error(L, "irc_client_get_net: no such network: %s", network); |
|
209 |
||
210 |
// wrap it |
|
211 |
return lua_net_create(L, net); |
|
212 |
} |
|
213 |
||
214 |
static int lua_client_quit (lua_State *L) |
|
215 |
{ |
|
216 |
struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client"); |
|
217 |
err_t err; |
|
218 |
||
219 |
// the message |
|
220 |
const char *message = luaL_checkstring(L, 2); |
|
221 |
||
222 |
// execute |
|
223 |
if ((err = irc_client_quit(lua_client->client, message))) |
|
224 |
return luaL_error(L, "irc_client_quit: %s", error_name(err)); |
|
225 |
||
226 |
// ok |
|
227 |
return 0; |
|
228 |
} |
|
229 |
||
230 |
static const struct luaL_Reg lua_client_methods[] = { |
|
231 |
{ "set_defaults", &lua_client_set_defaults }, |
|
232 |
{ "connect", &lua_client_connect }, |
|
233 |
{ "network", &lua_client_get_network }, |
|
234 |
{ "quit", &lua_client_quit }, |
|
235 |
{ NULL, NULL } |
|
236 |
}; |
|
237 |
||
238 |
/** |
|
239 |
* Initialize the evirc.client type for lua_client, and registers an instance bound to the given irc_client at |
|
240 |
* 'client'. |
|
241 |
*/ |
|
242 |
static void lua_client_init (lua_State *L, struct irc_client *client) |
|
243 |
{ |
|
244 |
// allocate the global "client" object |
|
245 |
struct lua_client *lua_client = lua_obj_create_global_type(L, "evirc.client", lua_client_methods, "client", sizeof(*lua_client)); |
|
246 |
||
247 |
// initialize it |
|
248 |
lua_client->client = client; |
|
249 |
} |
|
250 |
||
251 |
void lua_irc_init (struct nexus_lua *lua) |
|
252 |
{ |
|
253 |
lua_client_init(lua->st, lua->nexus->client); |
|
254 |
lua_net_init(lua->st); |
|
255 |
lua_chan_init(lua->st); |
|
256 |
} |
|
257 |