]> jfr.im git - irc/quakenet/newserv.git/blame - lua/lualocal.c
This code never worked\!
[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
16static int lua_registerlocaluser(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;
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
eda80ef2 57 lua_pushlong(ps, ln->nick->numeric);
0225bed3
CP
58 return 1;
59}
60
61void 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
73int 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
88static 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
3f1c5f43
CP
97 numeric = lua_tolong(ps, 1);
98
0225bed3
CP
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) {
9315931c
CP
105 if(lp) {
106 lp->next = l2->next;
107 } else {
108 l->nicks = l2->next;
109 }
110
0225bed3
CP
111 lua_freelocalnick(ps, l2, quitm);
112 LUA_RETURN(ps, LUA_OK);
113 }
114 }
115
116 LUA_RETURN(ps, LUA_FAIL);
117}
118
119void 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 */
132void lua_localnickhandler(nick *target, int type, void **args) {
133 nick *np;
134 char *p;
135 lua_localnick *ln;
136 lua_list *l;
e8b79634 137 channel *c;
0225bed3
CP
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
e8b79634
CP
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
0225bed3
CP
165 case LU_KILLED:
166 lua_vlpcall(l, ln, "irc_onkilled", "");
167
168 strlcpy(ln->nickname, target->nick, sizeof(ln->nickname));
169 strlcpy(ln->ident, target->ident, sizeof(ln->ident));
170 strlcpy(ln->hostname, target->host->name->content, sizeof(ln->hostname));
171 strlcpy(ln->realname, target->realname->name->content, sizeof(ln->realname));
172 strlcpy(ln->account, target->authname, sizeof(ln->account));
173
174 ln->umodes = target->umodes;
175 ln->nick = NULL;
176
177 ln->reconnect = scheduleoneshot(time(NULL) + 1, &lua_reconnectlocal, ln);
178
179 break;
180 }
181}
182
183void lua_reconnectlocal(void *arg) {
184 lua_list *l;
185 lua_localnick *ln = (lua_localnick *)arg;
186
187 ln->nick = registerlocaluser(ln->nickname, ln->ident, ln->hostname, ln->realname, ln->account, ln->umodes, &lua_localnickhandler);
188 if(!ln->nick) {
189 ln->reconnect = scheduleoneshot(time(NULL) + 1, &lua_reconnectlocal, ln);
190 return;
191 }
192
193 ln->reconnect = NULL;
194
195 if(lua_getlocalnickbynick(ln->nick, &l, &ln)) /* hacky! */
196 lua_vlpcall(l, ln, "irc_onkillreconnect", "");
197}
198
199static int lua_localjoin(lua_State *ps) {
200 nick *source;
201 channel *target;
202 char *chan;
203
204 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
205 LUA_RETURN(ps, LUA_FAIL);
206
207 source = getnickbynumeric(lua_tolong(ps, 1));
208 if(!source)
209 LUA_RETURN(ps, LUA_FAIL);
210
211 chan = (char *)lua_tostring(ps, 2);
212
213 if(!lua_lineok(chan))
214 LUA_RETURN(ps, LUA_FAIL);
215
216 target = findchannel(chan);
217 if(target) {
218 localjoinchannel(source, target);
219 } else {
e8b79634 220 localcreatechannel(source, chan);
0225bed3
CP
221 }
222
223 LUA_RETURN(ps, LUA_OK);
224}
225
b7252b44
CP
226static int lua_localpart(lua_State *ps) {
227 nick *source;
228 channel *target;
229 char *chan;
230
231 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2))
232 LUA_RETURN(ps, LUA_FAIL);
233
234 source = getnickbynumeric(lua_tolong(ps, 1));
235 if(!source)
236 LUA_RETURN(ps, LUA_FAIL);
237
238 chan = (char *)lua_tostring(ps, 2);
239
240 if(!lua_lineok(chan))
241 LUA_RETURN(ps, LUA_FAIL);
242
243 target = findchannel(chan);
244 if(target) {
245 localpartchannel(source, target);
246 } else {
247 LUA_RETURN(ps, LUA_FAIL);
248 }
249
250 LUA_RETURN(ps, LUA_OK);
251}
252
0225bed3
CP
253static int lua_localchanmsg(lua_State *ps) {
254 char *msg;
255 nick *source;
256 channel *target;
257
258 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
259 LUA_RETURN(ps, LUA_FAIL);
260
261 source = getnickbynumeric(lua_tolong(ps, 1));
262 if(!source)
263 LUA_RETURN(ps, LUA_FAIL);
264
265 target = findchannel((char *)lua_tostring(ps, 2));
266 if(!target)
267 LUA_RETURN(ps, LUA_FAIL);
268
269 msg = (char *)lua_tostring(ps, 3);
270
271 if(!lua_lineok(msg))
272 LUA_RETURN(ps, LUA_FAIL);
273
274 sendmessagetochannel(source, target, "%s", msg);
275
276 LUA_RETURN(ps, LUA_OK);
277}
278
279static int lua_localnotice(lua_State *ps) {
280 char *msg;
281 nick *source;
282 nick *target;
283
284 if(!lua_islong(ps, 1) || !lua_islong(ps, 2) || !lua_isstring(ps, 3))
285 LUA_RETURN(ps, LUA_FAIL);
286
287 source = getnickbynumeric(lua_tolong(ps, 1));
288 if(!source)
289 LUA_RETURN(ps, LUA_FAIL);
290
291 target = getnickbynumeric(lua_tolong(ps, 2));
292 if(!target)
293 LUA_RETURN(ps, LUA_FAIL);
294
295 msg = (char *)lua_tostring(ps, 3);
296
297 if(!lua_lineok(msg))
298 LUA_RETURN(ps, LUA_FAIL);
299
300 sendnoticetouser(source, target, "%s", msg);
301
302 LUA_RETURN(ps, LUA_OK);
303}
304
533af02d
CP
305static int lua_localovmode(lua_State *l) {
306 nick *source;
ba436ecf 307 channel *chan;
533af02d
CP
308 int state = 0, add, realmode, ignoring = 0;
309 modechanges changes;
ba436ecf 310
533af02d
CP
311 if(!lua_islong(l, 1) || !lua_isstring(l, 2) || !lua_istable(l, 3))
312 LUA_RETURN(l, LUA_FAIL);
ba436ecf 313
533af02d 314 source = getnickbynumeric(lua_tolong(l, 1));
ba436ecf 315 if(!source)
533af02d 316 LUA_RETURN(l, LUA_FAIL);
ba436ecf 317
533af02d 318 chan = findchannel((char *)lua_tostring(l, 2));
ba436ecf 319 if(!chan)
533af02d 320 LUA_RETURN(l, LUA_FAIL);
ba436ecf 321
533af02d 322 localsetmodeinit(&changes, chan, source);
ba436ecf 323
533af02d 324 lua_pushnil(l);
ba436ecf 325
533af02d
CP
326 while(lua_next(l, 3)) {
327 if(state == 0) {
328 ignoring = 0;
329
330 if(!lua_isboolean(l, -1)) {
331 ignoring = 1;
332 } else {
333 add = (int)lua_toboolean(l, -1);
334 }
335 } else if((state == 1) && !ignoring) {
336 if(!lua_isstring(l, -1)) {
337 ignoring = 1;
338 } else {
339 char *mode = (char *)lua_tostring(l, -1);
340 if((*mode == 'o') && add) {
341 realmode = MC_OP;
342 } else if (*mode == 'o') {
343 realmode = MC_DEOP;
344 } else if((*mode == 'v') && add) {
345 realmode = MC_VOICE;
346 } else if (*mode == 'v') {
347 realmode = MC_DEVOICE;
348 } else {
349 ignoring = 1;
350 }
351 }
352 } else if((state == 2) && !ignoring) {
353 if(lua_islong(l, -1)) {
354 nick *target = getnickbynumeric(lua_tolong(l, -1));
355 if(target)
356 localdosetmode_nick(&changes, target, realmode);
357 }
358 }
359
360 lua_pop(l, 1);
361
362 state = (state + 1) % 3;
ba436ecf
CP
363 }
364
533af02d 365 localsetmodeflush(&changes, 1);
ba436ecf 366
533af02d 367 LUA_RETURN(l, LUA_OK);
ba436ecf
CP
368}
369
f9f8ee13
CP
370static int lua_localtopic(lua_State *ps) {
371 nick *np;
372 channel *cp;
373 char *topic;
374
375 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
376 LUA_RETURN(ps, LUA_FAIL);
377
378 np = getnickbynumeric(lua_tolong(ps, 1));
379 if(!np)
380 LUA_RETURN(ps, LUA_FAIL);
381
382 cp = findchannel((char *)lua_tostring(ps, 2));
383 if(!cp)
384 LUA_RETURN(ps, LUA_FAIL);
385
386 topic = (char *)lua_tostring(ps, 3);
387 if(!topic || !lua_lineok(topic))
388 LUA_RETURN(ps, LUA_FAIL);
389
390 localsettopic(np, cp, topic);
391
392 LUA_RETURN(ps, LUA_OK);
393}
394
c0d574fa
CP
395static int lua_localban(lua_State *ps) {
396 channel *cp;
397 const char *mask;
398 modechanges changes;
399 nick *source;
400
401 int dir = MCB_ADD;
402
403 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3))
404 LUA_RETURN(ps, LUA_FAIL);
405
406 if(lua_isboolean(ps, 4) && lua_toboolean(ps, 4))
407 dir = MCB_DEL;
408
409 source = getnickbynumeric(lua_tolong(ps, 1));
410
411 cp = findchannel((char *)lua_tostring(ps, 2));
412 if(!cp)
413 LUA_RETURN(ps, LUA_FAIL);
414
415 mask = lua_tostring(ps, 3);
416 if(!mask || !mask[0] || !lua_lineok(mask))
417 LUA_RETURN(ps, LUA_FAIL);
418
419 localsetmodeinit(&changes, cp, source);
420 localdosetmode_ban(&changes, mask, dir);
421 localsetmodeflush(&changes, 1);
422
423 LUA_RETURN(ps, LUA_OK);
424}
425
426static int lua_localkick(lua_State *ps) {
427 const char *n, *msg, *chan;
428 nick *source, *np;
429 channel *cp;
430 int dochecks = 1;
431
432 if(!lua_islong(ps, 1) || !lua_isstring(ps, 2) || !lua_isstring(ps, 3) || !lua_isstring(ps, 4))
433 LUA_RETURN(ps, LUA_FAIL);
434
435 source = getnickbynumeric(lua_tolong(ps, 1));
436 chan = lua_tostring(ps, 2);
437 n = lua_tostring(ps, 3);
438 msg = lua_tostring(ps, 4);
439 if(!source)
440 LUA_RETURN(ps, LUA_FAIL);
441
442 if(lua_isboolean(ps, 4) && !lua_toboolean(ps, 4))
443 dochecks = 0;
444
445 np = getnickbynick(n);
446 if(!np)
447 LUA_RETURN(ps, LUA_FAIL);
448
449 if(dochecks && (IsOper(np) || IsXOper(np) || IsService(np)))
450 LUA_RETURN(ps, LUA_FAIL);
451
452 cp = findchannel((char *)chan);
453 if(!cp)
454 LUA_RETURN(ps, LUA_FAIL);
455
456 if(!lua_lineok(msg))
457 LUA_RETURN(ps, LUA_FAIL);
458
459 localkickuser(source, cp, np, msg);
460
461 LUA_RETURN(ps, LUA_OK);
462}
463
0225bed3
CP
464void lua_registerlocalcommands(lua_State *l) {
465 lua_register(l, "irc_localregisteruser", lua_registerlocaluser);
466 lua_register(l, "irc_localderegisteruser", lua_deregisterlocaluser);
467 lua_register(l, "irc_localjoin", lua_localjoin);
b7252b44 468 lua_register(l, "irc_localpart", lua_localpart);
0225bed3
CP
469 lua_register(l, "irc_localchanmsg", lua_localchanmsg);
470 lua_register(l, "irc_localnotice", lua_localnotice);
ba436ecf
CP
471
472 lua_register(l, "irc_localovmode", lua_localovmode);
f9f8ee13 473 lua_register(l, "irc_localtopic", lua_localtopic);
ba436ecf 474
c0d574fa
CP
475 lua_register(l, "irc_localban", lua_localban);
476 lua_register(l, "irc_localkick", lua_localkick);
0225bed3
CP
477}
478