]>
jfr.im git - irc/quakenet/newserv.git/blob - lua/luacommands.c
1 /* Copyright (C) Chris Porter 2005 */
2 /* ALL RIGHTS RESERVED. */
3 /* Don't put this into the SVN repo. */
5 #include "../channel/channel.h"
6 #include "../control/control.h"
7 #include "../nick/nick.h"
8 #include "../localuser/localuser.h"
9 #include "../localuser/localuserchannel.h"
10 #include "../lib/irc_string.h"
21 static int lua_smsg(lua_State
*ps
);
22 static int lua_skill(lua_State
*ps
);
24 int lua_lineok(const char *data
) {
25 if(strchr(data
, '\r') || strchr(data
, '\n'))
30 int lua_cmsg(char *channell
, char *message
, ...) {
35 va_start(va
, message
);
36 vsnprintf(buf
, sizeof(buf
), message
, va
);
39 cp
= findchannel(channell
);
46 lua_channelmessage(cp
, "%s", buf
);
51 static int lua_chanmsg(lua_State
*ps
) {
52 if(!lua_isstring(ps
, 1))
53 LUA_RETURN(ps
, LUA_FAIL
);
55 LUA_RETURN(ps
, lua_cmsg(LUA_PUKECHAN
, "lua: %s", lua_tostring(ps
, 1)));
58 static int lua_scripterror(lua_State
*ps
) {
59 if(!lua_isstring(ps
, 1))
60 LUA_RETURN(ps
, LUA_FAIL
);
62 LUA_RETURN(ps
, lua_cmsg(LUA_PUKECHAN
, "lua-error: %s", lua_tostring(ps
, 1)));
65 static int lua_ctcp(lua_State
*ps
) {
69 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
70 LUA_RETURN(ps
, LUA_FAIL
);
72 n
= lua_tostring(ps
, 1);
73 msg
= lua_tostring(ps
, 2);
75 np
= getnickbynick(n
);
76 if(!np
|| !lua_lineok(msg
))
77 LUA_RETURN(ps
, LUA_FAIL
);
79 lua_message(np
, "\001%s\001", msg
);
81 LUA_RETURN(ps
, lua_cmsg(LUA_PUKECHAN
, "lua-ctcp: %s (%s)", np
->nick
, msg
));
84 static int lua_noticecmd(lua_State
*ps
) {
88 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
89 LUA_RETURN(ps
, LUA_FAIL
);
91 n
= lua_tostring(ps
, 1);
92 msg
= lua_tostring(ps
, 2);
94 np
= getnickbynick(n
);
95 if(!np
|| !lua_lineok(msg
))
96 LUA_RETURN(ps
, LUA_FAIL
);
98 lua_notice(np
, "%s", msg
);
100 LUA_RETURN(ps
, LUA_OK
);
103 static int lua_kill(lua_State
*ps
) {
107 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
108 LUA_RETURN(ps
, LUA_FAIL
);
110 n
= lua_tostring(ps
, 1);
111 msg
= lua_tostring(ps
, 2);
113 np
= getnickbynick(n
);
115 LUA_RETURN(ps
, LUA_FAIL
);
117 if(IsOper(np
) || IsService(np
) || IsXOper(np
))
118 LUA_RETURN(ps
, LUA_FAIL
);
121 LUA_RETURN(ps
, LUA_FAIL
);
123 killuser(lua_nick
, np
, "%s", msg
);
125 LUA_RETURN(ps
, lua_cmsg(LUA_PUKECHAN
, "lua-KILL: %s (%s)", np
->nick
, msg
));
128 static int lua_kick(lua_State
*ps
) {
129 const char *n
, *msg
, *chan
;
134 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
135 LUA_RETURN(ps
, LUA_FAIL
);
137 chan
= lua_tostring(ps
, 1);
138 n
= lua_tostring(ps
, 2);
139 msg
= lua_tostring(ps
, 3);
141 if(lua_isboolean(ps
, 4) && !lua_toboolean(ps
, 4))
144 np
= getnickbynick(n
);
146 LUA_RETURN(ps
, LUA_FAIL
);
148 if(dochecks
&& (IsOper(np
) || IsXOper(np
) || IsService(np
)))
149 LUA_RETURN(ps
, LUA_FAIL
);
151 cp
= findchannel((char *)chan
);
153 LUA_RETURN(ps
, LUA_FAIL
);
156 LUA_RETURN(ps
, LUA_FAIL
);
158 localkickuser(lua_nick
, cp
, np
, msg
);
160 LUA_RETURN(ps
, LUA_OK
);
163 static int lua_invite(lua_State
*ps
) {
167 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
168 LUA_RETURN(ps
, LUA_FAIL
);
170 np
= getnickbynick((char *)lua_tostring(ps
, 1));
172 LUA_RETURN(ps
, LUA_FAIL
);
174 cp
= findchannel((char *)lua_tostring(ps
, 2));
176 LUA_RETURN(ps
, LUA_FAIL
);
178 localinvite(lua_nick
, cp
, np
);
180 LUA_RETURN(ps
, LUA_OK
);
183 static int lua_gline(lua_State
*ps
) {
187 int duration
, usercount
= 0;
190 if(!lua_isstring(ps
, 1) || !lua_isint(ps
, 2) || !lua_isstring(ps
, 3))
191 LUA_RETURN(ps
, LUA_FAIL
);
193 duration
= lua_toint(ps
, 2);
194 if((duration
< 1) || (duration
> 86400))
195 LUA_RETURN(ps
, LUA_FAIL
);
197 reason
= lua_tostring(ps
, 3);
198 if(!lua_lineok(reason
) || !reason
)
199 LUA_RETURN(ps
, LUA_FAIL
);
201 target
= getnickbynick(lua_tostring(ps
, 1));
202 if(!target
|| (IsOper(target
) || IsXOper(target
) || IsService(target
)))
203 LUA_RETURN(ps
, LUA_FAIL
);
207 LUA_RETURN(ps
, LUA_FAIL
);
209 usercount
= hp
->clonecount
;
210 if(usercount
> 10) { /* (decent) trusted host */
216 for (j
=0;j
<NICKHASHSIZE
;j
++)
217 for (np
=nicktable
[j
];np
;np
=np
->next
)
218 if (np
&& (np
->host
== hp
) && (!ircd_strcmp(np
->ident
, target
->ident
)))
222 LUA_RETURN(ps
, LUA_FAIL
);
224 snprintf(mask
, sizeof(mask
), "*%s@%s", target
->ident
, IPtostr(target
->ipaddress
));
226 snprintf(mask
, sizeof(mask
), "*@%s", IPtostr(target
->ipaddress
));
229 irc_send("%s GL * +%s %d :%s", mynumeric
->content
, mask
, duration
, reason
);
230 LUA_RETURN(ps
, lua_cmsg(LUA_PUKECHAN
, "lua-GLINE: %s (%d users, %d seconds -- %s)", mask
, usercount
, duration
, reason
));
233 static int lua_getchaninfo(lua_State
*ps
) {
236 if(!lua_isstring(ps
, 1))
239 cp
= findchannel((char *)lua_tostring(ps
, 1));
243 LUA_PUSHCHAN(ps
, cp
);
248 static int lua_opchan(lua_State
*ps
) {
252 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
253 LUA_RETURN(ps
, LUA_FAIL
);
255 cp
= findchannel((char *)lua_tostring(ps
, 1));
257 LUA_RETURN(ps
, LUA_FAIL
);
259 np
= getnickbynick((char *)lua_tostring(ps
, 2));
261 LUA_RETURN(ps
, LUA_FAIL
);
263 localsetmodes(lua_nick
, cp
, np
, MC_OP
);
264 LUA_RETURN(ps
, LUA_OK
);
267 static int lua_deopchan(lua_State
*ps
) {
271 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
272 LUA_RETURN(ps
, LUA_FAIL
);
274 cp
= findchannel((char *)lua_tostring(ps
, 1));
276 LUA_RETURN(ps
, LUA_FAIL
);
278 np
= getnickbynick((char *)lua_tostring(ps
, 2));
280 LUA_RETURN(ps
, LUA_FAIL
);
282 localsetmodes(lua_nick
, cp
, np
, MC_DEOP
);
283 LUA_RETURN(ps
, LUA_OK
);
286 static int lua_voicechan(lua_State
*ps
) {
290 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
291 LUA_RETURN(ps
, LUA_FAIL
);
293 cp
= findchannel((char *)lua_tostring(ps
, 1));
295 LUA_RETURN(ps
, LUA_FAIL
);
297 np
= getnickbynick((char *)lua_tostring(ps
, 2));
299 LUA_RETURN(ps
, LUA_FAIL
);
301 localsetmodes(lua_nick
, cp
, np
, MC_VOICE
);
302 LUA_RETURN(ps
, LUA_OK
);
305 static int lua_counthost(lua_State
*ps
) {
309 if(!lua_islong(ps
, 1))
312 numeric
= lua_tolong(ps
, 1);
314 np
= getnickbynumeric(numeric
);
318 lua_pushint(ps
, np
->host
->clonecount
);
322 static int lua_versioninfo(lua_State
*ps
) {
323 lua_pushstring(ps
, LUA_VERSION
);
324 lua_pushstring(ps
, LUA_BOTVERSION
);
325 lua_pushstring(ps
, __DATE__
);
326 lua_pushstring(ps
, __TIME__
);
328 #ifdef LUAJIT_VERSION
329 lua_pushstring(ps
, " + " LUAJIT_VERSION
);
331 lua_pushstring(ps
, "");
337 static int lua_basepath(lua_State
*ps
) {
338 lua_pushfstring(ps
, "%s/", cpath
->content
);
344 static int lua_getuserbyauth(lua_State
*l
) {
349 if(!lua_isstring(l
, 1))
352 for(i
=0;i
<NICKHASHSIZE
;i
++) {
353 for(np
=nicktable
[i
];np
;np
=np
->next
) {
354 if(np
&& np
->authname
&& !ircd_strcmp(np
->authname
, acc
)) {
364 static int lua_getnickchans(lua_State
*l
) {
369 if(!lua_islong(l
, 1))
372 np
= getnickbynumeric(lua_tolong(l
, 1));
376 channels
= (channel
**)np
->channels
->content
;
377 for(i
=0;i
<np
->channels
->cursi
;i
++)
378 lua_pushstring(l
, channels
[i
]->index
->name
->content
);
380 return np
->channels
->cursi
;
383 static int lua_getnickchanindex(lua_State
*l
) {
387 if(!lua_islong(l
, 1) || !lua_isint(l
, 2))
390 np
= getnickbynumeric(lua_tolong(l
, 1));
394 offset
= lua_toint(l
, 2);
395 if((offset
< 0) || (offset
>= np
->channels
->cursi
))
398 lua_pushstring(l
, ((channel
**)np
->channels
->content
)[offset
]->index
->name
->content
);
406 static int lua_getnextnick(lua_State
*l
) {
407 if(!lasthashnick
&& (hashindex
!= -1))
413 if(hashindex
>= NICKHASHSIZE
)
415 lasthashnick
= nicktable
[hashindex
];
417 lasthashnick
= lasthashnick
->next
;
419 } while(!lasthashnick
);
421 LUA_PUSHNICK(l
, lasthashnick
);
425 static int lua_getfirstnick(lua_State
*l
) {
429 return lua_getnextnick(l
);
432 static int lua_getnickchancount(lua_State
*l
) {
435 if(!lua_islong(l
, 1))
438 np
= getnickbynumeric(lua_tolong(l
, 1));
442 lua_pushint(l
, np
->channels
->cursi
);
447 static int lua_gethostusers(lua_State
*l
) {
451 if(!lua_islong(l
, 1))
454 np
= getnickbynumeric(lua_tolong(l
, 1));
455 if(!np
|| !np
->host
|| !np
->host
->nicks
)
458 np
= np
->host
->nicks
;
459 count
= np
->host
->clonecount
;
469 static int lua_getnickcountry(lua_State
*l
) {
473 ext
= findnickext("geoip");
477 if(!lua_islong(l
, 1))
480 np
= getnickbynumeric(lua_tolong(l
, 1));
484 lua_pushint(l
, (int)np
->exts
[ext
]);
489 static int lua_iteratenickhash(lua_State *l) {
494 if(!lua_isfunction(l, 1))
495 LUA_RETURN(LUA_FAIL);
497 fp = lua_touserdata(l, 1);
499 LUA_RETURN(LUA_FAIL);
501 for(i=0;i<NICKHASHSIZE;i++) {
502 for(np=nicktable[i];np;np=np->next) {
506 lua_getglobal(l, "scripterror");
510 lua_pcall(l, 1, 0, top + 1);
521 static int lua_chanfix(lua_State
*ps
) {
525 if(!lua_isstring(ps
, 1))
526 LUA_RETURN(ps
, LUA_FAIL
);
528 cp
= findchannel((char *)lua_tostring(ps
, 1));
530 LUA_RETURN(ps
, LUA_FAIL
);
532 np
= getnickbynick(LUA_CHANFIXBOT
);
534 LUA_RETURN(ps
, LUA_FAIL
);
536 lua_message(np
, "chanfix %s", cp
->index
->name
->content
);
538 LUA_RETURN(ps
, LUA_OK
);
541 static int lua_clearmode(lua_State
*ps
) {
548 if(!lua_isstring(ps
, 1))
549 LUA_RETURN(ps
, LUA_FAIL
);
551 cp
= findchannel((char *)lua_tostring(ps
, 1));
553 LUA_RETURN(ps
, LUA_FAIL
);
555 localsetmodeinit(&changes
, cp
, lua_nick
);
557 localdosetmode_key(&changes
, NULL
, MCB_DEL
);
558 localdosetmode_simple(&changes
, 0, CHANMODE_INVITEONLY
| CHANMODE_LIMIT
);
561 localdosetmode_ban(&changes
, bantostring(cp
->bans
), MCB_DEL
);
563 for(i
=0,lp
=cp
->users
->content
;i
<cp
->users
->hashsize
;i
++,lp
++)
564 if((*lp
!= nouser
) && (*lp
& CUMODE_OP
)) {
565 np
= getnickbynumeric(*lp
);
566 if(np
&& !IsService(np
))
567 localdosetmode_nick(&changes
, np
, MC_DEOP
);
570 localsetmodeflush(&changes
, 1);
572 LUA_RETURN(ps
, LUA_OK
);
575 static int lua_ban(lua_State
*ps
) {
581 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
582 LUA_RETURN(ps
, LUA_FAIL
);
584 if(lua_isboolean(ps
, 3) && lua_toboolean(ps
, 3))
587 cp
= findchannel((char *)lua_tostring(ps
, 1));
589 LUA_RETURN(ps
, LUA_FAIL
);
591 mask
= lua_tostring(ps
, 2);
592 if(!mask
|| !mask
[0] || !lua_lineok(mask
))
593 LUA_RETURN(ps
, LUA_FAIL
);
595 localsetmodeinit(&changes
, cp
, lua_nick
);
596 localdosetmode_ban(&changes
, mask
, dir
);
597 localsetmodeflush(&changes
, 1);
599 LUA_RETURN(ps
, LUA_OK
);
602 void lua_registercommands(lua_State
*l
) {
603 lua_register(l
, "irc_smsg", lua_smsg
);
604 lua_register(l
, "irc_skill", lua_skill
);
606 lua_register(l
, "chanmsg", lua_chanmsg
);
607 lua_register(l
, "scripterror", lua_scripterror
);
608 lua_register(l
, "versioninfo", lua_versioninfo
);
609 lua_register(l
, "basepath", lua_basepath
);
611 lua_register(l
, "irc_report", lua_chanmsg
);
612 lua_register(l
, "irc_ctcp", lua_ctcp
);
613 lua_register(l
, "irc_kill", lua_kill
);
614 lua_register(l
, "irc_kick", lua_kick
);
615 lua_register(l
, "irc_invite", lua_invite
);
616 lua_register(l
, "irc_gline", lua_gline
);
617 lua_register(l
, "irc_getchaninfo", lua_getchaninfo
);
618 lua_register(l
, "irc_counthost", lua_counthost
);
619 lua_register(l
, "irc_getuserbyauth", lua_getuserbyauth
);
620 lua_register(l
, "irc_notice", lua_noticecmd
);
621 lua_register(l
, "irc_opchan", lua_opchan
);
622 lua_register(l
, "irc_voicechan", lua_voicechan
);
623 lua_register(l
, "irc_chanfix", lua_chanfix
);
624 lua_register(l
, "irc_clearmode", lua_clearmode
);
625 lua_register(l
, "irc_ban", lua_ban
);
626 lua_register(l
, "irc_deopchan", lua_deopchan
);
628 lua_register(l
, "irc_getnickchans", lua_getnickchans
);
629 lua_register(l
, "irc_getnickchanindex", lua_getnickchanindex
);
630 lua_register(l
, "irc_getnickchancount", lua_getnickchancount
);
632 lua_register(l
, "irc_getfirstnick", lua_getfirstnick
);
633 lua_register(l
, "irc_getnextnick", lua_getnextnick
);
635 lua_register(l
, "irc_gethostusers", lua_gethostusers
);
636 lua_register(l
, "irc_getnickcountry", lua_getnickcountry
);
638 /* lua_register(l, "irc_iteratenickhash", lua_iteratenickhash); */
643 static int lua_smsg(lua_State
*ps
) {
644 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
645 LUA_RETURN(ps
, LUA_FAIL
);
647 LUA_RETURN(ps
, lua_cmsg((char *)lua_tostring(ps
, 2), "%s", lua_tostring(ps
, 1)));
650 static int lua_skill(lua_State
*ps
) {
654 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
655 LUA_RETURN(ps
, LUA_FAIL
);
657 n
= lua_tostring(ps
, 1);
658 msg
= lua_tostring(ps
, 2);
660 np
= getnickbynick(n
);
662 LUA_RETURN(ps
, LUA_FAIL
);
664 if(IsOper(np
) || IsService(np
) || IsXOper(np
))
665 LUA_RETURN(ps
, LUA_FAIL
);
668 LUA_RETURN(ps
, LUA_FAIL
);
670 killuser(lua_nick
, np
, "%s", msg
);
672 LUA_RETURN(ps
, LUA_OK
);