]> jfr.im git - irc/quakenet/newserv.git/blob - lua/lualocal.c
Ooops, merged.
[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_registerlocaluser(lua_State *ps) {
17 lua_list *l;
18 lua_localnick *ln;
19 char *nickname, *ident, *hostname, *realname, *account;
20 flag_t modes = 0;
21
22 if(!lua_isstring(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3) || !lua_isstring(ps, 4) || !lua_isstring(ps, 5) || !lua_isstring(ps, 6) || !lua_isfunction(ps, 7))
23 return 0;
24
25 nickname = (char *)lua_tostring(ps, 1);
26 ident = (char *)lua_tostring(ps, 2);
27 hostname = (char *)lua_tostring(ps, 3);
28 realname = (char *)lua_tostring(ps, 4);
29 account = (char *)lua_tostring(ps, 5);
30
31 setflags(&modes, UMODE_ALL, (char *)lua_tostring(ps, 6), umodeflags, REJECT_NONE);
32
33 if(!lua_lineok(nickname) || !lua_lineok(ident) || !lua_lineok(hostname) || !lua_lineok(realname) || !lua_lineok(account))
34 return 0;
35
36 l = lua_listfromstate(ps);
37 if(!l)
38 return 0;
39
40 ln = (lua_localnick *)malloc(sizeof(lua_localnick));
41 if(!ln)
42 return 0;
43
44 ln->reconnect = NULL;
45
46 ln->nick = registerlocaluser(nickname, ident, hostname, realname, account, modes, &lua_localnickhandler);
47 if(!ln->nick) {
48 free(ln);
49 return 0;
50 }
51
52 ln->handler = luaL_ref(ps, LUA_REGISTRYINDEX);
53
54 ln->next = l->nicks;
55 l->nicks = ln;
56
57 lua_pushlong(ps, ln->nick->numeric);
58 return 1;
59 }
60
61 void lua_freelocalnick(lua_State *ps, lua_localnick *l, char *quitm) {
62 if(l->nick)
63 deregisterlocaluser(l->nick, quitm);
64
65 if(l->reconnect)
66 deleteschedule(l->reconnect, &lua_reconnectlocal, l);
67
68 luaL_unref(ps, LUA_REGISTRYINDEX, l->handler);
69
70 free(l);
71 }
72
73 int lua_getlocalnickbynick(nick *np, lua_list **rl, lua_localnick **rln) {
74 lua_list *l;
75 lua_localnick *ln;
76
77 for(l=lua_head;l;l=l->next)
78 for(ln=l->nicks;ln;ln=ln->next)
79 if(ln->nick == np) {
80 *rl = l;
81 *rln = ln;
82 return 1;
83 }
84
85 return 0;
86 }
87
88 static int lua_deregisterlocaluser(lua_State *ps) {
89 lua_list *l;
90 lua_localnick *l2, *lp = NULL;
91 long numeric;
92 char *quitm;
93
94 if(!lua_islong(ps, 1))
95 LUA_RETURN(ps, LUA_FAIL);
96
97 numeric = lua_tolong(ps, 1);
98
99 quitm = lua_isstring(ps, 2)?(char *)lua_tostring(ps, 2):"localuser unregistered.";
100
101 l = lua_listfromstate(ps);
102
103 for(l2=l->nicks;l2;lp=l2,l2=l2->next) {
104 if(l2->nick->numeric == numeric) {
105 if(lp) {
106 lp->next = l2->next;
107 } else {
108 l->nicks = l2->next;
109 }
110
111 lua_freelocalnick(ps, l2, quitm);
112 LUA_RETURN(ps, LUA_OK);
113 }
114 }
115
116 LUA_RETURN(ps, LUA_FAIL);
117 }
118
119 void lua_deregisternicks(lua_list *l) {
120 struct lua_localnick *ln, *pn;
121
122 for(ln=l->nicks;ln;ln=pn) {
123 pn = ln->next;
124
125 lua_freelocalnick(l->l, ln, "Script unloaded.");
126 }
127
128 l->nicks = NULL;
129 }
130
131 /* todo */
132 void lua_localnickhandler(nick *target, int type, void **args) {
133 nick *np;
134 char *p;
135 lua_localnick *ln;
136 lua_list *l;
137 channel *c;
138
139 if(!lua_getlocalnickbynick(target, &l, &ln))
140 return;
141
142 switch(type) {
143 case LU_PRIVMSG:
144 np = (nick *)args[0];
145 p = (char *)args[1];
146
147 if(!np || !p)
148 return;
149
150 lua_vlpcall(l, ln, "irc_onmsg", "Ns", np, p);
151
152 break;
153
154 case LU_CHANMSG:
155 np = (nick *)args[0];
156 c = (channel *)args[1];
157 p = (char *)args[2];
158
159 if(!np || !p || !c || !c->index || !c->index->name || !c->index->name->content)
160 return;
161
162 lua_vlpcall(l, ln, "irc_onchanmsg", "Nss", np, c->index->name->content, p);
163 break;
164
165 case LU_KILLED:
166 p = (char *)args[2];
167 lua_vlpcall(l, ln, "irc_onkilled", "s", p);
168
169 strlcpy(ln->nickname, target->nick, sizeof(ln->nickname));
170 strlcpy(ln->ident, target->ident, sizeof(ln->ident));
171 strlcpy(ln->hostname, target->host->name->content, sizeof(ln->hostname));
172 strlcpy(ln->realname, target->realname->name->content, sizeof(ln->realname));
173 strlcpy(ln->account, target->authname, sizeof(ln->account));
174
175 ln->umodes = target->umodes;
176 ln->nick = NULL;
177
178 ln->reconnect = scheduleoneshot(time(NULL) + 1, &lua_reconnectlocal, ln);
179
180 break;
181 case LU_INVITE:
182 /* we were invited, check if someone invited us to PUBLICCHAN */
183 np = (nick *)args[0];
184 c = (channel *)args[1];
185
186 if(!c || !np || !c->index || !c->index->name || !c->index->name->content)
187 return;
188
189 lua_vlpcall(l, ln, "irc_oninvite", "Ns", np, c->index->name->content);
190 break;
191 }
192 }
193
194 void lua_reconnectlocal(void *arg) {
195 lua_list *l;
196 lua_localnick *ln = (lua_localnick *)arg;
197
198 ln->nick = registerlocaluser(ln->nickname, ln->ident, ln->hostname, ln->realname, ln->account, ln->umodes, &lua_localnickhandler);
199 if(!ln->nick) {
200 ln->reconnect = scheduleoneshot(time(NULL) + 1, &lua_reconnectlocal, ln);
201 return;
202 }
203
204 ln->reconnect = NULL;
205
206 if(lua_getlocalnickbynick(ln->nick, &l, &ln)) /* hacky! */
207 lua_vlpcall(l, ln, "irc_onkillreconnect", "");
208 }
209
210 static int lua_localjoin(lua_State *ps) {
211 nick *source;
212 channel *target;
213 char *chan;
214
215 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
216 LUA_RETURN(ps, LUA_FAIL);
217
218 source = getnickbynumeric(lua_tolong(ps, 1));
219 if(!source)
220 LUA_RETURN(ps, LUA_FAIL);
221
222 chan = (char *)lua_tostring(ps, 2);
223
224 if(!lua_lineok(chan))
225 LUA_RETURN(ps, LUA_FAIL);
226
227 target = findchannel(chan);
228 if(target) {
229 localjoinchannel(source, target);
230 } else {
231 localcreatechannel(source, chan);
232 }
233
234 LUA_RETURN(ps, LUA_OK);
235 }
236
237 static int lua_localpart(lua_State *ps) {
238 nick *source;
239 channel *target;
240 char *chan;
241
242 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
243 LUA_RETURN(ps, LUA_FAIL);
244
245 source = getnickbynumeric(lua_tolong(ps, 1));
246 if(!source)
247 LUA_RETURN(ps, LUA_FAIL);
248
249 chan = (char *)lua_tostring(ps, 2);
250
251 if(!lua_lineok(chan))
252 LUA_RETURN(ps, LUA_FAIL);
253
254 target = findchannel(chan);
255 if(target) {
256 localpartchannel(source, target);
257 } else {
258 LUA_RETURN(ps, LUA_FAIL);
259 }
260
261 LUA_RETURN(ps, LUA_OK);
262 }
263
264 static int lua_localchanmsg(lua_State *ps) {
265 char *msg;
266 nick *source;
267 channel *target;
268
269 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
270 LUA_RETURN(ps, LUA_FAIL);
271
272 source = getnickbynumeric(lua_tolong(ps, 1));
273 if(!source)
274 LUA_RETURN(ps, LUA_FAIL);
275
276 target = findchannel((char *)lua_tostring(ps, 2));
277 if(!target)
278 LUA_RETURN(ps, LUA_FAIL);
279
280 msg = (char *)lua_tostring(ps, 3);
281
282 if(!lua_lineok(msg))
283 LUA_RETURN(ps, LUA_FAIL);
284
285 sendmessagetochannel(source, target, "%s", msg);
286
287 LUA_RETURN(ps, LUA_OK);
288 }
289
290 static int lua_localnotice(lua_State *ps) {
291 char *msg;
292 nick *source;
293 nick *target;
294
295 if(!lua_islong(ps, 1) || !lua_islong(ps, 2) || !lua_isstring(ps, 3))
296 LUA_RETURN(ps, LUA_FAIL);
297
298 source = getnickbynumeric(lua_tolong(ps, 1));
299 if(!source)
300 LUA_RETURN(ps, LUA_FAIL);
301
302 target = getnickbynumeric(lua_tolong(ps, 2));
303 if(!target)
304 LUA_RETURN(ps, LUA_FAIL);
305
306 msg = (char *)lua_tostring(ps, 3);
307
308 if(!lua_lineok(msg))
309 LUA_RETURN(ps, LUA_FAIL);
310
311 sendnoticetouser(source, target, "%s", msg);
312
313 LUA_RETURN(ps, LUA_OK);
314 }
315
316 static int lua_localprivmsg(lua_State *ps) {
317 char *msg;
318 nick *source;
319 nick *target;
320
321 if(!lua_islong(ps, 1) || !lua_islong(ps, 2) || !lua_isstring(ps, 3))
322 LUA_RETURN(ps, LUA_FAIL);
323
324 source = getnickbynumeric(lua_tolong(ps, 1));
325 if(!source)
326 LUA_RETURN(ps, LUA_FAIL);
327
328 target = getnickbynumeric(lua_tolong(ps, 2));
329 if(!target)
330 LUA_RETURN(ps, LUA_FAIL);
331
332 msg = (char *)lua_tostring(ps, 3);
333
334 if(!lua_lineok(msg))
335 LUA_RETURN(ps, LUA_FAIL);
336
337 sendmessagetouser(source, target, "%s", msg);
338
339 LUA_RETURN(ps, LUA_OK);
340 }
341
342 static int lua_localovmode(lua_State *l) {
343 nick *source;
344 channel *chan;
345 int state = 0, add, realmode, ignoring = 0;
346 modechanges changes;
347
348 if(!lua_islong(l, 1) || !lua_isstring(l, 2) || !lua_istable(l, 3))
349 LUA_RETURN(l, LUA_FAIL);
350
351 source = getnickbynumeric(lua_tolong(l, 1));
352 if(!source)
353 LUA_RETURN(l, LUA_FAIL);
354
355 chan = findchannel((char *)lua_tostring(l, 2));
356 if(!chan)
357 LUA_RETURN(l, LUA_FAIL);
358
359 localsetmodeinit(&changes, chan, source);
360
361 lua_pushnil(l);
362
363 while(lua_next(l, 3)) {
364 if(state == 0) {
365 ignoring = 0;
366
367 if(!lua_isboolean(l, -1)) {
368 ignoring = 1;
369 } else {
370 add = (int)lua_toboolean(l, -1);
371 }
372 } else if((state == 1) && !ignoring) {
373 if(!lua_isstring(l, -1)) {
374 ignoring = 1;
375 } else {
376 char *mode = (char *)lua_tostring(l, -1);
377 if((*mode == 'o') && add) {
378 realmode = MC_OP;
379 } else if (*mode == 'o') {
380 realmode = MC_DEOP;
381 } else if((*mode == 'v') && add) {
382 realmode = MC_VOICE;
383 } else if (*mode == 'v') {
384 realmode = MC_DEVOICE;
385 } else {
386 ignoring = 1;
387 }
388 }
389 } else if((state == 2) && !ignoring) {
390 if(lua_islong(l, -1)) {
391 nick *target = getnickbynumeric(lua_tolong(l, -1));
392 if(target)
393 localdosetmode_nick(&changes, target, realmode);
394 }
395 }
396
397 lua_pop(l, 1);
398
399 state = (state + 1) % 3;
400 }
401
402 localsetmodeflush(&changes, 1);
403
404 LUA_RETURN(l, LUA_OK);
405 }
406
407 static int lua_localtopic(lua_State *ps) {
408 nick *np;
409 channel *cp;
410 char *topic;
411
412 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
413 LUA_RETURN(ps, LUA_FAIL);
414
415 np = getnickbynumeric(lua_tolong(ps, 1));
416 if(!np)
417 LUA_RETURN(ps, LUA_FAIL);
418
419 cp = findchannel((char *)lua_tostring(ps, 2));
420 if(!cp)
421 LUA_RETURN(ps, LUA_FAIL);
422
423 topic = (char *)lua_tostring(ps, 3);
424 if(!topic || !lua_lineok(topic))
425 LUA_RETURN(ps, LUA_FAIL);
426
427 localsettopic(np, cp, topic);
428
429 LUA_RETURN(ps, LUA_OK);
430 }
431
432 static int lua_localban(lua_State *ps) {
433 channel *cp;
434 const char *mask;
435 modechanges changes;
436 nick *source;
437
438 int dir = MCB_ADD;
439
440 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
441 LUA_RETURN(ps, LUA_FAIL);
442
443 if(lua_isboolean(ps, 4) && lua_toboolean(ps, 4))
444 dir = MCB_DEL;
445
446 source = getnickbynumeric(lua_tolong(ps, 1));
447
448 cp = findchannel((char *)lua_tostring(ps, 2));
449 if(!cp)
450 LUA_RETURN(ps, LUA_FAIL);
451
452 mask = lua_tostring(ps, 3);
453 if(!mask || !mask[0] || !lua_lineok(mask))
454 LUA_RETURN(ps, LUA_FAIL);
455
456 localsetmodeinit(&changes, cp, source);
457 localdosetmode_ban(&changes, mask, dir);
458 localsetmodeflush(&changes, 1);
459
460 LUA_RETURN(ps, LUA_OK);
461 }
462
463 static int lua_localkick(lua_State *ps) {
464 const char *n, *msg, *chan;
465 nick *source, *np;
466 channel *cp;
467 int dochecks = 1;
468
469 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3) || !lua_isstring(ps, 4))
470 LUA_RETURN(ps, LUA_FAIL);
471
472 source = getnickbynumeric(lua_tolong(ps, 1));
473 chan = lua_tostring(ps, 2);
474 n = lua_tostring(ps, 3);
475 msg = lua_tostring(ps, 4);
476 if(!source)
477 LUA_RETURN(ps, LUA_FAIL);
478
479 if(lua_isboolean(ps, 4) && !lua_toboolean(ps, 4))
480 dochecks = 0;
481
482 np = getnickbynick(n);
483 if(!np)
484 LUA_RETURN(ps, LUA_FAIL);
485
486 if(dochecks && (IsOper(np) || IsXOper(np) || IsService(np)))
487 LUA_RETURN(ps, LUA_FAIL);
488
489 cp = findchannel((char *)chan);
490 if(!cp)
491 LUA_RETURN(ps, LUA_FAIL);
492
493 if(!lua_lineok(msg))
494 LUA_RETURN(ps, LUA_FAIL);
495
496 localkickuser(source, cp, np, msg);
497
498 LUA_RETURN(ps, LUA_OK);
499 }
500
501 void lua_registerlocalcommands(lua_State *l) {
502 lua_register(l, "irc_localregisteruser", lua_registerlocaluser);
503 lua_register(l, "irc_localderegisteruser", lua_deregisterlocaluser);
504 lua_register(l, "irc_localjoin", lua_localjoin);
505 lua_register(l, "irc_localpart", lua_localpart);
506 lua_register(l, "irc_localchanmsg", lua_localchanmsg);
507 lua_register(l, "irc_localnotice", lua_localnotice);
508 lua_register(l, "irc_localprivmsg", lua_localprivmsg);
509
510 lua_register(l, "irc_localovmode", lua_localovmode);
511 lua_register(l, "irc_localtopic", lua_localtopic);
512
513 lua_register(l, "irc_localban", lua_localban);
514 lua_register(l, "irc_localkick", lua_localkick);
515 }
516