]>
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_registerlocaluserid(lua_State
*ps
) {
19 char *nickname
, *ident
, *hostname
, *realname
, *account
;
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))
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);
37 setflags(&modes
, UMODE_ALL
, (char *)lua_tostring(ps
, 7), umodeflags
, REJECT_NONE
);
39 if(!lua_lineok(nickname
) || !lua_lineok(ident
) || !lua_lineok(hostname
) || !lua_lineok(realname
) || !lua_lineok(account
))
42 l
= lua_listfromstate(ps
);
46 ln
= (lua_localnick
*)luamalloc(sizeof(lua_localnick
));
52 ln
->nick
= registerlocaluserflags(nickname
, ident
, hostname
, realname
, account
, userid
, 0, modes
, &lua_localnickhandler
);
58 ln
->handler
= luaL_ref(ps
, LUA_REGISTRYINDEX
);
63 lua_pushlong(ps
, ln
->nick
->numeric
);
67 void lua_freelocalnick(lua_State
*ps
, lua_localnick
*l
, char *quitm
) {
69 deregisterlocaluser(l
->nick
, quitm
);
72 deleteschedule(l
->reconnect
, &lua_reconnectlocal
, l
);
74 luaL_unref(ps
, LUA_REGISTRYINDEX
, l
->handler
);
79 int lua_getlocalnickbynick(nick
*np
, lua_list
**rl
, lua_localnick
**rln
) {
83 for(l
=lua_head
;l
;l
=l
->next
)
84 for(ln
=l
->nicks
;ln
;ln
=ln
->next
)
94 static int lua_deregisterlocaluser(lua_State
*ps
) {
96 lua_localnick
*l2
, *lp
= NULL
;
100 if(!lua_islong(ps
, 1))
101 LUA_RETURN(ps
, LUA_FAIL
);
103 numeric
= lua_tolong(ps
, 1);
105 quitm
= lua_isstring(ps
, 2)?(char *)lua_tostring(ps
, 2):"localuser unregistered.";
107 l
= lua_listfromstate(ps
);
109 for(l2
=l
->nicks
;l2
;lp
=l2
,l2
=l2
->next
) {
110 if(l2
->nick
&& l2
->nick
->numeric
== numeric
) {
117 lua_freelocalnick(ps
, l2
, quitm
);
118 LUA_RETURN(ps
, LUA_OK
);
122 LUA_RETURN(ps
, LUA_FAIL
);
125 void lua_deregisternicks(lua_list
*l
) {
126 struct lua_localnick
*ln
, *pn
;
133 lua_freelocalnick(l
->l
, ln
, "Script unloaded.");
140 void lua_localnickhandler(nick
*target
, int type
, void **args
) {
147 if(!lua_getlocalnickbynick(target
, &l
, &ln
))
152 np
= (nick
*)args
[0];
158 lua_vlpcall(l
, ln
, "irc_onmsg", "Ns", np
, p
);
163 np
= (nick
*)args
[0];
164 c
= (channel
*)args
[1];
167 if(!np
|| !p
|| !c
|| !c
->index
|| !c
->index
->name
|| !c
->index
->name
->content
)
170 lua_vlpcall(l
, ln
, "irc_onchanmsg", "Nss", np
, c
->index
->name
->content
, p
);
175 lua_vlpcall(l
, ln
, "irc_onkilled", "s", p
);
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
));
183 ln
->umodes
= target
->umodes
;
186 ln
->reconnect
= scheduleoneshot(time(NULL
) + 1, &lua_reconnectlocal
, ln
);
190 /* we were invited, check if someone invited us to PUBLICCHAN */
191 np
= (nick
*)args
[0];
192 c
= (channel
*)args
[1];
194 if(!c
|| !np
|| !c
->index
|| !c
->index
->name
|| !c
->index
->name
->content
)
197 lua_vlpcall(l
, ln
, "irc_oninvite", "Ns", np
, c
->index
->name
->content
);
202 void lua_reconnectlocal(void *arg
) {
204 lua_localnick
*ln
= (lua_localnick
*)arg
;
206 ln
->nick
= registerlocaluser(ln
->nickname
, ln
->ident
, ln
->hostname
, ln
->realname
, ln
->account
, ln
->umodes
, &lua_localnickhandler
);
208 ln
->reconnect
= scheduleoneshot(time(NULL
) + 1, &lua_reconnectlocal
, ln
);
212 ln
->reconnect
= NULL
;
214 if(lua_getlocalnickbynick(ln
->nick
, &l
, &ln
)) /* hacky! */
215 lua_vlpcall(l
, ln
, "irc_onkillreconnect", "");
218 static int lua_localjoin(lua_State
*ps
) {
223 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2))
224 LUA_RETURN(ps
, LUA_FAIL
);
226 source
= getnickbynumeric(lua_tolong(ps
, 1));
228 LUA_RETURN(ps
, LUA_FAIL
);
230 chan
= (char *)lua_tostring(ps
, 2);
232 LUA_RETURN(ps
, LUA_FAIL
);
234 if(!lua_lineok(chan
))
235 LUA_RETURN(ps
, LUA_FAIL
);
237 target
= findchannel(chan
);
239 localjoinchannel(source
, target
);
241 localcreatechannel(source
, chan
);
244 LUA_RETURN(ps
, LUA_OK
);
247 static int lua_localpart(lua_State
*ps
) {
252 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2))
253 LUA_RETURN(ps
, LUA_FAIL
);
255 source
= getnickbynumeric(lua_tolong(ps
, 1));
257 LUA_RETURN(ps
, LUA_FAIL
);
259 chan
= (char *)lua_tostring(ps
, 2);
261 if(!lua_lineok(chan
))
262 LUA_RETURN(ps
, LUA_FAIL
);
264 if(lua_isstring(ps
, 3)) {
265 reason
= (char *)lua_tostring(ps
, 3);
266 if(!lua_lineok(reason
))
267 LUA_RETURN(ps
, LUA_FAIL
);
272 target
= findchannel(chan
);
274 localpartchannel(source
, target
, reason
);
276 LUA_RETURN(ps
, LUA_FAIL
);
279 LUA_RETURN(ps
, LUA_OK
);
282 static int lua_localchanmsg(lua_State
*ps
) {
287 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
288 LUA_RETURN(ps
, LUA_FAIL
);
290 source
= getnickbynumeric(lua_tolong(ps
, 1));
292 LUA_RETURN(ps
, LUA_FAIL
);
294 target
= findchannel((char *)lua_tostring(ps
, 2));
296 LUA_RETURN(ps
, LUA_FAIL
);
298 msg
= (char *)lua_tostring(ps
, 3);
301 LUA_RETURN(ps
, LUA_FAIL
);
303 sendmessagetochannel(source
, target
, "%s", msg
);
305 LUA_RETURN(ps
, LUA_OK
);
308 static int lua_localnotice(lua_State
*ps
) {
313 if(!lua_islong(ps
, 1) || !lua_islong(ps
, 2) || !lua_isstring(ps
, 3))
314 LUA_RETURN(ps
, LUA_FAIL
);
316 source
= getnickbynumeric(lua_tolong(ps
, 1));
318 LUA_RETURN(ps
, LUA_FAIL
);
320 target
= getnickbynumeric(lua_tolong(ps
, 2));
322 LUA_RETURN(ps
, LUA_FAIL
);
324 msg
= (char *)lua_tostring(ps
, 3);
327 LUA_RETURN(ps
, LUA_FAIL
);
329 sendnoticetouser(source
, target
, "%s", msg
);
331 LUA_RETURN(ps
, LUA_OK
);
334 static int lua_localprivmsg(lua_State
*ps
) {
339 if(!lua_islong(ps
, 1) || !lua_islong(ps
, 2) || !lua_isstring(ps
, 3))
340 LUA_RETURN(ps
, LUA_FAIL
);
342 source
= getnickbynumeric(lua_tolong(ps
, 1));
344 LUA_RETURN(ps
, LUA_FAIL
);
346 target
= getnickbynumeric(lua_tolong(ps
, 2));
348 LUA_RETURN(ps
, LUA_FAIL
);
350 msg
= (char *)lua_tostring(ps
, 3);
353 LUA_RETURN(ps
, LUA_FAIL
);
355 sendmessagetouser(source
, target
, "%s", msg
);
357 LUA_RETURN(ps
, LUA_OK
);
360 static int lua_localovmode(lua_State
*l
) {
363 int state
= 0, add
= 0, realmode
= 0, ignoring
= 0;
366 if(!lua_islong(l
, 1) || !lua_isstring(l
, 2) || !lua_istable(l
, 3))
367 LUA_RETURN(l
, LUA_FAIL
);
369 source
= getnickbynumeric(lua_tolong(l
, 1));
371 LUA_RETURN(l
, LUA_FAIL
);
373 chan
= findchannel((char *)lua_tostring(l
, 2));
375 LUA_RETURN(l
, LUA_FAIL
);
377 localsetmodeinit(&changes
, chan
, source
);
381 while(lua_next(l
, 3)) {
385 if(!lua_isboolean(l
, -1)) {
388 add
= (int)lua_toboolean(l
, -1);
390 } else if((state
== 1) && !ignoring
) {
391 if(!lua_isstring(l
, -1)) {
394 char *mode
= (char *)lua_tostring(l
, -1);
395 if((*mode
== 'o') && add
) {
397 } else if (*mode
== 'o') {
399 } else if((*mode
== 'v') && add
) {
401 } else if (*mode
== 'v') {
402 realmode
= MC_DEVOICE
;
407 } else if((state
== 2) && !ignoring
) {
408 if(lua_islong(l
, -1)) {
409 nick
*target
= getnickbynumeric(lua_tolong(l
, -1));
411 localdosetmode_nick(&changes
, target
, realmode
);
417 state
= (state
+ 1) % 3;
420 localsetmodeflush(&changes
, 1);
422 LUA_RETURN(l
, LUA_OK
);
425 static int lua_localumodes(lua_State
*ps
) {
430 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2))
431 LUA_RETURN(ps
, LUA_FAIL
);
433 np
= getnickbynumeric(lua_tolong(ps
, 1));
435 LUA_RETURN(ps
, LUA_FAIL
);
437 modes
= (char *)lua_tostring(ps
, 2);
439 setflags(&newmodes
, UMODE_ALL
, modes
, umodeflags
, REJECT_NONE
);
441 localusersetumodes(np
, newmodes
);
442 LUA_RETURN(ps
, LUA_OK
);
445 static int lua_localtopic(lua_State
*ps
) {
450 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
451 LUA_RETURN(ps
, LUA_FAIL
);
453 np
= getnickbynumeric(lua_tolong(ps
, 1));
455 LUA_RETURN(ps
, LUA_FAIL
);
457 cp
= findchannel((char *)lua_tostring(ps
, 2));
459 LUA_RETURN(ps
, LUA_FAIL
);
461 topic
= (char *)lua_tostring(ps
, 3);
462 if(!topic
|| !lua_lineok(topic
))
463 LUA_RETURN(ps
, LUA_FAIL
);
465 localsettopic(np
, cp
, topic
);
467 LUA_RETURN(ps
, LUA_OK
);
470 static int lua_localban(lua_State
*ps
) {
478 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
479 LUA_RETURN(ps
, LUA_FAIL
);
481 if(lua_isboolean(ps
, 4) && lua_toboolean(ps
, 4))
484 source
= getnickbynumeric(lua_tolong(ps
, 1));
486 cp
= findchannel((char *)lua_tostring(ps
, 2));
488 LUA_RETURN(ps
, LUA_FAIL
);
490 mask
= lua_tostring(ps
, 3);
491 if(!mask
|| !mask
[0] || !lua_lineok(mask
))
492 LUA_RETURN(ps
, LUA_FAIL
);
494 localsetmodeinit(&changes
, cp
, source
);
495 localdosetmode_ban(&changes
, mask
, dir
);
496 localsetmodeflush(&changes
, 1);
498 LUA_RETURN(ps
, LUA_OK
);
501 static int lua_localkick(lua_State
*ps
) {
502 const char *n
, *msg
, *chan
;
507 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3) || !lua_isstring(ps
, 4))
508 LUA_RETURN(ps
, LUA_FAIL
);
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);
515 LUA_RETURN(ps
, LUA_FAIL
);
517 if(lua_isboolean(ps
, 4) && !lua_toboolean(ps
, 4))
520 np
= getnickbynick(n
);
522 LUA_RETURN(ps
, LUA_FAIL
);
524 if(dochecks
&& (IsOper(np
) || IsXOper(np
) || IsService(np
)))
525 LUA_RETURN(ps
, LUA_FAIL
);
527 cp
= findchannel((char *)chan
);
529 LUA_RETURN(ps
, LUA_FAIL
);
532 LUA_RETURN(ps
, LUA_FAIL
);
534 localkickuser(source
, cp
, np
, msg
);
536 LUA_RETURN(ps
, LUA_OK
);
539 static int lua_localrename(lua_State
*ps
) {
543 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) )
544 LUA_RETURN(ps
, LUA_FAIL
);
546 np
= getnickbynumeric(lua_tolong(ps
, 1));
547 changeto
= (char *)lua_tostring(ps
, 2);
549 if(!lua_lineok(changeto
))
550 LUA_RETURN(ps
, LUA_FAIL
);
552 renamelocaluser(np
, changeto
);
554 LUA_RETURN(ps
, LUA_OK
);
557 void lua_registerlocalcommands(lua_State
*l
) {
558 lua_register(l
, "irc_localregisteruserid", lua_registerlocaluserid
);
559 lua_register(l
, "irc_localderegisteruser", lua_deregisterlocaluser
);
560 lua_register(l
, "irc_localjoin", lua_localjoin
);
561 lua_register(l
, "irc_localpart", lua_localpart
);
562 lua_register(l
, "irc_localchanmsg", lua_localchanmsg
);
563 lua_register(l
, "irc_localnotice", lua_localnotice
);
564 lua_register(l
, "irc_localprivmsg", lua_localprivmsg
);
566 lua_register(l
, "irc_localovmode", lua_localovmode
);
567 lua_register(l
, "irc_localtopic", lua_localtopic
);
569 lua_register(l
, "irc_localban", lua_localban
);
570 lua_register(l
, "irc_localkick", lua_localkick
);
571 lua_register(l
, "irc_localumodes", lua_localumodes
);
573 lua_register(l
, "irc_localrename", lua_localrename
);