]> jfr.im git - irc/quakenet/newserv.git/blob - lua/nterfacer_lua.c
TRUSTS: require sqlite
[irc/quakenet/newserv.git] / lua / nterfacer_lua.c
1 /*
2 nterfacer lua module
3 Copyright (C) 2006 Chris Porter.
4 */
5
6 #include "../lib/version.h"
7
8 #include "../lua/lua.h"
9
10 #include "../nterfacer/library.h"
11 #include "../nterfacer/nterfacer.h"
12
13 MODULE_VERSION("");
14
15 #define ERR_SCRIPT_NOT_FOUND 0x01
16 #define ERR_SCRIPT_ERROR 0x02
17 #define ERR_NO_NTERFACER 0x03
18 #define ERR_SCRIPT_RETURN_ERROR 0x04
19 #define ERR_DATA_INVALID 0x05
20 #define ERROR_SCRIPT_ERROR_OFFSET 1000
21
22 int handle_scriptcommand(struct rline *li, int argc, char **argv);
23
24 struct service_node *u_node;
25
26 void _init(void) {
27 u_node = register_service("U");
28 if(!u_node)
29 return;
30
31 register_handler(u_node, "scriptcommand", 2, handle_scriptcommand);
32 }
33
34 void _fini(void) {
35 if(u_node)
36 deregister_service(u_node);
37 }
38
39 int handle_scriptcommand(struct rline *li, int argc, char **argv) {
40 lua_list *l2 = lua_scriptloaded(argv[0]);
41 lua_State *l;
42 int top;
43 int iresult = 0;
44 int ret = 0, i;
45 char *cresult = NULL;
46
47 if(!l2)
48 return ri_error(li, ERR_SCRIPT_NOT_FOUND, "Script not found.");
49
50 l = l2->l;
51
52 top = lua_gettop(l);
53
54 lua_getglobal(l, "scripterror");
55 lua_getglobal(l, "onnterfacer");
56 if(!lua_isfunction(l, -1)) {
57 lua_settop(l, top);
58 return ri_error(li, ERR_NO_NTERFACER, "No nterfacer handler exists for this script.");
59 }
60
61 for(i=1;i<argc;i++)
62 lua_pushstring(l, argv[i]);
63
64 if(lua_debugpcall(l, "onnterfacer", argc - 1, 2, top + 1)) {
65 ret = ri_error(li, ERR_SCRIPT_ERROR, "Script error: %s", lua_tostring(l, -1));
66 lua_settop(l, top);
67 return ret;
68 }
69
70 if(!lua_isint(l, -2) || (!lua_isstring(l, -1) && !lua_istable(l, -1))) {
71 lua_settop(l, top);
72 return ri_error(li, ERR_SCRIPT_RETURN_ERROR, "Script didn't return expected values.");
73 }
74
75 if(lua_isstring(l, -1)) {
76 cresult = (char *)lua_tostring(l, -1);
77 if(!lua_lineok(cresult)) {
78 lua_settop(l, top);
79 return ri_error(li, ERR_DATA_INVALID, "Script returned bad data.");
80 }
81 }
82
83 iresult = lua_toint(l, -2);
84 if(iresult) {
85 if(!lua_isstring(l, -1)) {
86 lua_settop(l, top);
87 return ri_error(li, ERR_SCRIPT_RETURN_ERROR, "Script didn't return expected values.");
88 } else {
89 ret = ri_error(li, iresult + ERROR_SCRIPT_ERROR_OFFSET, "Script error: %s", cresult);
90 lua_settop(l, top);
91 return ret;
92 }
93 }
94
95 if(lua_isstring(l, -1)) {
96 ret = ri_append(li, "%s", cresult);
97 } else {
98 lua_pushnil(l);
99
100 while(lua_next(l, -2)) {
101 if(!lua_isstring(l, -1)) {
102 lua_pop(l, 1);
103 lua_settop(l, top);
104 return ri_error(li, ERR_SCRIPT_RETURN_ERROR, "Script didn't return expected values.");
105 } else {
106 cresult = (char *)lua_tostring(l, -1);
107 if(!lua_lineok(cresult)) {
108 lua_pop(l, 1);
109 lua_settop(l, top);
110 return ri_error(li, ERR_DATA_INVALID, "Script returned bad data.");
111 } else {
112 ret = ri_append(li, "%s", cresult);
113 }
114 }
115
116 lua_pop(l, 1);
117
118 if(ret == BF_OVER)
119 break;
120 }
121 }
122
123 lua_settop(l, top);
124
125 if(ret == BF_OVER)
126 return ri_error(li, BF_OVER, "Buffer overflow");
127
128 return ri_final(li);
129 }