]> jfr.im git - irc/quakenet/newserv.git/blob - lua/luacommands.c
Add debug socket support.
[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_USEJIT
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 LUA_USEJIT
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 acc = lua_tostring(l, 1);
353
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)) {
357 LUA_PUSHNICK(l, np);
358 found++;
359 }
360 }
361 }
362
363 return found;
364 }
365
366 static int lua_getnickchans(lua_State *l) {
367 nick *np;
368 int i;
369 channel **channels;
370
371 if(!lua_islong(l, 1))
372 return 0;
373
374 np = getnickbynumeric(lua_tolong(l, 1));
375 if(!np)
376 return 0;
377
378 channels = (channel **)np->channels->content;
379 for(i=0;i<np->channels->cursi;i++)
380 lua_pushstring(l, channels[i]->index->name->content);
381
382 return np->channels->cursi;
383 }
384
385 static int lua_getnickchanindex(lua_State *l) {
386 nick *np;
387 int offset;
388
389 if(!lua_islong(l, 1) || !lua_isint(l, 2))
390 return 0;
391
392 np = getnickbynumeric(lua_tolong(l, 1));
393 if(!np)
394 return 0;
395
396 offset = lua_toint(l, 2);
397 if((offset < 0) || (offset >= np->channels->cursi))
398 return 0;
399
400 lua_pushstring(l, ((channel **)np->channels->content)[offset]->index->name->content);
401
402 return 1;
403 }
404
405 int hashindex;
406 nick *lasthashnick;
407
408 static int lua_getnextnick(lua_State *l) {
409 if(!lasthashnick && (hashindex != -1))
410 return 0;
411
412 do {
413 if(!lasthashnick) {
414 hashindex++;
415 if(hashindex >= NICKHASHSIZE)
416 return 0;
417 lasthashnick = nicktable[hashindex];
418 } else {
419 lasthashnick = lasthashnick->next;
420 }
421 } while(!lasthashnick);
422
423 LUA_PUSHNICK(l, lasthashnick);
424 return 1;
425 }
426
427 static int lua_getfirstnick(lua_State *l) {
428 hashindex = -1;
429 lasthashnick = NULL;
430
431 return lua_getnextnick(l);
432 }
433
434 static int lua_getnickchancount(lua_State *l) {
435 nick *np;
436
437 if(!lua_islong(l, 1))
438 return 0;
439
440 np = getnickbynumeric(lua_tolong(l, 1));
441 if(!np)
442 return 0;
443
444 lua_pushint(l, np->channels->cursi);
445
446 return 1;
447 }
448
449 static int lua_gethostusers(lua_State *l) {
450 nick *np;
451 int count;
452
453 if(!lua_islong(l, 1))
454 return 0;
455
456 np = getnickbynumeric(lua_tolong(l, 1));
457 if(!np || !np->host || !np->host->nicks)
458 return 0;
459
460 np = np->host->nicks;
461 count = np->host->clonecount;
462
463 do {
464 LUA_PUSHNICK(l, np);
465 np = np->nextbyhost;
466 } while(np);
467
468 return count;
469 }
470
471 static int lua_getnickcountry(lua_State *l) {
472 nick *np;
473 int ext;
474
475 ext = findnickext("geoip");
476 if(ext == -1)
477 return 0;
478
479 if(!lua_islong(l, 1))
480 return 0;
481
482 np = getnickbynumeric(lua_tolong(l, 1));
483 if(!np)
484 return 0;
485
486 lua_pushint(l, (int)np->exts[ext]);
487 return 1;
488 }
489
490 static int lua_chanfix(lua_State *ps) {
491 channel *cp;
492 nick *np;
493
494 if(!lua_isstring(ps, 1))
495 LUA_RETURN(ps, LUA_FAIL);
496
497 cp = findchannel((char *)lua_tostring(ps, 1));
498 if(!cp)
499 LUA_RETURN(ps, LUA_FAIL);
500
501 np = getnickbynick(LUA_CHANFIXBOT);
502 if(!np)
503 LUA_RETURN(ps, LUA_FAIL);
504
505 lua_message(np, "chanfix %s", cp->index->name->content);
506
507 LUA_RETURN(ps, LUA_OK);
508 }
509
510 static int lua_clearmode(lua_State *ps) {
511 channel *cp;
512 int i;
513 nick *np;
514 unsigned long *lp;
515 modechanges changes;
516
517 if(!lua_isstring(ps, 1))
518 LUA_RETURN(ps, LUA_FAIL);
519
520 cp = findchannel((char *)lua_tostring(ps, 1));
521 if(!cp)
522 LUA_RETURN(ps, LUA_FAIL);
523
524 localsetmodeinit(&changes, cp, lua_nick);
525
526 localdosetmode_key(&changes, NULL, MCB_DEL);
527 localdosetmode_simple(&changes, 0, CHANMODE_INVITEONLY | CHANMODE_LIMIT);
528
529 while(cp->bans)
530 localdosetmode_ban(&changes, bantostring(cp->bans), MCB_DEL);
531
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);
537 }
538
539 localsetmodeflush(&changes, 1);
540
541 LUA_RETURN(ps, LUA_OK);
542 }
543
544 static int lua_ban(lua_State *ps) {
545 channel *cp;
546 const char *mask;
547 modechanges changes;
548 int dir = MCB_ADD;
549
550 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
551 LUA_RETURN(ps, LUA_FAIL);
552
553 if(lua_isboolean(ps, 3) && lua_toboolean(ps, 3))
554 dir = MCB_DEL;
555
556 cp = findchannel((char *)lua_tostring(ps, 1));
557 if(!cp)
558 LUA_RETURN(ps, LUA_FAIL);
559
560 mask = lua_tostring(ps, 2);
561 if(!mask || !mask[0] || !lua_lineok(mask))
562 LUA_RETURN(ps, LUA_FAIL);
563
564 localsetmodeinit(&changes, cp, lua_nick);
565 localdosetmode_ban(&changes, mask, dir);
566 localsetmodeflush(&changes, 1);
567
568 LUA_RETURN(ps, LUA_OK);
569 }
570
571 void lua_registercommands(lua_State *l) {
572 lua_register(l, "irc_smsg", lua_smsg);
573 lua_register(l, "irc_skill", lua_skill);
574
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);
579
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);
596
597 lua_register(l, "irc_getnickchans", lua_getnickchans);
598 lua_register(l, "irc_getnickchanindex", lua_getnickchanindex);
599 lua_register(l, "irc_getnickchancount", lua_getnickchancount);
600
601 lua_register(l, "irc_getfirstnick", lua_getfirstnick);
602 lua_register(l, "irc_getnextnick", lua_getnextnick);
603
604 lua_register(l, "irc_gethostusers", lua_gethostusers);
605 lua_register(l, "irc_getnickcountry", lua_getnickcountry);
606 }
607
608 /* --- */
609
610 static int lua_smsg(lua_State *ps) {
611 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
612 LUA_RETURN(ps, LUA_FAIL);
613
614 LUA_RETURN(ps, lua_cmsg((char *)lua_tostring(ps, 2), "%s", lua_tostring(ps, 1)));
615 }
616
617 static int lua_skill(lua_State *ps) {
618 const char *n, *msg;
619 nick *np;
620
621 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
622 LUA_RETURN(ps, LUA_FAIL);
623
624 n = lua_tostring(ps, 1);
625 msg = lua_tostring(ps, 2);
626
627 np = getnickbynick(n);
628 if(!np)
629 LUA_RETURN(ps, LUA_FAIL);
630
631 if(IsOper(np) || IsService(np) || IsXOper(np))
632 LUA_RETURN(ps, LUA_FAIL);
633
634 if(!lua_lineok(msg))
635 LUA_RETURN(ps, LUA_FAIL);
636
637 killuser(lua_nick, np, "%s", msg);
638
639 LUA_RETURN(ps, LUA_OK);
640 }
641