]>
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
);
36 void lua_registerevents(void) {
37 registerhook(HOOK_CHANNEL_MODECHANGE
, &lua_onmode
);
38 registerhook(HOOK_NICK_NEWNICK
, &lua_onnewnick
);
39 registerhook(HOOK_IRC_DISCON
, &lua_ondisconnect
);
40 registerhook(HOOK_IRC_PRE_DISCON
, &lua_ondisconnect
);
41 registerhook(HOOK_NICK_ACCOUNT
, &lua_onauth
);
42 registerhook(HOOK_CHANNEL_TOPIC
, &lua_ontopic
);
43 registerhook(HOOK_CHANNEL_KICK
, &lua_onkick
);
44 registerhook(HOOK_CHANNEL_OPPED
, &lua_onop
);
45 registerhook(HOOK_CHANNEL_DEOPPED
, &lua_onop
);
46 registerhook(HOOK_NICK_LOSTNICK
, &lua_onquit
);
47 registerhook(HOOK_NICK_RENAME
, &lua_onrename
);
48 registerhook(HOOK_IRC_CONNECTED
, &lua_onconnect
);
49 registerhook(HOOK_SERVER_END_OF_BURST
, &lua_onconnect
);
50 registerhook(HOOK_CHANNEL_JOIN
, &lua_onjoin
);
51 registerhook(HOOK_CHANNEL_PART
, &lua_onpart
);
52 registerhook(HOOK_CHANNEL_CREATE
, &lua_onjoin
);
55 void lua_deregisterevents(void) {
56 deregisterhook(HOOK_CHANNEL_PART
, &lua_onpart
);
57 deregisterhook(HOOK_CHANNEL_CREATE
, &lua_onjoin
);
58 deregisterhook(HOOK_CHANNEL_JOIN
, &lua_onjoin
);
59 deregisterhook(HOOK_SERVER_END_OF_BURST
, &lua_onconnect
);
60 deregisterhook(HOOK_IRC_CONNECTED
, &lua_onconnect
);
61 deregisterhook(HOOK_NICK_RENAME
, &lua_onrename
);
62 deregisterhook(HOOK_NICK_LOSTNICK
, &lua_onquit
);
63 deregisterhook(HOOK_CHANNEL_DEOPPED
, &lua_onop
);
64 deregisterhook(HOOK_CHANNEL_OPPED
, &lua_onop
);
65 deregisterhook(HOOK_CHANNEL_KICK
, &lua_onkick
);
66 deregisterhook(HOOK_CHANNEL_TOPIC
, &lua_ontopic
);
67 deregisterhook(HOOK_NICK_ACCOUNT
, &lua_onauth
);
68 deregisterhook(HOOK_IRC_PRE_DISCON
, &lua_ondisconnect
);
69 deregisterhook(HOOK_IRC_DISCON
, &lua_ondisconnect
);
70 deregisterhook(HOOK_NICK_NEWNICK
, &lua_onnewnick
);
71 deregisterhook(HOOK_CHANNEL_MODECHANGE
, &lua_onmode
);
74 void lua_startbot(void *arg
) {
80 n
= getcopyconfigitem("lua", "botnick", "U", NICKLEN
);
81 lua_nick
= registerlocaluser(n
->content
, "lua", "quakenet.department.of.corrections", LUA_FULLVERSION
, "U", UMODE_ACCOUNT
| UMODE_DEAF
| UMODE_OPER
| UMODE_SERVICE
, &lua_bothandler
);
83 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
87 cp
= findchannel(LUA_OPERCHAN
);
88 if(cp
&& localjoinchannel(lua_nick
, cp
)) {
89 localgetops(lua_nick
, cp
);
91 localcreatechannel(lua_nick
, LUA_OPERCHAN
);
94 cp
= findchannel(LUA_PUKECHAN
);
95 if(cp
&& localjoinchannel(lua_nick
, cp
)) {
96 localgetops(lua_nick
, cp
);
98 localcreatechannel(lua_nick
, LUA_PUKECHAN
);
101 myublip
= schedulerecurring(time(NULL
) + 1, 0, 60, &lua_blip
, NULL
);
102 myutick
= schedulerecurring(time(NULL
) + 1, 0, 1, &lua_tick
, NULL
);
104 lua_registerevents();
107 void lua_destroybot(void) {
109 deleteschedule(myutick
, &lua_tick
, NULL
);
112 deleteschedule(myublip
, &lua_blip
, NULL
);
114 lua_deregisterevents();
117 deleteschedule(myureconnect
, &lua_startbot
, NULL
);
120 deregisterlocaluser(lua_nick
, NULL
);
123 int _lua_vpcall(lua_State
*l
, void *function
, int mode
, const char *sig
, ...) {
125 int narg
= 0, nres
, top
= lua_gettop(l
);
127 lua_getglobal(l
, "scripterror");
128 if(mode
== LUA_CHARMODE
) {
129 lua_getglobal(l
, (const char *)function
);
131 lua_rawgeti(l
, LUA_REGISTRYINDEX
, (long)function
);
134 if(!lua_isfunction(l
, -1)) {
144 lua_pushint(l
, va_arg(va
, int));
147 lua_pushlong(l
, va_arg(va
, long));
150 lua_pushstring(l
, va_arg(va
, char *));
153 lua_pushstring(l
, ((sstring
*)(va_arg(va
, sstring
*)))->content
);
155 case 'L': /* BE VERY CAREFUL USING THIS, MAKE SURE YOU CAST THE VALUE TO LONG */
157 char *p
= va_arg(va
, char *);
158 long len
= va_arg(va
, long);
159 lua_pushlstring(l
, p
, len
);
164 nick
*np
= va_arg(va
, nick
*);
165 lua_pushnumeric(l
, np
->numeric
);
170 channel
*cp
= va_arg(va
, channel
*);
178 lua_rawgeti(l
, LUA_REGISTRYINDEX
, va_arg(va
, long));
181 lua_pushboolean(l
, va_arg(va
, int));
187 Error("lua", ERR_ERROR
, "Unable to parse vpcall signature (%c)", *(sig
- 1));
196 if(lua_debugpcall(l
, (mode
==LUA_CHARMODE
)?function
:"some_handler", narg
, nres
, top
+ 1)) {
197 Error("lua", ERR_ERROR
, "Error pcalling %s: %s.", (mode
==LUA_CHARMODE
)?(char *)function
:"some_handler", lua_tostring(l
, -1));
203 Error("lua", ERR_ERROR
, "Unable to parse vpcall return structure (%c)", *(sig
- 1));
214 void lua_bothandler(nick
*target
, int type
, void **args
) {
221 np
= (nick
*)args
[0];
227 lua_avpcall("irc_onmsg", "ls", np
->numeric
, p
);
231 np
= (nick
*)args
[0];
238 if((le
> 1) && (p
[0] == '\001')) {
239 if(p
[le
- 1] == '\001')
242 lua_avpcall("irc_onctcp", "ls", np
->numeric
, p
+ 1);
251 deleteschedule(myublip
, &lua_blip
, NULL
);
256 deleteschedule(myutick
, &lua_tick
, NULL
);
260 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
266 void lua_blip(void *arg
) {
267 lua_avpcall("onblip", "");
270 void lua_tick(void *arg
) {
271 lua_avpcall("ontick", "");
274 void lua_onnewnick(int hooknum
, void *arg
) {
275 nick
*np
= (nick
*)arg
;
280 lua_avpcall("irc_onnewnick", "l", np
->numeric
);
283 void lua_onkick(int hooknum
, void *arg
) {
284 void **arglist
= (void **)arg
;
285 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
286 nick
*kicked
= arglist
[1];
287 nick
*kicker
= arglist
[2];
288 char *message
= (char *)arglist
[3];
291 if(!kicker
|| IsOper(kicker
) || IsService(kicker
) || IsXOper(kicker
)) /* bloody Cruicky */
295 lua_avpcall("irc_onkick", "Slls", ci
->name
, kicked
->numeric
, kicker
->numeric
, message
);
297 lua_avpcall("irc_onkickall", "Slls", ci
->name
, kicked
->numeric
, kicker
->numeric
, message
);
299 lua_avpcall("irc_onkickall", "Sl0s", ci
->name
, kicked
->numeric
, message
);
303 void lua_ontopic(int hooknum
, void *arg
) {
304 void **arglist
= (void **)arg
;
305 channel
*cp
=(channel
*)arglist
[0];
306 nick
*np
= (nick
*)arglist
[1];
308 if(!np
|| IsOper(np
) || IsService(np
) || IsXOper(np
))
310 if(!cp
|| !cp
->topic
)
313 lua_avpcall("irc_ontopic", "SlS", cp
->index
->name
, np
->numeric
, cp
->topic
);
316 void lua_onop(int hooknum
, void *arg
) {
317 void **arglist
= (void **)arg
;
318 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
319 nick
*np
= arglist
[1];
320 nick
*target
= arglist
[2];
326 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "Sll", ci
->name
, np
->numeric
, target
->numeric
);
328 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "S0l", ci
->name
, target
->numeric
);
332 void lua_onjoin(int hooknum
, void *arg
) {
333 void **arglist
= (void **)arg
;
334 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
335 nick
*np
= arglist
[1];
340 lua_avpcall("irc_onjoin", "Sl", ci
->name
, np
->numeric
);
343 void lua_onpart(int hooknum
, void *arg
) {
344 void **arglist
= (void **)arg
;
345 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
346 nick
*np
= arglist
[1];
351 lua_avpcall("irc_onpart", "Sl", ci
->name
, np
->numeric
);
354 void lua_onrename(int hooknum
, void *arg
) {
355 nick
*np
= (void *)arg
;
360 lua_avpcall("irc_onrename", "l", np
->numeric
);
363 void lua_onquit(int hooknum
, void *arg
) {
364 nick
*np
= (nick
*)arg
;
369 lua_avpcall("irc_onquit", "l", np
->numeric
);
372 void lua_onauth(int hooknum
, void *arg
) {
373 nick
*np
= (nick
*)arg
;
378 lua_avpcall("irc_onauth", "l", np
->numeric
);
381 void lua_ondisconnect(int hooknum
, void *arg
) {
382 lua_avpcall(hooknum
== HOOK_IRC_DISCON
?"irc_ondisconnect":"irc_onpredisconnect", "");
385 void lua_onconnect(int hooknum
, void *arg
) {
386 lua_avpcall(hooknum
== HOOK_IRC_CONNECTED
?"irc_onconnect":"irc_onendofburst", "");
389 void lua_onload(lua_State
*l
) {
390 lua_vpcall(l
, "onload", "");
393 void lua_onunload(lua_State
*l
) {
394 lua_vpcall(l
, "onunload", "");
397 int lua_channelmessage(channel
*cp
, char *message
, ...) {
401 va_start(va
, message
);
402 vsnprintf(buf
, sizeof(buf
), message
, va
);
405 sendmessagetochannel(lua_nick
, cp
, "%s", buf
);
410 int lua_message(nick
*np
, char *message
, ...) {
414 va_start(va
, message
);
415 vsnprintf(buf
, sizeof(buf
), message
, va
);
418 sendmessagetouser(lua_nick
, np
, "%s", buf
);
423 int lua_notice(nick
*np
, char *message
, ...) {
427 va_start(va
, message
);
428 vsnprintf(buf
, sizeof(buf
), message
, va
);
431 sendnoticetouser(lua_nick
, np
, "%s", buf
);
436 char *printallmodes(channel
*cp
) {
437 static char buf
[1024];
441 snprintf(buf2
, sizeof(buf2
), "%d", cp
->limit
);
443 snprintf(buf
, sizeof(buf
), "%s %s%s%s", printflags(cp
->flags
, cmodeflags
), IsLimit(cp
)?buf2
:"", IsLimit(cp
)?" ":"", IsKey(cp
)?cp
->key
->content
:"");
448 void lua_onmode(int hooknum
, void *arg
) {
449 void **arglist
= (void **)arg
;
450 channel
*cp
= (channel
*)arglist
[0];
451 chanindex
*ci
= cp
->index
;
452 nick
*np
= arglist
[1];
455 lua_avpcall("irc_onmode", "Sls", ci
->name
, np
->numeric
, printallmodes(cp
));
457 lua_avpcall("irc_onmode", "S0s", ci
->name
, printallmodes(cp
));