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