]>
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"
15 nick
*lua_nick
= NULL
;
16 void *myureconnect
= NULL
, *myublip
= NULL
, *myutick
= NULL
;
18 void lua_bothandler(nick
*target
, int type
, void **args
);
20 void lua_onnewnick(int hooknum
, void *arg
);
21 void lua_blip(void *arg
);
22 void lua_tick(void *arg
);
23 void lua_onkick(int hooknum
, void *arg
);
24 void lua_ontopic(int hooknum
, void *arg
);
25 void lua_onauth(int hooknum
, void *arg
);
26 void lua_ondisconnect(int hooknum
, void *arg
);
27 void lua_onmode(int hooknum
, void *arg
);
28 void lua_onop(int hooknum
, void *arg
);
29 void lua_onquit(int hooknum
, void *arg
);
30 void lua_onrename(int hooknum
, void *arg
);
31 void lua_onconnect(int hooknum
, void *arg
);
32 void lua_onjoin(int hooknum
, void *arg
);
33 void lua_onpart(int hooknum
, void *arg
);
35 void lua_registerevents(void) {
36 registerhook(HOOK_NICK_NEWNICK
, &lua_onnewnick
);
37 registerhook(HOOK_IRC_DISCON
, &lua_ondisconnect
);
38 registerhook(HOOK_IRC_PRE_DISCON
, &lua_ondisconnect
);
39 registerhook(HOOK_NICK_ACCOUNT
, &lua_onauth
);
40 registerhook(HOOK_CHANNEL_TOPIC
, &lua_ontopic
);
41 registerhook(HOOK_CHANNEL_KICK
, &lua_onkick
);
42 registerhook(HOOK_CHANNEL_OPPED
, &lua_onop
);
43 registerhook(HOOK_CHANNEL_DEOPPED
, &lua_onop
);
44 registerhook(HOOK_NICK_LOSTNICK
, &lua_onquit
);
45 registerhook(HOOK_NICK_RENAME
, &lua_onrename
);
46 registerhook(HOOK_IRC_CONNECTED
, &lua_onconnect
);
47 registerhook(HOOK_SERVER_END_OF_BURST
, &lua_onconnect
);
48 registerhook(HOOK_CHANNEL_JOIN
, &lua_onjoin
);
49 registerhook(HOOK_CHANNEL_PART
, &lua_onpart
);
50 registerhook(HOOK_CHANNEL_CREATE
, &lua_onjoin
);
53 void lua_deregisterevents(void) {
54 deregisterhook(HOOK_CHANNEL_PART
, &lua_onpart
);
55 deregisterhook(HOOK_CHANNEL_CREATE
, &lua_onjoin
);
56 deregisterhook(HOOK_CHANNEL_JOIN
, &lua_onjoin
);
57 deregisterhook(HOOK_SERVER_END_OF_BURST
, &lua_onconnect
);
58 deregisterhook(HOOK_IRC_CONNECTED
, &lua_onconnect
);
59 deregisterhook(HOOK_NICK_RENAME
, &lua_onrename
);
60 deregisterhook(HOOK_NICK_LOSTNICK
, &lua_onquit
);
61 deregisterhook(HOOK_CHANNEL_DEOPPED
, &lua_onop
);
62 deregisterhook(HOOK_CHANNEL_OPPED
, &lua_onop
);
63 deregisterhook(HOOK_CHANNEL_KICK
, &lua_onkick
);
64 deregisterhook(HOOK_CHANNEL_TOPIC
, &lua_ontopic
);
65 deregisterhook(HOOK_NICK_ACCOUNT
, &lua_onauth
);
66 deregisterhook(HOOK_IRC_PRE_DISCON
, &lua_ondisconnect
);
67 deregisterhook(HOOK_IRC_DISCON
, &lua_ondisconnect
);
68 deregisterhook(HOOK_NICK_NEWNICK
, &lua_onnewnick
);
71 void lua_startbot(void *arg
) {
76 lua_nick
= registerlocaluser("U", "lua", "quakenet.department.of.corrections", LUA_FULLVERSION
, "U", UMODE_ACCOUNT
| UMODE_DEAF
| UMODE_OPER
| UMODE_SERVICE
, &lua_bothandler
);
78 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
82 cp
= findchannel(LUA_OPERCHAN
);
83 if(cp
&& localjoinchannel(lua_nick
, cp
)) {
84 localgetops(lua_nick
, cp
);
86 localcreatechannel(lua_nick
, LUA_OPERCHAN
);
89 cp
= findchannel(LUA_PUKECHAN
);
90 if(cp
&& localjoinchannel(lua_nick
, cp
)) {
91 localgetops(lua_nick
, cp
);
93 localcreatechannel(lua_nick
, LUA_PUKECHAN
);
96 myublip
= schedulerecurring(time(NULL
) + 1, 0, 60, &lua_blip
, NULL
);
97 myutick
= schedulerecurring(time(NULL
) + 1, 0, 1, &lua_tick
, NULL
);
102 void lua_destroybot(void) {
104 deleteschedule(myutick
, &lua_tick
, NULL
);
107 deleteschedule(myublip
, &lua_blip
, NULL
);
109 lua_deregisterevents();
112 deleteschedule(myureconnect
, &lua_startbot
, NULL
);
115 deregisterlocaluser(lua_nick
, NULL
);
118 int _lua_vpcall(lua_State
*l
, void *function
, int mode
, const char *sig
, ...) {
120 int narg
= 0, nres
, top
= lua_gettop(l
);
122 lua_getglobal(l
, "scripterror");
123 if(mode
== LUA_CHARMODE
) {
124 lua_getglobal(l
, (const char *)function
);
126 lua_rawgeti(l
, LUA_REGISTRYINDEX
, (int)function
);
129 if(!lua_isfunction(l
, -1)) {
139 lua_pushint(l
, va_arg(va
, int));
142 lua_pushlong(l
, va_arg(va
, long));
145 lua_pushstring(l
, va_arg(va
, char *));
148 lua_pushstring(l
, ((sstring
*)(va_arg(va
, sstring
*)))->content
);
152 nick
*np
= va_arg(va
, nick
*);
158 channel
*cp
= va_arg(va
, channel
*);
169 Error("lua", ERR_ERROR
, "Unable to parse vpcall signature (%c)", *(sig
- 1));
178 if(lua_debugpcall(l
, (mode
==LUA_CHARMODE
)?function
:"some_handler", narg
, nres
, top
+ 1)) {
179 Error("lua", ERR_ERROR
, "Error pcalling %s: %s.", (mode
==LUA_CHARMODE
)?function
:"some_handler", lua_tostring(l
, -1));
185 Error("lua", ERR_ERROR
, "Unable to parse vpcall return structure (%c)", *(sig
- 1));
196 void lua_bothandler(nick
*target
, int type
, void **args
) {
203 np
= (nick
*)args
[0];
209 lua_avpcall("irc_onmsg", "ls", np
->numeric
, p
);
213 np
= (nick
*)args
[0];
220 if((le
> 1) && (p
[0] == '\001')) {
221 if(p
[le
- 1] == '\001')
224 lua_avpcall("irc_onctcp", "ls", np
->numeric
, p
+ 1);
233 deleteschedule(myublip
, &lua_blip
, NULL
);
238 deleteschedule(myutick
, &lua_tick
, NULL
);
242 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
248 void lua_blip(void *arg
) {
249 lua_avpcall("onblip", "");
252 void lua_tick(void *arg
) {
253 lua_avpcall("ontick", "");
256 void lua_onnewnick(int hooknum
, void *arg
) {
257 nick
*np
= (nick
*)arg
;
262 lua_avpcall("irc_onnewnick", "l", np
->numeric
);
265 void lua_onkick(int hooknum
, void *arg
) {
266 void **arglist
= (void **)arg
;
267 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
268 nick
*kicked
= arglist
[1];
269 nick
*kicker
= arglist
[2];
270 char *message
= (char *)arglist
[3];
272 if(!kicker
|| IsOper(kicker
) || IsService(kicker
) || IsXOper(kicker
)) /* bloody Cruicky */
276 lua_avpcall("irc_onkick", "Slls", ci
->name
, kicked
->numeric
, kicker
->numeric
, message
);
278 lua_avpcall("irc_onkick", "Sl0s", ci
->name
, kicked
->numeric
, message
);
282 void lua_ontopic(int hooknum
, void *arg
) {
283 void **arglist
= (void **)arg
;
284 channel
*cp
=(channel
*)arglist
[0];
285 nick
*np
= (nick
*)arglist
[1];
287 if(!np
|| IsOper(np
) || IsService(np
) || IsXOper(np
))
289 if(!cp
|| !cp
->topic
)
292 lua_avpcall("irc_ontopic", "SlS", cp
->index
->name
, np
->numeric
, cp
->topic
);
295 void lua_onop(int hooknum
, void *arg
) {
296 void **arglist
= (void **)arg
;
297 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
298 nick
*np
= arglist
[1];
299 nick
*target
= arglist
[2];
305 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "Sll", ci
->name
, np
->numeric
, target
->numeric
);
307 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "S0l", ci
->name
, target
->numeric
);
311 void lua_onjoin(int hooknum
, void *arg
) {
312 void **arglist
= (void **)arg
;
313 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
314 nick
*np
= arglist
[1];
319 lua_avpcall("irc_onjoin", "Sl", ci
->name
, np
->numeric
);
322 void lua_onpart(int hooknum
, void *arg
) {
323 void **arglist
= (void **)arg
;
324 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
325 nick
*np
= arglist
[1];
330 lua_avpcall("irc_onpart", "Sl", ci
->name
, np
->numeric
);
333 void lua_onrename(int hooknum
, void *arg
) {
334 nick
*np
= (void *)arg
;
339 lua_avpcall("irc_onrename", "l", np
->numeric
);
342 void lua_onquit(int hooknum
, void *arg
) {
343 nick
*np
= (nick
*)arg
;
348 lua_avpcall("irc_onquit", "l", np
->numeric
);
351 void lua_onauth(int hooknum
, void *arg
) {
352 nick
*np
= (nick
*)arg
;
357 lua_avpcall("irc_onauth", "l", np
->numeric
);
360 void lua_ondisconnect(int hooknum
, void *arg
) {
361 lua_avpcall(hooknum
== HOOK_IRC_DISCON
?"irc_ondisconnect":"irc_onpredisconnect", "");
364 void lua_onconnect(int hooknum
, void *arg
) {
365 lua_avpcall(hooknum
== HOOK_IRC_CONNECTED
?"irc_onconnect":"irc_onendofburst", "");
368 void lua_onload(lua_State
*l
) {
369 lua_vpcall(l
, "onload", "");
372 void lua_onunload(lua_State
*l
) {
373 lua_vpcall(l
, "onunload", "");
376 int lua_channelmessage(channel
*cp
, char *message
, ...) {
380 va_start(va
, message
);
381 vsnprintf(buf
, sizeof(buf
), message
, va
);
384 sendmessagetochannel(lua_nick
, cp
, "%s", buf
);
389 int lua_message(nick
*np
, char *message
, ...) {
393 va_start(va
, message
);
394 vsnprintf(buf
, sizeof(buf
), message
, va
);
397 sendmessagetouser(lua_nick
, np
, "%s", buf
);
402 int lua_notice(nick
*np
, char *message
, ...) {
406 va_start(va
, message
);
407 vsnprintf(buf
, sizeof(buf
), message
, va
);
410 sendnoticetouser(lua_nick
, np
, "%s", buf
);