]> jfr.im git - irc/quakenet/newserv.git/blob - lua/luacommands.c
Force inlining
[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 #ifdef LUA_JITLIBNAME
18 #include <luajit.h>
19 #endif
20
21 static int lua_smsg(lua_State *ps);
22 static int lua_skill(lua_State *ps);
23
24 int lua_lineok(const char *data) {
25 if(strchr(data, '\r') || strchr(data, '\n'))
26 return 0;
27 return 1;
28 }
29
30 int lua_cmsg(char *channell, char *message, ...) {
31 char buf[512];
32 va_list va;
33 channel *cp;
34
35 va_start(va, message);
36 vsnprintf(buf, sizeof(buf), message, va);
37 va_end(va);
38
39 cp = findchannel(channell);
40 if(!cp)
41 return LUA_FAIL;
42
43 if(!lua_lineok(buf))
44 return LUA_FAIL;
45
46 lua_channelmessage(cp, "%s", buf);
47
48 return LUA_OK;
49 }
50
51 static int lua_chanmsg(lua_State *ps) {
52 if(!lua_isstring(ps, 1))
53 LUA_RETURN(ps, LUA_FAIL);
54
55 LUA_RETURN(ps, lua_cmsg(LUA_PUKECHAN, "lua: %s", lua_tostring(ps, 1)));
56 }
57
58 static int lua_scripterror(lua_State *ps) {
59 if(!lua_isstring(ps, 1))
60 LUA_RETURN(ps, LUA_FAIL);
61
62 LUA_RETURN(ps, lua_cmsg(LUA_PUKECHAN, "lua-error: %s", lua_tostring(ps, 1)));
63 }
64
65 static int lua_ctcp(lua_State *ps) {
66 const char *n, *msg;
67 nick *np;
68
69 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
70 LUA_RETURN(ps, LUA_FAIL);
71
72 n = lua_tostring(ps, 1);
73 msg = lua_tostring(ps, 2);
74
75 np = getnickbynick(n);
76 if(!np || !lua_lineok(msg))
77 LUA_RETURN(ps, LUA_FAIL);
78
79 lua_message(np, "\001%s\001", msg);
80
81 LUA_RETURN(ps, lua_cmsg(LUA_PUKECHAN, "lua-ctcp: %s (%s)", np->nick, msg));
82 }
83
84 static int lua_noticecmd(lua_State *ps) {
85 const char *n, *msg;
86 nick *np;
87
88 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
89 LUA_RETURN(ps, LUA_FAIL);
90
91 n = lua_tostring(ps, 1);
92 msg = lua_tostring(ps, 2);
93
94 np = getnickbynick(n);
95 if(!np || !lua_lineok(msg))
96 LUA_RETURN(ps, LUA_FAIL);
97
98 lua_notice(np, "%s", msg);
99
100 LUA_RETURN(ps, LUA_OK);
101 }
102
103 static int lua_kill(lua_State *ps) {
104 const char *n, *msg;
105 nick *np;
106
107 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
108 LUA_RETURN(ps, LUA_FAIL);
109
110 n = lua_tostring(ps, 1);
111 msg = lua_tostring(ps, 2);
112
113 np = getnickbynick(n);
114 if(!np)
115 LUA_RETURN(ps, LUA_FAIL);
116
117 if(IsOper(np) || IsService(np) || IsXOper(np))
118 LUA_RETURN(ps, LUA_FAIL);
119
120 if(!lua_lineok(msg))
121 LUA_RETURN(ps, LUA_FAIL);
122
123 killuser(lua_nick, np, "%s", msg);
124
125 LUA_RETURN(ps, lua_cmsg(LUA_PUKECHAN, "lua-KILL: %s (%s)", np->nick, msg));
126 }
127
128 static int lua_kick(lua_State *ps) {
129 const char *n, *msg, *chan;
130 nick *np;
131 channel *cp;
132 int dochecks = 1;
133
134 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
135 LUA_RETURN(ps, LUA_FAIL);
136
137 chan = lua_tostring(ps, 1);
138 n = lua_tostring(ps, 2);
139 msg = lua_tostring(ps, 3);
140
141 if(lua_isboolean(ps, 4) && !lua_toboolean(ps, 4))
142 dochecks = 0;
143
144 np = getnickbynick(n);
145 if(!np)
146 LUA_RETURN(ps, LUA_FAIL);
147
148 if(dochecks && (IsOper(np) || IsXOper(np) || IsService(np)))
149 LUA_RETURN(ps, LUA_FAIL);
150
151 cp = findchannel((char *)chan);
152 if(!cp)
153 LUA_RETURN(ps, LUA_FAIL);
154
155 if(!lua_lineok(msg))
156 LUA_RETURN(ps, LUA_FAIL);
157
158 localkickuser(lua_nick, cp, np, msg);
159
160 LUA_RETURN(ps, LUA_OK);
161 }
162
163 static int lua_invite(lua_State *ps) {
164 nick *np;
165 channel *cp;
166
167 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
168 LUA_RETURN(ps, LUA_FAIL);
169
170 np = getnickbynick((char *)lua_tostring(ps, 1));
171 if(!np)
172 LUA_RETURN(ps, LUA_FAIL);
173
174 cp = findchannel((char *)lua_tostring(ps, 2));
175 if(!cp)
176 LUA_RETURN(ps, LUA_FAIL);
177
178 localinvite(lua_nick, cp, np);
179
180 LUA_RETURN(ps, LUA_OK);
181 }
182
183 static int lua_gline(lua_State *ps) {
184 const char *reason;
185 nick *target;
186 char mask[512];
187 int duration, usercount = 0;
188 host *hp;
189
190 if(!lua_isstring(ps, 1) || !lua_isint(ps, 2) || !lua_isstring(ps, 3))
191 LUA_RETURN(ps, LUA_FAIL);
192
193 duration = lua_toint(ps, 2);
194 if((duration < 1) || (duration > 86400))
195 LUA_RETURN(ps, LUA_FAIL);
196
197 reason = lua_tostring(ps, 3);
198 if(!lua_lineok(reason) || !reason)
199 LUA_RETURN(ps, LUA_FAIL);
200
201 target = getnickbynick(lua_tostring(ps, 1));
202 if(!target || (IsOper(target) || IsXOper(target) || IsService(target)))
203 LUA_RETURN(ps, LUA_FAIL);
204
205 hp = target->host;
206 if(!hp)
207 LUA_RETURN(ps, LUA_FAIL);
208
209 usercount = hp->clonecount;
210 if(usercount > 10) { /* (decent) trusted host */
211 int j;
212 nick *np;
213
214 usercount = 0;
215
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)))
219 usercount++;
220
221 if(usercount > 50)
222 LUA_RETURN(ps, LUA_FAIL);
223
224 snprintf(mask, sizeof(mask), "*%s@%s", target->ident, IPtostr(target->ipaddress));
225 } else {
226 snprintf(mask, sizeof(mask), "*@%s", IPtostr(target->ipaddress));
227 }
228
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));
231 }
232
233 static int lua_getchaninfo(lua_State *ps) {
234 channel *cp;
235
236 if(!lua_isstring(ps, 1))
237 return 0;
238
239 cp = findchannel((char *)lua_tostring(ps, 1));
240 if(!cp)
241 return 0;
242
243 LUA_PUSHCHAN(ps, cp);
244
245 return 1;
246 }
247
248 static int lua_opchan(lua_State *ps) {
249 channel *cp;
250 nick *np;
251
252 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
253 LUA_RETURN(ps, LUA_FAIL);
254
255 cp = findchannel((char *)lua_tostring(ps, 1));
256 if(!cp)
257 LUA_RETURN(ps, LUA_FAIL);
258
259 np = getnickbynick((char *)lua_tostring(ps, 2));
260 if(!np)
261 LUA_RETURN(ps, LUA_FAIL);
262
263 localsetmodes(lua_nick, cp, np, MC_OP);
264 LUA_RETURN(ps, LUA_OK);
265 }
266
267 static int lua_deopchan(lua_State *ps) {
268 channel *cp;
269 nick *np;
270
271 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
272 LUA_RETURN(ps, LUA_FAIL);
273
274 cp = findchannel((char *)lua_tostring(ps, 1));
275 if(!cp)
276 LUA_RETURN(ps, LUA_FAIL);
277
278 np = getnickbynick((char *)lua_tostring(ps, 2));
279 if(!np)
280 LUA_RETURN(ps, LUA_FAIL);
281
282 localsetmodes(lua_nick, cp, np, MC_DEOP);
283 LUA_RETURN(ps, LUA_OK);
284 }
285
286 static int lua_voicechan(lua_State *ps) {
287 channel *cp;
288 nick *np;
289
290 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
291 LUA_RETURN(ps, LUA_FAIL);
292
293 cp = findchannel((char *)lua_tostring(ps, 1));
294 if(!cp)
295 LUA_RETURN(ps, LUA_FAIL);
296
297 np = getnickbynick((char *)lua_tostring(ps, 2));
298 if(!np)
299 LUA_RETURN(ps, LUA_FAIL);
300
301 localsetmodes(lua_nick, cp, np, MC_VOICE);
302 LUA_RETURN(ps, LUA_OK);
303 }
304
305 static int lua_counthost(lua_State *ps) {
306 long numeric;
307 nick *np;
308
309 if(!lua_islong(ps, 1))
310 return 0;
311
312 numeric = lua_tolong(ps, 1);
313
314 np = getnickbynumeric(numeric);
315 if(!np)
316 return 0;
317
318 lua_pushint(ps, np->host->clonecount);
319 return 1;
320 }
321
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__);
327
328 #ifdef LUAJIT_VERSION
329 lua_pushstring(ps, " + " LUAJIT_VERSION);
330 #else
331 lua_pushstring(ps, "");
332 #endif
333
334 return 5;
335 }
336
337 static int lua_basepath(lua_State *ps) {
338 lua_pushfstring(ps, "%s/", cpath->content);
339
340 return 1;
341 }
342
343 /* O(n) */
344 static int lua_getuserbyauth(lua_State *l) {
345 const char *acc;
346 nick *np;
347 int i, found = 0;
348
349 if(!lua_isstring(l, 1))
350 return 0;
351
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)) {
355 LUA_PUSHNICK(l, np);
356 found++;
357 }
358 }
359 }
360
361 return found;
362 }
363
364 static int lua_getnickchans(lua_State *l) {
365 nick *np;
366 int i;
367 channel **channels;
368
369 if(!lua_islong(l, 1))
370 return 0;
371
372 np = getnickbynumeric(lua_tolong(l, 1));
373 if(!np)
374 return 0;
375
376 channels = (channel **)np->channels->content;
377 for(i=0;i<np->channels->cursi;i++)
378 lua_pushstring(l, channels[i]->index->name->content);
379
380 return np->channels->cursi;
381 }
382
383 static int lua_getnickchanindex(lua_State *l) {
384 nick *np;
385 int offset;
386
387 if(!lua_islong(l, 1) || !lua_isint(l, 2))
388 return 0;
389
390 np = getnickbynumeric(lua_tolong(l, 1));
391 if(!np)
392 return 0;
393
394 offset = lua_toint(l, 2);
395 if((offset < 0) || (offset >= np->channels->cursi))
396 return 0;
397
398 lua_pushstring(l, ((channel **)np->channels->content)[offset]->index->name->content);
399
400 return 1;
401 }
402
403 int hashindex;
404 nick *lasthashnick;
405
406 static int lua_getnextnick(lua_State *l) {
407 if(!lasthashnick && (hashindex != -1))
408 return 0;
409
410 do {
411 if(!lasthashnick) {
412 hashindex++;
413 if(hashindex >= NICKHASHSIZE)
414 return 0;
415 lasthashnick = nicktable[hashindex];
416 } else {
417 lasthashnick = lasthashnick->next;
418 }
419 } while(!lasthashnick);
420
421 LUA_PUSHNICK(l, lasthashnick);
422 return 1;
423 }
424
425 static int lua_getfirstnick(lua_State *l) {
426 hashindex = -1;
427 lasthashnick = NULL;
428
429 return lua_getnextnick(l);
430 }
431
432 static int lua_getnickchancount(lua_State *l) {
433 nick *np;
434
435 if(!lua_islong(l, 1))
436 return 0;
437
438 np = getnickbynumeric(lua_tolong(l, 1));
439 if(!np)
440 return 0;
441
442 lua_pushint(l, np->channels->cursi);
443
444 return 1;
445 }
446
447 static int lua_gethostusers(lua_State *l) {
448 nick *np;
449 int count;
450
451 if(!lua_islong(l, 1))
452 return 0;
453
454 np = getnickbynumeric(lua_tolong(l, 1));
455 if(!np || !np->host || !np->host->nicks)
456 return 0;
457
458 np = np->host->nicks;
459 count = np->host->clonecount;
460
461 do {
462 LUA_PUSHNICK(l, np);
463 np = np->nextbyhost;
464 } while(np);
465
466 return count;
467 }
468
469 static int lua_getnickcountry(lua_State *l) {
470 nick *np;
471 int ext;
472
473 ext = findnickext("geoip");
474 if(ext == -1)
475 return 0;
476
477 if(!lua_islong(l, 1))
478 return 0;
479
480 np = getnickbynumeric(lua_tolong(l, 1));
481 if(!np)
482 return 0;
483
484 lua_pushint(l, (int)np->exts[ext]);
485 return 1;
486 }
487
488 /*
489 static int lua_iteratenickhash(lua_State *l) {
490 nick *np;
491 int i, top;
492 void *fp;
493
494 if(!lua_isfunction(l, 1))
495 LUA_RETURN(LUA_FAIL);
496
497 fp = lua_touserdata(l, 1);
498 if(!fp)
499 LUA_RETURN(LUA_FAIL);
500
501 for(i=0;i<NICKHASHSIZE;i++) {
502 for(np=nicktable[i];np;np=np->next) {
503 if(np) {
504 top = lua_gettop(l);
505
506 lua_getglobal(l, "scripterror");
507 lua_insert
508
509 LUA_PUSHNICK(l, np);
510 lua_pcall(l, 1, 0, top + 1);
511
512 lua_settop(l, top);
513 }
514 }
515 }
516
517 LUA_RETURN(LUA_OK);
518 }
519 */
520
521 static int lua_chanfix(lua_State *ps) {
522 channel *cp;
523 nick *np;
524
525 if(!lua_isstring(ps, 1))
526 LUA_RETURN(ps, LUA_FAIL);
527
528 cp = findchannel((char *)lua_tostring(ps, 1));
529 if(!cp)
530 LUA_RETURN(ps, LUA_FAIL);
531
532 np = getnickbynick(LUA_CHANFIXBOT);
533 if(!np)
534 LUA_RETURN(ps, LUA_FAIL);
535
536 lua_message(np, "chanfix %s", cp->index->name->content);
537
538 LUA_RETURN(ps, LUA_OK);
539 }
540
541 static int lua_clearmode(lua_State *ps) {
542 channel *cp;
543 int i;
544 nick *np;
545 unsigned long *lp;
546 modechanges changes;
547
548 if(!lua_isstring(ps, 1))
549 LUA_RETURN(ps, LUA_FAIL);
550
551 cp = findchannel((char *)lua_tostring(ps, 1));
552 if(!cp)
553 LUA_RETURN(ps, LUA_FAIL);
554
555 localsetmodeinit(&changes, cp, lua_nick);
556
557 localdosetmode_key(&changes, NULL, MCB_DEL);
558 localdosetmode_simple(&changes, 0, CHANMODE_INVITEONLY | CHANMODE_LIMIT);
559
560 while(cp->bans)
561 localdosetmode_ban(&changes, bantostring(cp->bans), MCB_DEL);
562
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);
568 }
569
570 localsetmodeflush(&changes, 1);
571
572 LUA_RETURN(ps, LUA_OK);
573 }
574
575 static int lua_ban(lua_State *ps) {
576 channel *cp;
577 const char *mask;
578 modechanges changes;
579 int dir = MCB_ADD;
580
581 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
582 LUA_RETURN(ps, LUA_FAIL);
583
584 if(lua_isboolean(ps, 3) && lua_toboolean(ps, 3))
585 dir = MCB_DEL;
586
587 cp = findchannel((char *)lua_tostring(ps, 1));
588 if(!cp)
589 LUA_RETURN(ps, LUA_FAIL);
590
591 mask = lua_tostring(ps, 2);
592 if(!mask || !mask[0] || !lua_lineok(mask))
593 LUA_RETURN(ps, LUA_FAIL);
594
595 localsetmodeinit(&changes, cp, lua_nick);
596 localdosetmode_ban(&changes, mask, dir);
597 localsetmodeflush(&changes, 1);
598
599 LUA_RETURN(ps, LUA_OK);
600 }
601
602 void lua_registercommands(lua_State *l) {
603 lua_register(l, "irc_smsg", lua_smsg);
604 lua_register(l, "irc_skill", lua_skill);
605
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);
610
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);
627
628 lua_register(l, "irc_getnickchans", lua_getnickchans);
629 lua_register(l, "irc_getnickchanindex", lua_getnickchanindex);
630 lua_register(l, "irc_getnickchancount", lua_getnickchancount);
631
632 lua_register(l, "irc_getfirstnick", lua_getfirstnick);
633 lua_register(l, "irc_getnextnick", lua_getnextnick);
634
635 lua_register(l, "irc_gethostusers", lua_gethostusers);
636 lua_register(l, "irc_getnickcountry", lua_getnickcountry);
637
638 /* lua_register(l, "irc_iteratenickhash", lua_iteratenickhash); */
639 }
640
641 /* --- */
642
643 static int lua_smsg(lua_State *ps) {
644 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
645 LUA_RETURN(ps, LUA_FAIL);
646
647 LUA_RETURN(ps, lua_cmsg((char *)lua_tostring(ps, 2), "%s", lua_tostring(ps, 1)));
648 }
649
650 static int lua_skill(lua_State *ps) {
651 const char *n, *msg;
652 nick *np;
653
654 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
655 LUA_RETURN(ps, LUA_FAIL);
656
657 n = lua_tostring(ps, 1);
658 msg = lua_tostring(ps, 2);
659
660 np = getnickbynick(n);
661 if(!np)
662 LUA_RETURN(ps, LUA_FAIL);
663
664 if(IsOper(np) || IsService(np) || IsXOper(np))
665 LUA_RETURN(ps, LUA_FAIL);
666
667 if(!lua_lineok(msg))
668 LUA_RETURN(ps, LUA_FAIL);
669
670 killuser(lua_nick, np, "%s", msg);
671
672 LUA_RETURN(ps, LUA_OK);
673 }
674