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