]>
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_gline(lua_State
*ps
) {
128 int duration
, usercount
= 0;
131 if(!lua_isstring(ps
, 1) || !lua_isint(ps
, 2) || !lua_isstring(ps
, 3))
132 LUA_RETURN(ps
, LUA_FAIL
);
134 duration
= lua_toint(ps
, 2);
135 if((duration
< 1) || (duration
> 86400))
136 LUA_RETURN(ps
, LUA_FAIL
);
138 reason
= lua_tostring(ps
, 3);
139 if(!lua_lineok(reason
) || !reason
)
140 LUA_RETURN(ps
, LUA_FAIL
);
142 target
= getnickbynick(lua_tostring(ps
, 1));
143 if(!target
|| (IsOper(target
) || IsXOper(target
) || IsService(target
)))
144 LUA_RETURN(ps
, LUA_FAIL
);
148 LUA_RETURN(ps
, LUA_FAIL
);
150 usercount
= hp
->clonecount
;
151 if(usercount
> 10) { /* (decent) trusted host */
157 for (j
=0;j
<NICKHASHSIZE
;j
++)
158 for (np
=nicktable
[j
];np
;np
=np
->next
)
159 if (np
&& (np
->host
== hp
) && (!ircd_strcmp(np
->ident
, target
->ident
)))
163 LUA_RETURN(ps
, LUA_FAIL
);
165 snprintf(mask
, sizeof(mask
), "*%s@%s", target
->ident
, IPtostr(target
->ipaddress
));
167 snprintf(mask
, sizeof(mask
), "*@%s", IPtostr(target
->ipaddress
));
170 irc_send("%s GL * +%s %d %d :%s", mynumeric
->content
, mask
, duration
, getnettime(), reason
);
171 LUA_RETURN(ps
, lua_cmsg(LUA_PUKECHAN
, "lua-GLINE: %s (%d users, %d seconds -- %s)", mask
, usercount
, duration
, reason
));
174 static int lua_getchaninfo(lua_State
*ps
) {
177 if(!lua_isstring(ps
, 1))
180 cp
= findchannel((char *)lua_tostring(ps
, 1));
184 LUA_PUSHCHAN(ps
, cp
);
189 static int lua_opchan(lua_State
*ps
) {
193 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
194 LUA_RETURN(ps
, LUA_FAIL
);
196 cp
= findchannel((char *)lua_tostring(ps
, 1));
198 LUA_RETURN(ps
, LUA_FAIL
);
200 np
= getnickbynick((char *)lua_tostring(ps
, 2));
202 LUA_RETURN(ps
, LUA_FAIL
);
204 localsetmodes(lua_nick
, cp
, np
, MC_OP
);
205 LUA_RETURN(ps
, LUA_OK
);
208 static int lua_voicechan(lua_State
*ps
) {
212 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
213 LUA_RETURN(ps
, LUA_FAIL
);
215 cp
= findchannel((char *)lua_tostring(ps
, 1));
217 LUA_RETURN(ps
, LUA_FAIL
);
219 np
= getnickbynick((char *)lua_tostring(ps
, 2));
221 LUA_RETURN(ps
, LUA_FAIL
);
223 localsetmodes(lua_nick
, cp
, np
, MC_VOICE
);
224 LUA_RETURN(ps
, LUA_OK
);
227 static int lua_counthost(lua_State
*ps
) {
231 if(!lua_islong(ps
, 1))
234 numeric
= lua_tolong(ps
, 1);
236 np
= getnickbynumeric(numeric
);
240 lua_pushint(ps
, np
->host
->clonecount
);
244 static int lua_versioninfo(lua_State
*ps
) {
245 lua_pushstring(ps
, LUA_VERSION
);
246 lua_pushstring(ps
, LUA_BOTVERSION
);
247 lua_pushstring(ps
, __DATE__
);
248 lua_pushstring(ps
, __TIME__
);
254 static int lua_getuserbyauth(lua_State
*l
) {
259 if(!lua_isstring(l
, 1))
262 for(i
=0;i
<NICKHASHSIZE
;i
++) {
263 for(np
=nicktable
[i
];np
;np
=np
->next
) {
264 if(np
&& np
->authname
&& !ircd_strcmp(np
->authname
, acc
)) {
274 static int lua_getnickchans(lua_State
*l
) {
279 if(!lua_islong(l
, 1))
282 np
= getnickbynumeric(lua_tolong(l
, 1));
286 channels
= (channel
**)np
->channels
->content
;
287 for(i
=0;i
<np
->channels
->cursi
;i
++)
288 lua_pushstring(l
, channels
[i
]->index
->name
->content
);
290 return np
->channels
->cursi
;
293 static int lua_gethostusers(lua_State
*l
) {
297 if(!lua_islong(l
, 1))
300 np
= getnickbynumeric(lua_tolong(l
, 1));
301 if(!np
|| !np
->host
|| !np
->host
->nicks
)
304 np
= np
->host
->nicks
;
305 count
= np
->host
->clonecount
;
316 static int lua_iteratenickhash(lua_State *l) {
321 if(!lua_isfunction(l, 1))
322 LUA_RETURN(LUA_FAIL);
324 fp = lua_touserdata(l, 1);
326 LUA_RETURN(LUA_FAIL);
328 for(i=0;i<NICKHASHSIZE;i++) {
329 for(np=nicktable[i];np;np=np->next) {
333 lua_getglobal(l, "scripterror");
337 lua_pcall(l, 1, 0, top + 1);
348 static int lua_chanfix(lua_State
*ps
) {
352 if(!lua_isstring(ps
, 1))
353 LUA_RETURN(ps
, LUA_FAIL
);
355 cp
= findchannel((char *)lua_tostring(ps
, 1));
357 LUA_RETURN(ps
, LUA_FAIL
);
359 np
= getnickbynick(LUA_CHANFIXBOT
);
361 LUA_RETURN(ps
, LUA_FAIL
);
363 lua_message(np
, "chanfix %s", cp
->index
->name
->content
);
365 LUA_RETURN(ps
, LUA_OK
);
368 static int lua_clearmode(lua_State
*ps
) {
375 if(!lua_isstring(ps
, 1))
376 LUA_RETURN(ps
, LUA_FAIL
);
378 cp
= findchannel((char *)lua_tostring(ps
, 1));
380 LUA_RETURN(ps
, LUA_FAIL
);
382 localsetmodeinit(&changes
, cp
, lua_nick
);
384 localdosetmode_key(&changes
, NULL
, MCB_DEL
);
385 localdosetmode_simple(&changes
, 0, CHANMODE_INVITEONLY
| CHANMODE_LIMIT
);
388 localdosetmode_ban(&changes
, bantostring(cp
->bans
), MCB_DEL
);
390 for(i
=0,lp
=cp
->users
->content
;i
<cp
->users
->hashsize
;i
++,lp
++)
391 if((*lp
!= nouser
) && (*lp
& CUMODE_OP
)) {
392 np
= getnickbynumeric(*lp
);
393 if(np
&& !IsService(np
))
394 localdosetmode_nick(&changes
, np
, MC_DEOP
);
397 localsetmodeflush(&changes
, 1);
399 LUA_RETURN(ps
, LUA_OK
);
402 void lua_registercommands(lua_State
*l
) {
403 lua_register(l
, "irc_smsg", lua_smsg
);
404 lua_register(l
, "irc_skill", lua_skill
);
406 lua_register(l
, "chanmsg", lua_chanmsg
);
407 lua_register(l
, "scripterror", lua_scripterror
);
408 lua_register(l
, "versioninfo", lua_versioninfo
);
410 lua_register(l
, "irc_report", lua_chanmsg
);
411 lua_register(l
, "irc_ctcp", lua_ctcp
);
412 lua_register(l
, "irc_kill", lua_kill
);
413 lua_register(l
, "irc_gline", lua_gline
);
414 lua_register(l
, "irc_getchaninfo", lua_getchaninfo
);
415 lua_register(l
, "irc_counthost", lua_counthost
);
416 lua_register(l
, "irc_getuserbyauth", lua_getuserbyauth
);
417 lua_register(l
, "irc_notice", lua_noticecmd
);
418 lua_register(l
, "irc_opchan", lua_opchan
);
419 lua_register(l
, "irc_voicechan", lua_voicechan
);
420 lua_register(l
, "irc_chanfix", lua_chanfix
);
421 lua_register(l
, "irc_clearmode", lua_clearmode
);
423 lua_register(l
, "irc_getnickchans", lua_getnickchans
);
424 lua_register(l
, "irc_gethostusers", lua_gethostusers
);
426 /* lua_register(l, "irc_iteratenickhash", lua_iteratenickhash); */
431 static int lua_smsg(lua_State
*ps
) {
432 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
433 LUA_RETURN(ps
, LUA_FAIL
);
435 LUA_RETURN(ps
, lua_cmsg((char *)lua_tostring(ps
, 2), "%s", lua_tostring(ps
, 1)));
438 static int lua_skill(lua_State
*ps
) {
442 if(!lua_isstring(ps
, 1) || !lua_isstring(ps
, 2))
443 LUA_RETURN(ps
, LUA_FAIL
);
445 n
= lua_tostring(ps
, 1);
446 msg
= lua_tostring(ps
, 2);
448 np
= getnickbynick(n
);
450 LUA_RETURN(ps
, LUA_FAIL
);
452 if(IsOper(np
) || IsService(np
) || IsXOper(np
))
453 LUA_RETURN(ps
, LUA_FAIL
);
456 LUA_RETURN(ps
, LUA_FAIL
);
458 killuser(lua_nick
, np
, "%s", msg
);
460 LUA_RETURN(ps
, LUA_OK
);