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);
47 void lua_destroydb(void);
49 void lua_onunload(lua_State
*l
);
50 void lua_onload(lua_State
*l
);
52 void lua_setpath(void);
54 void lua_setupdebugsocket(void);
55 void lua_freedebugsocket(void);
57 void lua_deregisternicks(lua_list
*l
);
58 void lua_registerlocalcommands(lua_State
*ps
);
59 void lua_registerdebug(lua_State
*ps
);
60 void lua_socket_closeall(lua_list
*l
);
61 void lua_scheduler_freeall(lua_list
*l
);
62 void lua_registersocketcommands(lua_State
*ps
);
63 void lua_registercryptocommands(lua_State
*ps
);
64 void lua_registerschedulercommands(lua_State
*ps
);
66 #ifdef LUA_DEBUGSOCKET
68 struct sockaddr_in debugsocketdest
;
73 lua_list
*lua_head
= NULL
, *lua_tail
= NULL
;
75 sstring
*cpath
= NULL
, *suffix
= NULL
;
77 void *startsched
= NULL
;
81 struct rusage r_usages
;
82 struct rusage r_usagee
;
84 static const luaL_Reg ourlibs
[] = {
86 {LUA_LOADLIBNAME
, luaopen_package
},
87 {LUA_TABLIBNAME
, luaopen_table
},
88 {LUA_IOLIBNAME
, luaopen_io
},
89 {LUA_OSLIBNAME
, luaopen_os
},
90 {LUA_STRLIBNAME
, luaopen_string
},
91 {LUA_MATHLIBNAME
, luaopen_math
},
93 {LUA_JITLIBNAME
, luaopen_jit
},
101 lua_setupdebugsocket();
102 lua_initnickpusher();
103 lua_initchanpusher();
105 dummy
.name
= getsstring("???", 10);
107 Error("lua", ERR_ERROR
, "Cannot set dummy name.");
111 cpath
= getcopyconfigitem("lua", "scriptdir", "", 500);
113 if(!cpath
|| !cpath
->content
|| !cpath
->content
[0]) {
114 Error("lua", ERR_ERROR
, "Error loading path.");
118 suffix
= getcopyconfigitem("lua", "scriptsuffix", ".lua", 10);
120 Error("lua", ERR_ERROR
, "Error loading suffix.");
128 startsched
= scheduleoneshot(time(NULL
) + 1, &lua_startup
, NULL
);
131 void lua_startup(void *arg
) {
140 #ifdef BROKEN_DLCLOSE
148 deleteschedule(startsched
, &lua_startup
, NULL
);
151 lua_unloadscript(lua_head
);
154 lua_destroycontrol();
160 freesstring(dummy
.name
);
162 lua_freedebugsocket();
163 nscheckfreeall(POOL_LUA
);
166 void lua_loadscripts(void) {
169 ls
= getconfigitems("lua", "script");
171 Error("lua", ERR_INFO
, "No scripts loaded.");
173 sstring
**scripts
= (sstring
**)(ls
->content
);
175 for(i
=0;i
<ls
->cursi
;i
++)
176 lua_loadscript(scripts
[i
]->content
);
180 /* taken from the lua manual, modified to use nsmalloc */
181 static void *lua_nsmalloc(void *ud
, void *ptr
, size_t osize
, size_t nsize
) {
188 return luarealloc(ptr
, nsize
);
191 lua_State
*lua_loadscript(char *file
) {
192 char fullpath
[LUA_PATHLEN
];
199 if(!cpath
|| !suffix
)
202 strlcpy(buf
, file
, sizeof(buf
));
204 delchars(buf
, "./\\;");
206 if(lua_scriptloaded(buf
))
209 l
= lua_newstate(lua_nsmalloc
, NULL
);
213 n
= (lua_list
*)luamalloc(sizeof(lua_list
));;
215 Error("lua", ERR_ERROR
, "Error allocing list for %s.", buf
);
219 n
->name
= getsstring(buf
, LUA_PATHLEN
);
221 Error("lua", ERR_ERROR
, "Error allocing name item for %s.", buf
);
227 timerclear(&n
->ru_utime
);
228 timerclear(&n
->ru_stime
);
231 lua_registerdebug(l
);
232 lua_registercommands(l
);
233 lua_registerlocalcommands(l
);
234 lua_registerdbcommands(l
);
235 lua_registersocketcommands(l
);
236 lua_registercryptocommands(l
);
237 lua_registerschedulercommands(l
);
241 triggerhook(HOOK_LUA_LOADSCRIPT
, args
);
244 lua_require(l
, "lib/jit");
247 lua_require(l
, "lib/bootstrap");
249 snprintf(fullpath
, sizeof(fullpath
), "%s/%s%s", cpath
->content
, file
, suffix
->content
);
250 if(luaL_loadfile(l
, fullpath
)) {
251 Error("lua", ERR_ERROR
, "Error loading %s.", file
);
253 freesstring(n
->name
);
264 n
->schedulers
= NULL
;
276 if(lua_pcall(l
, 0, 0, 0)) {
277 Error("lua", ERR_ERROR
, "Error pcalling: %s.", file
);
279 freesstring(n
->name
);
286 lua_tail
->next
= NULL
;
294 Error("lua", ERR_INFO
, "Loaded %s.", file
);
300 void lua_unloadscript(lua_list
*l
) {
301 triggerhook(HOOK_LUA_UNLOADSCRIPT
, l
->l
);
304 lua_deregisternicks(l
);
305 lua_socket_closeall(l
);
306 lua_scheduler_freeall(l
);
308 freesstring(l
->name
);
310 /* well, at least it's O(1) */
312 if(!l
->prev
) { /* head */
317 lua_head
->prev
= NULL
;
320 if(!l
->next
) { /* tail */
321 lua_tail
= lua_tail
->prev
;
325 lua_tail
->next
= NULL
;
328 l
->prev
->next
= l
->next
;
329 l
->next
->prev
= l
->prev
;
336 void lua_setpath(void) {
337 char fullpath
[LUA_PATHLEN
];
339 snprintf(fullpath
, sizeof(fullpath
), "%s/?%s", cpath
->content
, suffix
->content
);
340 setenv("LUA_PATH", fullpath
, 1);
343 lua_list
*lua_scriptloaded(char *name
) {
346 for(l
=lua_head
;l
;l
=l
->next
)
347 if(!strcmp(l
->name
->content
, name
))
353 void lua_loadlibs(lua_State
*l
) {
354 const luaL_Reg
*lib
= ourlibs
;
356 for (;lib
->func
;lib
++) {
357 lua_pushcfunction(l
, lib
->func
);
358 lua_pushstring(l
, lib
->name
);
363 void lua_require(lua_State
*l
, char *module) {
364 int top
= lua_gettop(l
);
366 lua_getglobal(l
, "require");
367 lua_pushstring(l
, module);
369 if(lua_pcall(l
, 1, 1, 0))
370 Error("lua", ERR_ERROR
, "Error requiring %s: %s", module, lua_tostring(l
, -1));
375 void lua_setupdebugsocket(void) {
376 #ifdef LUA_DEBUGSOCKET
378 debugsocket
= socket(AF_INET
, SOCK_DGRAM
, 0);
379 if(debugsocket
< 0) {
381 Error("lua", ERR_ERROR
, "Cannot create debug socket.");
386 memset(&debugsocketdest
, 0, sizeof(debugsocketdest
));
388 debugsocketdest
.sin_family
= AF_INET
;
389 debugsocketdest
.sin_port
= htons(LUA_DEBUGSOCKET_PORT
);
390 debugsocketdest
.sin_addr
.s_addr
= inet_addr(LUA_DEBUGSOCKET_ADDRESS
);
395 void lua_freedebugsocket(void) {
396 #ifdef LUA_DEBUGSOCKET
398 if(debugsocket
== -1)
409 #ifdef LUA_DEBUGSOCKET
410 void lua_debugoutput(char *format
, ...) {
415 if(debugsocket
== -1)
418 va_start(va
, format
);
419 size
= vsnprintf(buf
, sizeof(buf
), format
, va
);
422 if(size
>= sizeof(buf
))
423 size
= sizeof(buf
) - 1;
426 sendto(debugsocket
, buf
, size
, 0, (struct sockaddr
*)&debugsocketdest
, sizeof(debugsocketdest
));
430 lua_list
*lua_listfromstate(lua_State
*l
) {
431 lua_list
*i
= lua_head
;
439 int lua_listexists(lua_list
*l
) {
442 for(i
=lua_head
;i
;i
=i
->next
)
449 int lua_lineok(const char *data
) {
450 if(strchr(data
, '\r') || strchr(data
, '\n'))
455 int lua_debugpcall(lua_State
*l
, char *message
, int a
, int b
, int c
) {
456 lua_list
*l2
= lua_listfromstate(l
);
459 #ifdef LUA_DEBUGSOCKET
460 DEBUGOUT("%s: %s\n", l2
->name
->content
, message
);
464 ACCOUNTING_START(l2
);
467 ret
= lua_pcall(l
, a
, b
, c
);