]>
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
*)malloc(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
->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
;
122 for(ln
=l
->nicks
;ln
;ln
=pn
) {
125 lua_freelocalnick(l
->l
, ln
, "Script unloaded.");
132 void lua_localnickhandler(nick
*target
, int type
, void **args
) {
139 if(!lua_getlocalnickbynick(target
, &l
, &ln
))
144 np
= (nick
*)args
[0];
150 lua_vlpcall(l
, ln
, "irc_onmsg", "Ns", np
, p
);
155 np
= (nick
*)args
[0];
156 c
= (channel
*)args
[1];
159 if(!np
|| !p
|| !c
|| !c
->index
|| !c
->index
->name
|| !c
->index
->name
->content
)
162 lua_vlpcall(l
, ln
, "irc_onchanmsg", "Nss", np
, c
->index
->name
->content
, p
);
167 lua_vlpcall(l
, ln
, "irc_onkilled", "s", p
);
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
));
175 ln
->umodes
= target
->umodes
;
178 ln
->reconnect
= scheduleoneshot(time(NULL
) + 1, &lua_reconnectlocal
, ln
);
182 /* we were invited, check if someone invited us to PUBLICCHAN */
183 np
= (nick
*)args
[0];
184 c
= (channel
*)args
[1];
186 if(!c
|| !np
|| !c
->index
|| !c
->index
->name
|| !c
->index
->name
->content
)
189 lua_vlpcall(l
, ln
, "irc_oninvite", "Ns", np
, c
->index
->name
->content
);
194 void lua_reconnectlocal(void *arg
) {
196 lua_localnick
*ln
= (lua_localnick
*)arg
;
198 ln
->nick
= registerlocaluser(ln
->nickname
, ln
->ident
, ln
->hostname
, ln
->realname
, ln
->account
, ln
->umodes
, &lua_localnickhandler
);
200 ln
->reconnect
= scheduleoneshot(time(NULL
) + 1, &lua_reconnectlocal
, ln
);
204 ln
->reconnect
= NULL
;
206 if(lua_getlocalnickbynick(ln
->nick
, &l
, &ln
)) /* hacky! */
207 lua_vlpcall(l
, ln
, "irc_onkillreconnect", "");
210 static int lua_localjoin(lua_State
*ps
) {
215 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2))
216 LUA_RETURN(ps
, LUA_FAIL
);
218 source
= getnickbynumeric(lua_tolong(ps
, 1));
220 LUA_RETURN(ps
, LUA_FAIL
);
222 chan
= (char *)lua_tostring(ps
, 2);
224 LUA_RETURN(ps
, LUA_FAIL
);
226 if(!lua_lineok(chan
))
227 LUA_RETURN(ps
, LUA_FAIL
);
229 target
= findchannel(chan
);
231 localjoinchannel(source
, target
);
233 localcreatechannel(source
, chan
);
236 LUA_RETURN(ps
, LUA_OK
);
239 static int lua_localpart(lua_State
*ps
) {
244 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2))
245 LUA_RETURN(ps
, LUA_FAIL
);
247 source
= getnickbynumeric(lua_tolong(ps
, 1));
249 LUA_RETURN(ps
, LUA_FAIL
);
251 chan
= (char *)lua_tostring(ps
, 2);
253 if(!lua_lineok(chan
))
254 LUA_RETURN(ps
, LUA_FAIL
);
256 target
= findchannel(chan
);
258 localpartchannel(source
, target
);
260 LUA_RETURN(ps
, LUA_FAIL
);
263 LUA_RETURN(ps
, LUA_OK
);
266 static int lua_localchanmsg(lua_State
*ps
) {
271 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
272 LUA_RETURN(ps
, LUA_FAIL
);
274 source
= getnickbynumeric(lua_tolong(ps
, 1));
276 LUA_RETURN(ps
, LUA_FAIL
);
278 target
= findchannel((char *)lua_tostring(ps
, 2));
280 LUA_RETURN(ps
, LUA_FAIL
);
282 msg
= (char *)lua_tostring(ps
, 3);
285 LUA_RETURN(ps
, LUA_FAIL
);
287 sendmessagetochannel(source
, target
, "%s", msg
);
289 LUA_RETURN(ps
, LUA_OK
);
292 static int lua_localnotice(lua_State
*ps
) {
297 if(!lua_islong(ps
, 1) || !lua_islong(ps
, 2) || !lua_isstring(ps
, 3))
298 LUA_RETURN(ps
, LUA_FAIL
);
300 source
= getnickbynumeric(lua_tolong(ps
, 1));
302 LUA_RETURN(ps
, LUA_FAIL
);
304 target
= getnickbynumeric(lua_tolong(ps
, 2));
306 LUA_RETURN(ps
, LUA_FAIL
);
308 msg
= (char *)lua_tostring(ps
, 3);
311 LUA_RETURN(ps
, LUA_FAIL
);
313 sendnoticetouser(source
, target
, "%s", msg
);
315 LUA_RETURN(ps
, LUA_OK
);
318 static int lua_localprivmsg(lua_State
*ps
) {
323 if(!lua_islong(ps
, 1) || !lua_islong(ps
, 2) || !lua_isstring(ps
, 3))
324 LUA_RETURN(ps
, LUA_FAIL
);
326 source
= getnickbynumeric(lua_tolong(ps
, 1));
328 LUA_RETURN(ps
, LUA_FAIL
);
330 target
= getnickbynumeric(lua_tolong(ps
, 2));
332 LUA_RETURN(ps
, LUA_FAIL
);
334 msg
= (char *)lua_tostring(ps
, 3);
337 LUA_RETURN(ps
, LUA_FAIL
);
339 sendmessagetouser(source
, target
, "%s", msg
);
341 LUA_RETURN(ps
, LUA_OK
);
344 static int lua_localovmode(lua_State
*l
) {
347 int state
= 0, add
, realmode
, ignoring
= 0;
350 if(!lua_islong(l
, 1) || !lua_isstring(l
, 2) || !lua_istable(l
, 3))
351 LUA_RETURN(l
, LUA_FAIL
);
353 source
= getnickbynumeric(lua_tolong(l
, 1));
355 LUA_RETURN(l
, LUA_FAIL
);
357 chan
= findchannel((char *)lua_tostring(l
, 2));
359 LUA_RETURN(l
, LUA_FAIL
);
361 localsetmodeinit(&changes
, chan
, source
);
365 while(lua_next(l
, 3)) {
369 if(!lua_isboolean(l
, -1)) {
372 add
= (int)lua_toboolean(l
, -1);
374 } else if((state
== 1) && !ignoring
) {
375 if(!lua_isstring(l
, -1)) {
378 char *mode
= (char *)lua_tostring(l
, -1);
379 if((*mode
== 'o') && add
) {
381 } else if (*mode
== 'o') {
383 } else if((*mode
== 'v') && add
) {
385 } else if (*mode
== 'v') {
386 realmode
= MC_DEVOICE
;
391 } else if((state
== 2) && !ignoring
) {
392 if(lua_islong(l
, -1)) {
393 nick
*target
= getnickbynumeric(lua_tolong(l
, -1));
395 localdosetmode_nick(&changes
, target
, realmode
);
401 state
= (state
+ 1) % 3;
404 localsetmodeflush(&changes
, 1);
406 LUA_RETURN(l
, LUA_OK
);
409 static int lua_localumodes(lua_State
*ps
) {
414 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2))
415 LUA_RETURN(ps
, LUA_FAIL
);
417 np
= getnickbynumeric(lua_tolong(ps
, 1));
419 LUA_RETURN(ps
, LUA_FAIL
);
421 modes
= (char *)lua_tostring(ps
, 2);
423 setflags(&newmodes
, UMODE_ALL
, modes
, umodeflags
, REJECT_NONE
);
425 localusersetumodes(np
, newmodes
);
426 LUA_RETURN(ps
, LUA_OK
);
429 static int lua_localtopic(lua_State
*ps
) {
434 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
435 LUA_RETURN(ps
, LUA_FAIL
);
437 np
= getnickbynumeric(lua_tolong(ps
, 1));
439 LUA_RETURN(ps
, LUA_FAIL
);
441 cp
= findchannel((char *)lua_tostring(ps
, 2));
443 LUA_RETURN(ps
, LUA_FAIL
);
445 topic
= (char *)lua_tostring(ps
, 3);
446 if(!topic
|| !lua_lineok(topic
))
447 LUA_RETURN(ps
, LUA_FAIL
);
449 localsettopic(np
, cp
, topic
);
451 LUA_RETURN(ps
, LUA_OK
);
454 static int lua_localban(lua_State
*ps
) {
462 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
463 LUA_RETURN(ps
, LUA_FAIL
);
465 if(lua_isboolean(ps
, 4) && lua_toboolean(ps
, 4))
468 source
= getnickbynumeric(lua_tolong(ps
, 1));
470 cp
= findchannel((char *)lua_tostring(ps
, 2));
472 LUA_RETURN(ps
, LUA_FAIL
);
474 mask
= lua_tostring(ps
, 3);
475 if(!mask
|| !mask
[0] || !lua_lineok(mask
))
476 LUA_RETURN(ps
, LUA_FAIL
);
478 localsetmodeinit(&changes
, cp
, source
);
479 localdosetmode_ban(&changes
, mask
, dir
);
480 localsetmodeflush(&changes
, 1);
482 LUA_RETURN(ps
, LUA_OK
);
485 static int lua_localkick(lua_State
*ps
) {
486 const char *n
, *msg
, *chan
;
491 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3) || !lua_isstring(ps
, 4))
492 LUA_RETURN(ps
, LUA_FAIL
);
494 source
= getnickbynumeric(lua_tolong(ps
, 1));
495 chan
= lua_tostring(ps
, 2);
496 n
= lua_tostring(ps
, 3);
497 msg
= lua_tostring(ps
, 4);
499 LUA_RETURN(ps
, LUA_FAIL
);
501 if(lua_isboolean(ps
, 4) && !lua_toboolean(ps
, 4))
504 np
= getnickbynick(n
);
506 LUA_RETURN(ps
, LUA_FAIL
);
508 if(dochecks
&& (IsOper(np
) || IsXOper(np
) || IsService(np
)))
509 LUA_RETURN(ps
, LUA_FAIL
);
511 cp
= findchannel((char *)chan
);
513 LUA_RETURN(ps
, LUA_FAIL
);
516 LUA_RETURN(ps
, LUA_FAIL
);
518 localkickuser(source
, cp
, np
, msg
);
520 LUA_RETURN(ps
, LUA_OK
);
523 void lua_registerlocalcommands(lua_State
*l
) {
524 lua_register(l
, "irc_localregisteruser", lua_registerlocaluser
);
525 lua_register(l
, "irc_localderegisteruser", lua_deregisterlocaluser
);
526 lua_register(l
, "irc_localjoin", lua_localjoin
);
527 lua_register(l
, "irc_localpart", lua_localpart
);
528 lua_register(l
, "irc_localchanmsg", lua_localchanmsg
);
529 lua_register(l
, "irc_localnotice", lua_localnotice
);
530 lua_register(l
, "irc_localprivmsg", lua_localprivmsg
);
532 lua_register(l
, "irc_localovmode", lua_localovmode
);
533 lua_register(l
, "irc_localtopic", lua_localtopic
);
535 lua_register(l
, "irc_localban", lua_localban
);
536 lua_register(l
, "irc_localkick", lua_localkick
);
537 lua_register(l
, "irc_localumodes", lua_localumodes
);