]>
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"
17 static int lua_smsg(lua_State
*ps
);
18 static int lua_skill(lua_State
*ps
);
20 int lua_lineok(const char *data
) {
21 if(strchr(data
, '\r') || strchr(data
, '\n'))
26 int lua_cmsg(char *channell
, char *message
, ...) {
31 va_start(va
, message
);
32 vsnprintf(buf
, sizeof(buf
), message
, va
);
35 cp
= findchannel(channell
);
42 lua_channelmessage(cp
, "%s", buf
);
47 static int lua_chanmsg(lua_State
*ps
) {
48 if(!lua_isstring(ps
, 1))
49 LUA_RETURN(ps
, LUA_FAIL
);
51 LUA_RETURN(ps
, lua_cmsg(LUA_PUKECHAN
, "lua: %s", lua_tostring(ps
, 1)));
54 static int lua_scripterror(lua_State
*ps
) {
55 if(!lua_isstring(ps
, 1))
56 LUA_RETURN(ps
, LUA_FAIL
);
58 LUA_RETURN(ps
, lua_cmsg(LUA_PUKECHAN
, "lua-error: %s", lua_tostring(ps
, 1)));
61 static int lua_ctcp(lua_State
*ps
) {
65 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
66 LUA_RETURN(ps
, LUA_FAIL
);
68 n
= lua_tostring(ps
, 1);
69 msg
= lua_tostring(ps
, 2);
71 np
= getnickbynick(n
);
72 if(!np
|| !lua_lineok(msg
))
73 LUA_RETURN(ps
, LUA_FAIL
);
75 lua_message(np
, "\001%s\001", msg
);
77 LUA_RETURN(ps
, lua_cmsg(LUA_PUKECHAN
, "lua-ctcp: %s (%s)", np
->nick
, msg
));
80 static int lua_noticecmd(lua_State
*ps
) {
84 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
85 LUA_RETURN(ps
, LUA_FAIL
);
87 n
= lua_tostring(ps
, 1);
88 msg
= lua_tostring(ps
, 2);
90 np
= getnickbynick(n
);
91 if(!np
|| !lua_lineok(msg
))
92 LUA_RETURN(ps
, LUA_FAIL
);
94 lua_notice(np
, "%s", msg
);
96 LUA_RETURN(ps
, LUA_OK
);
99 static int lua_kill(lua_State
*ps
) {
103 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
104 LUA_RETURN(ps
, LUA_FAIL
);
106 n
= lua_tostring(ps
, 1);
107 msg
= lua_tostring(ps
, 2);
109 np
= getnickbynick(n
);
111 LUA_RETURN(ps
, LUA_FAIL
);
113 if(IsOper(np
) || IsService(np
) || IsXOper(np
))
114 LUA_RETURN(ps
, LUA_FAIL
);
117 LUA_RETURN(ps
, LUA_FAIL
);
119 killuser(lua_nick
, np
, "%s", msg
);
121 LUA_RETURN(ps
, lua_cmsg(LUA_PUKECHAN
, "lua-KILL: %s (%s)", np
->nick
, msg
));
124 static int lua_kick(lua_State
*ps
) {
125 const char *n
, *msg
, *chan
;
130 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
131 LUA_RETURN(ps
, LUA_FAIL
);
133 chan
= lua_tostring(ps
, 1);
134 n
= lua_tostring(ps
, 2);
135 msg
= lua_tostring(ps
, 3);
137 if(lua_isboolean(ps
, 4) && !lua_toboolean(ps
, 4))
140 np
= getnickbynick(n
);
142 LUA_RETURN(ps
, LUA_FAIL
);
144 if(dochecks
&& (IsOper(np
) || IsXOper(np
) || IsService(np
)))
145 LUA_RETURN(ps
, LUA_FAIL
);
147 cp
= findchannel((char *)chan
);
149 LUA_RETURN(ps
, LUA_FAIL
);
152 LUA_RETURN(ps
, LUA_FAIL
);
154 localkickuser(lua_nick
, cp
, np
, msg
);
156 LUA_RETURN(ps
, LUA_OK
);
159 static int lua_invite(lua_State
*ps
) {
163 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
164 LUA_RETURN(ps
, LUA_FAIL
);
166 np
= getnickbynick((char *)lua_tostring(ps
, 1));
168 LUA_RETURN(ps
, LUA_FAIL
);
170 cp
= findchannel((char *)lua_tostring(ps
, 2));
172 LUA_RETURN(ps
, LUA_FAIL
);
174 localinvite(lua_nick
, cp
, np
);
176 LUA_RETURN(ps
, LUA_OK
);
179 static int lua_gline(lua_State
*ps
) {
183 int duration
, usercount
= 0;
186 if(!lua_isstring(ps
, 1) || !lua_isint(ps
, 2) || !lua_isstring(ps
, 3))
187 LUA_RETURN(ps
, LUA_FAIL
);
189 duration
= lua_toint(ps
, 2);
190 if((duration
< 1) || (duration
> 86400))
191 LUA_RETURN(ps
, LUA_FAIL
);
193 reason
= lua_tostring(ps
, 3);
194 if(!lua_lineok(reason
) || !reason
)
195 LUA_RETURN(ps
, LUA_FAIL
);
197 target
= getnickbynick(lua_tostring(ps
, 1));
198 if(!target
|| (IsOper(target
) || IsXOper(target
) || IsService(target
)))
199 LUA_RETURN(ps
, LUA_FAIL
);
203 LUA_RETURN(ps
, LUA_FAIL
);
205 usercount
= hp
->clonecount
;
206 if(usercount
> 10) { /* (decent) trusted host */
212 for (j
=0;j
<NICKHASHSIZE
;j
++)
213 for (np
=nicktable
[j
];np
;np
=np
->next
)
214 if (np
&& (np
->host
== hp
) && (!ircd_strcmp(np
->ident
, target
->ident
)))
218 LUA_RETURN(ps
, LUA_FAIL
);
220 snprintf(mask
, sizeof(mask
), "*%s@%s", target
->ident
, IPtostr(target
->ipaddress
));
222 snprintf(mask
, sizeof(mask
), "*@%s", IPtostr(target
->ipaddress
));
225 irc_send("%s GL * +%s %d :%s", mynumeric
->content
, mask
, duration
, reason
);
226 LUA_RETURN(ps
, lua_cmsg(LUA_PUKECHAN
, "lua-GLINE: %s (%d users, %d seconds -- %s)", mask
, usercount
, duration
, reason
));
229 static int lua_getchaninfo(lua_State
*ps
) {
232 if(!lua_isstring(ps
, 1))
235 cp
= findchannel((char *)lua_tostring(ps
, 1));
239 LUA_PUSHCHAN(ps
, cp
);
244 static int lua_opchan(lua_State
*ps
) {
248 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
249 LUA_RETURN(ps
, LUA_FAIL
);
251 cp
= findchannel((char *)lua_tostring(ps
, 1));
253 LUA_RETURN(ps
, LUA_FAIL
);
255 np
= getnickbynick((char *)lua_tostring(ps
, 2));
257 LUA_RETURN(ps
, LUA_FAIL
);
259 localsetmodes(lua_nick
, cp
, np
, MC_OP
);
260 LUA_RETURN(ps
, LUA_OK
);
263 static int lua_voicechan(lua_State
*ps
) {
267 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
268 LUA_RETURN(ps
, LUA_FAIL
);
270 cp
= findchannel((char *)lua_tostring(ps
, 1));
272 LUA_RETURN(ps
, LUA_FAIL
);
274 np
= getnickbynick((char *)lua_tostring(ps
, 2));
276 LUA_RETURN(ps
, LUA_FAIL
);
278 localsetmodes(lua_nick
, cp
, np
, MC_VOICE
);
279 LUA_RETURN(ps
, LUA_OK
);
282 static int lua_counthost(lua_State
*ps
) {
286 if(!lua_islong(ps
, 1))
289 numeric
= lua_tolong(ps
, 1);
291 np
= getnickbynumeric(numeric
);
295 lua_pushint(ps
, np
->host
->clonecount
);
299 static int lua_versioninfo(lua_State
*ps
) {
300 lua_pushstring(ps
, LUA_VERSION
);
301 lua_pushstring(ps
, LUA_BOTVERSION
);
302 lua_pushstring(ps
, __DATE__
);
303 lua_pushstring(ps
, __TIME__
);
309 static int lua_getuserbyauth(lua_State
*l
) {
314 if(!lua_isstring(l
, 1))
317 for(i
=0;i
<NICKHASHSIZE
;i
++) {
318 for(np
=nicktable
[i
];np
;np
=np
->next
) {
319 if(np
&& np
->authname
&& !ircd_strcmp(np
->authname
, acc
)) {
329 static int lua_getnickchans(lua_State
*l
) {
334 if(!lua_islong(l
, 1))
337 np
= getnickbynumeric(lua_tolong(l
, 1));
341 channels
= (channel
**)np
->channels
->content
;
342 for(i
=0;i
<np
->channels
->cursi
;i
++)
343 lua_pushstring(l
, channels
[i
]->index
->name
->content
);
345 return np
->channels
->cursi
;
348 static int lua_getnickchancount(lua_State
*l
) {
351 if(!lua_islong(l
, 1))
354 np
= getnickbynumeric(lua_tolong(l
, 1));
358 lua_pushint(l
, np
->channels
->cursi
);
363 static int lua_gethostusers(lua_State
*l
) {
367 if(!lua_islong(l
, 1))
370 np
= getnickbynumeric(lua_tolong(l
, 1));
371 if(!np
|| !np
->host
|| !np
->host
->nicks
)
374 np
= np
->host
->nicks
;
375 count
= np
->host
->clonecount
;
385 static int lua_getnickcountry(lua_State
*l
) {
389 ext
= findnickext("geoip");
393 if(!lua_islong(l
, 1))
396 np
= getnickbynumeric(lua_tolong(l
, 1));
400 lua_pushint(l
, (int)np
->exts
[ext
]);
405 static int lua_iteratenickhash(lua_State *l) {
410 if(!lua_isfunction(l, 1))
411 LUA_RETURN(LUA_FAIL);
413 fp = lua_touserdata(l, 1);
415 LUA_RETURN(LUA_FAIL);
417 for(i=0;i<NICKHASHSIZE;i++) {
418 for(np=nicktable[i];np;np=np->next) {
422 lua_getglobal(l, "scripterror");
426 lua_pcall(l, 1, 0, top + 1);
437 static int lua_chanfix(lua_State
*ps
) {
441 if(!lua_isstring(ps
, 1))
442 LUA_RETURN(ps
, LUA_FAIL
);
444 cp
= findchannel((char *)lua_tostring(ps
, 1));
446 LUA_RETURN(ps
, LUA_FAIL
);
448 np
= getnickbynick(LUA_CHANFIXBOT
);
450 LUA_RETURN(ps
, LUA_FAIL
);
452 lua_message(np
, "chanfix %s", cp
->index
->name
->content
);
454 LUA_RETURN(ps
, LUA_OK
);
457 static int lua_clearmode(lua_State
*ps
) {
464 if(!lua_isstring(ps
, 1))
465 LUA_RETURN(ps
, LUA_FAIL
);
467 cp
= findchannel((char *)lua_tostring(ps
, 1));
469 LUA_RETURN(ps
, LUA_FAIL
);
471 localsetmodeinit(&changes
, cp
, lua_nick
);
473 localdosetmode_key(&changes
, NULL
, MCB_DEL
);
474 localdosetmode_simple(&changes
, 0, CHANMODE_INVITEONLY
| CHANMODE_LIMIT
);
477 localdosetmode_ban(&changes
, bantostring(cp
->bans
), MCB_DEL
);
479 for(i
=0,lp
=cp
->users
->content
;i
<cp
->users
->hashsize
;i
++,lp
++)
480 if((*lp
!= nouser
) && (*lp
& CUMODE_OP
)) {
481 np
= getnickbynumeric(*lp
);
482 if(np
&& !IsService(np
))
483 localdosetmode_nick(&changes
, np
, MC_DEOP
);
486 localsetmodeflush(&changes
, 1);
488 LUA_RETURN(ps
, LUA_OK
);
491 static int lua_ban(lua_State
*ps
) {
497 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
498 LUA_RETURN(ps
, LUA_FAIL
);
500 if(lua_isboolean(ps
, 3) && lua_toboolean(ps
, 3))
503 cp
= findchannel((char *)lua_tostring(ps
, 1));
505 LUA_RETURN(ps
, LUA_FAIL
);
507 mask
= lua_tostring(ps
, 2);
508 if(!mask
|| !mask
[0] || !lua_lineok(mask
))
509 LUA_RETURN(ps
, LUA_FAIL
);
511 localsetmodeinit(&changes
, cp
, lua_nick
);
512 localdosetmode_ban(&changes
, mask
, dir
);
513 localsetmodeflush(&changes
, 1);
515 LUA_RETURN(ps
, LUA_OK
);
518 void lua_registercommands(lua_State
*l
) {
519 lua_register(l
, "irc_smsg", lua_smsg
);
520 lua_register(l
, "irc_skill", lua_skill
);
522 lua_register(l
, "chanmsg", lua_chanmsg
);
523 lua_register(l
, "scripterror", lua_scripterror
);
524 lua_register(l
, "versioninfo", lua_versioninfo
);
526 lua_register(l
, "irc_report", lua_chanmsg
);
527 lua_register(l
, "irc_ctcp", lua_ctcp
);
528 lua_register(l
, "irc_kill", lua_kill
);
529 lua_register(l
, "irc_kick", lua_kick
);
530 lua_register(l
, "irc_invite", lua_invite
);
531 lua_register(l
, "irc_gline", lua_gline
);
532 lua_register(l
, "irc_getchaninfo", lua_getchaninfo
);
533 lua_register(l
, "irc_counthost", lua_counthost
);
534 lua_register(l
, "irc_getuserbyauth", lua_getuserbyauth
);
535 lua_register(l
, "irc_notice", lua_noticecmd
);
536 lua_register(l
, "irc_opchan", lua_opchan
);
537 lua_register(l
, "irc_voicechan", lua_voicechan
);
538 lua_register(l
, "irc_chanfix", lua_chanfix
);
539 lua_register(l
, "irc_clearmode", lua_clearmode
);
540 lua_register(l
, "irc_ban", lua_ban
);
542 lua_register(l
, "irc_getnickchans", lua_getnickchans
);
543 lua_register(l
, "irc_getnickchancount", lua_getnickchancount
);
544 lua_register(l
, "irc_gethostusers", lua_gethostusers
);
545 lua_register(l
, "irc_getnickcountry", lua_getnickcountry
);
547 /* lua_register(l, "irc_iteratenickhash", lua_iteratenickhash); */
552 static int lua_smsg(lua_State
*ps
) {
553 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
554 LUA_RETURN(ps
, LUA_FAIL
);
556 LUA_RETURN(ps
, lua_cmsg((char *)lua_tostring(ps
, 2), "%s", lua_tostring(ps
, 1)));
559 static int lua_skill(lua_State
*ps
) {
563 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
564 LUA_RETURN(ps
, LUA_FAIL
);
566 n
= lua_tostring(ps
, 1);
567 msg
= lua_tostring(ps
, 2);
569 np
= getnickbynick(n
);
571 LUA_RETURN(ps
, LUA_FAIL
);
573 if(IsOper(np
) || IsService(np
) || IsXOper(np
))
574 LUA_RETURN(ps
, LUA_FAIL
);
577 LUA_RETURN(ps
, LUA_FAIL
);
579 killuser(lua_nick
, np
, "%s", msg
);
581 LUA_RETURN(ps
, LUA_OK
);