]>
jfr.im git - irc/quakenet/newserv.git/blob - lua/lualocal.c
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"
13 void lua_localnickhandler(nick
*target
, int type
, void **args
);
14 void lua_reconnectlocal(void *arg
);
16 static int lua_registerlocaluser(lua_State
*ps
) {
19 char *nickname
, *ident
, *hostname
, *realname
, *account
;
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))
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);
31 setflags(&modes
, UMODE_ALL
, (char *)lua_tostring(ps
, 6), umodeflags
, REJECT_NONE
);
33 if(!lua_lineok(nickname
) || !lua_lineok(ident
) || !lua_lineok(hostname
) || !lua_lineok(realname
) || !lua_lineok(account
))
36 l
= lua_listfromstate(ps
);
40 ln
= (lua_localnick
*)luamalloc(sizeof(lua_localnick
));
46 ln
->nick
= registerlocaluser(nickname
, ident
, hostname
, realname
, account
, modes
, &lua_localnickhandler
);
52 ln
->handler
= luaL_ref(ps
, LUA_REGISTRYINDEX
);
57 lua_pushlong(ps
, ln
->nick
->numeric
);
61 void lua_freelocalnick(lua_State
*ps
, lua_localnick
*l
, char *quitm
) {
63 deregisterlocaluser(l
->nick
, quitm
);
66 deleteschedule(l
->reconnect
, &lua_reconnectlocal
, l
);
68 luaL_unref(ps
, LUA_REGISTRYINDEX
, l
->handler
);
73 int lua_getlocalnickbynick(nick
*np
, lua_list
**rl
, lua_localnick
**rln
) {
77 for(l
=lua_head
;l
;l
=l
->next
)
78 for(ln
=l
->nicks
;ln
;ln
=ln
->next
)
88 static int lua_deregisterlocaluser(lua_State
*ps
) {
90 lua_localnick
*l2
, *lp
= NULL
;
94 if(!lua_islong(ps
, 1))
95 LUA_RETURN(ps
, LUA_FAIL
);
97 numeric
= lua_tolong(ps
, 1);
99 quitm
= lua_isstring(ps
, 2)?(char *)lua_tostring(ps
, 2):"localuser unregistered.";
101 l
= lua_listfromstate(ps
);
103 for(l2
=l
->nicks
;l2
;lp
=l2
,l2
=l2
->next
) {
104 if(l2
->nick
&& l2
->nick
->numeric
== numeric
) {
111 lua_freelocalnick(ps
, l2
, quitm
);
112 LUA_RETURN(ps
, LUA_OK
);
116 LUA_RETURN(ps
, LUA_FAIL
);
119 void lua_deregisternicks(lua_list
*l
) {
120 struct lua_localnick
*ln
, *pn
;
127 lua_freelocalnick(l
->l
, ln
, "Script unloaded.");
134 void lua_localnickhandler(nick
*target
, int type
, void **args
) {
141 if(!lua_getlocalnickbynick(target
, &l
, &ln
))
146 np
= (nick
*)args
[0];
152 lua_vlpcall(l
, ln
, "irc_onmsg", "Ns", np
, p
);
157 np
= (nick
*)args
[0];
158 c
= (channel
*)args
[1];
161 if(!np
|| !p
|| !c
|| !c
->index
|| !c
->index
->name
|| !c
->index
->name
->content
)
164 lua_vlpcall(l
, ln
, "irc_onchanmsg", "Nss", np
, c
->index
->name
->content
, p
);
169 lua_vlpcall(l
, ln
, "irc_onkilled", "s", p
);
171 strlcpy(ln
->nickname
, target
->nick
, sizeof(ln
->nickname
));
172 strlcpy(ln
->ident
, target
->ident
, sizeof(ln
->ident
));
173 strlcpy(ln
->hostname
, target
->host
->name
->content
, sizeof(ln
->hostname
));
174 strlcpy(ln
->realname
, target
->realname
->name
->content
, sizeof(ln
->realname
));
175 strlcpy(ln
->account
, target
->authname
, sizeof(ln
->account
));
177 ln
->umodes
= target
->umodes
;
180 ln
->reconnect
= scheduleoneshot(time(NULL
) + 1, &lua_reconnectlocal
, ln
);
184 /* we were invited, check if someone invited us to PUBLICCHAN */
185 np
= (nick
*)args
[0];
186 c
= (channel
*)args
[1];
188 if(!c
|| !np
|| !c
->index
|| !c
->index
->name
|| !c
->index
->name
->content
)
191 lua_vlpcall(l
, ln
, "irc_oninvite", "Ns", np
, c
->index
->name
->content
);
196 void lua_reconnectlocal(void *arg
) {
198 lua_localnick
*ln
= (lua_localnick
*)arg
;
200 ln
->nick
= registerlocaluser(ln
->nickname
, ln
->ident
, ln
->hostname
, ln
->realname
, ln
->account
, ln
->umodes
, &lua_localnickhandler
);
202 ln
->reconnect
= scheduleoneshot(time(NULL
) + 1, &lua_reconnectlocal
, ln
);
206 ln
->reconnect
= NULL
;
208 if(lua_getlocalnickbynick(ln
->nick
, &l
, &ln
)) /* hacky! */
209 lua_vlpcall(l
, ln
, "irc_onkillreconnect", "");
212 static int lua_localjoin(lua_State
*ps
) {
217 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2))
218 LUA_RETURN(ps
, LUA_FAIL
);
220 source
= getnickbynumeric(lua_tolong(ps
, 1));
222 LUA_RETURN(ps
, LUA_FAIL
);
224 chan
= (char *)lua_tostring(ps
, 2);
226 LUA_RETURN(ps
, LUA_FAIL
);
228 if(!lua_lineok(chan
))
229 LUA_RETURN(ps
, LUA_FAIL
);
231 target
= findchannel(chan
);
233 localjoinchannel(source
, target
);
235 localcreatechannel(source
, chan
);
238 LUA_RETURN(ps
, LUA_OK
);
241 static int lua_localpart(lua_State
*ps
) {
246 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2))
247 LUA_RETURN(ps
, LUA_FAIL
);
249 source
= getnickbynumeric(lua_tolong(ps
, 1));
251 LUA_RETURN(ps
, LUA_FAIL
);
253 chan
= (char *)lua_tostring(ps
, 2);
255 if(!lua_lineok(chan
))
256 LUA_RETURN(ps
, LUA_FAIL
);
258 if(lua_isstring(ps
, 3)) {
259 reason
= (char *)lua_tostring(ps
, 3);
260 if(!lua_lineok(reason
))
261 LUA_RETURN(ps
, LUA_FAIL
);
266 target
= findchannel(chan
);
268 localpartchannel(source
, target
, reason
);
270 LUA_RETURN(ps
, LUA_FAIL
);
273 LUA_RETURN(ps
, LUA_OK
);
276 static int lua_localchanmsg(lua_State
*ps
) {
281 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
282 LUA_RETURN(ps
, LUA_FAIL
);
284 source
= getnickbynumeric(lua_tolong(ps
, 1));
286 LUA_RETURN(ps
, LUA_FAIL
);
288 target
= findchannel((char *)lua_tostring(ps
, 2));
290 LUA_RETURN(ps
, LUA_FAIL
);
292 msg
= (char *)lua_tostring(ps
, 3);
295 LUA_RETURN(ps
, LUA_FAIL
);
297 sendmessagetochannel(source
, target
, "%s", msg
);
299 LUA_RETURN(ps
, LUA_OK
);
302 static int lua_localnotice(lua_State
*ps
) {
307 if(!lua_islong(ps
, 1) || !lua_islong(ps
, 2) || !lua_isstring(ps
, 3))
308 LUA_RETURN(ps
, LUA_FAIL
);
310 source
= getnickbynumeric(lua_tolong(ps
, 1));
312 LUA_RETURN(ps
, LUA_FAIL
);
314 target
= getnickbynumeric(lua_tolong(ps
, 2));
316 LUA_RETURN(ps
, LUA_FAIL
);
318 msg
= (char *)lua_tostring(ps
, 3);
321 LUA_RETURN(ps
, LUA_FAIL
);
323 sendnoticetouser(source
, target
, "%s", msg
);
325 LUA_RETURN(ps
, LUA_OK
);
328 static int lua_localprivmsg(lua_State
*ps
) {
333 if(!lua_islong(ps
, 1) || !lua_islong(ps
, 2) || !lua_isstring(ps
, 3))
334 LUA_RETURN(ps
, LUA_FAIL
);
336 source
= getnickbynumeric(lua_tolong(ps
, 1));
338 LUA_RETURN(ps
, LUA_FAIL
);
340 target
= getnickbynumeric(lua_tolong(ps
, 2));
342 LUA_RETURN(ps
, LUA_FAIL
);
344 msg
= (char *)lua_tostring(ps
, 3);
347 LUA_RETURN(ps
, LUA_FAIL
);
349 sendmessagetouser(source
, target
, "%s", msg
);
351 LUA_RETURN(ps
, LUA_OK
);
354 static int lua_localovmode(lua_State
*l
) {
357 int state
= 0, add
= 0, realmode
= 0, ignoring
= 0;
360 if(!lua_islong(l
, 1) || !lua_isstring(l
, 2) || !lua_istable(l
, 3))
361 LUA_RETURN(l
, LUA_FAIL
);
363 source
= getnickbynumeric(lua_tolong(l
, 1));
365 LUA_RETURN(l
, LUA_FAIL
);
367 chan
= findchannel((char *)lua_tostring(l
, 2));
369 LUA_RETURN(l
, LUA_FAIL
);
371 localsetmodeinit(&changes
, chan
, source
);
375 while(lua_next(l
, 3)) {
379 if(!lua_isboolean(l
, -1)) {
382 add
= (int)lua_toboolean(l
, -1);
384 } else if((state
== 1) && !ignoring
) {
385 if(!lua_isstring(l
, -1)) {
388 char *mode
= (char *)lua_tostring(l
, -1);
389 if((*mode
== 'o') && add
) {
391 } else if (*mode
== 'o') {
393 } else if((*mode
== 'v') && add
) {
395 } else if (*mode
== 'v') {
396 realmode
= MC_DEVOICE
;
401 } else if((state
== 2) && !ignoring
) {
402 if(lua_islong(l
, -1)) {
403 nick
*target
= getnickbynumeric(lua_tolong(l
, -1));
405 localdosetmode_nick(&changes
, target
, realmode
);
411 state
= (state
+ 1) % 3;
414 localsetmodeflush(&changes
, 1);
416 LUA_RETURN(l
, LUA_OK
);
419 static int lua_localumodes(lua_State
*ps
) {
424 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2))
425 LUA_RETURN(ps
, LUA_FAIL
);
427 np
= getnickbynumeric(lua_tolong(ps
, 1));
429 LUA_RETURN(ps
, LUA_FAIL
);
431 modes
= (char *)lua_tostring(ps
, 2);
433 setflags(&newmodes
, UMODE_ALL
, modes
, umodeflags
, REJECT_NONE
);
435 localusersetumodes(np
, newmodes
);
436 LUA_RETURN(ps
, LUA_OK
);
439 static int lua_localtopic(lua_State
*ps
) {
444 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
445 LUA_RETURN(ps
, LUA_FAIL
);
447 np
= getnickbynumeric(lua_tolong(ps
, 1));
449 LUA_RETURN(ps
, LUA_FAIL
);
451 cp
= findchannel((char *)lua_tostring(ps
, 2));
453 LUA_RETURN(ps
, LUA_FAIL
);
455 topic
= (char *)lua_tostring(ps
, 3);
456 if(!topic
|| !lua_lineok(topic
))
457 LUA_RETURN(ps
, LUA_FAIL
);
459 localsettopic(np
, cp
, topic
);
461 LUA_RETURN(ps
, LUA_OK
);
464 static int lua_localban(lua_State
*ps
) {
472 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
473 LUA_RETURN(ps
, LUA_FAIL
);
475 if(lua_isboolean(ps
, 4) && lua_toboolean(ps
, 4))
478 source
= getnickbynumeric(lua_tolong(ps
, 1));
480 cp
= findchannel((char *)lua_tostring(ps
, 2));
482 LUA_RETURN(ps
, LUA_FAIL
);
484 mask
= lua_tostring(ps
, 3);
485 if(!mask
|| !mask
[0] || !lua_lineok(mask
))
486 LUA_RETURN(ps
, LUA_FAIL
);
488 localsetmodeinit(&changes
, cp
, source
);
489 localdosetmode_ban(&changes
, mask
, dir
);
490 localsetmodeflush(&changes
, 1);
492 LUA_RETURN(ps
, LUA_OK
);
495 static int lua_localkick(lua_State
*ps
) {
496 const char *n
, *msg
, *chan
;
501 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3) || !lua_isstring(ps
, 4))
502 LUA_RETURN(ps
, LUA_FAIL
);
504 source
= getnickbynumeric(lua_tolong(ps
, 1));
505 chan
= lua_tostring(ps
, 2);
506 n
= lua_tostring(ps
, 3);
507 msg
= lua_tostring(ps
, 4);
509 LUA_RETURN(ps
, LUA_FAIL
);
511 if(lua_isboolean(ps
, 4) && !lua_toboolean(ps
, 4))
514 np
= getnickbynick(n
);
516 LUA_RETURN(ps
, LUA_FAIL
);
518 if(dochecks
&& (IsOper(np
) || IsXOper(np
) || IsService(np
)))
519 LUA_RETURN(ps
, LUA_FAIL
);
521 cp
= findchannel((char *)chan
);
523 LUA_RETURN(ps
, LUA_FAIL
);
526 LUA_RETURN(ps
, LUA_FAIL
);
528 localkickuser(source
, cp
, np
, msg
);
530 LUA_RETURN(ps
, LUA_OK
);
533 static int lua_localrename(lua_State
*ps
) {
537 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) )
538 LUA_RETURN(ps
, LUA_FAIL
);
540 np
= getnickbynumeric(lua_tolong(ps
, 1));
541 changeto
= (char *)lua_tostring(ps
, 2);
543 if(!lua_lineok(changeto
))
544 LUA_RETURN(ps
, LUA_FAIL
);
546 renamelocaluser(np
, changeto
);
548 LUA_RETURN(ps
, LUA_OK
);
551 void lua_registerlocalcommands(lua_State
*l
) {
552 lua_register(l
, "irc_localregisteruser", lua_registerlocaluser
);
553 lua_register(l
, "irc_localderegisteruser", lua_deregisterlocaluser
);
554 lua_register(l
, "irc_localjoin", lua_localjoin
);
555 lua_register(l
, "irc_localpart", lua_localpart
);
556 lua_register(l
, "irc_localchanmsg", lua_localchanmsg
);
557 lua_register(l
, "irc_localnotice", lua_localnotice
);
558 lua_register(l
, "irc_localprivmsg", lua_localprivmsg
);
560 lua_register(l
, "irc_localovmode", lua_localovmode
);
561 lua_register(l
, "irc_localtopic", lua_localtopic
);
563 lua_register(l
, "irc_localban", lua_localban
);
564 lua_register(l
, "irc_localkick", lua_localkick
);
565 lua_register(l
, "irc_localumodes", lua_localumodes
);
567 lua_register(l
, "irc_localrename", lua_localrename
);