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_scheduler_freeall(lua_list
*l
);
61 void lua_registersocketcommands(lua_State
*ps
);
62 void lua_registercryptocommands(lua_State
*ps
);
63 void lua_registerschedulercommands(lua_State
*ps
);
65 #ifdef LUA_DEBUGSOCKET
67 struct sockaddr_in debugsocketdest
;
72 lua_list
*lua_head
= NULL
, *lua_tail
= NULL
;
74 sstring
*cpath
= NULL
, *suffix
= NULL
;
76 void *startsched
= NULL
;
80 struct rusage r_usages
;
81 struct rusage r_usagee
;
83 static const luaL_Reg ourlibs
[] = {
85 {LUA_LOADLIBNAME
, luaopen_package
},
86 {LUA_TABLIBNAME
, luaopen_table
},
87 {LUA_IOLIBNAME
, luaopen_io
},
88 {LUA_OSLIBNAME
, luaopen_os
},
89 {LUA_STRLIBNAME
, luaopen_string
},
90 {LUA_MATHLIBNAME
, luaopen_math
},
92 {LUA_JITLIBNAME
, luaopen_jit
},
100 lua_setupdebugsocket();
101 lua_initnickpusher();
102 lua_initchanpusher();
104 dummy
.name
= getsstring("???", 10);
106 Error("lua", ERR_ERROR
, "Cannot set dummy name.");
110 cpath
= getcopyconfigitem("lua", "scriptdir", "", 500);
112 if(!cpath
|| !cpath
->content
|| !cpath
->content
[0]) {
113 Error("lua", ERR_ERROR
, "Error loading path.");
117 suffix
= getcopyconfigitem("lua", "scriptsuffix", ".lua", 10);
119 Error("lua", ERR_ERROR
, "Error loading suffix.");
127 startsched
= scheduleoneshot(time(NULL
) + 1, &lua_startup
, NULL
);
130 void lua_startup(void *arg
) {
139 #ifdef BROKEN_DLCLOSE
147 deleteschedule(startsched
, &lua_startup
, NULL
);
150 lua_unloadscript(lua_head
);
153 lua_destroycontrol();
158 freesstring(dummy
.name
);
160 lua_freedebugsocket();
161 nscheckfreeall(POOL_LUA
);
164 void lua_loadscripts(void) {
167 ls
= getconfigitems("lua", "script");
169 Error("lua", ERR_INFO
, "No scripts loaded.");
171 sstring
**scripts
= (sstring
**)(ls
->content
);
173 for(i
=0;i
<ls
->cursi
;i
++)
174 lua_loadscript(scripts
[i
]->content
);
178 /* taken from the lua manual, modified to use nsmalloc */
179 static void *lua_nsmalloc(void *ud
, void *ptr
, size_t osize
, size_t nsize
) {
186 return luarealloc(ptr
, nsize
);
189 lua_State
*lua_loadscript(char *file
) {
190 char fullpath
[LUA_PATHLEN
];
196 if(!cpath
|| !suffix
)
199 strlcpy(buf
, file
, sizeof(buf
));
201 delchars(buf
, "./\\;");
203 if(lua_scriptloaded(buf
))
206 l
= lua_newstate(lua_nsmalloc
, NULL
);
210 n
= (lua_list
*)luamalloc(sizeof(lua_list
));;
212 Error("lua", ERR_ERROR
, "Error allocing list for %s.", buf
);
216 n
->name
= getsstring(buf
, LUA_PATHLEN
);
218 Error("lua", ERR_ERROR
, "Error allocing name item for %s.", buf
);
224 timerclear(&n
->ru_utime
);
225 timerclear(&n
->ru_stime
);
228 lua_registerdebug(l
);
229 lua_registercommands(l
);
230 lua_registerlocalcommands(l
);
231 lua_registerdbcommands(l
);
232 lua_registersocketcommands(l
);
233 lua_registercryptocommands(l
);
234 lua_registerschedulercommands(l
);
237 lua_require(l
, "lib/jit");
240 lua_require(l
, "lib/bootstrap");
242 snprintf(fullpath
, sizeof(fullpath
), "%s/%s%s", cpath
->content
, file
, suffix
->content
);
243 if(luaL_loadfile(l
, fullpath
)) {
244 Error("lua", ERR_ERROR
, "Error loading %s.", file
);
246 freesstring(n
->name
);
257 n
->schedulers
= NULL
;
269 if(lua_pcall(l
, 0, 0, 0)) {
270 Error("lua", ERR_ERROR
, "Error pcalling: %s.", file
);
272 freesstring(n
->name
);
279 lua_tail
->next
= NULL
;
287 Error("lua", ERR_INFO
, "Loaded %s.", file
);
293 void lua_unloadscript(lua_list
*l
) {
295 lua_deregisternicks(l
);
296 lua_socket_closeall(l
);
297 lua_scheduler_freeall(l
);
299 freesstring(l
->name
);
301 /* well, at least it's O(1) */
303 if(!l
->prev
) { /* head */
308 lua_head
->prev
= NULL
;
311 if(!l
->next
) { /* tail */
312 lua_tail
= lua_tail
->prev
;
316 lua_tail
->next
= NULL
;
319 l
->prev
->next
= l
->next
;
320 l
->next
->prev
= l
->prev
;
327 void lua_setpath(void) {
328 char fullpath
[LUA_PATHLEN
];
330 snprintf(fullpath
, sizeof(fullpath
), "%s/?%s", cpath
->content
, suffix
->content
);
331 setenv("LUA_PATH", fullpath
, 1);
334 lua_list
*lua_scriptloaded(char *name
) {
337 for(l
=lua_head
;l
;l
=l
->next
)
338 if(!strcmp(l
->name
->content
, name
))
344 void lua_loadlibs(lua_State
*l
) {
345 const luaL_Reg
*lib
= ourlibs
;
347 for (;lib
->func
;lib
++) {
348 lua_pushcfunction(l
, lib
->func
);
349 lua_pushstring(l
, lib
->name
);
354 void lua_require(lua_State
*l
, char *module) {
355 int top
= lua_gettop(l
);
357 lua_getglobal(l
, "require");
358 lua_pushstring(l
, module);
360 if(lua_pcall(l
, 1, 1, 0))
361 Error("lua", ERR_ERROR
, "Error requiring %s: %s", module, lua_tostring(l
, -1));
366 void lua_setupdebugsocket(void) {
367 #ifdef LUA_DEBUGSOCKET
369 debugsocket
= socket(AF_INET
, SOCK_DGRAM
, 0);
370 if(debugsocket
< 0) {
372 Error("lua", ERR_ERROR
, "Cannot create debug socket.");
377 memset(&debugsocketdest
, 0, sizeof(debugsocketdest
));
379 debugsocketdest
.sin_family
= AF_INET
;
380 debugsocketdest
.sin_port
= htons(LUA_DEBUGSOCKET_PORT
);
381 debugsocketdest
.sin_addr
.s_addr
= inet_addr(LUA_DEBUGSOCKET_ADDRESS
);
386 void lua_freedebugsocket(void) {
387 #ifdef LUA_DEBUGSOCKET
389 if(debugsocket
== -1)
400 #ifdef LUA_DEBUGSOCKET
401 void lua_debugoutput(char *format
, ...) {
406 if(debugsocket
== -1)
409 va_start(va
, format
);
410 size
= vsnprintf(buf
, sizeof(buf
), format
, va
);
413 if(size
>= sizeof(buf
))
414 size
= sizeof(buf
) - 1;
417 sendto(debugsocket
, buf
, size
, 0, (struct sockaddr
*)&debugsocketdest
, sizeof(debugsocketdest
));
421 lua_list
*lua_listfromstate(lua_State
*l
) {
422 lua_list
*i
= lua_head
;
430 int lua_listexists(lua_list
*l
) {
433 for(i
=lua_head
;i
;i
=i
->next
)
440 int lua_lineok(const char *data
) {
441 if(strchr(data
, '\r') || strchr(data
, '\n'))
446 int lua_debugpcall(lua_State
*l
, char *message
, int a
, int b
, int c
) {
447 lua_list
*l2
= lua_listfromstate(l
);
450 #ifdef LUA_DEBUGSOCKET
451 DEBUGOUT("%s: %s\n", l2
->name
->content
, message
);
455 ACCOUNTING_START(l2
);
458 ret
= lua_pcall(l
, a
, b
, c
);