]>
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"
9 #include "../core/config.h"
16 nick
*lua_nick
= NULL
;
17 void *myureconnect
= NULL
, *myublip
= NULL
, *myutick
= NULL
;
19 void lua_bothandler(nick
*target
, int type
, void **args
);
21 void lua_onnewnick(int hooknum
, void *arg
);
22 void lua_blip(void *arg
);
23 void lua_tick(void *arg
);
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
);
34 void lua_onpart(int hooknum
, void *arg
);
38 void lua_registerevents(void) {
39 registerhook(HOOK_CHANNEL_MODECHANGE
, &lua_onmode
);
40 registerhook(HOOK_NICK_NEWNICK
, &lua_onnewnick
);
41 registerhook(HOOK_IRC_DISCON
, &lua_ondisconnect
);
42 registerhook(HOOK_IRC_PRE_DISCON
, &lua_ondisconnect
);
43 registerhook(HOOK_NICK_ACCOUNT
, &lua_onauth
);
44 registerhook(HOOK_CHANNEL_TOPIC
, &lua_ontopic
);
45 registerhook(HOOK_CHANNEL_KICK
, &lua_onkick
);
46 registerhook(HOOK_CHANNEL_OPPED
, &lua_onop
);
47 registerhook(HOOK_CHANNEL_DEOPPED
, &lua_onop
);
48 registerhook(HOOK_NICK_LOSTNICK
, &lua_onquit
);
49 registerhook(HOOK_NICK_RENAME
, &lua_onrename
);
50 registerhook(HOOK_IRC_CONNECTED
, &lua_onconnect
);
51 registerhook(HOOK_SERVER_END_OF_BURST
, &lua_onconnect
);
52 registerhook(HOOK_CHANNEL_JOIN
, &lua_onjoin
);
53 registerhook(HOOK_CHANNEL_PART
, &lua_onpart
);
54 registerhook(HOOK_CHANNEL_CREATE
, &lua_onjoin
);
57 void lua_deregisterevents(void) {
58 deregisterhook(HOOK_CHANNEL_PART
, &lua_onpart
);
59 deregisterhook(HOOK_CHANNEL_CREATE
, &lua_onjoin
);
60 deregisterhook(HOOK_CHANNEL_JOIN
, &lua_onjoin
);
61 deregisterhook(HOOK_SERVER_END_OF_BURST
, &lua_onconnect
);
62 deregisterhook(HOOK_IRC_CONNECTED
, &lua_onconnect
);
63 deregisterhook(HOOK_NICK_RENAME
, &lua_onrename
);
64 deregisterhook(HOOK_NICK_LOSTNICK
, &lua_onquit
);
65 deregisterhook(HOOK_CHANNEL_DEOPPED
, &lua_onop
);
66 deregisterhook(HOOK_CHANNEL_OPPED
, &lua_onop
);
67 deregisterhook(HOOK_CHANNEL_KICK
, &lua_onkick
);
68 deregisterhook(HOOK_CHANNEL_TOPIC
, &lua_ontopic
);
69 deregisterhook(HOOK_NICK_ACCOUNT
, &lua_onauth
);
70 deregisterhook(HOOK_IRC_PRE_DISCON
, &lua_ondisconnect
);
71 deregisterhook(HOOK_IRC_DISCON
, &lua_ondisconnect
);
72 deregisterhook(HOOK_NICK_NEWNICK
, &lua_onnewnick
);
73 deregisterhook(HOOK_CHANNEL_MODECHANGE
, &lua_onmode
);
76 void lua_startbot(void *arg
) {
82 luabotnick
= getcopyconfigitem("lua", "botnick", "U", NICKLEN
);
84 lua_nick
= registerlocaluser(luabotnick
->content
, "lua", "quakenet.department.of.corrections", LUA_FULLVERSION
, "U", UMODE_ACCOUNT
| UMODE_DEAF
| UMODE_OPER
| UMODE_SERVICE
, &lua_bothandler
);
86 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
90 cp
= findchannel(LUA_OPERCHAN
);
91 if(cp
&& localjoinchannel(lua_nick
, cp
)) {
92 localgetops(lua_nick
, cp
);
94 localcreatechannel(lua_nick
, LUA_OPERCHAN
);
97 cp
= findchannel(LUA_PUKECHAN
);
98 if(cp
&& localjoinchannel(lua_nick
, cp
)) {
99 localgetops(lua_nick
, cp
);
101 localcreatechannel(lua_nick
, LUA_PUKECHAN
);
104 myublip
= schedulerecurring(time(NULL
) + 1, 0, 60, &lua_blip
, NULL
);
105 myutick
= schedulerecurring(time(NULL
) + 1, 0, 1, &lua_tick
, NULL
);
107 lua_registerevents();
110 void lua_destroybot(void) {
112 deleteschedule(myutick
, &lua_tick
, NULL
);
115 deleteschedule(myublip
, &lua_blip
, NULL
);
117 lua_deregisterevents();
120 deleteschedule(myureconnect
, &lua_startbot
, NULL
);
123 deregisterlocaluser(lua_nick
, NULL
);
126 freesstring(luabotnick
);
131 int _lua_vpcall(lua_State
*l
, void *function
, int mode
, const char *sig
, ...) {
133 int narg
= 0, nres
, top
= lua_gettop(l
);
135 lua_getglobal(l
, "scripterror");
136 if(mode
== LUA_CHARMODE
) {
137 lua_getglobal(l
, (const char *)function
);
139 lua_rawgeti(l
, LUA_REGISTRYINDEX
, (long)function
);
142 if(!lua_isfunction(l
, -1)) {
152 lua_pushint(l
, va_arg(va
, int));
155 lua_pushlong(l
, va_arg(va
, long));
158 lua_pushstring(l
, va_arg(va
, char *));
161 lua_pushstring(l
, ((sstring
*)(va_arg(va
, sstring
*)))->content
);
163 case 'L': /* BE VERY CAREFUL USING THIS, MAKE SURE YOU CAST THE VALUE TO LONG */
165 char *p
= va_arg(va
, char *);
166 long len
= va_arg(va
, long);
167 lua_pushlstring(l
, p
, len
);
172 nick
*np
= va_arg(va
, nick
*);
173 lua_pushnumeric(l
, np
->numeric
);
178 channel
*cp
= va_arg(va
, channel
*);
186 lua_rawgeti(l
, LUA_REGISTRYINDEX
, va_arg(va
, long));
189 lua_pushboolean(l
, va_arg(va
, int));
195 Error("lua", ERR_ERROR
, "Unable to parse vpcall signature (%c)", *(sig
- 1));
204 if(lua_debugpcall(l
, (mode
==LUA_CHARMODE
)?function
:"some_handler", narg
, nres
, top
+ 1)) {
205 Error("lua", ERR_ERROR
, "Error pcalling %s: %s.", (mode
==LUA_CHARMODE
)?(char *)function
:"some_handler", lua_tostring(l
, -1));
211 Error("lua", ERR_ERROR
, "Unable to parse vpcall return structure (%c)", *(sig
- 1));
222 void lua_bothandler(nick
*target
, int type
, void **args
) {
229 np
= (nick
*)args
[0];
235 lua_avpcall("irc_onmsg", "ls", np
->numeric
, p
);
239 np
= (nick
*)args
[0];
246 if((le
> 1) && (p
[0] == '\001')) {
247 if(p
[le
- 1] == '\001')
250 lua_avpcall("irc_onctcp", "ls", np
->numeric
, p
+ 1);
252 lua_avpcall("irc_onnotice", "ls", np
->numeric
, p
);
260 deleteschedule(myublip
, &lua_blip
, NULL
);
265 deleteschedule(myutick
, &lua_tick
, NULL
);
269 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
275 void lua_blip(void *arg
) {
276 lua_avpcall("onblip", "");
279 void lua_tick(void *arg
) {
280 lua_avpcall("ontick", "");
283 void lua_onnewnick(int hooknum
, void *arg
) {
284 nick
*np
= (nick
*)arg
;
289 lua_avpcall("irc_onnewnick", "l", np
->numeric
);
292 void lua_onkick(int hooknum
, void *arg
) {
293 void **arglist
= (void **)arg
;
294 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
295 nick
*kicked
= arglist
[1];
296 nick
*kicker
= arglist
[2];
297 char *message
= (char *)arglist
[3];
300 if(!kicker
|| IsOper(kicker
) || IsService(kicker
) || IsXOper(kicker
)) /* bloody Cruicky */
304 lua_avpcall("irc_onkick", "Slls", ci
->name
, kicked
->numeric
, kicker
->numeric
, message
);
306 lua_avpcall("irc_onkickall", "Slls", ci
->name
, kicked
->numeric
, kicker
->numeric
, message
);
308 lua_avpcall("irc_onkickall", "Sl0s", ci
->name
, kicked
->numeric
, message
);
312 void lua_ontopic(int hooknum
, void *arg
) {
313 void **arglist
= (void **)arg
;
314 channel
*cp
=(channel
*)arglist
[0];
315 nick
*np
= (nick
*)arglist
[1];
317 if(!np
|| IsOper(np
) || IsService(np
) || IsXOper(np
))
319 if(!cp
|| !cp
->topic
)
322 lua_avpcall("irc_ontopic", "SlS", cp
->index
->name
, np
->numeric
, cp
->topic
);
325 void lua_onop(int hooknum
, void *arg
) {
326 void **arglist
= (void **)arg
;
327 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
328 nick
*np
= arglist
[1];
329 nick
*target
= arglist
[2];
335 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "Sll", ci
->name
, np
->numeric
, target
->numeric
);
337 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "S0l", ci
->name
, target
->numeric
);
341 void lua_onjoin(int hooknum
, void *arg
) {
342 void **arglist
= (void **)arg
;
343 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
344 nick
*np
= arglist
[1];
349 lua_avpcall("irc_onjoin", "Sl", ci
->name
, np
->numeric
);
352 void lua_onpart(int hooknum
, void *arg
) {
353 void **arglist
= (void **)arg
;
354 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
355 nick
*np
= arglist
[1];
356 char *reason
= arglist
[2];
361 lua_avpcall("irc_onpart", "Sls", ci
->name
, np
->numeric
, reason
);
364 void lua_onrename(int hooknum
, void *arg
) {
365 void **harg
= (void **)arg
;
367 char *oldnick
= harg
[1];
372 lua_avpcall("irc_onrename", "ls", np
->numeric
, oldnick
);
375 void lua_onquit(int hooknum
, void *arg
) {
376 nick
*np
= (nick
*)arg
;
381 lua_avpcall("irc_onquit", "l", np
->numeric
);
384 void lua_onauth(int hooknum
, void *arg
) {
385 nick
*np
= (nick
*)arg
;
390 lua_avpcall("irc_onauth", "l", np
->numeric
);
393 void lua_ondisconnect(int hooknum
, void *arg
) {
394 lua_avpcall(hooknum
== HOOK_IRC_DISCON
?"irc_ondisconnect":"irc_onpredisconnect", "");
397 void lua_onconnect(int hooknum
, void *arg
) {
398 lua_avpcall(hooknum
== HOOK_IRC_CONNECTED
?"irc_onconnect":"irc_onendofburst", "");
401 void lua_onload(lua_State
*l
) {
402 lua_vpcall(l
, "onload", "");
405 void lua_onunload(lua_State
*l
) {
406 lua_vpcall(l
, "onunload", "");
409 int lua_channelmessage(channel
*cp
, char *message
, ...) {
413 va_start(va
, message
);
414 vsnprintf(buf
, sizeof(buf
), message
, va
);
417 sendmessagetochannel(lua_nick
, cp
, "%s", buf
);
422 int lua_message(nick
*np
, char *message
, ...) {
426 va_start(va
, message
);
427 vsnprintf(buf
, sizeof(buf
), message
, va
);
430 sendmessagetouser(lua_nick
, np
, "%s", buf
);
435 int lua_notice(nick
*np
, char *message
, ...) {
439 va_start(va
, message
);
440 vsnprintf(buf
, sizeof(buf
), message
, va
);
443 sendnoticetouser(lua_nick
, np
, "%s", buf
);
448 char *printallmodes(channel
*cp
) {
449 static char buf
[1024];
453 snprintf(buf2
, sizeof(buf2
), "%d", cp
->limit
);
455 snprintf(buf
, sizeof(buf
), "%s %s%s%s", printflags(cp
->flags
, cmodeflags
), IsLimit(cp
)?buf2
:"", IsLimit(cp
)?" ":"", IsKey(cp
)?cp
->key
->content
:"");
460 static char *printlimitedmodes(channel
*cp
, flag_t before
) {
461 static char buf
[1024];
463 snprintf(buf
, sizeof(buf
), "%s", printflags(cp
->flags
, cmodeflags
));
468 void lua_onmode(int hooknum
, void *arg
) {
469 void **arglist
= (void **)arg
;
470 channel
*cp
= (channel
*)arglist
[0];
471 chanindex
*ci
= cp
->index
;
472 nick
*np
= arglist
[1];
473 flag_t beforeflags
= (flag_t
)(long)arglist
[3];
476 lua_avpcall("irc_onmode", "Slss", ci
->name
, np
->numeric
, printallmodes(cp
), printlimitedmodes(cp
, beforeflags
));
478 lua_avpcall("irc_onmode", "S0ss", ci
->name
, printallmodes(cp
), printlimitedmodes(cp
, beforeflags
));