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
);
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_registerdbcommands(lua_State
*l
);
35 void lua_initnickpusher(void);
36 void lua_initchanpusher(void);
37 void lua_loadlibs(lua_State
*l
);
38 void lua_require(lua_State
*l
, char *module);
40 void lua_startbot(void *arg
);
41 void lua_destroybot(void);
42 void lua_startcontrol(void);
43 void lua_destroycontrol(void);
45 void lua_onunload(lua_State
*l
);
46 void lua_onload(lua_State
*l
);
48 void lua_setpath(lua_State
*l
);
50 void lua_setupdebugsocket(void);
51 void lua_freedebugsocket(void);
53 void lua_deregisternicks(lua_list
*l
);
54 void lua_registerlocalcommands(lua_State
*ps
);
55 void lua_registerdebug(lua_State
*ps
);
56 void lua_socket_closeall(lua_list
*l
);
57 void lua_registersocketcommands(lua_State
*ps
);
58 void lua_registercryptocommands(lua_State
*ps
);
60 #ifdef LUA_DEBUGSOCKET
62 struct sockaddr_in debugsocketdest
;
67 lua_list
*lua_head
= NULL
, *lua_tail
= NULL
;
69 sstring
*cpath
= NULL
, *suffix
= NULL
;
71 void *startsched
= NULL
;
75 struct rusage r_usages
;
76 struct rusage r_usagee
;
78 static const luaL_Reg ourlibs
[] = {
80 {LUA_LOADLIBNAME
, luaopen_package
},
81 {LUA_TABLIBNAME
, luaopen_table
},
82 {LUA_IOLIBNAME
, luaopen_io
},
83 {LUA_OSLIBNAME
, luaopen_os
},
84 {LUA_STRLIBNAME
, luaopen_string
},
85 {LUA_MATHLIBNAME
, luaopen_math
},
87 {LUA_JITLIBNAME
, luaopen_jit
},
95 lua_setupdebugsocket();
99 dummy
.name
= getsstring("???", 10);
101 Error("lua", ERR_ERROR
, "Cannot set dummy name.");
105 cpath
= getcopyconfigitem("lua", "scriptdir", "", 500);
107 if(!cpath
|| !cpath
->content
|| !cpath
->content
[0]) {
108 Error("lua", ERR_ERROR
, "Error loading path.");
112 suffix
= getcopyconfigitem("lua", "scriptsuffix", ".lua", 10);
114 Error("lua", ERR_ERROR
, "Error loading suffix.");
120 startsched
= scheduleoneshot(time(NULL
) + 1, &lua_startup
, NULL
);
123 void lua_startup(void *arg
) {
132 #ifdef BROKEN_DLCLOSE
140 deleteschedule(startsched
, &lua_startup
, NULL
);
143 lua_unloadscript(lua_head
);
146 lua_destroycontrol();
151 freesstring(dummy
.name
);
153 lua_freedebugsocket();
154 nscheckfreeall(POOL_LUA
);
157 void lua_loadscripts(void) {
160 ls
= getconfigitems("lua", "script");
162 Error("lua", ERR_INFO
, "No scripts loaded.");
164 sstring
**scripts
= (sstring
**)(ls
->content
);
166 for(i
=0;i
<ls
->cursi
;i
++)
167 lua_loadscript(scripts
[i
]->content
);
171 /* taken from the lua manual, modified to use nsmalloc */
172 static void *lua_nsmalloc(void *ud
, void *ptr
, size_t osize
, size_t nsize
) {
179 return luarealloc(ptr
, nsize
);
182 lua_State
*lua_loadscript(char *file
) {
183 char fullpath
[LUA_PATHLEN
];
188 if(!cpath
|| !suffix
)
191 delchars(file
, "./\\;");
193 if(lua_scriptloaded(file
))
196 l
= lua_newstate(lua_nsmalloc
, NULL
);
200 n
= (lua_list
*)luamalloc(sizeof(lua_list
));;
202 Error("lua", ERR_ERROR
, "Error allocing list for %s.", file
);
206 n
->name
= getsstring(file
, LUA_PATHLEN
);
208 Error("lua", ERR_ERROR
, "Error allocing name item for %s.", file
);
214 timerclear(&n
->ru_utime
);
215 timerclear(&n
->ru_stime
);
218 lua_registerdebug(l
);
219 lua_registercommands(l
);
220 lua_registerlocalcommands(l
);
221 lua_registerdbcommands(l
);
222 lua_registersocketcommands(l
);
223 lua_registercryptocommands(l
);
228 lua_require(l
, "lib/jit");
231 lua_require(l
, "lib/bootstrap");
233 snprintf(fullpath
, sizeof(fullpath
), "%s/%s%s", cpath
->content
, file
, suffix
->content
);
234 if(luaL_loadfile(l
, fullpath
)) {
235 Error("lua", ERR_ERROR
, "Error loading %s.", file
);
237 freesstring(n
->name
);
259 if(lua_pcall(l
, 0, 0, 0)) {
260 Error("lua", ERR_ERROR
, "Error pcalling: %s.", file
);
262 freesstring(n
->name
);
269 lua_tail
->next
= NULL
;
277 Error("lua", ERR_INFO
, "Loaded %s.", file
);
283 void lua_unloadscript(lua_list
*l
) {
285 lua_deregisternicks(l
);
286 lua_socket_closeall(l
);
288 freesstring(l
->name
);
290 /* well, at least it's O(1) */
292 if(!l
->prev
) { /* head */
297 lua_head
->prev
= NULL
;
300 if(!l
->next
) { /* tail */
301 lua_tail
= lua_tail
->prev
;
305 lua_tail
->next
= NULL
;
308 l
->prev
->next
= l
->next
;
309 l
->next
->prev
= l
->prev
;
316 void lua_setpath(lua_State
*l
) {
317 char fullpath
[LUA_PATHLEN
], *prev
= NULL
;
319 int top
= lua_gettop(l
);
321 lua_getglobal(l
, "package");
322 if(!lua_istable(l
, 1)) {
323 Error("lua", ERR_ERROR
, "Unable to set package.path (package is not a table).");
327 lua_pushstring(l
, "path");
330 if(lua_isstring(l
, -1))
331 prev
= (char *)lua_tostring(l
, -1);
334 snprintf(fullpath
, sizeof(fullpath
), "%s;%s/?%s", prev
, cpath
->content
, suffix
->content
);
336 snprintf(fullpath
, sizeof(fullpath
), "%s/?%s", cpath
->content
, suffix
->content
);
341 lua_getglobal(l
, "package");
342 if(!lua_istable(l
, 1)) {
343 Error("lua", ERR_ERROR
, "Unable to set package.path (package is not a table).");
347 lua_pushstring(l
, "path");
349 lua_pushstring(l
, fullpath
);
355 lua_list
*lua_scriptloaded(char *name
) {
358 for(l
=lua_head
;l
;l
=l
->next
)
359 if(!strcmp(l
->name
->content
, name
))
365 void lua_loadlibs(lua_State
*l
) {
366 const luaL_Reg
*lib
= ourlibs
;
368 for (;lib
->func
;lib
++) {
369 lua_pushcfunction(l
, lib
->func
);
370 lua_pushstring(l
, lib
->name
);
375 void lua_require(lua_State
*l
, char *module) {
376 int top
= lua_gettop(l
);
378 lua_getglobal(l
, "require");
379 lua_pushstring(l
, module);
381 if(lua_pcall(l
, 1, 1, 0))
382 Error("lua", ERR_ERROR
, "Error requiring %s: %s", module, lua_tostring(l
, -1));
387 void lua_setupdebugsocket(void) {
388 #ifdef LUA_DEBUGSOCKET
390 debugsocket
= socket(AF_INET
, SOCK_DGRAM
, 0);
391 if(debugsocket
< 0) {
393 Error("lua", ERR_ERROR
, "Cannot create debug socket.");
398 memset(&debugsocketdest
, 0, sizeof(debugsocketdest
));
400 debugsocketdest
.sin_family
= AF_INET
;
401 debugsocketdest
.sin_port
= htons(LUA_DEBUGSOCKET_PORT
);
402 debugsocketdest
.sin_addr
.s_addr
= inet_addr(LUA_DEBUGSOCKET_ADDRESS
);
407 void lua_freedebugsocket(void) {
408 #ifdef LUA_DEBUGSOCKET
410 if(debugsocket
== -1)
421 #ifdef LUA_DEBUGSOCKET
422 void lua_debugoutput(char *format
, ...) {
427 if(debugsocket
== -1)
430 va_start(va
, format
);
431 size
= vsnprintf(buf
, sizeof(buf
), format
, va
);
434 if(size
>= sizeof(buf
))
435 size
= sizeof(buf
) - 1;
438 sendto(debugsocket
, buf
, size
, 0, (struct sockaddr
*)&debugsocketdest
, sizeof(debugsocketdest
));
442 lua_list
*lua_listfromstate(lua_State
*l
) {
443 lua_list
*i
= lua_head
;
451 int lua_listexists(lua_list
*l
) {
454 for(i
=lua_head
;i
;i
=i
->next
)
461 int lua_lineok(const char *data
) {
462 if(strchr(data
, '\r') || strchr(data
, '\n'))
467 INLINE
int lua_debugpcall(lua_State
*l
, char *message
, int a
, int b
, int c
) {
468 lua_list
*l2
= lua_listfromstate(l
);
471 #ifdef LUA_DEBUGSOCKET
472 DEBUGOUT("%s: %s\n", l2
->name
->content
, message
);
476 ACCOUNTING_START(l2
);
479 ret
= lua_pcall(l
, a
, b
, c
);