]>
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 lua_debugpcall(l
, function
, narg
, nres
, 0);
184 Error("lua", ERR_ERROR
, "Unable to parse vpcall return structure (%c)", *(sig
- 1));
194 void lua_bothandler(nick
*target
, int type
, void **args
) {
201 np
= (nick
*)args
[0];
207 lua_avpcall("irc_onmsg", "Ns", np
, p
);
211 np
= (nick
*)args
[0];
218 if((le
> 1) && (p
[0] == '\001')) {
219 if(p
[le
- 1] == '\001')
222 lua_avpcall("irc_onctcp", "Ns", np
, p
+ 1);
231 deleteschedule(myublip
, &lua_blip
, NULL
);
236 deleteschedule(myutick
, &lua_tick
, NULL
);
240 myureconnect
= scheduleoneshot(time(NULL
) + 1, &lua_startbot
, NULL
);
246 void lua_blip(void *arg
) {
247 lua_avpcall("onblip", "");
250 void lua_tick(void *arg
) {
251 lua_avpcall("ontick", "");
254 void lua_onnewnick(int hooknum
, void *arg
) {
255 nick
*np
= (nick
*)arg
;
260 lua_avpcall("irc_onnewnick", "N", np
);
263 void lua_onkick(int hooknum
, void *arg
) {
264 void **arglist
= (void **)arg
;
265 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
266 nick
*kicked
= arglist
[1];
267 nick
*kicker
= arglist
[2];
268 char *message
= (char *)arglist
[3];
270 if(!kicker
|| IsOper(kicker
) || IsService(kicker
) || IsXOper(kicker
)) /* bloody Cruicky */
273 lua_avpcall("irc_onkick", "SNNs", ci
->name
, kicked
, kicker
, message
);
276 void lua_ontopic(int hooknum
, void *arg
) {
277 void **arglist
= (void **)arg
;
278 channel
*cp
=(channel
*)arglist
[0];
279 nick
*np
= (nick
*)arglist
[1];
281 if(!np
|| IsOper(np
) || IsService(np
) || IsXOper(np
))
283 if(!cp
|| !cp
->topic
)
286 lua_avpcall("irc_ontopic", "SNS", cp
->index
->name
, np
, cp
->topic
);
289 void lua_onop(int hooknum
, void *arg
) {
290 void **arglist
= (void **)arg
;
291 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
292 nick
*np
= arglist
[1];
293 nick
*target
= arglist
[2];
299 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "SNN", ci
->name
, np
, target
);
301 lua_avpcall(hooknum
== HOOK_CHANNEL_OPPED
?"irc_onop":"irc_ondeop", "S0N", ci
->name
, target
);
305 void lua_onjoin(int hooknum
, void *arg
) {
306 void **arglist
= (void **)arg
;
307 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
308 nick
*np
= arglist
[1];
313 lua_avpcall("irc_onjoin", "SN", ci
->name
, np
);
316 void lua_onpart(int hooknum
, void *arg
) {
317 void **arglist
= (void **)arg
;
318 chanindex
*ci
= ((channel
*)arglist
[0])->index
;
319 nick
*np
= arglist
[1];
324 lua_avpcall("irc_onpart", "SN", ci
->name
, np
);
327 void lua_onrename(int hooknum
, void *arg
) {
328 nick
*np
= (void *)arg
;
333 lua_avpcall("irc_onrename", "N", np
);
336 void lua_onquit(int hooknum
, void *arg
) {
337 nick
*np
= (nick
*)arg
;
342 lua_avpcall("irc_onquit", "N", np
);
345 void lua_onauth(int hooknum
, void *arg
) {
346 nick
*np
= (nick
*)arg
;
351 lua_avpcall("irc_onauth", "N", np
);
354 void lua_ondisconnect(int hooknum
, void *arg
) {
355 lua_avpcall(hooknum
== HOOK_IRC_DISCON
?"irc_ondisconnect":"irc_onpredisconnect", "");
358 void lua_onconnect(int hooknum
, void *arg
) {
359 lua_avpcall(hooknum
== HOOK_IRC_CONNECTED
?"irc_onconnect":"irc_onendofburst", "");
362 void lua_onload(lua_State
*l
) {
363 lua_vpcall(l
, "onload", "");
366 void lua_onunload(lua_State
*l
) {
367 lua_vpcall(l
, "onunload", "");
370 int lua_channelmessage(channel
*cp
, char *message
, ...) {
374 va_start(va
, message
);
375 vsnprintf(buf
, sizeof(buf
), message
, va
);
378 sendmessagetochannel(lua_nick
, cp
, "%s", buf
);
383 int lua_message(nick
*np
, char *message
, ...) {
387 va_start(va
, message
);
388 vsnprintf(buf
, sizeof(buf
), message
, va
);
391 sendmessagetouser(lua_nick
, np
, "%s", buf
);
396 int lua_notice(nick
*np
, char *message
, ...) {
400 va_start(va
, message
);
401 vsnprintf(buf
, sizeof(buf
), message
, va
);
404 sendnoticetouser(lua_nick
, np
, "%s", buf
);