]>
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__
);
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 acc
= lua_tostring(l
, 1);
354 for(i
=0;i
<NICKHASHSIZE
;i
++) {
355 for(np
=nicktable
[i
];np
;np
=np
->next
) {
356 if(np
&& np
->authname
&& !ircd_strcmp(np
->authname
, acc
)) {
366 static int lua_getnickchans(lua_State
*l
) {
371 if(!lua_islong(l
, 1))
374 np
= getnickbynumeric(lua_tolong(l
, 1));
378 channels
= (channel
**)np
->channels
->content
;
379 for(i
=0;i
<np
->channels
->cursi
;i
++)
380 lua_pushstring(l
, channels
[i
]->index
->name
->content
);
382 return np
->channels
->cursi
;
385 static int lua_getnickchanindex(lua_State
*l
) {
389 if(!lua_islong(l
, 1) || !lua_isint(l
, 2))
392 np
= getnickbynumeric(lua_tolong(l
, 1));
396 offset
= lua_toint(l
, 2);
397 if((offset
< 0) || (offset
>= np
->channels
->cursi
))
400 lua_pushstring(l
, ((channel
**)np
->channels
->content
)[offset
]->index
->name
->content
);
408 static int lua_getnextnick(lua_State
*l
) {
409 if(!lasthashnick
&& (hashindex
!= -1))
415 if(hashindex
>= NICKHASHSIZE
)
417 lasthashnick
= nicktable
[hashindex
];
419 lasthashnick
= lasthashnick
->next
;
421 } while(!lasthashnick
);
423 LUA_PUSHNICK(l
, lasthashnick
);
427 static int lua_getfirstnick(lua_State
*l
) {
431 return lua_getnextnick(l
);
434 static int lua_getnickchancount(lua_State
*l
) {
437 if(!lua_islong(l
, 1))
440 np
= getnickbynumeric(lua_tolong(l
, 1));
444 lua_pushint(l
, np
->channels
->cursi
);
449 static int lua_gethostusers(lua_State
*l
) {
453 if(!lua_islong(l
, 1))
456 np
= getnickbynumeric(lua_tolong(l
, 1));
457 if(!np
|| !np
->host
|| !np
->host
->nicks
)
460 np
= np
->host
->nicks
;
461 count
= np
->host
->clonecount
;
471 static int lua_getnickcountry(lua_State
*l
) {
475 ext
= findnickext("geoip");
479 if(!lua_islong(l
, 1))
482 np
= getnickbynumeric(lua_tolong(l
, 1));
486 lua_pushint(l
, (int)np
->exts
[ext
]);
490 static int lua_chanfix(lua_State
*ps
) {
494 if(!lua_isstring(ps
, 1))
495 LUA_RETURN(ps
, LUA_FAIL
);
497 cp
= findchannel((char *)lua_tostring(ps
, 1));
499 LUA_RETURN(ps
, LUA_FAIL
);
501 np
= getnickbynick(LUA_CHANFIXBOT
);
503 LUA_RETURN(ps
, LUA_FAIL
);
505 lua_message(np
, "chanfix %s", cp
->index
->name
->content
);
507 LUA_RETURN(ps
, LUA_OK
);
510 static int lua_clearmode(lua_State
*ps
) {
517 if(!lua_isstring(ps
, 1))
518 LUA_RETURN(ps
, LUA_FAIL
);
520 cp
= findchannel((char *)lua_tostring(ps
, 1));
522 LUA_RETURN(ps
, LUA_FAIL
);
524 localsetmodeinit(&changes
, cp
, lua_nick
);
526 localdosetmode_key(&changes
, NULL
, MCB_DEL
);
527 localdosetmode_simple(&changes
, 0, CHANMODE_INVITEONLY
| CHANMODE_LIMIT
);
530 localdosetmode_ban(&changes
, bantostring(cp
->bans
), MCB_DEL
);
532 for(i
=0,lp
=cp
->users
->content
;i
<cp
->users
->hashsize
;i
++,lp
++)
533 if((*lp
!= nouser
) && (*lp
& CUMODE_OP
)) {
534 np
= getnickbynumeric(*lp
);
535 if(np
&& !IsService(np
))
536 localdosetmode_nick(&changes
, np
, MC_DEOP
);
539 localsetmodeflush(&changes
, 1);
541 LUA_RETURN(ps
, LUA_OK
);
544 static int lua_ban(lua_State
*ps
) {
550 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
551 LUA_RETURN(ps
, LUA_FAIL
);
553 if(lua_isboolean(ps
, 3) && lua_toboolean(ps
, 3))
556 cp
= findchannel((char *)lua_tostring(ps
, 1));
558 LUA_RETURN(ps
, LUA_FAIL
);
560 mask
= lua_tostring(ps
, 2);
561 if(!mask
|| !mask
[0] || !lua_lineok(mask
))
562 LUA_RETURN(ps
, LUA_FAIL
);
564 localsetmodeinit(&changes
, cp
, lua_nick
);
565 localdosetmode_ban(&changes
, mask
, dir
);
566 localsetmodeflush(&changes
, 1);
568 LUA_RETURN(ps
, LUA_OK
);
571 void lua_registercommands(lua_State
*l
) {
572 lua_register(l
, "irc_smsg", lua_smsg
);
573 lua_register(l
, "irc_skill", lua_skill
);
575 lua_register(l
, "chanmsg", lua_chanmsg
);
576 lua_register(l
, "scripterror", lua_scripterror
);
577 lua_register(l
, "versioninfo", lua_versioninfo
);
578 lua_register(l
, "basepath", lua_basepath
);
580 lua_register(l
, "irc_report", lua_chanmsg
);
581 lua_register(l
, "irc_ctcp", lua_ctcp
);
582 lua_register(l
, "irc_kill", lua_kill
);
583 lua_register(l
, "irc_kick", lua_kick
);
584 lua_register(l
, "irc_invite", lua_invite
);
585 lua_register(l
, "irc_gline", lua_gline
);
586 lua_register(l
, "irc_getchaninfo", lua_getchaninfo
);
587 lua_register(l
, "irc_counthost", lua_counthost
);
588 lua_register(l
, "irc_getuserbyauth", lua_getuserbyauth
);
589 lua_register(l
, "irc_notice", lua_noticecmd
);
590 lua_register(l
, "irc_opchan", lua_opchan
);
591 lua_register(l
, "irc_voicechan", lua_voicechan
);
592 lua_register(l
, "irc_chanfix", lua_chanfix
);
593 lua_register(l
, "irc_clearmode", lua_clearmode
);
594 lua_register(l
, "irc_ban", lua_ban
);
595 lua_register(l
, "irc_deopchan", lua_deopchan
);
597 lua_register(l
, "irc_getnickchans", lua_getnickchans
);
598 lua_register(l
, "irc_getnickchanindex", lua_getnickchanindex
);
599 lua_register(l
, "irc_getnickchancount", lua_getnickchancount
);
601 lua_register(l
, "irc_getfirstnick", lua_getfirstnick
);
602 lua_register(l
, "irc_getnextnick", lua_getnextnick
);
604 lua_register(l
, "irc_gethostusers", lua_gethostusers
);
605 lua_register(l
, "irc_getnickcountry", lua_getnickcountry
);
610 static int lua_smsg(lua_State
*ps
) {
611 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
612 LUA_RETURN(ps
, LUA_FAIL
);
614 LUA_RETURN(ps
, lua_cmsg((char *)lua_tostring(ps
, 2), "%s", lua_tostring(ps
, 1)));
617 static int lua_skill(lua_State
*ps
) {
621 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
622 LUA_RETURN(ps
, LUA_FAIL
);
624 n
= lua_tostring(ps
, 1);
625 msg
= lua_tostring(ps
, 2);
627 np
= getnickbynick(n
);
629 LUA_RETURN(ps
, LUA_FAIL
);
631 if(IsOper(np
) || IsService(np
) || IsXOper(np
))
632 LUA_RETURN(ps
, LUA_FAIL
);
635 LUA_RETURN(ps
, LUA_FAIL
);
637 killuser(lua_nick
, np
, "%s", msg
);
639 LUA_RETURN(ps
, LUA_OK
);