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 #include "../core/config.h"
8 #include "../core/error.h"
9 #include "../core/hooks.h"
10 #include "../lib/array.h"
11 #include "../lib/irc_string.h"
12 #include "../core/schedule.h"
13 #include "../lib/version.h"
17 MODULE_VERSION(LUA_FULLVERSION
" / $Id: lua.c 699 2006-06-07 14:17:26Z newserv $")
19 #ifdef LUA_DEBUGSOCKET
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
31 void lua_startup(void *arg
);
32 void lua_loadscripts(void);
33 void lua_registercommands(lua_State
*l
);
34 void lua_initnickpusher(void);
35 void lua_initchanpusher(void);
36 void lua_loadlibs(lua_State
*l
);
37 void lua_require(lua_State
*l
, char *module);
39 void lua_startbot(void *arg
);
40 void lua_destroybot(void);
41 void lua_startcontrol(void);
42 void lua_destroycontrol(void);
44 void lua_onunload(lua_State
*l
);
45 void lua_onload(lua_State
*l
);
47 void lua_setpath(lua_State
*l
);
49 void lua_setupdebugsocket(void);
50 void lua_freedebugsocket(void);
52 void lua_deregisternicks(lua_list
*l
);
53 void lua_registerlocalcommands(lua_State
*ps
);
54 void lua_registerdebug(lua_State
*ps
);
56 #ifdef LUA_DEBUGSOCKET
58 struct sockaddr_in debugsocketdest
;
63 lua_list
*lua_head
= NULL
, *lua_tail
= NULL
;
65 sstring
*cpath
= NULL
, *suffix
= NULL
;
67 void *startsched
= NULL
;
71 struct rusage r_usages
;
72 struct rusage r_usagee
;
74 static const luaL_Reg ourlibs
[] = {
76 {LUA_LOADLIBNAME
, luaopen_package
},
77 {LUA_TABLIBNAME
, luaopen_table
},
78 {LUA_IOLIBNAME
, luaopen_io
},
79 {LUA_OSLIBNAME
, luaopen_os
},
80 {LUA_STRLIBNAME
, luaopen_string
},
81 {LUA_MATHLIBNAME
, luaopen_math
},
83 {LUA_JITLIBNAME
, luaopen_jit
},
91 lua_setupdebugsocket();
95 dummy
.name
= getsstring("???", 10);
97 Error("lua", ERR_ERROR
, "Cannot set dummy name.");
101 cpath
= getcopyconfigitem("lua", "scriptdir", "", 500);
103 if(!cpath
|| !cpath
->content
|| !cpath
->content
[0]) {
104 Error("lua", ERR_ERROR
, "Error loading path.");
108 suffix
= getcopyconfigitem("lua", "scriptsuffix", ".lua", 10);
110 Error("lua", ERR_ERROR
, "Error loading suffix.");
116 startsched
= scheduleoneshot(time(NULL
) + 1, &lua_startup
, NULL
);
119 void lua_startup(void *arg
) {
128 #ifdef BROKEN_DLCLOSE
136 deleteschedule(startsched
, &lua_startup
, NULL
);
139 lua_unloadscript(lua_head
);
142 lua_destroycontrol();
147 freesstring(dummy
.name
);
149 lua_freedebugsocket();
152 void lua_loadscripts(void) {
155 ls
= getconfigitems("lua", "script");
157 Error("lua", ERR_INFO
, "No scripts loaded.");
159 sstring
**scripts
= (sstring
**)(ls
->content
);
161 for(i
=0;i
<ls
->cursi
;i
++)
162 lua_loadscript(scripts
[i
]->content
);
166 lua_State
*lua_loadscript(char *file
) {
167 char fullpath
[LUA_PATHLEN
];
172 if(!cpath
|| !suffix
)
175 delchars(file
, "./\\;");
177 if(lua_scriptloaded(file
))
184 n
= (lua_list
*)malloc(sizeof(lua_list
));;
186 Error("lua", ERR_ERROR
, "Error allocing list for %s.", file
);
190 n
->name
= getsstring(file
, LUA_PATHLEN
);
192 Error("lua", ERR_ERROR
, "Error allocing name item for %s.", file
);
198 timerclear(&n
->ru_utime
);
199 timerclear(&n
->ru_stime
);
202 lua_registerdebug(l
);
203 lua_registercommands(l
);
204 lua_registerlocalcommands(l
);
209 lua_require(l
, "lib/jit");
212 lua_require(l
, "lib/bootstrap");
214 snprintf(fullpath
, sizeof(fullpath
), "%s/%s%s", cpath
->content
, file
, suffix
->content
);
215 if(luaL_loadfile(l
, fullpath
)) {
216 Error("lua", ERR_ERROR
, "Error loading %s.", file
);
218 freesstring(n
->name
);
225 if(lua_pcall(l
, 0, 0, 0)) {
226 Error("lua", ERR_ERROR
, "Error pcalling: %s.", file
);
228 freesstring(n
->name
);
235 Error("lua", ERR_INFO
, "Loaded %s.", file
);
255 void lua_unloadscript(lua_list
*l
) {
257 lua_deregisternicks(l
);
259 freesstring(l
->name
);
261 /* well, at least it's O(1) */
263 if(!l
->prev
) { /* head */
268 lua_head
->prev
= NULL
;
271 if(!l
->next
) { /* tail */
272 lua_tail
= lua_tail
->prev
;
276 lua_tail
->next
= NULL
;
279 l
->prev
->next
= l
->next
;
280 l
->next
->prev
= l
->prev
;
287 void lua_setpath(lua_State
*l
) {
288 char fullpath
[LUA_PATHLEN
], *prev
= NULL
;
290 int top
= lua_gettop(l
);
292 lua_getglobal(l
, "package");
293 if(!lua_istable(l
, 1)) {
294 Error("lua", ERR_ERROR
, "Unable to set package.path (package is not a table).");
298 lua_pushstring(l
, "path");
301 if(lua_isstring(l
, -1))
302 prev
= (char *)lua_tostring(l
, -1);
305 snprintf(fullpath
, sizeof(fullpath
), "%s;%s/?%s", prev
, cpath
->content
, suffix
->content
);
307 snprintf(fullpath
, sizeof(fullpath
), "%s/?%s", cpath
->content
, suffix
->content
);
312 lua_getglobal(l
, "package");
313 if(!lua_istable(l
, 1)) {
314 Error("lua", ERR_ERROR
, "Unable to set package.path (package is not a table).");
318 lua_pushstring(l
, "path");
320 lua_pushstring(l
, fullpath
);
326 lua_list
*lua_scriptloaded(char *name
) {
329 for(l
=lua_head
;l
;l
=l
->next
)
330 if(!strcmp(l
->name
->content
, name
))
336 void lua_loadlibs(lua_State
*l
) {
337 const luaL_Reg
*lib
= ourlibs
;
339 for (;lib
->func
;lib
++) {
340 lua_pushcfunction(l
, lib
->func
);
341 lua_pushstring(l
, lib
->name
);
346 void lua_require(lua_State
*l
, char *module) {
347 int top
= lua_gettop(l
);
349 lua_getglobal(l
, "require");
350 lua_pushstring(l
, module);
352 if(lua_pcall(l
, 1, 1, 0))
353 Error("lua", ERR_ERROR
, "Error requiring %s: %s", module, lua_tostring(l
, -1));
358 void lua_setupdebugsocket(void) {
359 #ifdef LUA_DEBUGSOCKET
361 debugsocket
= socket(AF_INET
, SOCK_DGRAM
, 0);
362 if(debugsocket
< 0) {
364 Error("lua", ERR_ERROR
, "Cannot create debug socket.");
369 memset(&debugsocketdest
, 0, sizeof(debugsocketdest
));
371 debugsocketdest
.sin_family
= AF_INET
;
372 debugsocketdest
.sin_port
= htons(LUA_DEBUGSOCKET_PORT
);
373 debugsocketdest
.sin_addr
.s_addr
= inet_addr(LUA_DEBUGSOCKET_ADDRESS
);
378 void lua_freedebugsocket(void) {
379 #ifdef LUA_DEBUGSOCKET
381 if(debugsocket
== -1)
392 #ifdef LUA_DEBUGSOCKET
393 void lua_debugoutput(char *format
, ...) {
398 if(debugsocket
== -1)
401 va_start(va
, format
);
402 size
= vsnprintf(buf
, sizeof(buf
), format
, va
);
405 if(size
>= sizeof(buf
))
406 size
= sizeof(buf
) - 1;
409 sendto(debugsocket
, buf
, size
, 0, (struct sockaddr
*)&debugsocketdest
, sizeof(debugsocketdest
));
413 lua_list
*lua_listfromstate(lua_State
*l
) {
414 lua_list
*i
= lua_head
;
422 int lua_lineok(const char *data
) {
423 if(strchr(data
, '\r') || strchr(data
, '\n'))
428 INLINE
int lua_debugpcall(lua_State
*l
, char *message
, int a
, int b
, int c
) {
429 lua_list
*l2
= lua_listfromstate(l
);
432 #ifdef LUA_DEBUGSOCKET
433 DEBUGOUT("%s: %s\n", l2
->name
->content
, message
);
437 ACCOUNTING_START(l2
);
440 ret
= lua_pcall(l
, a
, b
, c
);