]> jfr.im git - irc/quakenet/newserv.git/blame - lua/lualocal.c
Merge.
[irc/quakenet/newserv.git] / lua / lualocal.c
CommitLineData
0225bed3
CP
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
13void lua_localnickhandler(nick *target, int type, void **args);
14void lua_reconnectlocal(void *arg);
15
5541f23c 16static int lua_registerlocaluserid(lua_State *ps) {
0225bed3
CP
17 lua_list *l;
18 lua_localnick *ln;
19 char *nickname, *ident, *hostname, *realname, *account;
20 flag_t modes = 0;
5541f23c 21 long userid;
0225bed3 22
5541f23c 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))
0225bed3
CP
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);
5541f23c
CP
31 if(lua_islong(ps, 6)) {
32 userid = lua_tolong(ps, 6);
33 } else {
34 userid = 0;
35 }
0225bed3 36
5541f23c 37 setflags(&modes, UMODE_ALL, (char *)lua_tostring(ps, 7), umodeflags, REJECT_NONE);
0225bed3
CP
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
d0e17ef9 46 ln = (lua_localnick *)luamalloc(sizeof(lua_localnick));
0225bed3
CP
47 if(!ln)
48 return 0;
49
50 ln->reconnect = NULL;
51
5541f23c 52 ln->nick = registerlocaluserflags(nickname, ident, hostname, realname, account, userid, 0, modes, &lua_localnickhandler);
0225bed3 53 if(!ln->nick) {
d0e17ef9 54 luafree(ln);
0225bed3
CP
55 return 0;
56 }
57
58 ln->handler = luaL_ref(ps, LUA_REGISTRYINDEX);
59
60 ln->next = l->nicks;
61 l->nicks = ln;
62
eda80ef2 63 lua_pushlong(ps, ln->nick->numeric);
0225bed3
CP
64 return 1;
65}
66
67void 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
d0e17ef9 76 luafree(l);
0225bed3
CP
77}
78
79int 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
94static 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
3f1c5f43
CP
103 numeric = lua_tolong(ps, 1);
104
0225bed3
CP
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) {
c0088ccb 110 if(l2->nick && l2->nick->numeric == numeric) {
9315931c
CP
111 if(lp) {
112 lp->next = l2->next;
113 } else {
114 l->nicks = l2->next;
115 }
116
0225bed3
CP
117 lua_freelocalnick(ps, l2, quitm);
118 LUA_RETURN(ps, LUA_OK);
119 }
120 }
121
122 LUA_RETURN(ps, LUA_FAIL);
123}
124
125void lua_deregisternicks(lua_list *l) {
126 struct lua_localnick *ln, *pn;
127
c0088ccb
CP
128 ln = l->nicks;
129 l->nicks = NULL;
130 for(;ln;ln=pn) {
0225bed3
CP
131 pn = ln->next;
132
133 lua_freelocalnick(l->l, ln, "Script unloaded.");
134 }
135
136 l->nicks = NULL;
137}
138
139/* todo */
140void lua_localnickhandler(nick *target, int type, void **args) {
141 nick *np;
142 char *p;
143 lua_localnick *ln;
144 lua_list *l;
e8b79634 145 channel *c;
0225bed3
CP
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
e8b79634
CP
162 case LU_CHANMSG:
163 np = (nick *)args[0];
164 c = (channel *)args[1];
165 p = (char *)args[2];
166
167 if(!np || !p || !c || !c->index || !c->index->name || !c->index->name->content)
168 return;
169
170 lua_vlpcall(l, ln, "irc_onchanmsg", "Nss", np, c->index->name->content, p);
171 break;
172
0225bed3 173 case LU_KILLED:
acd438c7
CP
174 p = (char *)args[2];
175 lua_vlpcall(l, ln, "irc_onkilled", "s", p);
0225bed3
CP
176
177 strlcpy(ln->nickname, target->nick, sizeof(ln->nickname));
178 strlcpy(ln->ident, target->ident, sizeof(ln->ident));
179 strlcpy(ln->hostname, target->host->name->content, sizeof(ln->hostname));
180 strlcpy(ln->realname, target->realname->name->content, sizeof(ln->realname));
181 strlcpy(ln->account, target->authname, sizeof(ln->account));
182
183 ln->umodes = target->umodes;
184 ln->nick = NULL;
185
186 ln->reconnect = scheduleoneshot(time(NULL) + 1, &lua_reconnectlocal, ln);
187
23518e11
CP
188 break;
189 case LU_INVITE:
190 /* we were invited, check if someone invited us to PUBLICCHAN */
191 np = (nick *)args[0];
192 c = (channel *)args[1];
193
194 if(!c || !np || !c->index || !c->index->name || !c->index->name->content)
195 return;
196
197 lua_vlpcall(l, ln, "irc_oninvite", "Ns", np, c->index->name->content);
0225bed3
CP
198 break;
199 }
200}
201
202void lua_reconnectlocal(void *arg) {
203 lua_list *l;
204 lua_localnick *ln = (lua_localnick *)arg;
205
206 ln->nick = registerlocaluser(ln->nickname, ln->ident, ln->hostname, ln->realname, ln->account, ln->umodes, &lua_localnickhandler);
207 if(!ln->nick) {
208 ln->reconnect = scheduleoneshot(time(NULL) + 1, &lua_reconnectlocal, ln);
209 return;
210 }
211
212 ln->reconnect = NULL;
213
214 if(lua_getlocalnickbynick(ln->nick, &l, &ln)) /* hacky! */
215 lua_vlpcall(l, ln, "irc_onkillreconnect", "");
216}
217
218static int lua_localjoin(lua_State *ps) {
219 nick *source;
220 channel *target;
221 char *chan;
222
223 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
224 LUA_RETURN(ps, LUA_FAIL);
225
226 source = getnickbynumeric(lua_tolong(ps, 1));
227 if(!source)
228 LUA_RETURN(ps, LUA_FAIL);
229
230 chan = (char *)lua_tostring(ps, 2);
89b1bd77
CP
231 if(chan[0] != '#')
232 LUA_RETURN(ps, LUA_FAIL);
0225bed3
CP
233
234 if(!lua_lineok(chan))
235 LUA_RETURN(ps, LUA_FAIL);
236
237 target = findchannel(chan);
238 if(target) {
239 localjoinchannel(source, target);
240 } else {
e8b79634 241 localcreatechannel(source, chan);
0225bed3
CP
242 }
243
244 LUA_RETURN(ps, LUA_OK);
245}
246
b7252b44
CP
247static int lua_localpart(lua_State *ps) {
248 nick *source;
249 channel *target;
d70f70a1 250 char *chan, *reason;
b7252b44
CP
251
252 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
253 LUA_RETURN(ps, LUA_FAIL);
254
255 source = getnickbynumeric(lua_tolong(ps, 1));
256 if(!source)
257 LUA_RETURN(ps, LUA_FAIL);
258
259 chan = (char *)lua_tostring(ps, 2);
260
261 if(!lua_lineok(chan))
262 LUA_RETURN(ps, LUA_FAIL);
d70f70a1
CP
263
264 if(lua_isstring(ps, 3)) {
265 reason = (char *)lua_tostring(ps, 3);
266 if(!lua_lineok(reason))
267 LUA_RETURN(ps, LUA_FAIL);
268 } else {
269 reason = NULL;
270 }
271
b7252b44
CP
272 target = findchannel(chan);
273 if(target) {
d70f70a1 274 localpartchannel(source, target, reason);
b7252b44
CP
275 } else {
276 LUA_RETURN(ps, LUA_FAIL);
277 }
278
279 LUA_RETURN(ps, LUA_OK);
280}
281
0225bed3
CP
282static int lua_localchanmsg(lua_State *ps) {
283 char *msg;
284 nick *source;
285 channel *target;
286
287 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
288 LUA_RETURN(ps, LUA_FAIL);
289
290 source = getnickbynumeric(lua_tolong(ps, 1));
291 if(!source)
292 LUA_RETURN(ps, LUA_FAIL);
293
294 target = findchannel((char *)lua_tostring(ps, 2));
295 if(!target)
296 LUA_RETURN(ps, LUA_FAIL);
297
298 msg = (char *)lua_tostring(ps, 3);
299
300 if(!lua_lineok(msg))
301 LUA_RETURN(ps, LUA_FAIL);
302
303 sendmessagetochannel(source, target, "%s", msg);
304
305 LUA_RETURN(ps, LUA_OK);
306}
307
308static int lua_localnotice(lua_State *ps) {
309 char *msg;
310 nick *source;
311 nick *target;
312
313 if(!lua_islong(ps, 1) || !lua_islong(ps, 2) || !lua_isstring(ps, 3))
314 LUA_RETURN(ps, LUA_FAIL);
315
316 source = getnickbynumeric(lua_tolong(ps, 1));
317 if(!source)
318 LUA_RETURN(ps, LUA_FAIL);
319
320 target = getnickbynumeric(lua_tolong(ps, 2));
321 if(!target)
322 LUA_RETURN(ps, LUA_FAIL);
323
324 msg = (char *)lua_tostring(ps, 3);
325
326 if(!lua_lineok(msg))
327 LUA_RETURN(ps, LUA_FAIL);
328
329 sendnoticetouser(source, target, "%s", msg);
330
331 LUA_RETURN(ps, LUA_OK);
332}
333
34552caa
CP
334static int lua_localprivmsg(lua_State *ps) {
335 char *msg;
336 nick *source;
337 nick *target;
338
339 if(!lua_islong(ps, 1) || !lua_islong(ps, 2) || !lua_isstring(ps, 3))
340 LUA_RETURN(ps, LUA_FAIL);
341
342 source = getnickbynumeric(lua_tolong(ps, 1));
343 if(!source)
344 LUA_RETURN(ps, LUA_FAIL);
345
346 target = getnickbynumeric(lua_tolong(ps, 2));
347 if(!target)
348 LUA_RETURN(ps, LUA_FAIL);
349
350 msg = (char *)lua_tostring(ps, 3);
351
352 if(!lua_lineok(msg))
353 LUA_RETURN(ps, LUA_FAIL);
354
355 sendmessagetouser(source, target, "%s", msg);
356
357 LUA_RETURN(ps, LUA_OK);
358}
359
533af02d
CP
360static int lua_localovmode(lua_State *l) {
361 nick *source;
ba436ecf 362 channel *chan;
09cb349b 363 int state = 0, add = 0, realmode = 0, ignoring = 0;
533af02d 364 modechanges changes;
ba436ecf 365
533af02d
CP
366 if(!lua_islong(l, 1) || !lua_isstring(l, 2) || !lua_istable(l, 3))
367 LUA_RETURN(l, LUA_FAIL);
ba436ecf 368
533af02d 369 source = getnickbynumeric(lua_tolong(l, 1));
ba436ecf 370 if(!source)
533af02d 371 LUA_RETURN(l, LUA_FAIL);
ba436ecf 372
533af02d 373 chan = findchannel((char *)lua_tostring(l, 2));
ba436ecf 374 if(!chan)
533af02d 375 LUA_RETURN(l, LUA_FAIL);
ba436ecf 376
533af02d 377 localsetmodeinit(&changes, chan, source);
ba436ecf 378
533af02d 379 lua_pushnil(l);
ba436ecf 380
533af02d
CP
381 while(lua_next(l, 3)) {
382 if(state == 0) {
383 ignoring = 0;
384
385 if(!lua_isboolean(l, -1)) {
386 ignoring = 1;
387 } else {
388 add = (int)lua_toboolean(l, -1);
389 }
390 } else if((state == 1) && !ignoring) {
391 if(!lua_isstring(l, -1)) {
392 ignoring = 1;
393 } else {
394 char *mode = (char *)lua_tostring(l, -1);
395 if((*mode == 'o') && add) {
396 realmode = MC_OP;
397 } else if (*mode == 'o') {
398 realmode = MC_DEOP;
399 } else if((*mode == 'v') && add) {
400 realmode = MC_VOICE;
401 } else if (*mode == 'v') {
402 realmode = MC_DEVOICE;
403 } else {
404 ignoring = 1;
405 }
406 }
407 } else if((state == 2) && !ignoring) {
408 if(lua_islong(l, -1)) {
409 nick *target = getnickbynumeric(lua_tolong(l, -1));
410 if(target)
411 localdosetmode_nick(&changes, target, realmode);
412 }
413 }
414
415 lua_pop(l, 1);
416
417 state = (state + 1) % 3;
ba436ecf
CP
418 }
419
533af02d 420 localsetmodeflush(&changes, 1);
ba436ecf 421
533af02d 422 LUA_RETURN(l, LUA_OK);
ba436ecf
CP
423}
424
eac66732
CP
425static int lua_localumodes(lua_State *ps) {
426 nick *np;
427 char *modes;
428 flag_t newmodes = 0;
429
430 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
431 LUA_RETURN(ps, LUA_FAIL);
432
433 np = getnickbynumeric(lua_tolong(ps, 1));
434 if(!np)
435 LUA_RETURN(ps, LUA_FAIL);
436
437 modes = (char *)lua_tostring(ps, 2);
438
439 setflags(&newmodes, UMODE_ALL, modes, umodeflags, REJECT_NONE);
440
441 localusersetumodes(np, newmodes);
442 LUA_RETURN(ps, LUA_OK);
443}
444
f9f8ee13
CP
445static int lua_localtopic(lua_State *ps) {
446 nick *np;
447 channel *cp;
448 char *topic;
449
450 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
451 LUA_RETURN(ps, LUA_FAIL);
452
453 np = getnickbynumeric(lua_tolong(ps, 1));
454 if(!np)
455 LUA_RETURN(ps, LUA_FAIL);
456
457 cp = findchannel((char *)lua_tostring(ps, 2));
458 if(!cp)
459 LUA_RETURN(ps, LUA_FAIL);
460
461 topic = (char *)lua_tostring(ps, 3);
462 if(!topic || !lua_lineok(topic))
463 LUA_RETURN(ps, LUA_FAIL);
464
465 localsettopic(np, cp, topic);
466
467 LUA_RETURN(ps, LUA_OK);
468}
469
c0d574fa
CP
470static int lua_localban(lua_State *ps) {
471 channel *cp;
472 const char *mask;
473 modechanges changes;
474 nick *source;
475
476 int dir = MCB_ADD;
477
478 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
479 LUA_RETURN(ps, LUA_FAIL);
480
481 if(lua_isboolean(ps, 4) && lua_toboolean(ps, 4))
482 dir = MCB_DEL;
483
484 source = getnickbynumeric(lua_tolong(ps, 1));
485
486 cp = findchannel((char *)lua_tostring(ps, 2));
487 if(!cp)
488 LUA_RETURN(ps, LUA_FAIL);
489
490 mask = lua_tostring(ps, 3);
491 if(!mask || !mask[0] || !lua_lineok(mask))
492 LUA_RETURN(ps, LUA_FAIL);
493
494 localsetmodeinit(&changes, cp, source);
495 localdosetmode_ban(&changes, mask, dir);
496 localsetmodeflush(&changes, 1);
497
498 LUA_RETURN(ps, LUA_OK);
499}
500
501static int lua_localkick(lua_State *ps) {
502 const char *n, *msg, *chan;
503 nick *source, *np;
504 channel *cp;
505 int dochecks = 1;
506
507 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3) || !lua_isstring(ps, 4))
508 LUA_RETURN(ps, LUA_FAIL);
509
510 source = getnickbynumeric(lua_tolong(ps, 1));
511 chan = lua_tostring(ps, 2);
512 n = lua_tostring(ps, 3);
513 msg = lua_tostring(ps, 4);
514 if(!source)
515 LUA_RETURN(ps, LUA_FAIL);
516
517 if(lua_isboolean(ps, 4) && !lua_toboolean(ps, 4))
518 dochecks = 0;
519
520 np = getnickbynick(n);
521 if(!np)
522 LUA_RETURN(ps, LUA_FAIL);
523
524 if(dochecks && (IsOper(np) || IsXOper(np) || IsService(np)))
525 LUA_RETURN(ps, LUA_FAIL);
526
527 cp = findchannel((char *)chan);
528 if(!cp)
529 LUA_RETURN(ps, LUA_FAIL);
530
531 if(!lua_lineok(msg))
532 LUA_RETURN(ps, LUA_FAIL);
533
534 localkickuser(source, cp, np, msg);
535
536 LUA_RETURN(ps, LUA_OK);
537}
538
c0088ccb
CP
539static int lua_localrename(lua_State *ps) {
540 nick *np;
541 char *changeto;
542
543 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) )
544 LUA_RETURN(ps, LUA_FAIL);
545
546 np = getnickbynumeric(lua_tolong(ps, 1));
547 changeto = (char *)lua_tostring(ps, 2);
548
549 if(!lua_lineok(changeto))
550 LUA_RETURN(ps, LUA_FAIL);
551
552 renamelocaluser(np, changeto);
553
554 LUA_RETURN(ps, LUA_OK);
555}
556
0225bed3 557void lua_registerlocalcommands(lua_State *l) {
5541f23c 558 lua_register(l, "irc_localregisteruserid", lua_registerlocaluserid);
0225bed3
CP
559 lua_register(l, "irc_localderegisteruser", lua_deregisterlocaluser);
560 lua_register(l, "irc_localjoin", lua_localjoin);
b7252b44 561 lua_register(l, "irc_localpart", lua_localpart);
0225bed3
CP
562 lua_register(l, "irc_localchanmsg", lua_localchanmsg);
563 lua_register(l, "irc_localnotice", lua_localnotice);
34552caa 564 lua_register(l, "irc_localprivmsg", lua_localprivmsg);
ba436ecf
CP
565
566 lua_register(l, "irc_localovmode", lua_localovmode);
f9f8ee13 567 lua_register(l, "irc_localtopic", lua_localtopic);
ba436ecf 568
c0d574fa
CP
569 lua_register(l, "irc_localban", lua_localban);
570 lua_register(l, "irc_localkick", lua_localkick);
eac66732 571 lua_register(l, "irc_localumodes", lua_localumodes);
c0088ccb
CP
572
573 lua_register(l, "irc_localrename", lua_localrename);
0225bed3
CP
574}
575