]> jfr.im git - irc/quakenet/newserv.git/blob - lua/lualocal.c
LUA: port luadb to dbapi2 to drop postgres dependency
[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_localchanopnotice(lua_State *ps) {
319 char *msg;
320 nick *source;
321 channel *target;
322
323 if(!lua_islong(ps, 1) || !lua_isstring(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 = findchannel((char *)lua_tostring(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 sendopnoticetochannel(source, target, "%s", msg);
340
341 LUA_RETURN(ps, LUA_OK);
342 }
343
344 static int lua_localnotice(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 sendnoticetouser(source, target, "%s", msg);
366
367 LUA_RETURN(ps, LUA_OK);
368 }
369
370 static int lua_localprivmsg(lua_State *ps) {
371 char *msg;
372 nick *source;
373 nick *target;
374
375 if(!lua_islong(ps, 1) || !lua_islong(ps, 2) || !lua_isstring(ps, 3))
376 LUA_RETURN(ps, LUA_FAIL);
377
378 source = getnickbynumeric(lua_tolong(ps, 1));
379 if(!source)
380 LUA_RETURN(ps, LUA_FAIL);
381
382 target = getnickbynumeric(lua_tolong(ps, 2));
383 if(!target)
384 LUA_RETURN(ps, LUA_FAIL);
385
386 msg = (char *)lua_tostring(ps, 3);
387
388 if(!lua_lineok(msg))
389 LUA_RETURN(ps, LUA_FAIL);
390
391 sendmessagetouser(source, target, "%s", msg);
392
393 LUA_RETURN(ps, LUA_OK);
394 }
395
396 static int lua_localovmode(lua_State *l) {
397 nick *source;
398 channel *chan;
399 int state = 0, add = 0, realmode = 0, ignoring = 0;
400 modechanges changes;
401
402 if(!lua_islong(l, 1) || !lua_isstring(l, 2) || !lua_istable(l, 3))
403 LUA_RETURN(l, LUA_FAIL);
404
405 source = getnickbynumeric(lua_tolong(l, 1));
406 if(!source)
407 LUA_RETURN(l, LUA_FAIL);
408
409 chan = findchannel((char *)lua_tostring(l, 2));
410 if(!chan)
411 LUA_RETURN(l, LUA_FAIL);
412
413 localsetmodeinit(&changes, chan, source);
414
415 lua_pushnil(l);
416
417 while(lua_next(l, 3)) {
418 if(state == 0) {
419 ignoring = 0;
420
421 if(!lua_isboolean(l, -1)) {
422 ignoring = 1;
423 } else {
424 add = (int)lua_toboolean(l, -1);
425 }
426 } else if((state == 1) && !ignoring) {
427 if(!lua_isstring(l, -1)) {
428 ignoring = 1;
429 } else {
430 char *mode = (char *)lua_tostring(l, -1);
431 if((*mode == 'o') && add) {
432 realmode = MC_OP;
433 } else if (*mode == 'o') {
434 realmode = MC_DEOP;
435 } else if((*mode == 'v') && add) {
436 realmode = MC_VOICE;
437 } else if (*mode == 'v') {
438 realmode = MC_DEVOICE;
439 } else {
440 ignoring = 1;
441 }
442 }
443 } else if((state == 2) && !ignoring) {
444 if(lua_islong(l, -1)) {
445 nick *target = getnickbynumeric(lua_tolong(l, -1));
446 if(target)
447 localdosetmode_nick(&changes, target, realmode);
448 }
449 }
450
451 lua_pop(l, 1);
452
453 state = (state + 1) % 3;
454 }
455
456 localsetmodeflush(&changes, 1);
457
458 LUA_RETURN(l, LUA_OK);
459 }
460
461 static int lua_localumodes(lua_State *ps) {
462 nick *np;
463 char *modes;
464 flag_t newmodes = 0;
465
466 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
467 LUA_RETURN(ps, LUA_FAIL);
468
469 np = getnickbynumeric(lua_tolong(ps, 1));
470 if(!np)
471 LUA_RETURN(ps, LUA_FAIL);
472
473 modes = (char *)lua_tostring(ps, 2);
474
475 setflags(&newmodes, UMODE_ALL, modes, umodeflags, REJECT_NONE);
476
477 localusersetumodes(np, newmodes);
478 LUA_RETURN(ps, LUA_OK);
479 }
480
481 static int lua_localtopic(lua_State *ps) {
482 nick *np;
483 channel *cp;
484 char *topic;
485
486 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
487 LUA_RETURN(ps, LUA_FAIL);
488
489 np = getnickbynumeric(lua_tolong(ps, 1));
490 if(!np)
491 LUA_RETURN(ps, LUA_FAIL);
492
493 cp = findchannel((char *)lua_tostring(ps, 2));
494 if(!cp)
495 LUA_RETURN(ps, LUA_FAIL);
496
497 topic = (char *)lua_tostring(ps, 3);
498 if(!topic || !lua_lineok(topic))
499 LUA_RETURN(ps, LUA_FAIL);
500
501 localsettopic(np, cp, topic);
502
503 LUA_RETURN(ps, LUA_OK);
504 }
505
506 static int lua_localban(lua_State *ps) {
507 channel *cp;
508 const char *mask;
509 modechanges changes;
510 nick *source;
511
512 int dir = MCB_ADD;
513
514 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
515 LUA_RETURN(ps, LUA_FAIL);
516
517 if(lua_isboolean(ps, 4) && lua_toboolean(ps, 4))
518 dir = MCB_DEL;
519
520 source = getnickbynumeric(lua_tolong(ps, 1));
521
522 cp = findchannel((char *)lua_tostring(ps, 2));
523 if(!cp)
524 LUA_RETURN(ps, LUA_FAIL);
525
526 mask = lua_tostring(ps, 3);
527 if(!mask || !mask[0] || !lua_lineok(mask))
528 LUA_RETURN(ps, LUA_FAIL);
529
530 localsetmodeinit(&changes, cp, source);
531 localdosetmode_ban(&changes, mask, dir);
532 localsetmodeflush(&changes, 1);
533
534 LUA_RETURN(ps, LUA_OK);
535 }
536
537 static int lua_localkick(lua_State *ps) {
538 const char *n, *msg, *chan;
539 nick *source, *np;
540 channel *cp;
541 int dochecks = 1;
542
543 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3) || !lua_isstring(ps, 4))
544 LUA_RETURN(ps, LUA_FAIL);
545
546 source = getnickbynumeric(lua_tolong(ps, 1));
547 chan = lua_tostring(ps, 2);
548 n = lua_tostring(ps, 3);
549 msg = lua_tostring(ps, 4);
550 if(!source)
551 LUA_RETURN(ps, LUA_FAIL);
552
553 if(lua_isboolean(ps, 4) && !lua_toboolean(ps, 4))
554 dochecks = 0;
555
556 np = getnickbynick(n);
557 if(!np)
558 LUA_RETURN(ps, LUA_FAIL);
559
560 if(dochecks && (IsOper(np) || IsXOper(np) || IsService(np)))
561 LUA_RETURN(ps, LUA_FAIL);
562
563 cp = findchannel((char *)chan);
564 if(!cp)
565 LUA_RETURN(ps, LUA_FAIL);
566
567 if(!lua_lineok(msg))
568 LUA_RETURN(ps, LUA_FAIL);
569
570 localkickuser(source, cp, np, msg);
571
572 LUA_RETURN(ps, LUA_OK);
573 }
574
575 static int lua_localrename(lua_State *ps) {
576 nick *np;
577 char *changeto;
578
579 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) )
580 LUA_RETURN(ps, LUA_FAIL);
581
582 np = getnickbynumeric(lua_tolong(ps, 1));
583 changeto = (char *)lua_tostring(ps, 2);
584
585 if(!lua_lineok(changeto))
586 LUA_RETURN(ps, LUA_FAIL);
587
588 renamelocaluser(np, changeto);
589
590 LUA_RETURN(ps, LUA_OK);
591 }
592
593 static int lua_localwallusers(lua_State *ps) {
594 char *msg;
595 nick *source;
596 char senderstr[6];
597
598 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
599 LUA_RETURN(ps, LUA_FAIL);
600
601 source = getnickbynumeric(lua_tolong(ps, 1));
602 if(!source)
603 LUA_RETURN(ps, LUA_FAIL);
604
605 msg = (char *)lua_tostring(ps, 2);
606
607 if(!lua_lineok(msg))
608 LUA_RETURN(ps, LUA_FAIL);
609
610 longtonumeric2(source->numeric,5,senderstr);
611 irc_send("%s WU :%s", senderstr, msg);
612
613 LUA_RETURN(ps, LUA_OK);
614 }
615
616 static int lua_localwallops(lua_State *ps) {
617 char *msg;
618 nick *source;
619 char senderstr[6];
620
621 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
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 msg = (char *)lua_tostring(ps, 2);
629
630 if(!lua_lineok(msg))
631 LUA_RETURN(ps, LUA_FAIL);
632
633 longtonumeric2(source->numeric,5,senderstr);
634 irc_send("%s WA :%s", senderstr, msg);
635
636 LUA_RETURN(ps, LUA_OK);
637 }
638
639 static int lua_localsimplechanmode(lua_State *ps) {
640 nick *source;
641 channel *cp;
642 char *modes;
643 flag_t add = 0, del = ~add;
644 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;
645 modechanges changes;
646
647 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
648 LUA_RETURN(ps, LUA_FAIL);
649
650 source = getnickbynumeric(lua_tolong(ps, 1));
651 if(!source)
652 LUA_RETURN(ps, LUA_FAIL);
653
654 cp = findchannel((char *)lua_tostring(ps, 2));
655 if(!cp)
656 LUA_RETURN(ps, LUA_FAIL);
657
658 modes = (char *)lua_tostring(ps, 3);
659 if(!modes)
660 LUA_RETURN(ps, LUA_FAIL);
661
662 if(setflags(&add, permitted, modes, cmodeflags, REJECT_DISALLOWED|REJECT_UNKNOWN) != REJECT_NONE)
663 LUA_RETURN(ps, LUA_FAIL);
664
665 if(setflags(&del, permitted, modes, cmodeflags, REJECT_DISALLOWED|REJECT_UNKNOWN) != REJECT_NONE)
666 LUA_RETURN(ps, LUA_FAIL);
667
668 localsetmodeinit(&changes, cp, source);
669 localdosetmode_simple(&changes, add, ~del);
670 localsetmodeflush(&changes, 1);
671
672 LUA_RETURN(ps, LUA_OK);
673 }
674
675 void lua_registerlocalcommands(lua_State *l) {
676 lua_register(l, "irc_localregisteruserid", lua_registerlocaluserid);
677 lua_register(l, "irc_localderegisteruser", lua_deregisterlocaluser);
678 lua_register(l, "irc_localjoin", lua_localjoin);
679 lua_register(l, "irc_localpart", lua_localpart);
680 lua_register(l, "irc_localchanmsg", lua_localchanmsg);
681 lua_register(l, "irc_localnotice", lua_localnotice);
682 lua_register(l, "irc_localprivmsg", lua_localprivmsg);
683
684 lua_register(l, "irc_localovmode", lua_localovmode);
685 lua_register(l, "irc_localtopic", lua_localtopic);
686
687 lua_register(l, "irc_localban", lua_localban);
688 lua_register(l, "irc_localkick", lua_localkick);
689 lua_register(l, "irc_localumodes", lua_localumodes);
690
691 lua_register(l, "irc_localrename", lua_localrename);
692
693 lua_register(l, "irc_localwallusers", lua_localwallusers);
694 lua_register(l, "irc_localwallops", lua_localwallops);
695
696 lua_register(l, "irc_localsimplechanmode", lua_localsimplechanmode);
697 lua_register(l, "irc_localchanopnotice", lua_localchanopnotice);
698 }
699