]>
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 if(!lua_lineok(chan
))
225 LUA_RETURN(ps
, LUA_FAIL
);
227 target
= findchannel(chan
);
229 localjoinchannel(source
, target
);
231 localcreatechannel(source
, chan
);
234 LUA_RETURN(ps
, LUA_OK
);
237 static int lua_localpart(lua_State
*ps
) {
242 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2))
243 LUA_RETURN(ps
, LUA_FAIL
);
245 source
= getnickbynumeric(lua_tolong(ps
, 1));
247 LUA_RETURN(ps
, LUA_FAIL
);
249 chan
= (char *)lua_tostring(ps
, 2);
251 if(!lua_lineok(chan
))
252 LUA_RETURN(ps
, LUA_FAIL
);
254 target
= findchannel(chan
);
256 localpartchannel(source
, target
);
258 LUA_RETURN(ps
, LUA_FAIL
);
261 LUA_RETURN(ps
, LUA_OK
);
264 static int lua_localchanmsg(lua_State
*ps
) {
269 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
270 LUA_RETURN(ps
, LUA_FAIL
);
272 source
= getnickbynumeric(lua_tolong(ps
, 1));
274 LUA_RETURN(ps
, LUA_FAIL
);
276 target
= findchannel((char *)lua_tostring(ps
, 2));
278 LUA_RETURN(ps
, LUA_FAIL
);
280 msg
= (char *)lua_tostring(ps
, 3);
283 LUA_RETURN(ps
, LUA_FAIL
);
285 sendmessagetochannel(source
, target
, "%s", msg
);
287 LUA_RETURN(ps
, LUA_OK
);
290 static int lua_localnotice(lua_State
*ps
) {
295 if(!lua_islong(ps
, 1) || !lua_islong(ps
, 2) || !lua_isstring(ps
, 3))
296 LUA_RETURN(ps
, LUA_FAIL
);
298 source
= getnickbynumeric(lua_tolong(ps
, 1));
300 LUA_RETURN(ps
, LUA_FAIL
);
302 target
= getnickbynumeric(lua_tolong(ps
, 2));
304 LUA_RETURN(ps
, LUA_FAIL
);
306 msg
= (char *)lua_tostring(ps
, 3);
309 LUA_RETURN(ps
, LUA_FAIL
);
311 sendnoticetouser(source
, target
, "%s", msg
);
313 LUA_RETURN(ps
, LUA_OK
);
316 static int lua_localprivmsg(lua_State
*ps
) {
321 if(!lua_islong(ps
, 1) || !lua_islong(ps
, 2) || !lua_isstring(ps
, 3))
322 LUA_RETURN(ps
, LUA_FAIL
);
324 source
= getnickbynumeric(lua_tolong(ps
, 1));
326 LUA_RETURN(ps
, LUA_FAIL
);
328 target
= getnickbynumeric(lua_tolong(ps
, 2));
330 LUA_RETURN(ps
, LUA_FAIL
);
332 msg
= (char *)lua_tostring(ps
, 3);
335 LUA_RETURN(ps
, LUA_FAIL
);
337 sendmessagetouser(source
, target
, "%s", msg
);
339 LUA_RETURN(ps
, LUA_OK
);
342 static int lua_localovmode(lua_State
*l
) {
345 int state
= 0, add
, realmode
, ignoring
= 0;
348 if(!lua_islong(l
, 1) || !lua_isstring(l
, 2) || !lua_istable(l
, 3))
349 LUA_RETURN(l
, LUA_FAIL
);
351 source
= getnickbynumeric(lua_tolong(l
, 1));
353 LUA_RETURN(l
, LUA_FAIL
);
355 chan
= findchannel((char *)lua_tostring(l
, 2));
357 LUA_RETURN(l
, LUA_FAIL
);
359 localsetmodeinit(&changes
, chan
, source
);
363 while(lua_next(l
, 3)) {
367 if(!lua_isboolean(l
, -1)) {
370 add
= (int)lua_toboolean(l
, -1);
372 } else if((state
== 1) && !ignoring
) {
373 if(!lua_isstring(l
, -1)) {
376 char *mode
= (char *)lua_tostring(l
, -1);
377 if((*mode
== 'o') && add
) {
379 } else if (*mode
== 'o') {
381 } else if((*mode
== 'v') && add
) {
383 } else if (*mode
== 'v') {
384 realmode
= MC_DEVOICE
;
389 } else if((state
== 2) && !ignoring
) {
390 if(lua_islong(l
, -1)) {
391 nick
*target
= getnickbynumeric(lua_tolong(l
, -1));
393 localdosetmode_nick(&changes
, target
, realmode
);
399 state
= (state
+ 1) % 3;
402 localsetmodeflush(&changes
, 1);
404 LUA_RETURN(l
, LUA_OK
);
407 static int lua_localtopic(lua_State
*ps
) {
412 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
413 LUA_RETURN(ps
, LUA_FAIL
);
415 np
= getnickbynumeric(lua_tolong(ps
, 1));
417 LUA_RETURN(ps
, LUA_FAIL
);
419 cp
= findchannel((char *)lua_tostring(ps
, 2));
421 LUA_RETURN(ps
, LUA_FAIL
);
423 topic
= (char *)lua_tostring(ps
, 3);
424 if(!topic
|| !lua_lineok(topic
))
425 LUA_RETURN(ps
, LUA_FAIL
);
427 localsettopic(np
, cp
, topic
);
429 LUA_RETURN(ps
, LUA_OK
);
432 static int lua_localban(lua_State
*ps
) {
440 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
441 LUA_RETURN(ps
, LUA_FAIL
);
443 if(lua_isboolean(ps
, 4) && lua_toboolean(ps
, 4))
446 source
= getnickbynumeric(lua_tolong(ps
, 1));
448 cp
= findchannel((char *)lua_tostring(ps
, 2));
450 LUA_RETURN(ps
, LUA_FAIL
);
452 mask
= lua_tostring(ps
, 3);
453 if(!mask
|| !mask
[0] || !lua_lineok(mask
))
454 LUA_RETURN(ps
, LUA_FAIL
);
456 localsetmodeinit(&changes
, cp
, source
);
457 localdosetmode_ban(&changes
, mask
, dir
);
458 localsetmodeflush(&changes
, 1);
460 LUA_RETURN(ps
, LUA_OK
);
463 static int lua_localkick(lua_State
*ps
) {
464 const char *n
, *msg
, *chan
;
469 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3) || !lua_isstring(ps
, 4))
470 LUA_RETURN(ps
, LUA_FAIL
);
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);
477 LUA_RETURN(ps
, LUA_FAIL
);
479 if(lua_isboolean(ps
, 4) && !lua_toboolean(ps
, 4))
482 np
= getnickbynick(n
);
484 LUA_RETURN(ps
, LUA_FAIL
);
486 if(dochecks
&& (IsOper(np
) || IsXOper(np
) || IsService(np
)))
487 LUA_RETURN(ps
, LUA_FAIL
);
489 cp
= findchannel((char *)chan
);
491 LUA_RETURN(ps
, LUA_FAIL
);
494 LUA_RETURN(ps
, LUA_FAIL
);
496 localkickuser(source
, cp
, np
, msg
);
498 LUA_RETURN(ps
, LUA_OK
);
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
);
510 lua_register(l
, "irc_localovmode", lua_localovmode
);
511 lua_register(l
, "irc_localtopic", lua_localtopic
);
513 lua_register(l
, "irc_localban", lua_localban
);
514 lua_register(l
, "irc_localkick", lua_localkick
);