1 /* Lua bindings for Newserv */
3 /* Copyright (C) Chris Porter 2005 */
4 /* ALL RIGHTS RESERVED. */
5 /* Don't put this into the SVN repo. */
7 #define _POSIX_C_SOURCE 200112L
10 #include "../core/config.h"
11 #include "../core/error.h"
12 #include "../core/hooks.h"
13 #include "../lib/array.h"
14 #include "../lib/irc_string.h"
15 #include "../core/schedule.h"
16 #include "../lib/version.h"
17 #include "../lib/strlfunc.h"
20 MODULE_VERSION(LUA_SMALLVERSION
);
22 #ifdef LUA_DEBUGSOCKET
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
34 void lua_startup(void *arg
);
35 void lua_loadscripts(void);
36 void lua_registercommands(lua_State
*l
);
37 void lua_registerdbcommands(lua_State
*l
);
38 void lua_initnickpusher(void);
39 void lua_initchanpusher(void);
40 void lua_loadlibs(lua_State
*l
);
41 void lua_require(lua_State
*l
, char *module);
43 void lua_startbot(void *arg
);
44 void lua_destroybot(void);
45 void lua_startcontrol(void);
46 void lua_destroycontrol(void);
48 void lua_onunload(lua_State
*l
);
49 void lua_onload(lua_State
*l
);
51 void lua_setpath(void);
53 void lua_setupdebugsocket(void);
54 void lua_freedebugsocket(void);
56 void lua_deregisternicks(lua_list
*l
);
57 void lua_registerlocalcommands(lua_State
*ps
);
58 void lua_registerdebug(lua_State
*ps
);
59 void lua_socket_closeall(lua_list
*l
);
60 void lua_registersocketcommands(lua_State
*ps
);
61 void lua_registercryptocommands(lua_State
*ps
);
63 #ifdef LUA_DEBUGSOCKET
65 struct sockaddr_in debugsocketdest
;
70 lua_list
*lua_head
= NULL
, *lua_tail
= NULL
;
72 sstring
*cpath
= NULL
, *suffix
= NULL
;
74 void *startsched
= NULL
;
78 struct rusage r_usages
;
79 struct rusage r_usagee
;
81 static const luaL_Reg ourlibs
[] = {
83 {LUA_LOADLIBNAME
, luaopen_package
},
84 {LUA_TABLIBNAME
, luaopen_table
},
85 {LUA_IOLIBNAME
, luaopen_io
},
86 {LUA_OSLIBNAME
, luaopen_os
},
87 {LUA_STRLIBNAME
, luaopen_string
},
88 {LUA_MATHLIBNAME
, luaopen_math
},
90 {LUA_JITLIBNAME
, luaopen_jit
},
98 lua_setupdebugsocket();
100 lua_initchanpusher();
102 dummy
.name
= getsstring("???", 10);
104 Error("lua", ERR_ERROR
, "Cannot set dummy name.");
108 cpath
= getcopyconfigitem("lua", "scriptdir", "", 500);
110 if(!cpath
|| !cpath
->content
|| !cpath
->content
[0]) {
111 Error("lua", ERR_ERROR
, "Error loading path.");
115 suffix
= getcopyconfigitem("lua", "scriptsuffix", ".lua", 10);
117 Error("lua", ERR_ERROR
, "Error loading suffix.");
125 startsched
= scheduleoneshot(time(NULL
) + 1, &lua_startup
, NULL
);
128 void lua_startup(void *arg
) {
137 #ifdef BROKEN_DLCLOSE
145 deleteschedule(startsched
, &lua_startup
, NULL
);
148 lua_unloadscript(lua_head
);
151 lua_destroycontrol();
156 freesstring(dummy
.name
);
158 lua_freedebugsocket();
159 nscheckfreeall(POOL_LUA
);
162 void lua_loadscripts(void) {
165 ls
= getconfigitems("lua", "script");
167 Error("lua", ERR_INFO
, "No scripts loaded.");
169 sstring
**scripts
= (sstring
**)(ls
->content
);
171 for(i
=0;i
<ls
->cursi
;i
++)
172 lua_loadscript(scripts
[i
]->content
);
176 /* taken from the lua manual, modified to use nsmalloc */
177 static void *lua_nsmalloc(void *ud
, void *ptr
, size_t osize
, size_t nsize
) {
184 return luarealloc(ptr
, nsize
);
187 lua_State
*lua_loadscript(char *file
) {
188 char fullpath
[LUA_PATHLEN
];
194 if(!cpath
|| !suffix
)
197 strlcpy(buf
, file
, sizeof(buf
));
199 delchars(buf
, "./\\;");
201 if(lua_scriptloaded(buf
))
204 l
= lua_newstate(lua_nsmalloc
, NULL
);
208 n
= (lua_list
*)luamalloc(sizeof(lua_list
));;
210 Error("lua", ERR_ERROR
, "Error allocing list for %s.", buf
);
214 n
->name
= getsstring(buf
, LUA_PATHLEN
);
216 Error("lua", ERR_ERROR
, "Error allocing name item for %s.", buf
);
222 timerclear(&n
->ru_utime
);
223 timerclear(&n
->ru_stime
);
226 lua_registerdebug(l
);
227 lua_registercommands(l
);
228 lua_registerlocalcommands(l
);
229 lua_registerdbcommands(l
);
230 lua_registersocketcommands(l
);
231 lua_registercryptocommands(l
);
234 lua_require(l
, "lib/jit");
237 lua_require(l
, "lib/bootstrap");
239 snprintf(fullpath
, sizeof(fullpath
), "%s/%s%s", cpath
->content
, file
, suffix
->content
);
240 if(luaL_loadfile(l
, fullpath
)) {
241 Error("lua", ERR_ERROR
, "Error loading %s.", file
);
243 freesstring(n
->name
);
265 if(lua_pcall(l
, 0, 0, 0)) {
266 Error("lua", ERR_ERROR
, "Error pcalling: %s.", file
);
268 freesstring(n
->name
);
275 lua_tail
->next
= NULL
;
283 Error("lua", ERR_INFO
, "Loaded %s.", file
);
289 void lua_unloadscript(lua_list
*l
) {
291 lua_deregisternicks(l
);
292 lua_socket_closeall(l
);
294 freesstring(l
->name
);
296 /* well, at least it's O(1) */
298 if(!l
->prev
) { /* head */
303 lua_head
->prev
= NULL
;
306 if(!l
->next
) { /* tail */
307 lua_tail
= lua_tail
->prev
;
311 lua_tail
->next
= NULL
;
314 l
->prev
->next
= l
->next
;
315 l
->next
->prev
= l
->prev
;
322 void lua_setpath(void) {
323 char fullpath
[LUA_PATHLEN
];
325 snprintf(fullpath
, sizeof(fullpath
), "%s/?%s", cpath
->content
, suffix
->content
);
326 setenv("LUA_PATH", fullpath
, 1);
329 lua_list
*lua_scriptloaded(char *name
) {
332 for(l
=lua_head
;l
;l
=l
->next
)
333 if(!strcmp(l
->name
->content
, name
))
339 void lua_loadlibs(lua_State
*l
) {
340 const luaL_Reg
*lib
= ourlibs
;
342 for (;lib
->func
;lib
++) {
343 lua_pushcfunction(l
, lib
->func
);
344 lua_pushstring(l
, lib
->name
);
349 void lua_require(lua_State
*l
, char *module) {
350 int top
= lua_gettop(l
);
352 lua_getglobal(l
, "require");
353 lua_pushstring(l
, module);
355 if(lua_pcall(l
, 1, 1, 0))
356 Error("lua", ERR_ERROR
, "Error requiring %s: %s", module, lua_tostring(l
, -1));
361 void lua_setupdebugsocket(void) {
362 #ifdef LUA_DEBUGSOCKET
364 debugsocket
= socket(AF_INET
, SOCK_DGRAM
, 0);
365 if(debugsocket
< 0) {
367 Error("lua", ERR_ERROR
, "Cannot create debug socket.");
372 memset(&debugsocketdest
, 0, sizeof(debugsocketdest
));
374 debugsocketdest
.sin_family
= AF_INET
;
375 debugsocketdest
.sin_port
= htons(LUA_DEBUGSOCKET_PORT
);
376 debugsocketdest
.sin_addr
.s_addr
= inet_addr(LUA_DEBUGSOCKET_ADDRESS
);
381 void lua_freedebugsocket(void) {
382 #ifdef LUA_DEBUGSOCKET
384 if(debugsocket
== -1)
395 #ifdef LUA_DEBUGSOCKET
396 void lua_debugoutput(char *format
, ...) {
401 if(debugsocket
== -1)
404 va_start(va
, format
);
405 size
= vsnprintf(buf
, sizeof(buf
), format
, va
);
408 if(size
>= sizeof(buf
))
409 size
= sizeof(buf
) - 1;
412 sendto(debugsocket
, buf
, size
, 0, (struct sockaddr
*)&debugsocketdest
, sizeof(debugsocketdest
));
416 lua_list
*lua_listfromstate(lua_State
*l
) {
417 lua_list
*i
= lua_head
;
425 int lua_listexists(lua_list
*l
) {
428 for(i
=lua_head
;i
;i
=i
->next
)
435 int lua_lineok(const char *data
) {
436 if(strchr(data
, '\r') || strchr(data
, '\n'))
441 int lua_debugpcall(lua_State
*l
, char *message
, int a
, int b
, int c
) {
442 lua_list
*l2
= lua_listfromstate(l
);
445 #ifdef LUA_DEBUGSOCKET
446 DEBUGOUT("%s: %s\n", l2
->name
->content
, message
);
450 ACCOUNTING_START(l2
);
453 ret
= lua_pcall(l
, a
, b
, c
);