]>
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"
12 nick
*lua_nick
= NULL
;
13 void *myureconnect
= NULL
, *myublip
= NULL
, *myutick
= NULL
;
15 void lua_bothandler(nick
*target
, int type
, void **args
);
17 void lua_onnewnick(int hooknum
, void *arg
);
18 void lua_blip(void *arg
);
19 void lua_tick(void *arg
);
20 void lua_onkick(int hooknum
, void *arg
);
21 void lua_ontopic(int hooknum
, void *arg
);
22 void lua_onauth(int hooknum
, void *arg
);
23 void lua_ondisconnect(int hooknum
, void *arg
);
24 void lua_onmode(int hooknum
, void *arg
);
25 void lua_onop(int hooknum
, void *arg
);
26 void lua_onquit(int hooknum
, void *arg
);
27 void lua_onrename(int hooknum
, void *arg
);
28 void lua_onconnect(int hooknum
, void *arg
);
29 void lua_onjoin(int hooknum
, void *arg
);
31 void lua_registerevents(void) {
32 registerhook(HOOK_NICK_NEWNICK
, &lua_onnewnick
);
33 registerhook(HOOK_IRC_DISCON
, &lua_ondisconnect
);
34 registerhook(HOOK_IRC_PRE_DISCON
, &lua_ondisconnect
);
35 registerhook(HOOK_NICK_ACCOUNT
, &lua_onauth
);
36 registerhook(HOOK_CHANNEL_TOPIC
, &lua_ontopic
);
37 registerhook(HOOK_CHANNEL_KICK
, &lua_onkick
);
38 registerhook(HOOK_CHANNEL_OPPED
, &lua_onop
);
39 registerhook(HOOK_CHANNEL_DEOPPED
, &lua_onop
);
40 registerhook(HOOK_NICK_LOSTNICK
, &lua_onquit
);
41 registerhook(HOOK_NICK_RENAME
, &lua_onrename
);
42 registerhook(HOOK_IRC_CONNECTED
, &lua_onconnect
);
43 registerhook(HOOK_SERVER_END_OF_BURST
, &lua_onconnect
);
44 registerhook(HOOK_CHANNEL_JOIN
, &lua_onjoin
);
47 void lua_deregisterevents(void) {
48 deregisterhook(HOOK_CHANNEL_JOIN
, &lua_onjoin
);
49 deregisterhook(HOOK_SERVER_END_OF_BURST
, &lua_onconnect
);
50 deregisterhook(HOOK_IRC_CONNECTED
, &lua_onconnect
);
51 deregisterhook(HOOK_NICK_RENAME
, &lua_onrename
);
52 deregisterhook(HOOK_NICK_LOSTNICK
, &lua_onquit
);
53 deregisterhook(HOOK_CHANNEL_DEOPPED
, &lua_onop
);
54 deregisterhook(HOOK_CHANNEL_OPPED
, &lua_onop
);
55 deregisterhook(HOOK_CHANNEL_KICK
, &lua_onkick
);
56 deregisterhook(HOOK_CHANNEL_TOPIC
, &lua_ontopic
);
57 deregisterhook(HOOK_NICK_ACCOUNT
, &lua_onauth
);
58 deregisterhook(HOOK_IRC_PRE_DISCON
, &lua_ondisconnect
);
59 deregisterhook(HOOK_IRC_DISCON
, &lua_ondisconnect
);
60 deregisterhook(HOOK_NICK_NEWNICK
, &lua_onnewnick
);
63 void lua_startbot(void *arg
) {
68 lua_nick
= registerlocaluser("U", "lua", "quakenet.department.of.corrections", "Lua engine v" LUA_BOTVERSION
" (" LUA_VERSION
")", "U", UMODE_ACCOUNT
| UMODE_DEAF
| UMODE_OPER
| UMODE_SERVICE
, &lua_bothandler
);
70 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
74 cp
= findchannel(LUA_OPERCHAN
);
75 if(cp
&& localjoinchannel(lua_nick
, cp
))
76 localgetops(lua_nick
, cp
);
78 cp
= findchannel(LUA_PUKECHAN
);
79 if(cp
&& localjoinchannel(lua_nick
, cp
))
80 localgetops(lua_nick
, cp
);
82 myublip
= schedulerecurring(time(NULL
) + 1, 0, 60, &lua_blip
, NULL
);
83 myutick
= schedulerecurring(time(NULL
) + 1, 0, 1, &lua_tick
, NULL
);
88 void lua_destroybot(void) {
90 deleteschedule(myutick
, &lua_tick
, NULL
);
93 deleteschedule(myublip
, &lua_blip
, NULL
);
95 lua_deregisterevents();
98 deleteschedule(myureconnect
, &lua_startbot
, NULL
);
101 deregisterlocaluser(lua_nick
, NULL
);
104 void lua_bothandler(nick
*target
, int type
, void **args
) {
112 np
= (nick
*)args
[0];
122 lua_getglobal(l
, "scripterror");
123 lua_getglobal(l
, "irc_onmsg");
125 if(lua_isfunction(l
, -1)) {
127 lua_pushstring(l
, p
);
129 lua_pcall(l
, 2, 0, top
+ 1);
138 np
= (nick
*)args
[0];
145 if((le
> 1) && (p
[0] == '\001')) {
146 if(p
[le
- 1] == '\001')
153 lua_getglobal(l
, "scripterror");
154 lua_getglobal(l
, "irc_onctcp");
156 if(lua_isfunction(l
, -1)) {
158 lua_pushstring(l
, p
+ 1);
160 lua_pcall(l
, 2, 0, top
+ 1);
174 deleteschedule(myublip
, &lua_blip
, NULL
);
179 deleteschedule(myutick
, &lua_tick
, NULL
);
183 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
189 void lua_blip(void *arg
) {
196 lua_getglobal(l
, "scripterror");
197 lua_getglobal(l
, "onblip");
199 if(lua_isfunction(l
, -1))
200 lua_pcall(l
, 0, 0, top
+ 1);
206 void lua_tick(void *arg
) {
213 lua_getglobal(l
, "scripterror");
214 lua_getglobal(l
, "ontick");
216 if(lua_isfunction(l
, -1))
217 lua_pcall(l
, 0, 0, top
+ 1);
223 void lua_onnewnick(int hooknum
, void *arg
) {
224 nick
*np
= (nick
*)arg
;
234 lua_getglobal(l
, "scripterror");
235 lua_getglobal(l
, "irc_onnewnick");
237 if(lua_isfunction(l
, -1)) {
240 lua_pcall(l
, 1, 0, top
+ 1);
247 void lua_onkick(int hooknum
, void *arg
) {
248 void **arglist
= (void **)arg
;
249 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
250 nick
*kicked
= arglist
[1];
251 nick
*kicker
= arglist
[2];
252 char *message
= (char *)arglist
[3];
256 if(!kicker
|| IsOper(kicker
) || IsService(kicker
) || IsXOper(kicker
)) /* bloody Cruicky */
262 lua_getglobal(l
, "scripterror");
263 lua_getglobal(l
, "irc_onkick");
265 if(lua_isfunction(l
, -1)) {
266 lua_pushstring(l
, ci
->name
->content
);
267 LUA_PUSHNICK(l
, kicked
);
268 LUA_PUSHNICK(l
, kicker
);
269 lua_pushstring(l
, message
);
271 lua_pcall(l
, 4, 0, top
+ 1);
278 void lua_ontopic(int hooknum
, void *arg
) {
279 void **arglist
= (void **)arg
;
280 channel
*cp
=(channel
*)arglist
[0];
281 nick
*np
= (nick
*)arglist
[1];
285 if(!np
|| IsOper(np
) || IsService(np
) || IsXOper(np
))
287 if(!cp
|| !cp
->topic
)
293 lua_getglobal(l
, "scripterror");
294 lua_getglobal(l
, "irc_ontopic");
296 if(lua_isfunction(l
, -1)) {
297 lua_pushstring(l
, cp
->index
->name
->content
);
299 lua_pushstring(l
, cp
->topic
->content
);
301 lua_pcall(l
, 3, 0, top
+ 1);
307 void lua_onop(int hooknum
, void *arg
) {
308 void **arglist
= (void **)arg
;
309 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
310 nick
*np
= arglist
[1];
311 nick
*target
= arglist
[2];
321 lua_getglobal(l
, "scripterror");
322 if(hooknum
== HOOK_CHANNEL_OPPED
) {
323 lua_getglobal(l
, "irc_onop");
325 lua_getglobal(l
, "irc_ondeop");
328 if(lua_isfunction(l
, -1)) {
329 lua_pushstring(l
, ci
->name
->content
);
336 LUA_PUSHNICK(l
, target
);
338 lua_pcall(l
, 3, 0, top
+ 1);
345 void lua_onjoin(int hooknum
, void *arg
) {
346 void **arglist
= (void **)arg
;
347 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
348 nick
*np
= arglist
[1];
358 lua_getglobal(l
, "scripterror");
359 lua_getglobal(l
, "irc_onjoin");
361 if(lua_isfunction(l
, -1)) {
362 lua_pushstring(l
, ci
->name
->content
);
366 lua_pcall(l
, 2, 0, top
+ 1);
373 void lua_onrename(int hooknum
, void *arg
) {
374 nick
*np
= (void *)arg
;
384 lua_getglobal(l
, "scripterror");
385 lua_getglobal(l
, "irc_onrename");
387 if(lua_isfunction(l
, -1)) {
390 lua_pcall(l
, 1, 0, top
+ 1);
397 void lua_onquit(int hooknum
, void *arg
) {
398 nick
*np
= (nick
*)arg
;
408 lua_getglobal(l
, "scripterror");
409 lua_getglobal(l
, "irc_onquit");
411 if(lua_isfunction(l
, -1)) {
414 lua_pcall(l
, 1, 0, top
+ 1);
421 void lua_onauth(int hooknum
, void *arg
) {
422 nick
*np
= (nick
*)arg
;
432 lua_getglobal(l
, "scripterror");
433 lua_getglobal(l
, "irc_onauth");
435 if(lua_isfunction(l
, -1)) {
438 lua_pcall(l
, 1, 0, top
+ 1);
444 void lua_ondisconnect(int hooknum
, void *arg
) {
451 lua_getglobal(l
, "scripterror");
452 if(hooknum
== HOOK_IRC_DISCON
) {
453 lua_getglobal(l
, "irc_ondisconnect");
455 lua_getglobal(l
, "irc_onpredisconnect");
457 if(lua_isfunction(l
, -1))
458 lua_pcall(l
, 0, 0, top
+ 1);
463 void lua_onconnect(int hooknum
, void *arg
) {
470 lua_getglobal(l
, "scripterror");
471 if(hooknum
== HOOK_IRC_CONNECTED
) {
472 lua_getglobal(l
, "irc_onconnect");
474 lua_getglobal(l
, "irc_onendofburst");
476 if(lua_isfunction(l
, -1))
477 lua_pcall(l
, 0, 0, top
+ 1);
482 void lua_onload(lua_State
*l
) {
487 lua_getglobal(l
, "scripterror");
488 lua_getglobal(l
, "onload");
490 if(lua_isfunction(l
, -1))
491 lua_pcall(l
, 0, 0, top
+ 1);
495 void lua_onunload(lua_State
*l
) {
500 lua_getglobal(l
, "scripterror");
501 lua_getglobal(l
, "onunload");
503 if(lua_isfunction(l
, -1))
504 lua_pcall(l
, 0, 0, top
+ 1);
508 int lua_channelmessage(channel
*cp
, char *message
, ...) {
512 va_start(va
, message
);
513 vsnprintf(buf
, sizeof(buf
), message
, va
);
516 sendmessagetochannel(lua_nick
, cp
, "%s", buf
);
521 int lua_message(nick
*np
, char *message
, ...) {
525 va_start(va
, message
);
526 vsnprintf(buf
, sizeof(buf
), message
, va
);
529 sendmessagetouser(lua_nick
, np
, "%s", buf
);
534 int lua_notice(nick
*np
, char *message
, ...) {
538 va_start(va
, message
);
539 vsnprintf(buf
, sizeof(buf
), message
, va
);
542 sendnoticetouser(lua_nick
, np
, "%s", buf
);