]> jfr.im git - irc/quakenet/newserv.git/blob - lua/lua.c
merge
[irc/quakenet/newserv.git] / lua / lua.c
1 /* Lua bindings for Newserv */
2
3 /* Copyright (C) Chris Porter 2005 */
4 /* ALL RIGHTS RESERVED. */
5 /* Don't put this into the SVN repo. */
6
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"
14
15 #include "lua.h"
16
17 MODULE_VERSION(LUA_FULLVERSION);
18
19 #ifdef LUA_DEBUGSOCKET
20
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <netdb.h>
26 #include <unistd.h>
27 #include <stdarg.h>
28
29 #endif
30
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);
39
40 void lua_startbot(void *arg);
41 void lua_destroybot(void);
42 void lua_startcontrol(void);
43 void lua_destroycontrol(void);
44
45 void lua_onunload(lua_State *l);
46 void lua_onload(lua_State *l);
47
48 void lua_setpath(lua_State *l);
49
50 void lua_setupdebugsocket(void);
51 void lua_freedebugsocket(void);
52
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);
59
60 #ifdef LUA_DEBUGSOCKET
61
62 struct sockaddr_in debugsocketdest;
63 int debugsocket = -1;
64
65 #endif
66
67 lua_list *lua_head = NULL, *lua_tail = NULL;
68
69 sstring *cpath = NULL, *suffix = NULL;
70
71 void *startsched = NULL;
72
73 int loaded = 0;
74
75 struct rusage r_usages;
76 struct rusage r_usagee;
77
78 static const luaL_Reg ourlibs[] = {
79 {"", luaopen_base},
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},
86 #ifdef LUA_USEJIT
87 {LUA_JITLIBNAME, luaopen_jit},
88 #endif
89 {NULL, NULL}
90 };
91
92 lua_list dummy;
93
94 void _init() {
95 lua_setupdebugsocket();
96 lua_initnickpusher();
97 lua_initchanpusher();
98
99 dummy.name = getsstring("???", 10);
100 if(!dummy.name) {
101 Error("lua", ERR_ERROR, "Cannot set dummy name.");
102 return;
103 }
104
105 cpath = getcopyconfigitem("lua", "scriptdir", "", 500);
106
107 if(!cpath || !cpath->content || !cpath->content[0]) {
108 Error("lua", ERR_ERROR, "Error loading path.");
109 return;
110 }
111
112 suffix = getcopyconfigitem("lua", "scriptsuffix", ".lua", 10);
113 if(!suffix) {
114 Error("lua", ERR_ERROR, "Error loading suffix.");
115 return;
116 }
117
118 loaded = 1;
119
120 startsched = scheduleoneshot(time(NULL) + 1, &lua_startup, NULL);
121 }
122
123 void lua_startup(void *arg) {
124 startsched = NULL;
125
126 lua_startcontrol();
127 lua_startbot(NULL);
128
129 lua_loadscripts();
130 }
131
132 #ifdef BROKEN_DLCLOSE
133 void __fini() {
134 #else
135 void _fini() {
136 #endif
137
138 if(loaded) {
139 if(startsched)
140 deleteschedule(startsched, &lua_startup, NULL);
141
142 while(lua_head)
143 lua_unloadscript(lua_head);
144
145 lua_destroybot();
146 lua_destroycontrol();
147 }
148
149 freesstring(cpath);
150 freesstring(suffix);
151 freesstring(dummy.name);
152
153 lua_freedebugsocket();
154 nscheckfreeall(POOL_LUA);
155 }
156
157 void lua_loadscripts(void) {
158 array *ls;
159
160 ls = getconfigitems("lua", "script");
161 if(!ls) {
162 Error("lua", ERR_INFO, "No scripts loaded.");
163 } else {
164 sstring **scripts = (sstring **)(ls->content);
165 int i;
166 for(i=0;i<ls->cursi;i++)
167 lua_loadscript(scripts[i]->content);
168 }
169 }
170
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) {
173 if(nsize == 0) {
174 if(ptr != NULL)
175 luafree(ptr);
176 return NULL;
177 }
178
179 return luarealloc(ptr, nsize);
180 }
181
182 lua_State *lua_loadscript(char *file) {
183 char fullpath[LUA_PATHLEN];
184 int top;
185 lua_State *l;
186 lua_list *n;
187
188 if(!cpath || !suffix)
189 return NULL;
190
191 delchars(file, "./\\;");
192
193 if(lua_scriptloaded(file))
194 return NULL;
195
196 l = lua_newstate(lua_nsmalloc, NULL);
197 if(!l)
198 return NULL;
199
200 n = (lua_list *)luamalloc(sizeof(lua_list));;
201 if(!n) {
202 Error("lua", ERR_ERROR, "Error allocing list for %s.", file);
203 return NULL;
204 }
205
206 n->name = getsstring(file, LUA_PATHLEN);
207 if(!n->name) {
208 Error("lua", ERR_ERROR, "Error allocing name item for %s.", file);
209 luafree(n);
210 return NULL;
211 }
212 n->calls = 0;
213
214 timerclear(&n->ru_utime);
215 timerclear(&n->ru_stime);
216
217 lua_loadlibs(l);
218 lua_registerdebug(l);
219 lua_registercommands(l);
220 lua_registerlocalcommands(l);
221 lua_registerdbcommands(l);
222 lua_registersocketcommands(l);
223 lua_registercryptocommands(l);
224
225 lua_setpath(l);
226
227 #ifdef LUA_USEJIT
228 lua_require(l, "lib/jit");
229 #endif
230
231 lua_require(l, "lib/bootstrap");
232
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);
236 lua_close(l);
237 freesstring(n->name);
238 luafree(n);
239 return NULL;
240 }
241
242 n->l = l;
243
244 n->next = NULL;
245 n->prev = lua_tail;
246 n->nicks = NULL;
247 n->sockets = NULL;
248
249 if(!lua_head) {
250 lua_head = n;
251 } else {
252 lua_tail->next = n;
253 }
254
255 lua_tail = n;
256
257 top = lua_gettop(l);
258
259 if(lua_pcall(l, 0, 0, 0)) {
260 Error("lua", ERR_ERROR, "Error pcalling: %s.", file);
261 lua_close(l);
262 freesstring(n->name);
263 luafree(n);
264 return NULL;
265 }
266
267 lua_settop(l, top);
268
269 Error("lua", ERR_INFO, "Loaded %s.", file);
270 lua_onload(l);
271
272 return l;
273 }
274
275 void lua_unloadscript(lua_list *l) {
276 lua_onunload(l->l);
277 lua_deregisternicks(l);
278 lua_socket_closeall(l);
279 lua_close(l->l);
280 freesstring(l->name);
281
282 /* well, at least it's O(1) */
283
284 if(!l->prev) { /* head */
285 lua_head = l->next;
286 if(!lua_head) {
287 lua_tail = NULL;
288 } else {
289 lua_head->prev = NULL;
290 }
291 } else {
292 if(!l->next) { /* tail */
293 lua_tail = lua_tail->prev;
294 if(!lua_tail) {
295 lua_head = NULL;
296 } else {
297 lua_tail->next = NULL;
298 }
299 } else {
300 l->prev->next = l->next;
301 l->next->prev = l->prev;
302 }
303 }
304
305 luafree(l);
306 }
307
308 void lua_setpath(lua_State *l) {
309 char fullpath[LUA_PATHLEN], *prev = NULL;
310
311 int top = lua_gettop(l);
312
313 lua_getglobal(l, "package");
314 if(!lua_istable(l, 1)) {
315 Error("lua", ERR_ERROR, "Unable to set package.path (package is not a table).");
316 return;
317 }
318
319 lua_pushstring(l, "path");
320 lua_gettable(l, -2);
321
322 if(lua_isstring(l, -1))
323 prev = (char *)lua_tostring(l, -1);
324
325 if(prev) {
326 snprintf(fullpath, sizeof(fullpath), "%s;%s/?%s", prev, cpath->content, suffix->content);
327 } else {
328 snprintf(fullpath, sizeof(fullpath), "%s/?%s", cpath->content, suffix->content);
329 }
330
331 /* pop broke! */
332
333 lua_getglobal(l, "package");
334 if(!lua_istable(l, 1)) {
335 Error("lua", ERR_ERROR, "Unable to set package.path (package is not a table).");
336 return;
337 }
338
339 lua_pushstring(l, "path");
340
341 lua_pushstring(l, fullpath);
342 lua_settable(l, -3);
343
344 lua_settop(l, top);
345 }
346
347 lua_list *lua_scriptloaded(char *name) {
348 lua_list *l;
349
350 for(l=lua_head;l;l=l->next)
351 if(!strcmp(l->name->content, name))
352 return l;
353
354 return NULL;
355 }
356
357 void lua_loadlibs(lua_State *l) {
358 const luaL_Reg *lib = ourlibs;
359
360 for (;lib->func;lib++) {
361 lua_pushcfunction(l, lib->func);
362 lua_pushstring(l, lib->name);
363 lua_call(l, 1, 0);
364 }
365 }
366
367 void lua_require(lua_State *l, char *module) {
368 int top = lua_gettop(l);
369
370 lua_getglobal(l, "require");
371 lua_pushstring(l, module);
372
373 if(lua_pcall(l, 1, 1, 0))
374 Error("lua", ERR_ERROR, "Error requiring %s: %s", module, lua_tostring(l, -1));
375
376 lua_settop(l, top);
377 }
378
379 void lua_setupdebugsocket(void) {
380 #ifdef LUA_DEBUGSOCKET
381
382 debugsocket = socket(AF_INET, SOCK_DGRAM, 0);
383 if(debugsocket < 0) {
384 debugsocket = -1;
385 Error("lua", ERR_ERROR, "Cannot create debug socket.");
386
387 return;
388 }
389
390 memset(&debugsocketdest, 0, sizeof(debugsocketdest));
391
392 debugsocketdest.sin_family = AF_INET;
393 debugsocketdest.sin_port = htons(LUA_DEBUGSOCKET_PORT);
394 debugsocketdest.sin_addr.s_addr = inet_addr(LUA_DEBUGSOCKET_ADDRESS);
395
396 #endif
397 }
398
399 void lua_freedebugsocket(void) {
400 #ifdef LUA_DEBUGSOCKET
401
402 if(debugsocket == -1)
403 return;
404
405 close(debugsocket);
406
407
408 debugsocket = -1;
409
410 #endif
411 }
412
413 #ifdef LUA_DEBUGSOCKET
414 void lua_debugoutput(char *format, ...) {
415 char buf[1024];
416 va_list va;
417 int size;
418
419 if(debugsocket == -1)
420 return;
421
422 va_start(va, format);
423 size = vsnprintf(buf, sizeof(buf), format, va);
424 va_end(va);
425
426 if(size >= sizeof(buf))
427 size = sizeof(buf) - 1;
428
429 if(size > 0)
430 sendto(debugsocket, buf, size, 0, (struct sockaddr *)&debugsocketdest, sizeof(debugsocketdest));
431 }
432 #endif
433
434 lua_list *lua_listfromstate(lua_State *l) {
435 lua_list *i = lua_head;
436 for(;i;i=i->next)
437 if(i->l == l)
438 return i;
439
440 return &dummy;
441 }
442
443 int lua_listexists(lua_list *l) {
444 lua_list *i;
445
446 for(i=lua_head;i;i=i->next)
447 if(i == l)
448 return 1;
449
450 return 0;
451 }
452
453 int lua_lineok(const char *data) {
454 if(strchr(data, '\r') || strchr(data, '\n'))
455 return 0;
456 return 1;
457 }
458
459 INLINE int lua_debugpcall(lua_State *l, char *message, int a, int b, int c) {
460 lua_list *l2 = lua_listfromstate(l);
461 int ret;
462
463 #ifdef LUA_DEBUGSOCKET
464 DEBUGOUT("%s: %s\n", l2->name->content, message);
465 #endif
466
467 #ifdef LUA_PROFILE
468 ACCOUNTING_START(l2);
469 #endif
470
471 ret = lua_pcall(l, a, b, c);
472
473 #ifdef LUA_PROFILE
474 ACCOUNTING_STOP(l2);
475 #endif
476
477 return ret;
478 }
479