]>
Commit | Line | Data |
---|---|---|
2da56f0d CP |
1 | /* Copyright (C) Chris Porter 2005 */ |
2 | /* ALL RIGHTS RESERVED. */ | |
3 | /* Don't put this into the SVN repo. */ | |
4 | ||
5 | #include "../localuser/localuser.h" | |
6 | #include "../localuser/localuserchannel.h" | |
7 | #include "../core/schedule.h" | |
026b1c90 | 8 | #include "../lib/irc_string.h" |
dbe16450 | 9 | #include "../core/config.h" |
2da56f0d CP |
10 | |
11 | #include "lua.h" | |
12 | #include "luabot.h" | |
13 | ||
3522f515 CP |
14 | #include <stdarg.h> |
15 | ||
2da56f0d | 16 | nick *lua_nick = NULL; |
b86d82f2 | 17 | void *myureconnect = NULL, *myublip = NULL, *myutick = NULL; |
2da56f0d CP |
18 | |
19 | void lua_bothandler(nick *target, int type, void **args); | |
20 | ||
21 | void lua_onnewnick(int hooknum, void *arg); | |
22 | void lua_blip(void *arg); | |
b86d82f2 | 23 | void lua_tick(void *arg); |
2da56f0d CP |
24 | void lua_onkick(int hooknum, void *arg); |
25 | void lua_ontopic(int hooknum, void *arg); | |
26 | void lua_onauth(int hooknum, void *arg); | |
27 | void lua_ondisconnect(int hooknum, void *arg); | |
28 | void lua_onmode(int hooknum, void *arg); | |
29 | void lua_onop(int hooknum, void *arg); | |
30 | void lua_onquit(int hooknum, void *arg); | |
31 | void lua_onrename(int hooknum, void *arg); | |
32 | void lua_onconnect(int hooknum, void *arg); | |
33 | void lua_onjoin(int hooknum, void *arg); | |
e8b79634 | 34 | void lua_onpart(int hooknum, void *arg); |
2da56f0d CP |
35 | |
36 | void lua_registerevents(void) { | |
8e965b0c | 37 | registerhook(HOOK_CHANNEL_MODECHANGE, &lua_onmode); |
2da56f0d CP |
38 | registerhook(HOOK_NICK_NEWNICK, &lua_onnewnick); |
39 | registerhook(HOOK_IRC_DISCON, &lua_ondisconnect); | |
40 | registerhook(HOOK_IRC_PRE_DISCON, &lua_ondisconnect); | |
41 | registerhook(HOOK_NICK_ACCOUNT, &lua_onauth); | |
42 | registerhook(HOOK_CHANNEL_TOPIC, &lua_ontopic); | |
43 | registerhook(HOOK_CHANNEL_KICK, &lua_onkick); | |
44 | registerhook(HOOK_CHANNEL_OPPED, &lua_onop); | |
45 | registerhook(HOOK_CHANNEL_DEOPPED, &lua_onop); | |
46 | registerhook(HOOK_NICK_LOSTNICK, &lua_onquit); | |
47 | registerhook(HOOK_NICK_RENAME, &lua_onrename); | |
48 | registerhook(HOOK_IRC_CONNECTED, &lua_onconnect); | |
49 | registerhook(HOOK_SERVER_END_OF_BURST, &lua_onconnect); | |
50 | registerhook(HOOK_CHANNEL_JOIN, &lua_onjoin); | |
e8b79634 | 51 | registerhook(HOOK_CHANNEL_PART, &lua_onpart); |
6361ce98 | 52 | registerhook(HOOK_CHANNEL_CREATE, &lua_onjoin); |
2da56f0d CP |
53 | } |
54 | ||
55 | void lua_deregisterevents(void) { | |
e8b79634 | 56 | deregisterhook(HOOK_CHANNEL_PART, &lua_onpart); |
6361ce98 | 57 | deregisterhook(HOOK_CHANNEL_CREATE, &lua_onjoin); |
2da56f0d CP |
58 | deregisterhook(HOOK_CHANNEL_JOIN, &lua_onjoin); |
59 | deregisterhook(HOOK_SERVER_END_OF_BURST, &lua_onconnect); | |
60 | deregisterhook(HOOK_IRC_CONNECTED, &lua_onconnect); | |
61 | deregisterhook(HOOK_NICK_RENAME, &lua_onrename); | |
62 | deregisterhook(HOOK_NICK_LOSTNICK, &lua_onquit); | |
63 | deregisterhook(HOOK_CHANNEL_DEOPPED, &lua_onop); | |
64 | deregisterhook(HOOK_CHANNEL_OPPED, &lua_onop); | |
65 | deregisterhook(HOOK_CHANNEL_KICK, &lua_onkick); | |
66 | deregisterhook(HOOK_CHANNEL_TOPIC, &lua_ontopic); | |
67 | deregisterhook(HOOK_NICK_ACCOUNT, &lua_onauth); | |
68 | deregisterhook(HOOK_IRC_PRE_DISCON, &lua_ondisconnect); | |
69 | deregisterhook(HOOK_IRC_DISCON, &lua_ondisconnect); | |
70 | deregisterhook(HOOK_NICK_NEWNICK, &lua_onnewnick); | |
8e965b0c | 71 | deregisterhook(HOOK_CHANNEL_MODECHANGE, &lua_onmode); |
2da56f0d CP |
72 | } |
73 | ||
74 | void lua_startbot(void *arg) { | |
75 | channel *cp; | |
dbe16450 | 76 | sstring *n; |
2da56f0d CP |
77 | |
78 | myureconnect = NULL; | |
79 | ||
dbe16450 CP |
80 | n = getcopyconfigitem("lua", "botnick", "U", NICKLEN); |
81 | lua_nick = registerlocaluser(n->content, "lua", "quakenet.department.of.corrections", LUA_FULLVERSION, "U", UMODE_ACCOUNT | UMODE_DEAF | UMODE_OPER | UMODE_SERVICE, &lua_bothandler); | |
2da56f0d CP |
82 | if(!lua_nick) { |
83 | myureconnect = scheduleoneshot(time(NULL) + 1, &lua_startbot, NULL); | |
84 | return; | |
85 | } | |
86 | ||
87 | cp = findchannel(LUA_OPERCHAN); | |
0225bed3 | 88 | if(cp && localjoinchannel(lua_nick, cp)) { |
2da56f0d | 89 | localgetops(lua_nick, cp); |
0225bed3 CP |
90 | } else { |
91 | localcreatechannel(lua_nick, LUA_OPERCHAN); | |
92 | } | |
2da56f0d CP |
93 | |
94 | cp = findchannel(LUA_PUKECHAN); | |
0225bed3 | 95 | if(cp && localjoinchannel(lua_nick, cp)) { |
2da56f0d | 96 | localgetops(lua_nick, cp); |
0225bed3 CP |
97 | } else { |
98 | localcreatechannel(lua_nick, LUA_PUKECHAN); | |
99 | } | |
2da56f0d CP |
100 | |
101 | myublip = schedulerecurring(time(NULL) + 1, 0, 60, &lua_blip, NULL); | |
b86d82f2 | 102 | myutick = schedulerecurring(time(NULL) + 1, 0, 1, &lua_tick, NULL); |
2da56f0d CP |
103 | |
104 | lua_registerevents(); | |
105 | } | |
106 | ||
107 | void lua_destroybot(void) { | |
b86d82f2 CP |
108 | if(myutick) |
109 | deleteschedule(myutick, &lua_tick, NULL); | |
110 | ||
2da56f0d CP |
111 | if(myublip) |
112 | deleteschedule(myublip, &lua_blip, NULL); | |
113 | ||
114 | lua_deregisterevents(); | |
115 | ||
116 | if (myureconnect) | |
117 | deleteschedule(myureconnect, &lua_startbot, NULL); | |
118 | ||
119 | if(lua_nick) | |
120 | deregisterlocaluser(lua_nick, NULL); | |
121 | } | |
122 | ||
0225bed3 | 123 | int _lua_vpcall(lua_State *l, void *function, int mode, const char *sig, ...) { |
3522f515 CP |
124 | va_list va; |
125 | int narg = 0, nres, top = lua_gettop(l); | |
126 | ||
127 | lua_getglobal(l, "scripterror"); | |
0225bed3 CP |
128 | if(mode == LUA_CHARMODE) { |
129 | lua_getglobal(l, (const char *)function); | |
130 | } else { | |
abc75f54 | 131 | lua_rawgeti(l, LUA_REGISTRYINDEX, (long)function); |
0225bed3 CP |
132 | } |
133 | ||
3522f515 CP |
134 | if(!lua_isfunction(l, -1)) { |
135 | lua_settop(l, top); | |
136 | return 1; | |
137 | } | |
138 | ||
139 | va_start(va, sig); | |
140 | ||
141 | while(*sig) { | |
142 | switch(*sig++) { | |
143 | case 'i': | |
144 | lua_pushint(l, va_arg(va, int)); | |
145 | break; | |
146 | case 'l': | |
147 | lua_pushlong(l, va_arg(va, long)); | |
148 | break; | |
149 | case 's': | |
150 | lua_pushstring(l, va_arg(va, char *)); | |
151 | break; | |
152 | case 'S': | |
153 | lua_pushstring(l, ((sstring *)(va_arg(va, sstring *)))->content); | |
154 | break; | |
77762537 | 155 | case 'L': /* BE VERY CAREFUL USING THIS, MAKE SURE YOU CAST THE VALUE TO LONG */ |
065c0091 CP |
156 | { |
157 | char *p = va_arg(va, char *); | |
158 | long len = va_arg(va, long); | |
159 | lua_pushlstring(l, p, len); | |
160 | } | |
161 | break; | |
3522f515 CP |
162 | case 'N': |
163 | { | |
164 | nick *np = va_arg(va, nick *); | |
6606ba24 | 165 | lua_pushnumeric(l, np->numeric); |
3522f515 CP |
166 | break; |
167 | } | |
168 | case 'C': | |
169 | { | |
170 | channel *cp = va_arg(va, channel *); | |
171 | LUA_PUSHCHAN(l, cp); | |
172 | break; | |
173 | } | |
174 | case '0': | |
175 | lua_pushnil(l); | |
176 | break; | |
55cee062 CP |
177 | case 'R': |
178 | lua_rawgeti(l, LUA_REGISTRYINDEX, va_arg(va, long)); | |
179 | break; | |
180 | case 'b': | |
181 | lua_pushboolean(l, va_arg(va, int)); | |
182 | break; | |
3522f515 CP |
183 | case '>': |
184 | goto endwhile; | |
185 | ||
186 | default: | |
187 | Error("lua", ERR_ERROR, "Unable to parse vpcall signature (%c)", *(sig - 1)); | |
188 | } | |
189 | narg++; | |
190 | } | |
191 | ||
192 | endwhile: | |
193 | ||
194 | nres = strlen(sig); | |
195 | ||
ea15be33 | 196 | if(lua_debugpcall(l, (mode==LUA_CHARMODE)?function:"some_handler", narg, nres, top + 1)) { |
16739dbe | 197 | Error("lua", ERR_ERROR, "Error pcalling %s: %s.", (mode==LUA_CHARMODE)?(char *)function:"some_handler", lua_tostring(l, -1)); |
ea15be33 CP |
198 | } else { |
199 | nres = -nres; | |
200 | while(*sig) { | |
201 | switch(*sig++) { | |
202 | default: | |
203 | Error("lua", ERR_ERROR, "Unable to parse vpcall return structure (%c)", *(sig - 1)); | |
204 | } | |
205 | nres++; | |
3522f515 | 206 | } |
3522f515 CP |
207 | } |
208 | ||
209 | lua_settop(l, top); | |
210 | va_end(va); | |
211 | return 0; | |
212 | } | |
213 | ||
2da56f0d CP |
214 | void lua_bothandler(nick *target, int type, void **args) { |
215 | nick *np; | |
216 | char *p; | |
3522f515 | 217 | int le; |
2da56f0d CP |
218 | |
219 | switch(type) { | |
220 | case LU_PRIVMSG: | |
221 | np = (nick *)args[0]; | |
222 | p = (char *)args[1]; | |
223 | ||
224 | if(!np || !p) | |
225 | return; | |
226 | ||
1f57d0e2 | 227 | lua_avpcall("irc_onmsg", "ls", np->numeric, p); |
2da56f0d CP |
228 | |
229 | break; | |
230 | case LU_PRIVNOTICE: | |
231 | np = (nick *)args[0]; | |
232 | p = (char *)args[1]; | |
233 | ||
234 | if(!np || !p) | |
235 | return; | |
236 | ||
237 | le = strlen(p); | |
238 | if((le > 1) && (p[0] == '\001')) { | |
239 | if(p[le - 1] == '\001') | |
240 | p[le - 1] = '\000'; | |
241 | ||
1f57d0e2 | 242 | lua_avpcall("irc_onctcp", "ls", np->numeric, p + 1); |
2da56f0d CP |
243 | |
244 | } | |
245 | ||
246 | break; | |
247 | ||
248 | case LU_KILLED: | |
249 | if(myublip) { | |
250 | myublip = NULL; | |
251 | deleteschedule(myublip, &lua_blip, NULL); | |
252 | } | |
253 | ||
b86d82f2 CP |
254 | if(myutick) { |
255 | myutick = NULL; | |
256 | deleteschedule(myutick, &lua_tick, NULL); | |
257 | } | |
258 | ||
2da56f0d CP |
259 | lua_nick = NULL; |
260 | myureconnect = scheduleoneshot(time(NULL) + 1, &lua_startbot, NULL); | |
261 | ||
262 | break; | |
263 | } | |
264 | } | |
265 | ||
266 | void lua_blip(void *arg) { | |
3522f515 | 267 | lua_avpcall("onblip", ""); |
b86d82f2 CP |
268 | } |
269 | ||
270 | void lua_tick(void *arg) { | |
3522f515 | 271 | lua_avpcall("ontick", ""); |
2da56f0d CP |
272 | } |
273 | ||
274 | void lua_onnewnick(int hooknum, void *arg) { | |
275 | nick *np = (nick *)arg; | |
2da56f0d CP |
276 | |
277 | if(!np) | |
278 | return; | |
279 | ||
1f57d0e2 | 280 | lua_avpcall("irc_onnewnick", "l", np->numeric); |
2da56f0d CP |
281 | } |
282 | ||
283 | void lua_onkick(int hooknum, void *arg) { | |
284 | void **arglist = (void **)arg; | |
285 | chanindex *ci = ((channel *)arglist[0])->index; | |
286 | nick *kicked = arglist[1]; | |
287 | nick *kicker = arglist[2]; | |
288 | char *message = (char *)arglist[3]; | |
a5ad613d | 289 | int mode = 1; |
2da56f0d CP |
290 | |
291 | if(!kicker || IsOper(kicker) || IsService(kicker) || IsXOper(kicker)) /* bloody Cruicky */ | |
a5ad613d | 292 | mode = 0; |
2da56f0d | 293 | |
21e8832b | 294 | if(mode) { |
ebdfcaa9 | 295 | lua_avpcall("irc_onkick", "Slls", ci->name, kicked->numeric, kicker->numeric, message); |
21e8832b CP |
296 | } else if(kicker) { |
297 | lua_avpcall("irc_onkickall", "Slls", ci->name, kicked->numeric, kicker->numeric, message); | |
1f57d0e2 | 298 | } else { |
21e8832b | 299 | lua_avpcall("irc_onkickall", "Sl0s", ci->name, kicked->numeric, message); |
1f57d0e2 | 300 | } |
2da56f0d CP |
301 | } |
302 | ||
303 | void lua_ontopic(int hooknum, void *arg) { | |
304 | void **arglist = (void **)arg; | |
305 | channel *cp=(channel*)arglist[0]; | |
306 | nick *np = (nick *)arglist[1]; | |
2da56f0d CP |
307 | |
308 | if(!np || IsOper(np) || IsService(np) || IsXOper(np)) | |
309 | return; | |
310 | if(!cp || !cp->topic) | |
311 | return; | |
312 | ||
1f57d0e2 | 313 | lua_avpcall("irc_ontopic", "SlS", cp->index->name, np->numeric, cp->topic); |
2da56f0d CP |
314 | } |
315 | ||
316 | void lua_onop(int hooknum, void *arg) { | |
317 | void **arglist = (void **)arg; | |
318 | chanindex *ci = ((channel *)arglist[0])->index; | |
319 | nick *np = arglist[1]; | |
320 | nick *target = arglist[2]; | |
2da56f0d CP |
321 | |
322 | if(!target) | |
323 | return; | |
324 | ||
3522f515 | 325 | if(np) { |
1f57d0e2 | 326 | lua_avpcall(hooknum == HOOK_CHANNEL_OPPED?"irc_onop":"irc_ondeop", "Sll", ci->name, np->numeric, target->numeric); |
3522f515 | 327 | } else { |
1f57d0e2 | 328 | lua_avpcall(hooknum == HOOK_CHANNEL_OPPED?"irc_onop":"irc_ondeop", "S0l", ci->name, target->numeric); |
3522f515 | 329 | } |
2da56f0d CP |
330 | } |
331 | ||
332 | void lua_onjoin(int hooknum, void *arg) { | |
333 | void **arglist = (void **)arg; | |
334 | chanindex *ci = ((channel *)arglist[0])->index; | |
335 | nick *np = arglist[1]; | |
2da56f0d CP |
336 | |
337 | if(!ci || !np) | |
338 | return; | |
339 | ||
1f57d0e2 | 340 | lua_avpcall("irc_onjoin", "Sl", ci->name, np->numeric); |
2da56f0d CP |
341 | } |
342 | ||
e8b79634 CP |
343 | void lua_onpart(int hooknum, void *arg) { |
344 | void **arglist = (void **)arg; | |
345 | chanindex *ci = ((channel *)arglist[0])->index; | |
346 | nick *np = arglist[1]; | |
347 | ||
348 | if(!ci || !np) | |
349 | return; | |
350 | ||
1f57d0e2 | 351 | lua_avpcall("irc_onpart", "Sl", ci->name, np->numeric); |
e8b79634 CP |
352 | } |
353 | ||
2da56f0d CP |
354 | void lua_onrename(int hooknum, void *arg) { |
355 | nick *np = (void *)arg; | |
2da56f0d CP |
356 | |
357 | if(!np) | |
358 | return; | |
359 | ||
1f57d0e2 | 360 | lua_avpcall("irc_onrename", "l", np->numeric); |
2da56f0d CP |
361 | } |
362 | ||
363 | void lua_onquit(int hooknum, void *arg) { | |
364 | nick *np = (nick *)arg; | |
2da56f0d CP |
365 | |
366 | if(!np) | |
367 | return; | |
368 | ||
1f57d0e2 | 369 | lua_avpcall("irc_onquit", "l", np->numeric); |
2da56f0d CP |
370 | } |
371 | ||
372 | void lua_onauth(int hooknum, void *arg) { | |
373 | nick *np = (nick *)arg; | |
2da56f0d CP |
374 | |
375 | if(!np) | |
376 | return; | |
377 | ||
1f57d0e2 | 378 | lua_avpcall("irc_onauth", "l", np->numeric); |
2da56f0d CP |
379 | } |
380 | ||
381 | void lua_ondisconnect(int hooknum, void *arg) { | |
3522f515 | 382 | lua_avpcall(hooknum == HOOK_IRC_DISCON?"irc_ondisconnect":"irc_onpredisconnect", ""); |
2da56f0d CP |
383 | } |
384 | ||
385 | void lua_onconnect(int hooknum, void *arg) { | |
3522f515 | 386 | lua_avpcall(hooknum == HOOK_IRC_CONNECTED?"irc_onconnect":"irc_onendofburst", ""); |
2da56f0d CP |
387 | } |
388 | ||
389 | void lua_onload(lua_State *l) { | |
3522f515 | 390 | lua_vpcall(l, "onload", ""); |
2da56f0d CP |
391 | } |
392 | ||
393 | void lua_onunload(lua_State *l) { | |
3522f515 | 394 | lua_vpcall(l, "onunload", ""); |
2da56f0d CP |
395 | } |
396 | ||
397 | int lua_channelmessage(channel *cp, char *message, ...) { | |
398 | char buf[512]; | |
399 | va_list va; | |
400 | ||
401 | va_start(va, message); | |
402 | vsnprintf(buf, sizeof(buf), message, va); | |
403 | va_end(va); | |
404 | ||
405 | sendmessagetochannel(lua_nick, cp, "%s", buf); | |
406 | ||
407 | return 0; | |
408 | } | |
409 | ||
410 | int lua_message(nick *np, char *message, ...) { | |
411 | char buf[512]; | |
412 | va_list va; | |
413 | ||
414 | va_start(va, message); | |
415 | vsnprintf(buf, sizeof(buf), message, va); | |
416 | va_end(va); | |
417 | ||
418 | sendmessagetouser(lua_nick, np, "%s", buf); | |
419 | ||
420 | return 0; | |
421 | } | |
422 | ||
423 | int lua_notice(nick *np, char *message, ...) { | |
424 | char buf[512]; | |
425 | va_list va; | |
426 | ||
427 | va_start(va, message); | |
428 | vsnprintf(buf, sizeof(buf), message, va); | |
429 | va_end(va); | |
430 | ||
431 | sendnoticetouser(lua_nick, np, "%s", buf); | |
432 | ||
433 | return 0; | |
434 | } | |
68c9a3de CP |
435 | |
436 | char *printallmodes(channel *cp) { | |
437 | static char buf[1024]; | |
438 | char buf2[12]; | |
439 | ||
440 | if(IsLimit(cp)) | |
441 | snprintf(buf2, sizeof(buf2), "%d", cp->limit); | |
442 | ||
443 | snprintf(buf, sizeof(buf), "%s %s%s%s", printflags(cp->flags, cmodeflags), IsLimit(cp)?buf2:"", IsLimit(cp)?" ":"", IsKey(cp)?cp->key->content:""); | |
444 | ||
445 | return buf; | |
446 | } | |
447 | ||
8e965b0c CP |
448 | void lua_onmode(int hooknum, void *arg) { |
449 | void **arglist = (void **)arg; | |
450 | channel *cp = (channel *)arglist[0]; | |
451 | chanindex *ci = cp->index; | |
452 | nick *np = arglist[1]; | |
453 | ||
454 | if(np) { | |
455 | lua_avpcall("irc_onmode", "Sls", ci->name, np->numeric, printallmodes(cp)); | |
456 | } else { | |
457 | lua_avpcall("irc_onmode", "S0s", ci->name, printallmodes(cp)); | |
458 | } | |
459 | } | |
460 | ||
461 |