]>
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
*);
171 Error("lua", ERR_ERROR
, "Unable to parse vpcall signature (%c)", *(sig
- 1));
180 if(lua_debugpcall(l
, (mode
==LUA_CHARMODE
)?function
:"some_handler", narg
, nres
, top
+ 1)) {
181 Error("lua", ERR_ERROR
, "Error pcalling %s: %s.", (mode
==LUA_CHARMODE
)?function
:"some_handler", lua_tostring(l
, -1));
187 Error("lua", ERR_ERROR
, "Unable to parse vpcall return structure (%c)", *(sig
- 1));
198 void lua_bothandler(nick
*target
, int type
, void **args
) {
205 np
= (nick
*)args
[0];
211 lua_avpcall("irc_onmsg", "ls", np
->numeric
, p
);
215 np
= (nick
*)args
[0];
222 if((le
> 1) && (p
[0] == '\001')) {
223 if(p
[le
- 1] == '\001')
226 lua_avpcall("irc_onctcp", "ls", np
->numeric
, p
+ 1);
235 deleteschedule(myublip
, &lua_blip
, NULL
);
240 deleteschedule(myutick
, &lua_tick
, NULL
);
244 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
250 void lua_blip(void *arg
) {
251 lua_avpcall("onblip", "");
254 void lua_tick(void *arg
) {
255 lua_avpcall("ontick", "");
258 void lua_onnewnick(int hooknum
, void *arg
) {
259 nick
*np
= (nick
*)arg
;
264 lua_avpcall("irc_onnewnick", "l", np
->numeric
);
267 void lua_onkick(int hooknum
, void *arg
) {
268 void **arglist
= (void **)arg
;
269 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
270 nick
*kicked
= arglist
[1];
271 nick
*kicker
= arglist
[2];
272 char *message
= (char *)arglist
[3];
274 if(!kicker
|| IsOper(kicker
) || IsService(kicker
) || IsXOper(kicker
)) /* bloody Cruicky */
278 lua_avpcall("irc_onkick", "Slls", ci
->name
, kicked
->numeric
, kicker
->numeric
, message
);
280 lua_avpcall("irc_onkick", "Sl0s", ci
->name
, kicked
->numeric
, message
);
284 void lua_ontopic(int hooknum
, void *arg
) {
285 void **arglist
= (void **)arg
;
286 channel
*cp
=(channel
*)arglist
[0];
287 nick
*np
= (nick
*)arglist
[1];
289 if(!np
|| IsOper(np
) || IsService(np
) || IsXOper(np
))
291 if(!cp
|| !cp
->topic
)
294 lua_avpcall("irc_ontopic", "SlS", cp
->index
->name
, np
->numeric
, cp
->topic
);
297 void lua_onop(int hooknum
, void *arg
) {
298 void **arglist
= (void **)arg
;
299 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
300 nick
*np
= arglist
[1];
301 nick
*target
= arglist
[2];
307 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "Sll", ci
->name
, np
->numeric
, target
->numeric
);
309 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "S0l", ci
->name
, target
->numeric
);
313 void lua_onjoin(int hooknum
, void *arg
) {
314 void **arglist
= (void **)arg
;
315 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
316 nick
*np
= arglist
[1];
321 lua_avpcall("irc_onjoin", "Sl", ci
->name
, np
->numeric
);
324 void lua_onpart(int hooknum
, void *arg
) {
325 void **arglist
= (void **)arg
;
326 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
327 nick
*np
= arglist
[1];
332 lua_avpcall("irc_onpart", "Sl", ci
->name
, np
->numeric
);
335 void lua_onrename(int hooknum
, void *arg
) {
336 nick
*np
= (void *)arg
;
341 lua_avpcall("irc_onrename", "l", np
->numeric
);
344 void lua_onquit(int hooknum
, void *arg
) {
345 nick
*np
= (nick
*)arg
;
350 lua_avpcall("irc_onquit", "l", np
->numeric
);
353 void lua_onauth(int hooknum
, void *arg
) {
354 nick
*np
= (nick
*)arg
;
359 lua_avpcall("irc_onauth", "l", np
->numeric
);
362 void lua_ondisconnect(int hooknum
, void *arg
) {
363 lua_avpcall(hooknum
== HOOK_IRC_DISCON
?"irc_ondisconnect":"irc_onpredisconnect", "");
366 void lua_onconnect(int hooknum
, void *arg
) {
367 lua_avpcall(hooknum
== HOOK_IRC_CONNECTED
?"irc_onconnect":"irc_onendofburst", "");
370 void lua_onload(lua_State
*l
) {
371 lua_vpcall(l
, "onload", "");
374 void lua_onunload(lua_State
*l
) {
375 lua_vpcall(l
, "onunload", "");
378 int lua_channelmessage(channel
*cp
, char *message
, ...) {
382 va_start(va
, message
);
383 vsnprintf(buf
, sizeof(buf
), message
, va
);
386 sendmessagetochannel(lua_nick
, cp
, "%s", buf
);
391 int lua_message(nick
*np
, char *message
, ...) {
395 va_start(va
, message
);
396 vsnprintf(buf
, sizeof(buf
), message
, va
);
399 sendmessagetouser(lua_nick
, np
, "%s", buf
);
404 int lua_notice(nick
*np
, char *message
, ...) {
408 va_start(va
, message
);
409 vsnprintf(buf
, sizeof(buf
), message
, va
);
412 sendnoticetouser(lua_nick
, np
, "%s", buf
);
417 char *printallmodes(channel
*cp
) {
418 static char buf
[1024];
422 snprintf(buf2
, sizeof(buf2
), "%d", cp
->limit
);
424 snprintf(buf
, sizeof(buf
), "%s %s%s%s", printflags(cp
->flags
, cmodeflags
), IsLimit(cp
)?buf2
:"", IsLimit(cp
)?" ":"", IsKey(cp
)?cp
->key
->content
:"");
429 void lua_onmode(int hooknum
, void *arg
) {
430 void **arglist
= (void **)arg
;
431 channel
*cp
= (channel
*)arglist
[0];
432 chanindex
*ci
= cp
->index
;
433 nick
*np
= arglist
[1];
436 lua_avpcall("irc_onmode", "Sls", ci
->name
, np
->numeric
, printallmodes(cp
));
438 lua_avpcall("irc_onmode", "S0s", ci
->name
, printallmodes(cp
));