]>
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
) {
20 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
, 6) || !lua_isfunction(ps
, 7))
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);
32 setflags(&modes
, UMODE_ALL
, (char *)lua_tostring(ps
, 6), umodeflags
, REJECT_NONE
);
34 if(!lua_lineok(nickname
) || !lua_lineok(ident
) || !lua_lineok(hostname
) || !lua_lineok(realname
) || !lua_lineok(account
))
37 l
= lua_listfromstate(ps
);
41 ln
= (lua_localnick
*)malloc(sizeof(lua_localnick
));
47 ln
->nick
= registerlocaluser(nickname
, ident
, hostname
, realname
, account
, modes
, &lua_localnickhandler
);
53 ln
->handler
= luaL_ref(ps
, LUA_REGISTRYINDEX
);
58 lua_pushlong(ps
, np
->numeric
);
62 void lua_freelocalnick(lua_State
*ps
, lua_localnick
*l
, char *quitm
) {
64 deregisterlocaluser(l
->nick
, quitm
);
67 deleteschedule(l
->reconnect
, &lua_reconnectlocal
, l
);
69 luaL_unref(ps
, LUA_REGISTRYINDEX
, l
->handler
);
74 int lua_getlocalnickbynick(nick
*np
, lua_list
**rl
, lua_localnick
**rln
) {
78 for(l
=lua_head
;l
;l
=l
->next
)
79 for(ln
=l
->nicks
;ln
;ln
=ln
->next
)
89 static int lua_deregisterlocaluser(lua_State
*ps
) {
91 lua_localnick
*l2
, *lp
= NULL
;
95 if(!lua_islong(ps
, 1))
96 LUA_RETURN(ps
, LUA_FAIL
);
98 quitm
= lua_isstring(ps
, 2)?(char *)lua_tostring(ps
, 2):"localuser unregistered.";
100 l
= lua_listfromstate(ps
);
102 for(l2
=l
->nicks
;l2
;lp
=l2
,l2
=l2
->next
) {
103 if(l2
->nick
->numeric
== numeric
) {
105 lua_freelocalnick(ps
, l2
, quitm
);
106 LUA_RETURN(ps
, LUA_OK
);
110 LUA_RETURN(ps
, LUA_FAIL
);
113 void lua_deregisternicks(lua_list
*l
) {
114 struct lua_localnick
*ln
, *pn
;
116 for(ln
=l
->nicks
;ln
;ln
=pn
) {
119 lua_freelocalnick(l
->l
, ln
, "Script unloaded.");
126 void lua_localnickhandler(nick
*target
, int type
, void **args
) {
132 if(!lua_getlocalnickbynick(target
, &l
, &ln
))
137 np
= (nick
*)args
[0];
143 lua_vlpcall(l
, ln
, "irc_onmsg", "Ns", np
, p
);
148 lua_vlpcall(l
, ln
, "irc_onkilled", "");
150 strlcpy(ln
->nickname
, target
->nick
, sizeof(ln
->nickname
));
151 strlcpy(ln
->ident
, target
->ident
, sizeof(ln
->ident
));
152 strlcpy(ln
->hostname
, target
->host
->name
->content
, sizeof(ln
->hostname
));
153 strlcpy(ln
->realname
, target
->realname
->name
->content
, sizeof(ln
->realname
));
154 strlcpy(ln
->account
, target
->authname
, sizeof(ln
->account
));
156 ln
->umodes
= target
->umodes
;
159 ln
->reconnect
= scheduleoneshot(time(NULL
) + 1, &lua_reconnectlocal
, ln
);
165 void lua_reconnectlocal(void *arg
) {
167 lua_localnick
*ln
= (lua_localnick
*)arg
;
169 ln
->nick
= registerlocaluser(ln
->nickname
, ln
->ident
, ln
->hostname
, ln
->realname
, ln
->account
, ln
->umodes
, &lua_localnickhandler
);
171 ln
->reconnect
= scheduleoneshot(time(NULL
) + 1, &lua_reconnectlocal
, ln
);
175 ln
->reconnect
= NULL
;
177 if(lua_getlocalnickbynick(ln
->nick
, &l
, &ln
)) /* hacky! */
178 lua_vlpcall(l
, ln
, "irc_onkillreconnect", "");
181 static int lua_localjoin(lua_State
*ps
) {
186 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2))
187 LUA_RETURN(ps
, LUA_FAIL
);
189 source
= getnickbynumeric(lua_tolong(ps
, 1));
191 LUA_RETURN(ps
, LUA_FAIL
);
193 chan
= (char *)lua_tostring(ps
, 2);
195 if(!lua_lineok(chan
))
196 LUA_RETURN(ps
, LUA_FAIL
);
198 target
= findchannel(chan
);
200 localjoinchannel(source
, target
);
202 localcreatechannel(lua_nick
, chan
);
205 LUA_RETURN(ps
, LUA_OK
);
208 static int lua_localchanmsg(lua_State
*ps
) {
213 if(!lua_islong(ps
, 1) || !lua_isstring(ps
, 2) || !lua_isstring(ps
, 3))
214 LUA_RETURN(ps
, LUA_FAIL
);
216 source
= getnickbynumeric(lua_tolong(ps
, 1));
218 LUA_RETURN(ps
, LUA_FAIL
);
220 target
= findchannel((char *)lua_tostring(ps
, 2));
222 LUA_RETURN(ps
, LUA_FAIL
);
224 msg
= (char *)lua_tostring(ps
, 3);
227 LUA_RETURN(ps
, LUA_FAIL
);
229 sendmessagetochannel(source
, target
, "%s", msg
);
231 LUA_RETURN(ps
, LUA_OK
);
234 static int lua_localnotice(lua_State
*ps
) {
239 if(!lua_islong(ps
, 1) || !lua_islong(ps
, 2) || !lua_isstring(ps
, 3))
240 LUA_RETURN(ps
, LUA_FAIL
);
242 source
= getnickbynumeric(lua_tolong(ps
, 1));
244 LUA_RETURN(ps
, LUA_FAIL
);
246 target
= getnickbynumeric(lua_tolong(ps
, 2));
248 LUA_RETURN(ps
, LUA_FAIL
);
250 msg
= (char *)lua_tostring(ps
, 3);
253 LUA_RETURN(ps
, LUA_FAIL
);
255 sendnoticetouser(source
, target
, "%s", msg
);
257 LUA_RETURN(ps
, LUA_OK
);
260 void lua_registerlocalcommands(lua_State
*l
) {
261 lua_register(l
, "irc_localregisteruser", lua_registerlocaluser
);
262 lua_register(l
, "irc_localderegisteruser", lua_deregisterlocaluser
);
263 lua_register(l
, "irc_localjoin", lua_localjoin
);
264 lua_register(l
, "irc_localchanmsg", lua_localchanmsg
);
265 lua_register(l
, "irc_localnotice", lua_localnotice
);