]>
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_onprequit(int hooknum
, void *arg
);
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
);
35 void lua_onpart(int hooknum
, void *arg
);
39 void lua_registerevents(void) {
40 registerhook(HOOK_CHANNEL_MODECHANGE
, &lua_onmode
);
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
);
49 registerhook(HOOK_NICK_PRE_LOSTNICK
, &lua_onprequit
);
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
);
55 registerhook(HOOK_CHANNEL_PART
, &lua_onpart
);
56 registerhook(HOOK_CHANNEL_CREATE
, &lua_onjoin
);
59 void lua_deregisterevents(void) {
60 deregisterhook(HOOK_CHANNEL_PART
, &lua_onpart
);
61 deregisterhook(HOOK_CHANNEL_CREATE
, &lua_onjoin
);
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
);
67 deregisterhook(HOOK_NICK_PRE_LOSTNICK
, &lua_onprequit
);
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
);
76 deregisterhook(HOOK_CHANNEL_MODECHANGE
, &lua_onmode
);
79 void lua_startbot(void *arg
) {
85 luabotnick
= getcopyconfigitem("lua", "botnick", "U", NICKLEN
);
87 lua_nick
= registerlocaluser(luabotnick
->content
, "lua", "quakenet.department.of.corrections", LUA_FULLVERSION
, "U", UMODE_ACCOUNT
| UMODE_DEAF
| UMODE_OPER
| UMODE_SERVICE
, &lua_bothandler
);
89 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
93 cp
= findchannel(LUA_OPERCHAN
);
94 if(cp
&& localjoinchannel(lua_nick
, cp
)) {
95 localgetops(lua_nick
, cp
);
97 localcreatechannel(lua_nick
, LUA_OPERCHAN
);
100 cp
= findchannel(LUA_PUKECHAN
);
101 if(cp
&& localjoinchannel(lua_nick
, cp
)) {
102 localgetops(lua_nick
, cp
);
104 localcreatechannel(lua_nick
, LUA_PUKECHAN
);
107 myublip
= schedulerecurring(time(NULL
) + 1, 0, 60, &lua_blip
, NULL
);
108 myutick
= schedulerecurring(time(NULL
) + 1, 0, 1, &lua_tick
, NULL
);
110 lua_registerevents();
113 void lua_destroybot(void) {
115 deleteschedule(myutick
, &lua_tick
, NULL
);
118 deleteschedule(myublip
, &lua_blip
, NULL
);
120 lua_deregisterevents();
123 deleteschedule(myureconnect
, &lua_startbot
, NULL
);
126 deregisterlocaluser(lua_nick
, NULL
);
129 freesstring(luabotnick
);
134 int _lua_vpcall(lua_State
*l
, void *function
, int mode
, const char *sig
, ...) {
136 int narg
= 0, nres
, top
= lua_gettop(l
);
138 lua_getglobal(l
, "scripterror");
139 if(mode
== LUA_CHARMODE
) {
140 lua_getglobal(l
, (const char *)function
);
142 lua_rawgeti(l
, LUA_REGISTRYINDEX
, (long)function
);
145 if(!lua_isfunction(l
, -1)) {
155 lua_pushint(l
, va_arg(va
, int));
158 lua_pushlong(l
, va_arg(va
, long));
161 lua_pushstring(l
, va_arg(va
, char *));
164 lua_pushstring(l
, ((sstring
*)(va_arg(va
, sstring
*)))->content
);
166 case 'L': /* BE VERY CAREFUL USING THIS, MAKE SURE YOU CAST THE VALUE TO LONG */
168 char *p
= va_arg(va
, char *);
169 long len
= va_arg(va
, long);
170 lua_pushlstring(l
, p
, len
);
175 nick
*np
= va_arg(va
, nick
*);
176 lua_pushnumeric(l
, np
->numeric
);
181 channel
*cp
= va_arg(va
, channel
*);
189 lua_rawgeti(l
, LUA_REGISTRYINDEX
, va_arg(va
, long));
192 lua_pushboolean(l
, va_arg(va
, int));
198 Error("lua", ERR_ERROR
, "Unable to parse vpcall signature (%c)", *(sig
- 1));
207 if(lua_debugpcall(l
, (mode
==LUA_CHARMODE
)?function
:"some_handler", narg
, nres
, top
+ 1)) {
208 Error("lua", ERR_ERROR
, "Error pcalling %s: %s.", (mode
==LUA_CHARMODE
)?(char *)function
:"some_handler", lua_tostring(l
, -1));
214 Error("lua", ERR_ERROR
, "Unable to parse vpcall return structure (%c)", *(sig
- 1));
225 void lua_bothandler(nick
*target
, int type
, void **args
) {
232 np
= (nick
*)args
[0];
238 lua_avpcall("irc_onmsg", "ls", np
->numeric
, p
);
242 np
= (nick
*)args
[0];
249 if((le
> 1) && (p
[0] == '\001')) {
250 if(p
[le
- 1] == '\001')
253 lua_avpcall("irc_onctcp", "ls", np
->numeric
, p
+ 1);
255 lua_avpcall("irc_onnotice", "ls", np
->numeric
, p
);
263 deleteschedule(myublip
, &lua_blip
, NULL
);
268 deleteschedule(myutick
, &lua_tick
, NULL
);
272 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
278 void lua_blip(void *arg
) {
279 lua_avpcall("onblip", "");
282 void lua_tick(void *arg
) {
283 lua_avpcall("ontick", "");
286 void lua_onnewnick(int hooknum
, void *arg
) {
287 nick
*np
= (nick
*)arg
;
292 lua_avpcall("irc_onnewnick", "l", np
->numeric
);
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];
303 lua_avpcall("irc_onkick", "Slls", ci
->name
, kicked
->numeric
, kicker
->numeric
, message
);
305 lua_avpcall("irc_onkick", "Sl0s", ci
->name
, kicked
->numeric
, message
);
308 void lua_ontopic(int hooknum
, void *arg
) {
309 void **arglist
= (void **)arg
;
310 channel
*cp
=(channel
*)arglist
[0];
311 nick
*np
= (nick
*)arglist
[1];
313 if(!cp
|| !cp
->topic
)
317 lua_avpcall("irc_ontopic", "SlS", cp
->index
->name
, np
->numeric
, cp
->topic
);
319 lua_avpcall("irc_ontopic", "S0S", cp
->index
->name
, cp
->topic
);
322 void lua_onop(int hooknum
, void *arg
) {
323 void **arglist
= (void **)arg
;
324 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
325 nick
*np
= arglist
[1];
326 nick
*target
= arglist
[2];
332 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "Sll", ci
->name
, np
->numeric
, target
->numeric
);
334 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "S0l", ci
->name
, target
->numeric
);
338 void lua_onjoin(int hooknum
, void *arg
) {
339 void **arglist
= (void **)arg
;
340 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
341 nick
*np
= arglist
[1];
346 lua_avpcall("irc_onjoin", "Sl", ci
->name
, np
->numeric
);
349 void lua_onpart(int hooknum
, void *arg
) {
350 void **arglist
= (void **)arg
;
351 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
352 nick
*np
= arglist
[1];
353 char *reason
= arglist
[2];
358 lua_avpcall("irc_onpart", "Sls", ci
->name
, np
->numeric
, reason
);
361 void lua_onrename(int hooknum
, void *arg
) {
362 void **harg
= (void **)arg
;
364 char *oldnick
= harg
[1];
369 lua_avpcall("irc_onrename", "ls", np
->numeric
, oldnick
);
372 void lua_onquit(int hooknum
, void *arg
) {
373 nick
*np
= (nick
*)arg
;
378 lua_avpcall("irc_onquit", "l", np
->numeric
);
381 void lua_onprequit(int hooknum
, void *arg
) {
382 nick
*np
= (nick
*)arg
;
387 lua_avpcall("irc_onprequit", "l", np
->numeric
);
390 void lua_onauth(int hooknum
, void *arg
) {
391 nick
*np
= (nick
*)arg
;
396 lua_avpcall("irc_onauth", "l", np
->numeric
);
399 void lua_ondisconnect(int hooknum
, void *arg
) {
400 lua_avpcall(hooknum
== HOOK_IRC_DISCON
?"irc_ondisconnect":"irc_onpredisconnect", "");
403 void lua_onconnect(int hooknum
, void *arg
) {
404 lua_avpcall(hooknum
== HOOK_IRC_CONNECTED
?"irc_onconnect":"irc_onendofburst", "");
407 void lua_onload(lua_State
*l
) {
408 lua_vpcall(l
, "onload", "");
411 void lua_onunload(lua_State
*l
) {
412 lua_vpcall(l
, "onunload", "");
415 int lua_channelmessage(channel
*cp
, char *message
, ...) {
419 va_start(va
, message
);
420 vsnprintf(buf
, sizeof(buf
), message
, va
);
423 sendmessagetochannel(lua_nick
, cp
, "%s", buf
);
428 int lua_message(nick
*np
, char *message
, ...) {
432 va_start(va
, message
);
433 vsnprintf(buf
, sizeof(buf
), message
, va
);
436 sendmessagetouser(lua_nick
, np
, "%s", buf
);
441 int lua_notice(nick
*np
, char *message
, ...) {
445 va_start(va
, message
);
446 vsnprintf(buf
, sizeof(buf
), message
, va
);
449 sendnoticetouser(lua_nick
, np
, "%s", buf
);
454 char *printallmodes(channel
*cp
) {
455 static char buf
[1024];
459 snprintf(buf2
, sizeof(buf2
), "%d", cp
->limit
);
461 snprintf(buf
, sizeof(buf
), "%s %s%s%s", printflags(cp
->flags
, cmodeflags
), IsLimit(cp
)?buf2
:"", IsLimit(cp
)?" ":"", IsKey(cp
)?cp
->key
->content
:"");
466 static char *printlimitedmodes(channel
*cp
, flag_t before
) {
467 static char buf
[1024];
469 snprintf(buf
, sizeof(buf
), "%s", printflags(cp
->flags
, cmodeflags
));
474 void lua_onmode(int hooknum
, void *arg
) {
475 void **arglist
= (void **)arg
;
476 channel
*cp
= (channel
*)arglist
[0];
477 chanindex
*ci
= cp
->index
;
478 nick
*np
= arglist
[1];
479 flag_t beforeflags
= (flag_t
)(long)arglist
[3];
482 lua_avpcall("irc_onmode", "Slss", ci
->name
, np
->numeric
, printallmodes(cp
), printlimitedmodes(cp
, beforeflags
));
484 lua_avpcall("irc_onmode", "S0ss", ci
->name
, printallmodes(cp
), printlimitedmodes(cp
, beforeflags
));