]> jfr.im git - irc/quakenet/newserv.git/blob - lua/luacommands.c
Add count function
[irc/quakenet/newserv.git] / lua / luacommands.c
1 /* Copyright (C) Chris Porter 2005 */
2 /* ALL RIGHTS RESERVED. */
3 /* Don't put this into the SVN repo. */
4
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"
11
12 #include "lua.h"
13 #include "luabot.h"
14
15 #include <stdarg.h>
16
17 static int lua_smsg(lua_State *ps);
18 static int lua_skill(lua_State *ps);
19
20 int lua_lineok(const char *data) {
21 if(strchr(data, '\r') || strchr(data, '\n'))
22 return 0;
23 return 1;
24 }
25
26 int lua_cmsg(char *channell, char *message, ...) {
27 char buf[512];
28 va_list va;
29 channel *cp;
30
31 va_start(va, message);
32 vsnprintf(buf, sizeof(buf), message, va);
33 va_end(va);
34
35 cp = findchannel(channell);
36 if(!cp)
37 return LUA_FAIL;
38
39 if(!lua_lineok(buf))
40 return LUA_FAIL;
41
42 lua_channelmessage(cp, "%s", buf);
43
44 return LUA_OK;
45 }
46
47 static int lua_chanmsg(lua_State *ps) {
48 if(!lua_isstring(ps, 1))
49 LUA_RETURN(ps, LUA_FAIL);
50
51 LUA_RETURN(ps, lua_cmsg(LUA_PUKECHAN, "lua: %s", lua_tostring(ps, 1)));
52 }
53
54 static int lua_scripterror(lua_State *ps) {
55 if(!lua_isstring(ps, 1))
56 LUA_RETURN(ps, LUA_FAIL);
57
58 LUA_RETURN(ps, lua_cmsg(LUA_PUKECHAN, "lua-error: %s", lua_tostring(ps, 1)));
59 }
60
61 static int lua_ctcp(lua_State *ps) {
62 const char *n, *msg;
63 nick *np;
64
65 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
66 LUA_RETURN(ps, LUA_FAIL);
67
68 n = lua_tostring(ps, 1);
69 msg = lua_tostring(ps, 2);
70
71 np = getnickbynick(n);
72 if(!np || !lua_lineok(msg))
73 LUA_RETURN(ps, LUA_FAIL);
74
75 lua_message(np, "\001%s\001", msg);
76
77 LUA_RETURN(ps, lua_cmsg(LUA_PUKECHAN, "lua-ctcp: %s (%s)", np->nick, msg));
78 }
79
80 static int lua_noticecmd(lua_State *ps) {
81 const char *n, *msg;
82 nick *np;
83
84 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
85 LUA_RETURN(ps, LUA_FAIL);
86
87 n = lua_tostring(ps, 1);
88 msg = lua_tostring(ps, 2);
89
90 np = getnickbynick(n);
91 if(!np || !lua_lineok(msg))
92 LUA_RETURN(ps, LUA_FAIL);
93
94 lua_notice(np, "%s", msg);
95
96 LUA_RETURN(ps, LUA_OK);
97 }
98
99 static int lua_kill(lua_State *ps) {
100 const char *n, *msg;
101 nick *np;
102
103 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
104 LUA_RETURN(ps, LUA_FAIL);
105
106 n = lua_tostring(ps, 1);
107 msg = lua_tostring(ps, 2);
108
109 np = getnickbynick(n);
110 if(!np)
111 LUA_RETURN(ps, LUA_FAIL);
112
113 if(IsOper(np) || IsService(np) || IsXOper(np))
114 LUA_RETURN(ps, LUA_FAIL);
115
116 if(!lua_lineok(msg))
117 LUA_RETURN(ps, LUA_FAIL);
118
119 killuser(lua_nick, np, "%s", msg);
120
121 LUA_RETURN(ps, lua_cmsg(LUA_PUKECHAN, "lua-KILL: %s (%s)", np->nick, msg));
122 }
123
124 static int lua_kick(lua_State *ps) {
125 const char *n, *msg, *chan;
126 nick *np;
127 channel *cp;
128 int dochecks = 1;
129
130 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
131 LUA_RETURN(ps, LUA_FAIL);
132
133 chan = lua_tostring(ps, 1);
134 n = lua_tostring(ps, 2);
135 msg = lua_tostring(ps, 3);
136
137 if(lua_isboolean(ps, 4) && !lua_toboolean(ps, 4))
138 dochecks = 0;
139
140 np = getnickbynick(n);
141 if(!np)
142 LUA_RETURN(ps, LUA_FAIL);
143
144 if(dochecks && (IsOper(np) || IsXOper(np) || IsService(np)))
145 LUA_RETURN(ps, LUA_FAIL);
146
147 cp = findchannel((char *)chan);
148 if(!cp)
149 LUA_RETURN(ps, LUA_FAIL);
150
151 if(!lua_lineok(msg))
152 LUA_RETURN(ps, LUA_FAIL);
153
154 localkickuser(lua_nick, cp, np, msg);
155
156 LUA_RETURN(ps, LUA_OK);
157 }
158
159 static int lua_invite(lua_State *ps) {
160 nick *np;
161 channel *cp;
162
163 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
164 LUA_RETURN(ps, LUA_FAIL);
165
166 np = getnickbynick((char *)lua_tostring(ps, 1));
167 if(!np)
168 LUA_RETURN(ps, LUA_FAIL);
169
170 cp = findchannel((char *)lua_tostring(ps, 2));
171 if(!cp)
172 LUA_RETURN(ps, LUA_FAIL);
173
174 localinvite(lua_nick, cp, np);
175
176 LUA_RETURN(ps, LUA_OK);
177 }
178
179 static int lua_gline(lua_State *ps) {
180 const char *reason;
181 nick *target;
182 char mask[512];
183 int duration, usercount = 0;
184 host *hp;
185
186 if(!lua_isstring(ps, 1) || !lua_isint(ps, 2) || !lua_isstring(ps, 3))
187 LUA_RETURN(ps, LUA_FAIL);
188
189 duration = lua_toint(ps, 2);
190 if((duration < 1) || (duration > 86400))
191 LUA_RETURN(ps, LUA_FAIL);
192
193 reason = lua_tostring(ps, 3);
194 if(!lua_lineok(reason) || !reason)
195 LUA_RETURN(ps, LUA_FAIL);
196
197 target = getnickbynick(lua_tostring(ps, 1));
198 if(!target || (IsOper(target) || IsXOper(target) || IsService(target)))
199 LUA_RETURN(ps, LUA_FAIL);
200
201 hp = target->host;
202 if(!hp)
203 LUA_RETURN(ps, LUA_FAIL);
204
205 usercount = hp->clonecount;
206 if(usercount > 10) { /* (decent) trusted host */
207 int j;
208 nick *np;
209
210 usercount = 0;
211
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)))
215 usercount++;
216
217 if(usercount > 50)
218 LUA_RETURN(ps, LUA_FAIL);
219
220 snprintf(mask, sizeof(mask), "*%s@%s", target->ident, IPtostr(target->ipaddress));
221 } else {
222 snprintf(mask, sizeof(mask), "*@%s", IPtostr(target->ipaddress));
223 }
224
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));
227 }
228
229 static int lua_getchaninfo(lua_State *ps) {
230 channel *cp;
231
232 if(!lua_isstring(ps, 1))
233 return 0;
234
235 cp = findchannel((char *)lua_tostring(ps, 1));
236 if(!cp)
237 return 0;
238
239 LUA_PUSHCHAN(ps, cp);
240
241 return 1;
242 }
243
244 static int lua_opchan(lua_State *ps) {
245 channel *cp;
246 nick *np;
247
248 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
249 LUA_RETURN(ps, LUA_FAIL);
250
251 cp = findchannel((char *)lua_tostring(ps, 1));
252 if(!cp)
253 LUA_RETURN(ps, LUA_FAIL);
254
255 np = getnickbynick((char *)lua_tostring(ps, 2));
256 if(!np)
257 LUA_RETURN(ps, LUA_FAIL);
258
259 localsetmodes(lua_nick, cp, np, MC_OP);
260 LUA_RETURN(ps, LUA_OK);
261 }
262
263 static int lua_voicechan(lua_State *ps) {
264 channel *cp;
265 nick *np;
266
267 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
268 LUA_RETURN(ps, LUA_FAIL);
269
270 cp = findchannel((char *)lua_tostring(ps, 1));
271 if(!cp)
272 LUA_RETURN(ps, LUA_FAIL);
273
274 np = getnickbynick((char *)lua_tostring(ps, 2));
275 if(!np)
276 LUA_RETURN(ps, LUA_FAIL);
277
278 localsetmodes(lua_nick, cp, np, MC_VOICE);
279 LUA_RETURN(ps, LUA_OK);
280 }
281
282 static int lua_counthost(lua_State *ps) {
283 long numeric;
284 nick *np;
285
286 if(!lua_islong(ps, 1))
287 return 0;
288
289 numeric = lua_tolong(ps, 1);
290
291 np = getnickbynumeric(numeric);
292 if(!np)
293 return 0;
294
295 lua_pushint(ps, np->host->clonecount);
296 return 1;
297 }
298
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__);
304
305 return 4;
306 }
307
308 /* O(n) */
309 static int lua_getuserbyauth(lua_State *l) {
310 const char *acc;
311 nick *np;
312 int i, found = 0;
313
314 if(!lua_isstring(l, 1))
315 return 0;
316
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)) {
320 LUA_PUSHNICK(l, np);
321 found++;
322 }
323 }
324 }
325
326 return found;
327 }
328
329 static int lua_getnickchans(lua_State *l) {
330 nick *np;
331 int i;
332 channel **channels;
333
334 if(!lua_islong(l, 1))
335 return 0;
336
337 np = getnickbynumeric(lua_tolong(l, 1));
338 if(!np)
339 return 0;
340
341 channels = (channel **)np->channels->content;
342 for(i=0;i<np->channels->cursi;i++)
343 lua_pushstring(l, channels[i]->index->name->content);
344
345 return np->channels->cursi;
346 }
347
348 static int lua_getnickchancount(lua_State *l) {
349 nick *np;
350
351 if(!lua_islong(l, 1))
352 return 0;
353
354 np = getnickbynumeric(lua_tolong(l, 1));
355 if(!np)
356 return 0;
357
358 lua_pushint(l, np->channels->cursi);
359
360 return 1;
361 }
362
363 static int lua_gethostusers(lua_State *l) {
364 nick *np;
365 int count;
366
367 if(!lua_islong(l, 1))
368 return 0;
369
370 np = getnickbynumeric(lua_tolong(l, 1));
371 if(!np || !np->host || !np->host->nicks)
372 return 0;
373
374 np = np->host->nicks;
375 count = np->host->clonecount;
376
377 do {
378 LUA_PUSHNICK(l, np);
379 np = np->nextbyhost;
380 } while(np);
381
382 return count;
383 }
384
385 static int lua_getnickcountry(lua_State *l) {
386 nick *np;
387 int ext;
388
389 ext = findnickext("geoip");
390 if(ext == -1)
391 return 0;
392
393 if(!lua_islong(l, 1))
394 return 0;
395
396 np = getnickbynumeric(lua_tolong(l, 1));
397 if(!np)
398 return 0;
399
400 lua_pushint(l, (int)np->exts[ext]);
401 return 1;
402 }
403
404 /*
405 static int lua_iteratenickhash(lua_State *l) {
406 nick *np;
407 int i, top;
408 void *fp;
409
410 if(!lua_isfunction(l, 1))
411 LUA_RETURN(LUA_FAIL);
412
413 fp = lua_touserdata(l, 1);
414 if(!fp)
415 LUA_RETURN(LUA_FAIL);
416
417 for(i=0;i<NICKHASHSIZE;i++) {
418 for(np=nicktable[i];np;np=np->next) {
419 if(np) {
420 top = lua_gettop(l);
421
422 lua_getglobal(l, "scripterror");
423 lua_insert
424
425 LUA_PUSHNICK(l, np);
426 lua_pcall(l, 1, 0, top + 1);
427
428 lua_settop(l, top);
429 }
430 }
431 }
432
433 LUA_RETURN(LUA_OK);
434 }
435 */
436
437 static int lua_chanfix(lua_State *ps) {
438 channel *cp;
439 nick *np;
440
441 if(!lua_isstring(ps, 1))
442 LUA_RETURN(ps, LUA_FAIL);
443
444 cp = findchannel((char *)lua_tostring(ps, 1));
445 if(!cp)
446 LUA_RETURN(ps, LUA_FAIL);
447
448 np = getnickbynick(LUA_CHANFIXBOT);
449 if(!np)
450 LUA_RETURN(ps, LUA_FAIL);
451
452 lua_message(np, "chanfix %s", cp->index->name->content);
453
454 LUA_RETURN(ps, LUA_OK);
455 }
456
457 static int lua_clearmode(lua_State *ps) {
458 channel *cp;
459 int i;
460 nick *np;
461 unsigned long *lp;
462 modechanges changes;
463
464 if(!lua_isstring(ps, 1))
465 LUA_RETURN(ps, LUA_FAIL);
466
467 cp = findchannel((char *)lua_tostring(ps, 1));
468 if(!cp)
469 LUA_RETURN(ps, LUA_FAIL);
470
471 localsetmodeinit(&changes, cp, lua_nick);
472
473 localdosetmode_key(&changes, NULL, MCB_DEL);
474 localdosetmode_simple(&changes, 0, CHANMODE_INVITEONLY | CHANMODE_LIMIT);
475
476 while(cp->bans)
477 localdosetmode_ban(&changes, bantostring(cp->bans), MCB_DEL);
478
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);
484 }
485
486 localsetmodeflush(&changes, 1);
487
488 LUA_RETURN(ps, LUA_OK);
489 }
490
491 static int lua_ban(lua_State *ps) {
492 channel *cp;
493 const char *mask;
494 modechanges changes;
495 int dir = MCB_ADD;
496
497 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
498 LUA_RETURN(ps, LUA_FAIL);
499
500 if(lua_isboolean(ps, 3) && lua_toboolean(ps, 3))
501 dir = MCB_DEL;
502
503 cp = findchannel((char *)lua_tostring(ps, 1));
504 if(!cp)
505 LUA_RETURN(ps, LUA_FAIL);
506
507 mask = lua_tostring(ps, 2);
508 if(!mask || !mask[0] || !lua_lineok(mask))
509 LUA_RETURN(ps, LUA_FAIL);
510
511 localsetmodeinit(&changes, cp, lua_nick);
512 localdosetmode_ban(&changes, mask, dir);
513 localsetmodeflush(&changes, 1);
514
515 LUA_RETURN(ps, LUA_OK);
516 }
517
518 void lua_registercommands(lua_State *l) {
519 lua_register(l, "irc_smsg", lua_smsg);
520 lua_register(l, "irc_skill", lua_skill);
521
522 lua_register(l, "chanmsg", lua_chanmsg);
523 lua_register(l, "scripterror", lua_scripterror);
524 lua_register(l, "versioninfo", lua_versioninfo);
525
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);
541
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);
546
547 /* lua_register(l, "irc_iteratenickhash", lua_iteratenickhash); */
548 }
549
550 /* --- */
551
552 static int lua_smsg(lua_State *ps) {
553 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
554 LUA_RETURN(ps, LUA_FAIL);
555
556 LUA_RETURN(ps, lua_cmsg((char *)lua_tostring(ps, 2), "%s", lua_tostring(ps, 1)));
557 }
558
559 static int lua_skill(lua_State *ps) {
560 const char *n, *msg;
561 nick *np;
562
563 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
564 LUA_RETURN(ps, LUA_FAIL);
565
566 n = lua_tostring(ps, 1);
567 msg = lua_tostring(ps, 2);
568
569 np = getnickbynick(n);
570 if(!np)
571 LUA_RETURN(ps, LUA_FAIL);
572
573 if(IsOper(np) || IsService(np) || IsXOper(np))
574 LUA_RETURN(ps, LUA_FAIL);
575
576 if(!lua_lineok(msg))
577 LUA_RETURN(ps, LUA_FAIL);
578
579 killuser(lua_nick, np, "%s", msg);
580
581 LUA_RETURN(ps, LUA_OK);
582 }
583