]>
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);
259 deleteschedule(myublip
, &lua_blip
, NULL
);
264 deleteschedule(myutick
, &lua_tick
, NULL
);
268 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
274 void lua_blip(void *arg
) {
275 lua_avpcall("onblip", "");
278 void lua_tick(void *arg
) {
279 lua_avpcall("ontick", "");
282 void lua_onnewnick(int hooknum
, void *arg
) {
283 nick
*np
= (nick
*)arg
;
288 lua_avpcall("irc_onnewnick", "l", np
->numeric
);
291 void lua_onkick(int hooknum
, void *arg
) {
292 void **arglist
= (void **)arg
;
293 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
294 nick
*kicked
= arglist
[1];
295 nick
*kicker
= arglist
[2];
296 char *message
= (char *)arglist
[3];
299 if(!kicker
|| IsOper(kicker
) || IsService(kicker
) || IsXOper(kicker
)) /* bloody Cruicky */
303 lua_avpcall("irc_onkick", "Slls", ci
->name
, kicked
->numeric
, kicker
->numeric
, message
);
305 lua_avpcall("irc_onkickall", "Slls", ci
->name
, kicked
->numeric
, kicker
->numeric
, message
);
307 lua_avpcall("irc_onkickall", "Sl0s", ci
->name
, kicked
->numeric
, message
);
311 void lua_ontopic(int hooknum
, void *arg
) {
312 void **arglist
= (void **)arg
;
313 channel
*cp
=(channel
*)arglist
[0];
314 nick
*np
= (nick
*)arglist
[1];
316 if(!np
|| IsOper(np
) || IsService(np
) || IsXOper(np
))
318 if(!cp
|| !cp
->topic
)
321 lua_avpcall("irc_ontopic", "SlS", cp
->index
->name
, np
->numeric
, cp
->topic
);
324 void lua_onop(int hooknum
, void *arg
) {
325 void **arglist
= (void **)arg
;
326 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
327 nick
*np
= arglist
[1];
328 nick
*target
= arglist
[2];
334 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "Sll", ci
->name
, np
->numeric
, target
->numeric
);
336 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "S0l", ci
->name
, target
->numeric
);
340 void lua_onjoin(int hooknum
, void *arg
) {
341 void **arglist
= (void **)arg
;
342 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
343 nick
*np
= arglist
[1];
348 lua_avpcall("irc_onjoin", "Sl", ci
->name
, np
->numeric
);
351 void lua_onpart(int hooknum
, void *arg
) {
352 void **arglist
= (void **)arg
;
353 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
354 nick
*np
= arglist
[1];
355 char *reason
= arglist
[2];
360 lua_avpcall("irc_onpart", "Sls", ci
->name
, np
->numeric
, reason
);
363 void lua_onrename(int hooknum
, void *arg
) {
364 void **harg
= (void **)arg
;
366 char *oldnick
= harg
[1];
371 lua_avpcall("irc_onrename", "ls", np
->numeric
, oldnick
);
374 void lua_onquit(int hooknum
, void *arg
) {
375 nick
*np
= (nick
*)arg
;
380 lua_avpcall("irc_onquit", "l", np
->numeric
);
383 void lua_onauth(int hooknum
, void *arg
) {
384 nick
*np
= (nick
*)arg
;
389 lua_avpcall("irc_onauth", "l", np
->numeric
);
392 void lua_ondisconnect(int hooknum
, void *arg
) {
393 lua_avpcall(hooknum
== HOOK_IRC_DISCON
?"irc_ondisconnect":"irc_onpredisconnect", "");
396 void lua_onconnect(int hooknum
, void *arg
) {
397 lua_avpcall(hooknum
== HOOK_IRC_CONNECTED
?"irc_onconnect":"irc_onendofburst", "");
400 void lua_onload(lua_State
*l
) {
401 lua_vpcall(l
, "onload", "");
404 void lua_onunload(lua_State
*l
) {
405 lua_vpcall(l
, "onunload", "");
408 int lua_channelmessage(channel
*cp
, char *message
, ...) {
412 va_start(va
, message
);
413 vsnprintf(buf
, sizeof(buf
), message
, va
);
416 sendmessagetochannel(lua_nick
, cp
, "%s", buf
);
421 int lua_message(nick
*np
, char *message
, ...) {
425 va_start(va
, message
);
426 vsnprintf(buf
, sizeof(buf
), message
, va
);
429 sendmessagetouser(lua_nick
, np
, "%s", buf
);
434 int lua_notice(nick
*np
, char *message
, ...) {
438 va_start(va
, message
);
439 vsnprintf(buf
, sizeof(buf
), message
, va
);
442 sendnoticetouser(lua_nick
, np
, "%s", buf
);
447 char *printallmodes(channel
*cp
) {
448 static char buf
[1024];
452 snprintf(buf2
, sizeof(buf2
), "%d", cp
->limit
);
454 snprintf(buf
, sizeof(buf
), "%s %s%s%s", printflags(cp
->flags
, cmodeflags
), IsLimit(cp
)?buf2
:"", IsLimit(cp
)?" ":"", IsKey(cp
)?cp
->key
->content
:"");
459 static char *printlimitedmodes(channel
*cp
, flag_t before
) {
460 static char buf
[1024];
462 snprintf(buf
, sizeof(buf
), "%s", printflags(cp
->flags
, cmodeflags
));
467 void lua_onmode(int hooknum
, void *arg
) {
468 void **arglist
= (void **)arg
;
469 channel
*cp
= (channel
*)arglist
[0];
470 chanindex
*ci
= cp
->index
;
471 nick
*np
= arglist
[1];
472 flag_t beforeflags
= (flag_t
)(long)arglist
[3];
475 lua_avpcall("irc_onmode", "Slss", ci
->name
, np
->numeric
, printallmodes(cp
), printlimitedmodes(cp
, beforeflags
));
477 lua_avpcall("irc_onmode", "S0ss", ci
->name
, printallmodes(cp
), printlimitedmodes(cp
, beforeflags
));