]> jfr.im git - irc/quakenet/newserv.git/blob - lua/luacommands.c
Mergey merge.
[irc/quakenet/newserv.git] / lua / luacommands.c
1 /* Copyright (C) Chris Porter 2005-2007 */
2 /* ALL RIGHTS RESERVED. */
3 /* Don't put this into the SVN repo. */
4
5 /*
6 @todo
7 - Write a nick printf type thing for pcalled functions.
8 - Make commands register as apposed to blinding calling.
9 - Use numerics instead of huge structures, and add lookup functions.
10 */
11
12 #include "../channel/channel.h"
13 #include "../control/control.h"
14 #include "../nick/nick.h"
15 #include "../localuser/localuser.h"
16 #include "../localuser/localuserchannel.h"
17 #include "../lib/irc_string.h"
18 #include "../lib/flags.h"
19
20 #include "lua.h"
21 #include "luabot.h"
22
23 #include <stdarg.h>
24 #include <stddef.h>
25
26 #define MAX_PUSHER 50
27
28 static int lua_smsg(lua_State *ps);
29 static int lua_skill(lua_State *ps);
30
31 typedef struct lua_pusher {
32 short argtype;
33 short offset;
34 const char *structname;
35 } lua_pusher;
36
37 struct lua_pusher nickpusher[MAX_PUSHER];
38 struct lua_pusher chanpusher[MAX_PUSHER];
39 int nickpushercount, chanpushercount;
40
41 INLINE void lua_setuppusher(struct lua_pusher *pusherlist, lua_State *l, int index, struct lua_pusher **lp, int max, int pcount);
42 INLINE int lua_usepusher(lua_State *l, struct lua_pusher **lp, void *np);
43
44 void lua_initnickpusher(void);
45 void lua_initchanpusher(void);
46
47 #define lua_setupnickpusher(L2, I2, P2, M2) lua_setuppusher(&nickpusher[0], L2, I2, P2, M2, nickpushercount)
48 #define lua_setupchanpusher(L2, I2, P2, M2) lua_setuppusher(&chanpusher[0], L2, I2, P2, M2, chanpushercount)
49
50 int lua_cmsg(char *channell, char *message, ...) {
51 char buf[512];
52 va_list va;
53 channel *cp;
54
55 va_start(va, message);
56 vsnprintf(buf, sizeof(buf), message, va);
57 va_end(va);
58
59 cp = findchannel(channell);
60 if(!cp)
61 return LUA_FAIL;
62
63 if(!lua_lineok(buf))
64 return LUA_FAIL;
65
66 lua_channelmessage(cp, "%s", buf);
67
68 return LUA_OK;
69 }
70
71 static int lua_chanmsg(lua_State *ps) {
72 if(!lua_isstring(ps, 1))
73 LUA_RETURN(ps, LUA_FAIL);
74
75 LUA_RETURN(ps, lua_cmsg(LUA_PUKECHAN, "lua: %s", lua_tostring(ps, 1)));
76 }
77
78 static int lua_ctcp(lua_State *ps) {
79 const char *n, *msg;
80 nick *np;
81
82 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
83 LUA_RETURN(ps, LUA_FAIL);
84
85 n = lua_tostring(ps, 1);
86 msg = lua_tostring(ps, 2);
87
88 np = getnickbynick(n);
89 if(!np || !lua_lineok(msg))
90 LUA_RETURN(ps, LUA_FAIL);
91
92 lua_message(np, "\001%s\001", msg);
93
94 LUA_RETURN(ps, lua_cmsg(LUA_PUKECHAN, "lua-ctcp: %s (%s)", np->nick, msg));
95 }
96
97 static int lua_noticecmd(lua_State *ps) {
98 const char *n, *msg;
99 nick *np;
100
101 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
102 LUA_RETURN(ps, LUA_FAIL);
103
104 n = lua_tostring(ps, 1);
105 msg = lua_tostring(ps, 2);
106
107 np = getnickbynick(n);
108 if(!np || !lua_lineok(msg))
109 LUA_RETURN(ps, LUA_FAIL);
110
111 lua_notice(np, "%s", msg);
112
113 LUA_RETURN(ps, LUA_OK);
114 }
115
116 static int lua_privmsgcmd(lua_State *ps) {
117 const char *n, *msg;
118 nick *np;
119
120 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
121 LUA_RETURN(ps, LUA_FAIL);
122
123 n = lua_tostring(ps, 1);
124 msg = lua_tostring(ps, 2);
125
126 np = getnickbynick(n);
127 if(!np || !lua_lineok(msg))
128 LUA_RETURN(ps, LUA_FAIL);
129
130 lua_message(np, "%s", msg);
131
132 LUA_RETURN(ps, LUA_OK);
133 }
134
135 static int lua_kill(lua_State *ps) {
136 const char *n, *msg;
137 nick *np;
138
139 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
140 LUA_RETURN(ps, LUA_FAIL);
141
142 n = lua_tostring(ps, 1);
143 msg = lua_tostring(ps, 2);
144
145 np = getnickbynick(n);
146 if(!np)
147 LUA_RETURN(ps, LUA_FAIL);
148
149 if(IsOper(np) || IsService(np) || IsXOper(np))
150 LUA_RETURN(ps, LUA_FAIL);
151
152 if(!lua_lineok(msg))
153 LUA_RETURN(ps, LUA_FAIL);
154
155 killuser(lua_nick, np, "%s", msg);
156
157 LUA_RETURN(ps, lua_cmsg(LUA_PUKECHAN, "lua-KILL: %s (%s)", np->nick, msg));
158 }
159
160 static int lua_kick(lua_State *ps) {
161 const char *n, *msg, *chan;
162 nick *np;
163 channel *cp;
164 int dochecks = 1;
165
166 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
167 LUA_RETURN(ps, LUA_FAIL);
168
169 chan = lua_tostring(ps, 1);
170 n = lua_tostring(ps, 2);
171 msg = lua_tostring(ps, 3);
172
173 if(lua_isboolean(ps, 4) && !lua_toboolean(ps, 4))
174 dochecks = 0;
175
176 np = getnickbynick(n);
177 if(!np)
178 LUA_RETURN(ps, LUA_FAIL);
179
180 if(dochecks && (IsOper(np) || IsXOper(np) || IsService(np)))
181 LUA_RETURN(ps, LUA_FAIL);
182
183 cp = findchannel((char *)chan);
184 if(!cp)
185 LUA_RETURN(ps, LUA_FAIL);
186
187 if(!lua_lineok(msg))
188 LUA_RETURN(ps, LUA_FAIL);
189
190 localkickuser(lua_nick, cp, np, msg);
191
192 LUA_RETURN(ps, LUA_OK);
193 }
194
195 static int lua_invite(lua_State *ps) {
196 nick *np;
197 channel *cp;
198
199 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
200 LUA_RETURN(ps, LUA_FAIL);
201
202 np = getnickbynick((char *)lua_tostring(ps, 1));
203 if(!np)
204 LUA_RETURN(ps, LUA_FAIL);
205
206 cp = findchannel((char *)lua_tostring(ps, 2));
207 if(!cp)
208 LUA_RETURN(ps, LUA_FAIL);
209
210 localinvite(lua_nick, cp, np);
211
212 LUA_RETURN(ps, LUA_OK);
213 }
214
215 static int lua_gline(lua_State *ps) {
216 const char *reason;
217 nick *target;
218 char mask[512];
219 int duration, usercount = 0;
220 host *hp;
221
222 if(!lua_isstring(ps, 1) || !lua_isint(ps, 2) || !lua_isstring(ps, 3))
223 LUA_RETURN(ps, LUA_FAIL);
224
225 duration = lua_toint(ps, 2);
226 if((duration < 1) || (duration > 31 * 86400))
227 LUA_RETURN(ps, LUA_FAIL);
228
229 reason = lua_tostring(ps, 3);
230 if(!lua_lineok(reason) || !reason)
231 LUA_RETURN(ps, LUA_FAIL);
232
233 target = getnickbynick(lua_tostring(ps, 1));
234 if(!target || (IsOper(target) || IsXOper(target) || IsService(target)))
235 LUA_RETURN(ps, LUA_FAIL);
236
237 hp = target->host;
238 if(!hp)
239 LUA_RETURN(ps, LUA_FAIL);
240
241 usercount = hp->clonecount;
242 if(usercount > 10) { /* (decent) trusted host */
243 int j;
244 nick *np;
245
246 usercount = 0;
247
248 for (j=0;j<NICKHASHSIZE;j++)
249 for (np=nicktable[j];np;np=np->next)
250 if (np && (np->host == hp) && (!ircd_strcmp(np->ident, target->ident)))
251 usercount++;
252
253 if(usercount > 50)
254 LUA_RETURN(ps, LUA_FAIL);
255
256 snprintf(mask, sizeof(mask), "*%s@%s", target->ident, IPtostr(target->p_ipaddr));
257 } else {
258 snprintf(mask, sizeof(mask), "*@%s", IPtostr(target->p_ipaddr));
259 }
260
261 irc_send("%s GL * +%s %d :%s", mynumeric->content, mask, duration, reason);
262 LUA_RETURN(ps, lua_cmsg(LUA_PUKECHAN, "lua-GLINE: %s (%d users, %d seconds -- %s)", mask, usercount, duration, reason));
263 }
264
265 static int lua_getchaninfo(lua_State *ps) {
266 channel *cp;
267
268 if(!lua_isstring(ps, 1))
269 return 0;
270
271 cp = findchannel((char *)lua_tostring(ps, 1));
272 if(!cp)
273 return 0;
274
275 LUA_PUSHCHAN(ps, cp);
276
277 return 1;
278 }
279
280 static int lua_opchan(lua_State *ps) {
281 channel *cp;
282 nick *np;
283
284 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
285 LUA_RETURN(ps, LUA_FAIL);
286
287 cp = findchannel((char *)lua_tostring(ps, 1));
288 if(!cp)
289 LUA_RETURN(ps, LUA_FAIL);
290
291 np = getnickbynick((char *)lua_tostring(ps, 2));
292 if(!np)
293 LUA_RETURN(ps, LUA_FAIL);
294
295 localsetmodes(lua_nick, cp, np, MC_OP);
296 LUA_RETURN(ps, LUA_OK);
297 }
298
299 static int lua_deopchan(lua_State *ps) {
300 channel *cp;
301 nick *np;
302
303 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
304 LUA_RETURN(ps, LUA_FAIL);
305
306 cp = findchannel((char *)lua_tostring(ps, 1));
307 if(!cp)
308 LUA_RETURN(ps, LUA_FAIL);
309
310 np = getnickbynick((char *)lua_tostring(ps, 2));
311 if(!np)
312 LUA_RETURN(ps, LUA_FAIL);
313
314 localsetmodes(lua_nick, cp, np, MC_DEOP);
315 LUA_RETURN(ps, LUA_OK);
316 }
317
318 static int lua_voicechan(lua_State *ps) {
319 channel *cp;
320 nick *np;
321
322 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
323 LUA_RETURN(ps, LUA_FAIL);
324
325 cp = findchannel((char *)lua_tostring(ps, 1));
326 if(!cp)
327 LUA_RETURN(ps, LUA_FAIL);
328
329 np = getnickbynick((char *)lua_tostring(ps, 2));
330 if(!np)
331 LUA_RETURN(ps, LUA_FAIL);
332
333 localsetmodes(lua_nick, cp, np, MC_VOICE);
334 LUA_RETURN(ps, LUA_OK);
335 }
336
337 static int lua_counthost(lua_State *ps) {
338 long numeric;
339 nick *np;
340
341 if(!lua_islong(ps, 1))
342 return 0;
343
344 numeric = lua_tolong(ps, 1);
345
346 np = getnickbynumeric(numeric);
347 if(!np)
348 return 0;
349
350 lua_pushint(ps, np->host->clonecount);
351 return 1;
352 }
353
354 static int lua_versioninfo(lua_State *ps) {
355 lua_pushstring(ps, LUA_VERSION);
356 lua_pushstring(ps, LUA_BOTVERSION);
357 lua_pushstring(ps, __DATE__);
358 lua_pushstring(ps, __TIME__);
359
360 lua_pushstring(ps, LUA_AUXVERSION);
361
362 return 5;
363 }
364
365 static int lua_basepath(lua_State *ps) {
366 lua_pushfstring(ps, "%s/", cpath->content);
367
368 return 1;
369 }
370
371 /* O(n) */
372 static int lua_getuserbyauth(lua_State *l) {
373 const char *acc;
374 nick *np;
375 int i, found = 0;
376
377 if(!lua_isstring(l, 1))
378 return 0;
379
380 acc = lua_tostring(l, 1);
381
382 for(i=0;i<NICKHASHSIZE;i++) {
383 for(np=nicktable[i];np;np=np->next) {
384 if(np && np->authname && !ircd_strcmp(np->authname, acc)) {
385 LUA_PUSHNICK(l, np);
386 found++;
387 }
388 }
389 }
390
391 return found;
392 }
393
394 static int lua_getnickchans(lua_State *l) {
395 nick *np;
396 int i;
397 channel **channels;
398
399 if(!lua_islong(l, 1))
400 return 0;
401
402 np = getnickbynumeric(lua_tolong(l, 1));
403 if(!np)
404 return 0;
405
406 channels = (channel **)np->channels->content;
407 for(i=0;i<np->channels->cursi;i++)
408 lua_pushstring(l, channels[i]->index->name->content);
409
410 return np->channels->cursi;
411 }
412
413 static int lua_getnickchanindex(lua_State *l) {
414 nick *np;
415 int offset;
416
417 if(!lua_islong(l, 1) || !lua_isint(l, 2))
418 return 0;
419
420 np = getnickbynumeric(lua_tolong(l, 1));
421 if(!np)
422 return 0;
423
424 offset = lua_toint(l, 2);
425 if((offset < 0) || (offset >= np->channels->cursi))
426 return 0;
427
428 lua_pushstring(l, ((channel **)np->channels->content)[offset]->index->name->content);
429
430 return 1;
431 }
432
433 int hashindex;
434 nick *lasthashnick;
435
436 struct lua_pusher *nickhashpusher[MAX_PUSHER];
437
438 static int lua_getnextnick(lua_State *l) {
439 if(!lasthashnick && (hashindex != -1))
440 return 0;
441
442 do {
443 if(!lasthashnick) {
444 hashindex++;
445 if(hashindex >= NICKHASHSIZE)
446 return 0;
447 lasthashnick = nicktable[hashindex];
448 } else {
449 lasthashnick = lasthashnick->next;
450 }
451 } while(!lasthashnick);
452
453 return lua_usepusher(l, nickhashpusher, lasthashnick);
454 }
455
456 static int lua_getfirstnick(lua_State *l) {
457 hashindex = -1;
458 lasthashnick = NULL;
459
460 lua_setupnickpusher(l, 1, nickhashpusher, MAX_PUSHER);
461
462 return lua_getnextnick(l);
463 }
464
465 int chanhashindex;
466 chanindex *lasthashchan;
467
468 struct lua_pusher *chanhashpusher[MAX_PUSHER];
469
470 static int lua_getnextchan(lua_State *l) {
471 if(!lasthashchan && (chanhashindex != -1))
472 return 0;
473
474 do {
475 if(!lasthashchan) {
476 chanhashindex++;
477 if(chanhashindex >= CHANNELHASHSIZE)
478 return 0;
479 lasthashchan = chantable[chanhashindex];
480 } else {
481 lasthashchan = lasthashchan->next;
482 }
483 } while(!lasthashchan || !lasthashchan->channel);
484
485 return lua_usepusher(l, chanhashpusher, lasthashchan);
486 }
487
488 static int lua_getfirstchan(lua_State *l) {
489 chanhashindex = -1;
490 lasthashchan = NULL;
491
492 lua_setupchanpusher(l, 1, chanhashpusher, MAX_PUSHER);
493
494 return lua_getnextchan(l);
495 }
496
497 static int lua_getnickchancount(lua_State *l) {
498 nick *np;
499
500 if(!lua_islong(l, 1))
501 return 0;
502
503 np = getnickbynumeric(lua_tolong(l, 1));
504 if(!np)
505 return 0;
506
507 lua_pushint(l, np->channels->cursi);
508
509 return 1;
510 }
511
512 static int lua_gethostusers(lua_State *l) {
513 nick *np;
514 int count;
515
516 if(!lua_islong(l, 1))
517 return 0;
518
519 np = getnickbynumeric(lua_tolong(l, 1));
520 if(!np || !np->host || !np->host->nicks)
521 return 0;
522
523 np = np->host->nicks;
524 count = np->host->clonecount;
525
526 do {
527 LUA_PUSHNICK(l, np);
528 np = np->nextbyhost;
529 } while(np);
530
531 return count;
532 }
533
534 static int lua_getnickcountry(lua_State *l) {
535 nick *np;
536 int ext;
537
538 ext = findnickext("geoip");
539 if(ext == -1)
540 return 0;
541
542 if(!lua_islong(l, 1))
543 return 0;
544
545 np = getnickbynumeric(lua_tolong(l, 1));
546 if(!np)
547 return 0;
548
549 lua_pushint(l, (long)np->exts[ext]);
550 return 1;
551 }
552
553 static int lua_chanfix(lua_State *ps) {
554 channel *cp;
555 nick *np;
556
557 if(!lua_isstring(ps, 1))
558 LUA_RETURN(ps, LUA_FAIL);
559
560 cp = findchannel((char *)lua_tostring(ps, 1));
561 if(!cp || !cp->index)
562 LUA_RETURN(ps, LUA_FAIL);
563
564 np = getnickbynick(LUA_CHANFIXBOT);
565 if(!np)
566 LUA_RETURN(ps, LUA_FAIL);
567
568 lua_message(np, "chanfix %s", cp->index->name->content);
569
570 LUA_RETURN(ps, LUA_OK);
571 }
572
573 static int lua_clearmode(lua_State *ps) {
574 channel *cp;
575 int i;
576 nick *np;
577 unsigned long *lp;
578 modechanges changes;
579
580 if(!lua_isstring(ps, 1))
581 LUA_RETURN(ps, LUA_FAIL);
582
583 cp = findchannel((char *)lua_tostring(ps, 1));
584 if(!cp || !cp->users)
585 LUA_RETURN(ps, LUA_FAIL);
586
587 localsetmodeinit(&changes, cp, lua_nick);
588
589 localdosetmode_key(&changes, NULL, MCB_DEL);
590 localdosetmode_simple(&changes, 0, CHANMODE_INVITEONLY | CHANMODE_LIMIT);
591
592 while(cp->bans)
593 localdosetmode_ban(&changes, bantostring(cp->bans), MCB_DEL);
594
595 for(i=0,lp=cp->users->content;i<cp->users->hashsize;i++,lp++)
596 if((*lp != nouser) && (*lp & CUMODE_OP)) {
597 np = getnickbynumeric(*lp);
598 if(np && !IsService(np))
599 localdosetmode_nick(&changes, np, MC_DEOP);
600 }
601
602 localsetmodeflush(&changes, 1);
603
604 LUA_RETURN(ps, LUA_OK);
605 }
606
607 static int lua_ban(lua_State *ps) {
608 channel *cp;
609 const char *mask;
610 modechanges changes;
611 int dir = MCB_ADD;
612
613 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
614 LUA_RETURN(ps, LUA_FAIL);
615
616 if(lua_isboolean(ps, 3) && lua_toboolean(ps, 3))
617 dir = MCB_DEL;
618
619 cp = findchannel((char *)lua_tostring(ps, 1));
620 if(!cp)
621 LUA_RETURN(ps, LUA_FAIL);
622
623 mask = lua_tostring(ps, 2);
624 if(!mask || !mask[0] || !lua_lineok(mask))
625 LUA_RETURN(ps, LUA_FAIL);
626
627 localsetmodeinit(&changes, cp, lua_nick);
628 localdosetmode_ban(&changes, mask, dir);
629 localsetmodeflush(&changes, 1);
630
631 LUA_RETURN(ps, LUA_OK);
632 }
633
634 static int lua_topic(lua_State *ps) {
635 channel *cp;
636 char *topic;
637
638 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
639 LUA_RETURN(ps, LUA_FAIL);
640
641 cp = findchannel((char *)lua_tostring(ps, 1));
642 if(!cp)
643 LUA_RETURN(ps, LUA_FAIL);
644
645 topic = (char *)lua_tostring(ps, 2);
646 if(!topic || !lua_lineok(topic))
647 LUA_RETURN(ps, LUA_FAIL);
648
649 localsettopic(lua_nick, cp, topic);
650
651 LUA_RETURN(ps, LUA_OK);
652 }
653
654 static int lua_getuserchanmodes(lua_State *l) {
655 nick *np;
656 channel *cp;
657 unsigned long *lp;
658
659 if(!lua_islong(l, 1) || !lua_isstring(l, 2))
660 return 0;
661
662 np = getnickbynumeric(lua_tolong(l, 1));
663 if(!np)
664 return 0;
665
666 cp = findchannel((char *)lua_tostring(l, 2));
667 if(!cp || !cp->users)
668 return 0;
669
670 lp = getnumerichandlefromchanhash(cp->users, np->numeric);
671 if(!lp)
672 return 0;
673
674 LUA_PUSHNICKCHANMODES(l, lp);
675 return 1;
676 }
677
678 static int lua_getusermodes(lua_State *l) {
679 nick *np;
680
681 if(!lua_islong(l, 1))
682 return 0;
683
684 np = getnickbynumeric(lua_tolong(l, 1));
685 if(!np)
686 return 0;
687
688 lua_pushstring(l, printflags(np->umodes, umodeflags));
689 return 1;
690 }
691
692 static int lua_getnickbynick(lua_State *l) {
693 nick *np;
694
695 if(!lua_isstring(l, 1))
696 return 0;
697
698 np = getnickbynick(lua_tostring(l, 1));
699 if(!np)
700 return 0;
701
702 LUA_PUSHNICK(l, np);
703 return 1;
704 }
705
706 static int lua_getnickbynumeric(lua_State *l) {
707 nick *np;
708
709 if(!lua_islong(l, 1))
710 return 0;
711
712 np = getnickbynumeric(lua_tolong(l, 1));
713 if(!np)
714 return 0;
715
716 LUA_PUSHNICK(l, np);
717 return 1;
718 }
719
720 static int lua_fastgetnickbynumeric(lua_State *l) {
721 static struct lua_pusher *ourpusher[MAX_PUSHER];
722 nick *np;
723
724 if(!lua_islong(l, 1))
725 return 0;
726
727 np = getnickbynumeric(lua_tolong(l, 1));
728 if(!np)
729 return 0;
730
731 lua_setupnickpusher(l, 2, ourpusher, MAX_PUSHER);
732 return lua_usepusher(l, ourpusher, np);
733 }
734
735 int channelnicklistindex, channelnicklistcount = -1;
736 channel *channelnicklist;
737
738 struct lua_pusher *channelnickpusher[MAX_PUSHER];
739
740 static int lua_getnextchannick(lua_State *l) {
741 nick *np;
742
743 do {
744 channelnicklistindex++;
745
746 if(channelnicklistindex >= channelnicklistcount)
747 return 0;
748 } while((channelnicklist->users->content[channelnicklistindex] == nouser) || !(np = getnickbynumeric(channelnicklist->users->content[channelnicklistindex])));
749
750 return lua_usepusher(l, channelnickpusher, np);
751 }
752
753 static int lua_getfirstchannick(lua_State *l) {
754 if(!lua_isstring(l, 1))
755 return 0;
756
757 channelnicklist = findchannel((char *)lua_tostring(l, 1));
758 if(!channelnicklist|| !channelnicklist->users)
759 return 0;
760
761 channelnicklistindex = -1;
762 channelnicklistcount = channelnicklist->users->hashsize;
763
764 lua_setupnickpusher(l, 2, channelnickpusher, MAX_PUSHER);
765
766 return lua_getnextchannick(l);
767 }
768
769 static int lua_nickonchan(lua_State *l) {
770 int success = 0;
771 if(lua_islong(l, 1) && lua_isstring(l, 2)) {
772 channel *cp = findchannel((char *)lua_tostring(l, 2));
773 if(cp && cp->users) {
774 unsigned long *lp = getnumerichandlefromchanhash(cp->users, lua_tolong(l, 1));
775 if(lp)
776 success = 1;
777 }
778 }
779
780 lua_pushboolean(l, success);
781 return 1;
782 }
783
784 static int lua_simplechanmode(lua_State *ps) {
785 channel *cp;
786 char *modes;
787 flag_t add = 0, del = ~add;
788 flag_t permitted = CHANMODE_NOEXTMSG | CHANMODE_TOPICLIMIT | CHANMODE_SECRET | CHANMODE_PRIVATE | CHANMODE_INVITEONLY | CHANMODE_MODERATE | CHANMODE_NOCOLOUR | CHANMODE_NOCTCP | CHANMODE_REGONLY | CHANMODE_DELJOINS | CHANMODE_NOQUITMSG | CHANMODE_NONOTICE;
789 modechanges changes;
790
791 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
792 LUA_RETURN(ps, LUA_FAIL);
793
794 cp = findchannel((char *)lua_tostring(ps, 1));
795 if(!cp)
796 LUA_RETURN(ps, LUA_FAIL);
797
798 modes = (char *)lua_tostring(ps, 2);
799 if(!modes)
800 LUA_RETURN(ps, LUA_FAIL);
801
802 if(setflags(&add, permitted, modes, cmodeflags, REJECT_DISALLOWED|REJECT_UNKNOWN) != REJECT_NONE)
803 LUA_RETURN(ps, LUA_FAIL);
804
805 if(setflags(&del, permitted, modes, cmodeflags, REJECT_DISALLOWED|REJECT_UNKNOWN) != REJECT_NONE)
806 LUA_RETURN(ps, LUA_FAIL);
807
808 localsetmodeinit(&changes, cp, lua_nick);
809 localdosetmode_simple(&changes, add, ~del);
810 localsetmodeflush(&changes, 1);
811
812 LUA_RETURN(ps, LUA_OK);
813 }
814
815 void lua_registercommands(lua_State *l) {
816 lua_register(l, "irc_smsg", lua_smsg);
817 lua_register(l, "irc_skill", lua_skill);
818
819 lua_register(l, "chanmsg", lua_chanmsg);
820 lua_register(l, "versioninfo", lua_versioninfo);
821 lua_register(l, "basepath", lua_basepath);
822
823 lua_register(l, "irc_report", lua_chanmsg);
824 lua_register(l, "irc_ctcp", lua_ctcp);
825 lua_register(l, "irc_kill", lua_kill);
826 lua_register(l, "irc_kick", lua_kick);
827 lua_register(l, "irc_invite", lua_invite);
828 lua_register(l, "irc_gline", lua_gline);
829 lua_register(l, "irc_getchaninfo", lua_getchaninfo);
830 lua_register(l, "irc_counthost", lua_counthost);
831 lua_register(l, "irc_getuserbyauth", lua_getuserbyauth);
832 lua_register(l, "irc_notice", lua_noticecmd);
833 lua_register(l, "irc_privmsg", lua_privmsgcmd);
834 lua_register(l, "irc_opchan", lua_opchan);
835 lua_register(l, "irc_voicechan", lua_voicechan);
836 lua_register(l, "irc_chanfix", lua_chanfix);
837 lua_register(l, "irc_clearmode", lua_clearmode);
838 lua_register(l, "irc_ban", lua_ban);
839 lua_register(l, "irc_deopchan", lua_deopchan);
840 lua_register(l, "irc_topic", lua_topic);
841
842 lua_register(l, "irc_getnickbynick", lua_getnickbynick);
843 lua_register(l, "irc_getnickbynumeric", lua_getnickbynumeric);
844 lua_register(l, "irc_getfirstnick", lua_getfirstnick);
845 lua_register(l, "irc_getnextnick", lua_getnextnick);
846
847 lua_register(l, "irc_getnickchans", lua_getnickchans);
848 lua_register(l, "irc_getnickchanindex", lua_getnickchanindex);
849 lua_register(l, "irc_getnickchancount", lua_getnickchancount);
850
851 lua_register(l, "irc_getuserchanmodes", lua_getuserchanmodes);
852
853 lua_register(l, "irc_getfirstchannick", lua_getfirstchannick);
854 lua_register(l, "irc_getnextchannick", lua_getnextchannick);
855
856 lua_register(l, "irc_gethostusers", lua_gethostusers);
857 lua_register(l, "irc_getnickcountry", lua_getnickcountry);
858
859 lua_register(l, "irc_getfirstchan", lua_getfirstchan);
860 lua_register(l, "irc_getnextchan", lua_getnextchan);
861 lua_register(l, "irc_getusermodes", lua_getusermodes);
862 lua_register(l, "irc_nickonchan", lua_nickonchan);
863
864 lua_register(l, "irc_fastgetnickbynumeric", lua_fastgetnickbynumeric);
865
866 lua_register(l, "irc_simplechanmode", lua_simplechanmode);
867 }
868
869 /* --- */
870
871 static int lua_smsg(lua_State *ps) {
872 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
873 LUA_RETURN(ps, LUA_FAIL);
874
875 LUA_RETURN(ps, lua_cmsg((char *)lua_tostring(ps, 2), "%s", lua_tostring(ps, 1)));
876 }
877
878 static int lua_skill(lua_State *ps) {
879 const char *n, *msg;
880 nick *np;
881
882 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2))
883 LUA_RETURN(ps, LUA_FAIL);
884
885 n = lua_tostring(ps, 1);
886 msg = lua_tostring(ps, 2);
887
888 np = getnickbynick(n);
889 if(!np)
890 LUA_RETURN(ps, LUA_FAIL);
891
892 if(IsOper(np) || IsService(np) || IsXOper(np))
893 LUA_RETURN(ps, LUA_FAIL);
894
895 if(!lua_lineok(msg))
896 LUA_RETURN(ps, LUA_FAIL);
897
898 killuser(lua_nick, np, "%s", msg);
899
900 LUA_RETURN(ps, LUA_OK);
901 }
902
903 #define PUSHER_STRING 1
904 #define PUSHER_REALNAME 2
905 #define PUSHER_IP 3
906 #define PUSHER_LONG 4
907 #define PUSHER_HOSTNAME 5
908 #define PUSHER_SSTRING 6
909 #define PUSHER_TOTALUSERS 7
910 #define PUSHER_TOPIC 8
911
912 void lua_initnickpusher(void) {
913 int i = 0;
914
915 #define PUSH_NICKPUSHER(F2, O2) nickpusher[i].argtype = F2; nickpusher[i].structname = #O2; nickpusher[i].offset = offsetof(nick, O2); i++;
916
917 PUSH_NICKPUSHER(PUSHER_STRING, nick);
918 PUSH_NICKPUSHER(PUSHER_STRING, ident);
919 PUSH_NICKPUSHER(PUSHER_HOSTNAME, host);
920 PUSH_NICKPUSHER(PUSHER_REALNAME, realname);
921 PUSH_NICKPUSHER(PUSHER_STRING, authname);
922 PUSH_NICKPUSHER(PUSHER_IP, ipnode);
923 PUSH_NICKPUSHER(PUSHER_LONG, numeric);
924
925 nickpushercount = i;
926 nickpusher[i].argtype = 0;
927 }
928
929 INLINE void lua_setuppusher(struct lua_pusher *pusherlist, lua_State *l, int index, struct lua_pusher **lp, int max, int pcount) {
930 int current = 0;
931
932 if(max > 0)
933 lp[0] = NULL;
934
935 if(!lua_istable(l, index) || (max < 2))
936 return;
937
938 lua_pushnil(l);
939
940 max--;
941
942 while(lua_next(l, index)) {
943 if(lua_isint(l, -1)) {
944 int index = lua_toint(l, -1);
945 if((index >= 0) && (index < pcount))
946 lp[current++] = &pusherlist[index];
947 }
948
949 lua_pop(l, 1);
950
951 if(current == max)
952 break;
953 }
954
955 lp[current] = NULL;
956 }
957
958 INLINE int lua_usepusher(lua_State *l, struct lua_pusher **lp, void *np) {
959 int i = 0;
960
961 while(*lp) {
962 void *offset = (void *)np + (*lp)->offset;
963
964 switch((*lp)->argtype) {
965 case PUSHER_STRING:
966 lua_pushstring(l, (char *)offset);
967 break;
968 case PUSHER_HOSTNAME:
969 lua_pushstring(l, (*(host **)offset)->name->content);
970 break;
971 case PUSHER_REALNAME:
972 lua_pushstring(l, (*(realname **)offset)->name->content);
973 break;
974 case PUSHER_SSTRING:
975 lua_pushstring(l, (*((sstring **)offset))->content);
976 break;
977 case PUSHER_LONG:
978 lua_pushlong(l, *((long *)offset));
979 break;
980 case PUSHER_IP:
981 lua_pushstring(l, IPtostr((((patricia_node_t *)offset)->prefix->sin)));
982 break;
983 case PUSHER_TOTALUSERS:
984 lua_pushint(l, (*((channel **)offset))->users->totalusers);
985 break;
986 case PUSHER_TOPIC:
987 if((*((channel **)offset))->topic) {
988 lua_pushstring(l, (*((channel **)offset))->topic->content);
989 } else {
990 lua_pushnil(l);
991 }
992 break;
993 }
994
995 i++;
996 lp++;
997 }
998
999 return i;
1000 }
1001
1002 void lua_initchanpusher(void) {
1003 int i = 0;
1004
1005 #define PUSH_CHANPUSHER(F2, O2, N2) chanpusher[i].argtype = F2; chanpusher[i].structname = N2; chanpusher[i].offset = offsetof(chanindex, O2); i++;
1006
1007 PUSH_CHANPUSHER(PUSHER_SSTRING, name, "name");
1008 PUSH_CHANPUSHER(PUSHER_TOTALUSERS, channel, "totalusers");
1009 PUSH_CHANPUSHER(PUSHER_TOPIC, channel, "topic");
1010
1011 chanpushercount = i;
1012 chanpusher[i].argtype = 0;
1013 }