]> jfr.im git - irc/quakenet/newserv.git/blob - lua/lualocal.c
Merge chanserv-live into default.
[irc/quakenet/newserv.git] / lua / lualocal.c
1 #include <stdlib.h>
2
3 #include "../localuser/localuser.h"
4 #include "../lib/strlfunc.h"
5 #include "../core/schedule.h"
6 #include "../channel/channel.h"
7 #include "../localuser/localuserchannel.h"
8
9 #include "lua.h"
10 #include "luabot.h"
11 #include "lualocal.h"
12
13 void lua_localnickhandler(nick *target, int type, void **args);
14 void lua_reconnectlocal(void *arg);
15
16 static int lua_registerlocaluserid(lua_State *ps) {
17 lua_list *l;
18 lua_localnick *ln;
19 char *nickname, *ident, *hostname, *realname, *account;
20 flag_t modes = 0;
21 long userid;
22
23 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3) || !lua_isstring(ps, 4) || !lua_isstring(ps, 5) || !lua_isstring(ps, 7) || !lua_isfunction(ps, 8))
24 return 0;
25
26 nickname = (char *)lua_tostring(ps, 1);
27 ident = (char *)lua_tostring(ps, 2);
28 hostname = (char *)lua_tostring(ps, 3);
29 realname = (char *)lua_tostring(ps, 4);
30 account = (char *)lua_tostring(ps, 5);
31 if(lua_islong(ps, 6)) {
32 userid = lua_tolong(ps, 6);
33 } else {
34 userid = 0;
35 }
36
37 setflags(&modes, UMODE_ALL, (char *)lua_tostring(ps, 7), umodeflags, REJECT_NONE);
38
39 if(!lua_lineok(nickname) || !lua_lineok(ident) || !lua_lineok(hostname) || !lua_lineok(realname) || !lua_lineok(account))
40 return 0;
41
42 l = lua_listfromstate(ps);
43 if(!l)
44 return 0;
45
46 ln = (lua_localnick *)luamalloc(sizeof(lua_localnick));
47 if(!ln)
48 return 0;
49
50 ln->reconnect = NULL;
51
52 ln->nick = registerlocaluserflags(nickname, ident, hostname, realname, account, userid, 0, modes, &lua_localnickhandler);
53 if(!ln->nick) {
54 luafree(ln);
55 return 0;
56 }
57
58 ln->handler = luaL_ref(ps, LUA_REGISTRYINDEX);
59
60 ln->next = l->nicks;
61 l->nicks = ln;
62
63 lua_pushlong(ps, ln->nick->numeric);
64 return 1;
65 }
66
67 void lua_freelocalnick(lua_State *ps, lua_localnick *l, char *quitm) {
68 if(l->nick)
69 deregisterlocaluser(l->nick, quitm);
70
71 if(l->reconnect)
72 deleteschedule(l->reconnect, &lua_reconnectlocal, l);
73
74 luaL_unref(ps, LUA_REGISTRYINDEX, l->handler);
75
76 luafree(l);
77 }
78
79 int lua_getlocalnickbynick(nick *np, lua_list **rl, lua_localnick **rln) {
80 lua_list *l;
81 lua_localnick *ln;
82
83 for(l=lua_head;l;l=l->next)
84 for(ln=l->nicks;ln;ln=ln->next)
85 if(ln->nick == np) {
86 *rl = l;
87 *rln = ln;
88 return 1;
89 }
90
91 return 0;
92 }
93
94 static int lua_deregisterlocaluser(lua_State *ps) {
95 lua_list *l;
96 lua_localnick *l2, *lp = NULL;
97 long numeric;
98 char *quitm;
99
100 if(!lua_islong(ps, 1))
101 LUA_RETURN(ps, LUA_FAIL);
102
103 numeric = lua_tolong(ps, 1);
104
105 quitm = lua_isstring(ps, 2)?(char *)lua_tostring(ps, 2):"localuser unregistered.";
106
107 l = lua_listfromstate(ps);
108
109 for(l2=l->nicks;l2;lp=l2,l2=l2->next) {
110 if(l2->nick && l2->nick->numeric == numeric) {
111 if(lp) {
112 lp->next = l2->next;
113 } else {
114 l->nicks = l2->next;
115 }
116
117 lua_freelocalnick(ps, l2, quitm);
118 LUA_RETURN(ps, LUA_OK);
119 }
120 }
121
122 LUA_RETURN(ps, LUA_FAIL);
123 }
124
125 void lua_deregisternicks(lua_list *l) {
126 struct lua_localnick *ln, *pn;
127
128 ln = l->nicks;
129 l->nicks = NULL;
130 for(;ln;ln=pn) {
131 pn = ln->next;
132
133 lua_freelocalnick(l->l, ln, "Script unloaded.");
134 }
135
136 l->nicks = NULL;
137 }
138
139 /* todo */
140 void lua_localnickhandler(nick *target, int type, void **args) {
141 nick *np;
142 char *p;
143 lua_localnick *ln;
144 lua_list *l;
145 channel *c;
146
147 if(!lua_getlocalnickbynick(target, &l, &ln))
148 return;
149
150 switch(type) {
151 case LU_PRIVMSG:
152 np = (nick *)args[0];
153 p = (char *)args[1];
154
155 if(!np || !p)
156 return;
157
158 lua_vlpcall(l, ln, "irc_onmsg", "Ns", np, p);
159
160 break;
161
162 case LU_PRIVNOTICE:
163 np = (nick *)args[0];
164 p = (char *)args[1];
165
166 if(!np || !p)
167 return;
168
169 lua_vlpcall(l, ln, "irc_onnotice", "Ns", np, p);
170
171 break;
172
173 case LU_CHANMSG:
174 np = (nick *)args[0];
175 c = (channel *)args[1];
176 p = (char *)args[2];
177
178 if(!np || !p || !c || !c->index || !c->index->name || !c->index->name->content)
179 return;
180
181 lua_vlpcall(l, ln, "irc_onchanmsg", "Nss", np, c->index->name->content, p);
182 break;
183
184 case LU_KILLED:
185 lua_vlpcall(l, ln, "irc_onkilled", "");
186
187 strlcpy(ln->nickname, target->nick, sizeof(ln->nickname));
188 strlcpy(ln->ident, target->ident, sizeof(ln->ident));
189 strlcpy(ln->hostname, target->host->name->content, sizeof(ln->hostname));
190 strlcpy(ln->realname, target->realname->name->content, sizeof(ln->realname));
191 strlcpy(ln->account, target->authname, sizeof(ln->account));
192
193 ln->umodes = target->umodes;
194 ln->nick = NULL;
195
196 ln->reconnect = scheduleoneshot(time(NULL) + 1, &lua_reconnectlocal, ln);
197
198 break;
199 case LU_INVITE:
200 /* we were invited, check if someone invited us to PUBLICCHAN */
201 np = (nick *)args[0];
202 c = (channel *)args[1];
203
204 if(!c || !np || !c->index || !c->index->name)
205 return;
206
207 lua_vlpcall(l, ln, "irc_oninvite", "Ns", np, c->index->name->content);
208 break;
209 }
210 }
211
212 void lua_reconnectlocal(void *arg) {
213 lua_list *l;
214 lua_localnick *ln = (lua_localnick *)arg;
215
216 ln->nick = registerlocaluser(ln->nickname, ln->ident, ln->hostname, ln->realname, ln->account, ln->umodes, &lua_localnickhandler);
217 if(!ln->nick) {
218 ln->reconnect = scheduleoneshot(time(NULL) + 1, &lua_reconnectlocal, ln);
219 return;
220 }
221
222 ln->reconnect = NULL;
223
224 if(lua_getlocalnickbynick(ln->nick, &l, &ln)) /* hacky! */
225 lua_vlpcall(l, ln, "irc_onkillreconnect", "");
226 }
227
228 static int lua_localjoin(lua_State *ps) {
229 nick *source;
230 channel *target;
231 char *chan;
232
233 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
234 LUA_RETURN(ps, LUA_FAIL);
235
236 source = getnickbynumeric(lua_tolong(ps, 1));
237 if(!source)
238 LUA_RETURN(ps, LUA_FAIL);
239
240 chan = (char *)lua_tostring(ps, 2);
241 if(chan[0] != '#')
242 LUA_RETURN(ps, LUA_FAIL);
243
244 if(!lua_lineok(chan))
245 LUA_RETURN(ps, LUA_FAIL);
246
247 target = findchannel(chan);
248 if(target) {
249 localjoinchannel(source, target);
250 } else {
251 localcreatechannel(source, chan);
252 }
253
254 LUA_RETURN(ps, LUA_OK);
255 }
256
257 static int lua_localpart(lua_State *ps) {
258 nick *source;
259 channel *target;
260 char *chan, *reason;
261
262 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
263 LUA_RETURN(ps, LUA_FAIL);
264
265 source = getnickbynumeric(lua_tolong(ps, 1));
266 if(!source)
267 LUA_RETURN(ps, LUA_FAIL);
268
269 chan = (char *)lua_tostring(ps, 2);
270
271 if(!lua_lineok(chan))
272 LUA_RETURN(ps, LUA_FAIL);
273
274 if(lua_isstring(ps, 3)) {
275 reason = (char *)lua_tostring(ps, 3);
276 if(!lua_lineok(reason))
277 LUA_RETURN(ps, LUA_FAIL);
278 } else {
279 reason = NULL;
280 }
281
282 target = findchannel(chan);
283 if(target) {
284 localpartchannel(source, target, reason);
285 } else {
286 LUA_RETURN(ps, LUA_FAIL);
287 }
288
289 LUA_RETURN(ps, LUA_OK);
290 }
291
292 static int lua_localchanmsg(lua_State *ps) {
293 char *msg;
294 nick *source;
295 channel *target;
296
297 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
298 LUA_RETURN(ps, LUA_FAIL);
299
300 source = getnickbynumeric(lua_tolong(ps, 1));
301 if(!source)
302 LUA_RETURN(ps, LUA_FAIL);
303
304 target = findchannel((char *)lua_tostring(ps, 2));
305 if(!target)
306 LUA_RETURN(ps, LUA_FAIL);
307
308 msg = (char *)lua_tostring(ps, 3);
309
310 if(!lua_lineok(msg))
311 LUA_RETURN(ps, LUA_FAIL);
312
313 sendmessagetochannel(source, target, "%s", msg);
314
315 LUA_RETURN(ps, LUA_OK);
316 }
317
318 static int lua_localnotice(lua_State *ps) {
319 char *msg;
320 nick *source;
321 nick *target;
322
323 if(!lua_islong(ps, 1) || !lua_islong(ps, 2) || !lua_isstring(ps, 3))
324 LUA_RETURN(ps, LUA_FAIL);
325
326 source = getnickbynumeric(lua_tolong(ps, 1));
327 if(!source)
328 LUA_RETURN(ps, LUA_FAIL);
329
330 target = getnickbynumeric(lua_tolong(ps, 2));
331 if(!target)
332 LUA_RETURN(ps, LUA_FAIL);
333
334 msg = (char *)lua_tostring(ps, 3);
335
336 if(!lua_lineok(msg))
337 LUA_RETURN(ps, LUA_FAIL);
338
339 sendnoticetouser(source, target, "%s", msg);
340
341 LUA_RETURN(ps, LUA_OK);
342 }
343
344 static int lua_localprivmsg(lua_State *ps) {
345 char *msg;
346 nick *source;
347 nick *target;
348
349 if(!lua_islong(ps, 1) || !lua_islong(ps, 2) || !lua_isstring(ps, 3))
350 LUA_RETURN(ps, LUA_FAIL);
351
352 source = getnickbynumeric(lua_tolong(ps, 1));
353 if(!source)
354 LUA_RETURN(ps, LUA_FAIL);
355
356 target = getnickbynumeric(lua_tolong(ps, 2));
357 if(!target)
358 LUA_RETURN(ps, LUA_FAIL);
359
360 msg = (char *)lua_tostring(ps, 3);
361
362 if(!lua_lineok(msg))
363 LUA_RETURN(ps, LUA_FAIL);
364
365 sendmessagetouser(source, target, "%s", msg);
366
367 LUA_RETURN(ps, LUA_OK);
368 }
369
370 static int lua_localovmode(lua_State *l) {
371 nick *source;
372 channel *chan;
373 int state = 0, add = 0, realmode = 0, ignoring = 0;
374 modechanges changes;
375
376 if(!lua_islong(l, 1) || !lua_isstring(l, 2) || !lua_istable(l, 3))
377 LUA_RETURN(l, LUA_FAIL);
378
379 source = getnickbynumeric(lua_tolong(l, 1));
380 if(!source)
381 LUA_RETURN(l, LUA_FAIL);
382
383 chan = findchannel((char *)lua_tostring(l, 2));
384 if(!chan)
385 LUA_RETURN(l, LUA_FAIL);
386
387 localsetmodeinit(&changes, chan, source);
388
389 lua_pushnil(l);
390
391 while(lua_next(l, 3)) {
392 if(state == 0) {
393 ignoring = 0;
394
395 if(!lua_isboolean(l, -1)) {
396 ignoring = 1;
397 } else {
398 add = (int)lua_toboolean(l, -1);
399 }
400 } else if((state == 1) && !ignoring) {
401 if(!lua_isstring(l, -1)) {
402 ignoring = 1;
403 } else {
404 char *mode = (char *)lua_tostring(l, -1);
405 if((*mode == 'o') && add) {
406 realmode = MC_OP;
407 } else if (*mode == 'o') {
408 realmode = MC_DEOP;
409 } else if((*mode == 'v') && add) {
410 realmode = MC_VOICE;
411 } else if (*mode == 'v') {
412 realmode = MC_DEVOICE;
413 } else {
414 ignoring = 1;
415 }
416 }
417 } else if((state == 2) && !ignoring) {
418 if(lua_islong(l, -1)) {
419 nick *target = getnickbynumeric(lua_tolong(l, -1));
420 if(target)
421 localdosetmode_nick(&changes, target, realmode);
422 }
423 }
424
425 lua_pop(l, 1);
426
427 state = (state + 1) % 3;
428 }
429
430 localsetmodeflush(&changes, 1);
431
432 LUA_RETURN(l, LUA_OK);
433 }
434
435 static int lua_localumodes(lua_State *ps) {
436 nick *np;
437 char *modes;
438 flag_t newmodes = 0;
439
440 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
441 LUA_RETURN(ps, LUA_FAIL);
442
443 np = getnickbynumeric(lua_tolong(ps, 1));
444 if(!np)
445 LUA_RETURN(ps, LUA_FAIL);
446
447 modes = (char *)lua_tostring(ps, 2);
448
449 setflags(&newmodes, UMODE_ALL, modes, umodeflags, REJECT_NONE);
450
451 localusersetumodes(np, newmodes);
452 LUA_RETURN(ps, LUA_OK);
453 }
454
455 static int lua_localtopic(lua_State *ps) {
456 nick *np;
457 channel *cp;
458 char *topic;
459
460 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
461 LUA_RETURN(ps, LUA_FAIL);
462
463 np = getnickbynumeric(lua_tolong(ps, 1));
464 if(!np)
465 LUA_RETURN(ps, LUA_FAIL);
466
467 cp = findchannel((char *)lua_tostring(ps, 2));
468 if(!cp)
469 LUA_RETURN(ps, LUA_FAIL);
470
471 topic = (char *)lua_tostring(ps, 3);
472 if(!topic || !lua_lineok(topic))
473 LUA_RETURN(ps, LUA_FAIL);
474
475 localsettopic(np, cp, topic);
476
477 LUA_RETURN(ps, LUA_OK);
478 }
479
480 static int lua_localban(lua_State *ps) {
481 channel *cp;
482 const char *mask;
483 modechanges changes;
484 nick *source;
485
486 int dir = MCB_ADD;
487
488 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
489 LUA_RETURN(ps, LUA_FAIL);
490
491 if(lua_isboolean(ps, 4) && lua_toboolean(ps, 4))
492 dir = MCB_DEL;
493
494 source = getnickbynumeric(lua_tolong(ps, 1));
495
496 cp = findchannel((char *)lua_tostring(ps, 2));
497 if(!cp)
498 LUA_RETURN(ps, LUA_FAIL);
499
500 mask = lua_tostring(ps, 3);
501 if(!mask || !mask[0] || !lua_lineok(mask))
502 LUA_RETURN(ps, LUA_FAIL);
503
504 localsetmodeinit(&changes, cp, source);
505 localdosetmode_ban(&changes, mask, dir);
506 localsetmodeflush(&changes, 1);
507
508 LUA_RETURN(ps, LUA_OK);
509 }
510
511 static int lua_localkick(lua_State *ps) {
512 const char *n, *msg, *chan;
513 nick *source, *np;
514 channel *cp;
515 int dochecks = 1;
516
517 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3) || !lua_isstring(ps, 4))
518 LUA_RETURN(ps, LUA_FAIL);
519
520 source = getnickbynumeric(lua_tolong(ps, 1));
521 chan = lua_tostring(ps, 2);
522 n = lua_tostring(ps, 3);
523 msg = lua_tostring(ps, 4);
524 if(!source)
525 LUA_RETURN(ps, LUA_FAIL);
526
527 if(lua_isboolean(ps, 4) && !lua_toboolean(ps, 4))
528 dochecks = 0;
529
530 np = getnickbynick(n);
531 if(!np)
532 LUA_RETURN(ps, LUA_FAIL);
533
534 if(dochecks && (IsOper(np) || IsXOper(np) || IsService(np)))
535 LUA_RETURN(ps, LUA_FAIL);
536
537 cp = findchannel((char *)chan);
538 if(!cp)
539 LUA_RETURN(ps, LUA_FAIL);
540
541 if(!lua_lineok(msg))
542 LUA_RETURN(ps, LUA_FAIL);
543
544 localkickuser(source, cp, np, msg);
545
546 LUA_RETURN(ps, LUA_OK);
547 }
548
549 static int lua_localrename(lua_State *ps) {
550 nick *np;
551 char *changeto;
552
553 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) )
554 LUA_RETURN(ps, LUA_FAIL);
555
556 np = getnickbynumeric(lua_tolong(ps, 1));
557 changeto = (char *)lua_tostring(ps, 2);
558
559 if(!lua_lineok(changeto))
560 LUA_RETURN(ps, LUA_FAIL);
561
562 renamelocaluser(np, changeto);
563
564 LUA_RETURN(ps, LUA_OK);
565 }
566
567 static int lua_localwallusers(lua_State *ps) {
568 char *msg;
569 nick *source;
570 char senderstr[6];
571
572 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
573 LUA_RETURN(ps, LUA_FAIL);
574
575 source = getnickbynumeric(lua_tolong(ps, 1));
576 if(!source)
577 LUA_RETURN(ps, LUA_FAIL);
578
579 msg = (char *)lua_tostring(ps, 2);
580
581 if(!lua_lineok(msg))
582 LUA_RETURN(ps, LUA_FAIL);
583
584 longtonumeric2(source->numeric,5,senderstr);
585 irc_send("%s WU :%s", senderstr, msg);
586
587 LUA_RETURN(ps, LUA_OK);
588 }
589
590 static int lua_localwallops(lua_State *ps) {
591 char *msg;
592 nick *source;
593 char senderstr[6];
594
595 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
596 LUA_RETURN(ps, LUA_FAIL);
597
598 source = getnickbynumeric(lua_tolong(ps, 1));
599 if(!source)
600 LUA_RETURN(ps, LUA_FAIL);
601
602 msg = (char *)lua_tostring(ps, 2);
603
604 if(!lua_lineok(msg))
605 LUA_RETURN(ps, LUA_FAIL);
606
607 longtonumeric2(source->numeric,5,senderstr);
608 irc_send("%s WA :%s", senderstr, msg);
609
610 LUA_RETURN(ps, LUA_OK);
611 }
612
613 static int lua_localsimplechanmode(lua_State *ps) {
614 nick *source;
615 channel *cp;
616 char *modes;
617 flag_t add = 0, del = ~add;
618 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 | CHANMODE_MODNOAUTH | CHANMODE_SINGLETARG;
619 modechanges changes;
620
621 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
622 LUA_RETURN(ps, LUA_FAIL);
623
624 source = getnickbynumeric(lua_tolong(ps, 1));
625 if(!source)
626 LUA_RETURN(ps, LUA_FAIL);
627
628 cp = findchannel((char *)lua_tostring(ps, 2));
629 if(!cp)
630 LUA_RETURN(ps, LUA_FAIL);
631
632 modes = (char *)lua_tostring(ps, 3);
633 if(!modes)
634 LUA_RETURN(ps, LUA_FAIL);
635
636 if(setflags(&add, permitted, modes, cmodeflags, REJECT_DISALLOWED|REJECT_UNKNOWN) != REJECT_NONE)
637 LUA_RETURN(ps, LUA_FAIL);
638
639 if(setflags(&del, permitted, modes, cmodeflags, REJECT_DISALLOWED|REJECT_UNKNOWN) != REJECT_NONE)
640 LUA_RETURN(ps, LUA_FAIL);
641
642 localsetmodeinit(&changes, cp, source);
643 localdosetmode_simple(&changes, add, ~del);
644 localsetmodeflush(&changes, 1);
645
646 LUA_RETURN(ps, LUA_OK);
647 }
648
649 void lua_registerlocalcommands(lua_State *l) {
650 lua_register(l, "irc_localregisteruserid", lua_registerlocaluserid);
651 lua_register(l, "irc_localderegisteruser", lua_deregisterlocaluser);
652 lua_register(l, "irc_localjoin", lua_localjoin);
653 lua_register(l, "irc_localpart", lua_localpart);
654 lua_register(l, "irc_localchanmsg", lua_localchanmsg);
655 lua_register(l, "irc_localnotice", lua_localnotice);
656 lua_register(l, "irc_localprivmsg", lua_localprivmsg);
657
658 lua_register(l, "irc_localovmode", lua_localovmode);
659 lua_register(l, "irc_localtopic", lua_localtopic);
660
661 lua_register(l, "irc_localban", lua_localban);
662 lua_register(l, "irc_localkick", lua_localkick);
663 lua_register(l, "irc_localumodes", lua_localumodes);
664
665 lua_register(l, "irc_localrename", lua_localrename);
666
667 lua_register(l, "irc_localwallusers", lua_localwallusers);
668 lua_register(l, "irc_localwallops", lua_localwallops);
669
670 lua_register(l, "irc_localsimplechanmode", lua_localsimplechanmode);
671 }
672