]>
jfr.im git - irc/quakenet/newserv.git/blob - lua/luabot.c
1 /* Copyright (C) Chris Porter 2005 */
2 /* ALL RIGHTS RESERVED. */
3 /* Don't put this into the SVN repo. */
5 #include "../localuser/localuser.h"
6 #include "../localuser/localuserchannel.h"
7 #include "../core/schedule.h"
8 #include "../lib/irc_string.h"
15 nick
*lua_nick
= NULL
;
16 void *myureconnect
= NULL
, *myublip
= NULL
, *myutick
= NULL
;
18 void lua_bothandler(nick
*target
, int type
, void **args
);
20 void lua_onnewnick(int hooknum
, void *arg
);
21 void lua_blip(void *arg
);
22 void lua_tick(void *arg
);
23 void lua_onkick(int hooknum
, void *arg
);
24 void lua_ontopic(int hooknum
, void *arg
);
25 void lua_onauth(int hooknum
, void *arg
);
26 void lua_ondisconnect(int hooknum
, void *arg
);
27 void lua_onmode(int hooknum
, void *arg
);
28 void lua_onop(int hooknum
, void *arg
);
29 void lua_onquit(int hooknum
, void *arg
);
30 void lua_onrename(int hooknum
, void *arg
);
31 void lua_onconnect(int hooknum
, void *arg
);
32 void lua_onjoin(int hooknum
, void *arg
);
33 void lua_onpart(int hooknum
, void *arg
);
35 void lua_registerevents(void) {
36 registerhook(HOOK_CHANNEL_MODECHANGE
, &lua_onmode
);
37 registerhook(HOOK_NICK_NEWNICK
, &lua_onnewnick
);
38 registerhook(HOOK_IRC_DISCON
, &lua_ondisconnect
);
39 registerhook(HOOK_IRC_PRE_DISCON
, &lua_ondisconnect
);
40 registerhook(HOOK_NICK_ACCOUNT
, &lua_onauth
);
41 registerhook(HOOK_CHANNEL_TOPIC
, &lua_ontopic
);
42 registerhook(HOOK_CHANNEL_KICK
, &lua_onkick
);
43 registerhook(HOOK_CHANNEL_OPPED
, &lua_onop
);
44 registerhook(HOOK_CHANNEL_DEOPPED
, &lua_onop
);
45 registerhook(HOOK_NICK_LOSTNICK
, &lua_onquit
);
46 registerhook(HOOK_NICK_RENAME
, &lua_onrename
);
47 registerhook(HOOK_IRC_CONNECTED
, &lua_onconnect
);
48 registerhook(HOOK_SERVER_END_OF_BURST
, &lua_onconnect
);
49 registerhook(HOOK_CHANNEL_JOIN
, &lua_onjoin
);
50 registerhook(HOOK_CHANNEL_PART
, &lua_onpart
);
51 registerhook(HOOK_CHANNEL_CREATE
, &lua_onjoin
);
54 void lua_deregisterevents(void) {
55 deregisterhook(HOOK_CHANNEL_PART
, &lua_onpart
);
56 deregisterhook(HOOK_CHANNEL_CREATE
, &lua_onjoin
);
57 deregisterhook(HOOK_CHANNEL_JOIN
, &lua_onjoin
);
58 deregisterhook(HOOK_SERVER_END_OF_BURST
, &lua_onconnect
);
59 deregisterhook(HOOK_IRC_CONNECTED
, &lua_onconnect
);
60 deregisterhook(HOOK_NICK_RENAME
, &lua_onrename
);
61 deregisterhook(HOOK_NICK_LOSTNICK
, &lua_onquit
);
62 deregisterhook(HOOK_CHANNEL_DEOPPED
, &lua_onop
);
63 deregisterhook(HOOK_CHANNEL_OPPED
, &lua_onop
);
64 deregisterhook(HOOK_CHANNEL_KICK
, &lua_onkick
);
65 deregisterhook(HOOK_CHANNEL_TOPIC
, &lua_ontopic
);
66 deregisterhook(HOOK_NICK_ACCOUNT
, &lua_onauth
);
67 deregisterhook(HOOK_IRC_PRE_DISCON
, &lua_ondisconnect
);
68 deregisterhook(HOOK_IRC_DISCON
, &lua_ondisconnect
);
69 deregisterhook(HOOK_NICK_NEWNICK
, &lua_onnewnick
);
70 deregisterhook(HOOK_CHANNEL_MODECHANGE
, &lua_onmode
);
73 void lua_startbot(void *arg
) {
78 lua_nick
= registerlocaluser("U", "lua", "quakenet.department.of.corrections", LUA_FULLVERSION
, "U", UMODE_ACCOUNT
| UMODE_DEAF
| UMODE_OPER
| UMODE_SERVICE
, &lua_bothandler
);
80 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
84 cp
= findchannel(LUA_OPERCHAN
);
85 if(cp
&& localjoinchannel(lua_nick
, cp
)) {
86 localgetops(lua_nick
, cp
);
88 localcreatechannel(lua_nick
, LUA_OPERCHAN
);
91 cp
= findchannel(LUA_PUKECHAN
);
92 if(cp
&& localjoinchannel(lua_nick
, cp
)) {
93 localgetops(lua_nick
, cp
);
95 localcreatechannel(lua_nick
, LUA_PUKECHAN
);
98 myublip
= schedulerecurring(time(NULL
) + 1, 0, 60, &lua_blip
, NULL
);
99 myutick
= schedulerecurring(time(NULL
) + 1, 0, 1, &lua_tick
, NULL
);
101 lua_registerevents();
104 void lua_destroybot(void) {
106 deleteschedule(myutick
, &lua_tick
, NULL
);
109 deleteschedule(myublip
, &lua_blip
, NULL
);
111 lua_deregisterevents();
114 deleteschedule(myureconnect
, &lua_startbot
, NULL
);
117 deregisterlocaluser(lua_nick
, NULL
);
120 int _lua_vpcall(lua_State
*l
, void *function
, int mode
, const char *sig
, ...) {
122 int narg
= 0, nres
, top
= lua_gettop(l
);
124 lua_getglobal(l
, "scripterror");
125 if(mode
== LUA_CHARMODE
) {
126 lua_getglobal(l
, (const char *)function
);
128 lua_rawgeti(l
, LUA_REGISTRYINDEX
, (long)function
);
131 if(!lua_isfunction(l
, -1)) {
141 lua_pushint(l
, va_arg(va
, int));
144 lua_pushlong(l
, va_arg(va
, long));
147 lua_pushstring(l
, va_arg(va
, char *));
150 lua_pushstring(l
, ((sstring
*)(va_arg(va
, sstring
*)))->content
);
154 nick
*np
= va_arg(va
, nick
*);
160 channel
*cp
= va_arg(va
, channel
*);
168 lua_rawgeti(l
, LUA_REGISTRYINDEX
, va_arg(va
, long));
171 lua_pushboolean(l
, va_arg(va
, int));
177 Error("lua", ERR_ERROR
, "Unable to parse vpcall signature (%c)", *(sig
- 1));
186 if(lua_debugpcall(l
, (mode
==LUA_CHARMODE
)?function
:"some_handler", narg
, nres
, top
+ 1)) {
187 Error("lua", ERR_ERROR
, "Error pcalling %s: %s.", (mode
==LUA_CHARMODE
)?function
:"some_handler", lua_tostring(l
, -1));
193 Error("lua", ERR_ERROR
, "Unable to parse vpcall return structure (%c)", *(sig
- 1));
204 void lua_bothandler(nick
*target
, int type
, void **args
) {
211 np
= (nick
*)args
[0];
217 lua_avpcall("irc_onmsg", "ls", np
->numeric
, p
);
221 np
= (nick
*)args
[0];
228 if((le
> 1) && (p
[0] == '\001')) {
229 if(p
[le
- 1] == '\001')
232 lua_avpcall("irc_onctcp", "ls", np
->numeric
, p
+ 1);
241 deleteschedule(myublip
, &lua_blip
, NULL
);
246 deleteschedule(myutick
, &lua_tick
, NULL
);
250 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
256 void lua_blip(void *arg
) {
257 lua_avpcall("onblip", "");
260 void lua_tick(void *arg
) {
261 lua_avpcall("ontick", "");
264 void lua_onnewnick(int hooknum
, void *arg
) {
265 nick
*np
= (nick
*)arg
;
270 lua_avpcall("irc_onnewnick", "l", np
->numeric
);
273 void lua_onkick(int hooknum
, void *arg
) {
274 void **arglist
= (void **)arg
;
275 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
276 nick
*kicked
= arglist
[1];
277 nick
*kicker
= arglist
[2];
278 char *message
= (char *)arglist
[3];
281 if(IsOper(kicker
) || IsService(kicker
) || IsXOper(kicker
)) /* bloody Cruicky */
285 lua_avpcall(mode
?"irc_onkick":"irc_onkickall", "Slls", ci
->name
, kicked
->numeric
, kicker
->numeric
, message
);
287 lua_avpcall(mode
?"irc_onkick":"irc_onkickall", "Sl0s", ci
->name
, kicked
->numeric
, message
);
291 void lua_ontopic(int hooknum
, void *arg
) {
292 void **arglist
= (void **)arg
;
293 channel
*cp
=(channel
*)arglist
[0];
294 nick
*np
= (nick
*)arglist
[1];
296 if(!np
|| IsOper(np
) || IsService(np
) || IsXOper(np
))
298 if(!cp
|| !cp
->topic
)
301 lua_avpcall("irc_ontopic", "SlS", cp
->index
->name
, np
->numeric
, cp
->topic
);
304 void lua_onop(int hooknum
, void *arg
) {
305 void **arglist
= (void **)arg
;
306 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
307 nick
*np
= arglist
[1];
308 nick
*target
= arglist
[2];
314 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "Sll", ci
->name
, np
->numeric
, target
->numeric
);
316 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "S0l", ci
->name
, target
->numeric
);
320 void lua_onjoin(int hooknum
, void *arg
) {
321 void **arglist
= (void **)arg
;
322 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
323 nick
*np
= arglist
[1];
328 lua_avpcall("irc_onjoin", "Sl", ci
->name
, np
->numeric
);
331 void lua_onpart(int hooknum
, void *arg
) {
332 void **arglist
= (void **)arg
;
333 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
334 nick
*np
= arglist
[1];
339 lua_avpcall("irc_onpart", "Sl", ci
->name
, np
->numeric
);
342 void lua_onrename(int hooknum
, void *arg
) {
343 nick
*np
= (void *)arg
;
348 lua_avpcall("irc_onrename", "l", np
->numeric
);
351 void lua_onquit(int hooknum
, void *arg
) {
352 nick
*np
= (nick
*)arg
;
357 lua_avpcall("irc_onquit", "l", np
->numeric
);
360 void lua_onauth(int hooknum
, void *arg
) {
361 nick
*np
= (nick
*)arg
;
366 lua_avpcall("irc_onauth", "l", np
->numeric
);
369 void lua_ondisconnect(int hooknum
, void *arg
) {
370 lua_avpcall(hooknum
== HOOK_IRC_DISCON
?"irc_ondisconnect":"irc_onpredisconnect", "");
373 void lua_onconnect(int hooknum
, void *arg
) {
374 lua_avpcall(hooknum
== HOOK_IRC_CONNECTED
?"irc_onconnect":"irc_onendofburst", "");
377 void lua_onload(lua_State
*l
) {
378 lua_vpcall(l
, "onload", "");
381 void lua_onunload(lua_State
*l
) {
382 lua_vpcall(l
, "onunload", "");
385 int lua_channelmessage(channel
*cp
, char *message
, ...) {
389 va_start(va
, message
);
390 vsnprintf(buf
, sizeof(buf
), message
, va
);
393 sendmessagetochannel(lua_nick
, cp
, "%s", buf
);
398 int lua_message(nick
*np
, char *message
, ...) {
402 va_start(va
, message
);
403 vsnprintf(buf
, sizeof(buf
), message
, va
);
406 sendmessagetouser(lua_nick
, np
, "%s", buf
);
411 int lua_notice(nick
*np
, char *message
, ...) {
415 va_start(va
, message
);
416 vsnprintf(buf
, sizeof(buf
), message
, va
);
419 sendnoticetouser(lua_nick
, np
, "%s", buf
);
424 char *printallmodes(channel
*cp
) {
425 static char buf
[1024];
429 snprintf(buf2
, sizeof(buf2
), "%d", cp
->limit
);
431 snprintf(buf
, sizeof(buf
), "%s %s%s%s", printflags(cp
->flags
, cmodeflags
), IsLimit(cp
)?buf2
:"", IsLimit(cp
)?" ":"", IsKey(cp
)?cp
->key
->content
:"");
436 void lua_onmode(int hooknum
, void *arg
) {
437 void **arglist
= (void **)arg
;
438 channel
*cp
= (channel
*)arglist
[0];
439 chanindex
*ci
= cp
->index
;
440 nick
*np
= arglist
[1];
443 lua_avpcall("irc_onmode", "Sls", ci
->name
, np
->numeric
, printallmodes(cp
));
445 lua_avpcall("irc_onmode", "S0s", ci
->name
, printallmodes(cp
));