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