]> jfr.im git - irc/quakenet/newserv.git/blob - lua/luadb.c
CHANSERV: authtracker now keeps 240 days history
[irc/quakenet/newserv.git] / lua / luadb.c
1 #include "lua.h"
2 #include "luabot.h"
3
4 #include "../config.h"
5
6 #ifdef HAVE_PGSQL
7 #include "../dbapi/dbapi.h"
8
9 #warning Needs testing with new db API.
10
11 static int lua_dbcreatequery(lua_State *ps) {
12 char *s = (char *)lua_tostring(ps, 1);
13
14 if(!s)
15 LUA_RETURN(ps, LUA_FAIL);
16
17 dbcreatequery("%s", s);
18 LUA_RETURN(ps, LUA_OK);
19 }
20
21 /* TODO */
22 /*
23 static int lua_dbloadtable(lua_State *ps) {
24 lua_list *l = lua_listfromstate(ps);
25 if(!l)
26 LUA_RETURN(ps, LUA_FAIL);
27
28 }
29 */
30
31 typedef struct lua_callback {
32 lua_list *l;
33 long handler, args;
34 } lua_callback;
35
36 /* hack */
37 DBResult *pgres;
38
39 static int lua_dbnumfields(lua_State *ps) {
40 lua_pushint(ps, dbnumfields(pgres));
41
42 return 1;
43 }
44
45 /* TODO */
46 /*
47 static int lua_dbgetrow(lua_State *ps) {
48 char *r;
49 int tuple, field, len;
50
51 if(!lua_isint(ps, 1))
52 return 0;
53
54 lua_pushstring(ps, dbgetvalue(pgres, lua_toint(ps, 1)), len);
55
56 return 1;
57 }
58 */
59
60 void lua_dbcallback(DBConn *dbconn, void *arg) {
61 pgres = dbgetresult(dbconn);
62 lua_callback *c = (lua_callback *)arg;
63
64 if(!lua_listexists(c->l)) {
65 luafree(c);
66 return;
67 }
68
69 if(!dbquerysuccessful(pgres)) {
70 _lua_vpcall(c->l->l, (void *)c->handler, LUA_POINTERMODE, "bR", 0, c->args);
71
72 luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->handler);
73 luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->args);
74 luafree(c);
75 return;
76 }
77
78 _lua_vpcall(c->l->l, (void *)c->handler, LUA_POINTERMODE, "bR", 1, c->args);
79
80 luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->handler);
81 luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->args);
82 luafree(c);
83 dbclear(pgres);
84 pgres = NULL;
85 }
86
87 static int lua_dbquery(lua_State *ps) {
88 lua_list *l = lua_listfromstate(ps);
89 char *q = (char *)lua_tostring(ps, 1);
90 lua_callback *cb;
91
92 /* what happens if 3 isn't there? */
93 if(!l || !q)
94 LUA_RETURN(ps, LUA_FAIL);
95
96 if(!lua_isfunction(ps, 2)) {
97 dbquery("%s", q);
98 LUA_RETURN(ps, LUA_OK);
99 }
100
101 cb = (lua_callback *)luamalloc(sizeof(lua_callback));
102 if(!cb)
103 LUA_RETURN(ps, LUA_FAIL);
104
105 cb->l = l;
106 cb->args = luaL_ref(ps, LUA_REGISTRYINDEX);
107 cb->handler = luaL_ref(ps, LUA_REGISTRYINDEX);
108
109 dbasyncquery(lua_dbcallback, cb, "%s", q);
110
111 LUA_RETURN(ps, LUA_OK);
112 }
113
114 static int lua_dbescape(lua_State *ps) {
115 char ebuf[8192 * 2 + 1];
116 char *s = (char *)lua_tostring(ps, 1);
117 int len;
118
119 if(!s)
120 return 0;
121
122 len = lua_strlen(ps, 1);
123 if(len > sizeof(ebuf) / 2 - 5)
124 return 0;
125
126 dbescapestring(ebuf, s, len);
127 lua_pushstring(ps, ebuf);
128
129 return 1;
130 }
131
132 void lua_registerdbcommands(lua_State *l) {
133 lua_register(l, "db_createquery", lua_dbcreatequery);
134
135 /* lua_register(l, "db_loadtable", lua_dbloadtable); */
136
137 lua_register(l, "db_query", lua_dbquery);
138 lua_register(l, "db_escape", lua_dbescape);
139 lua_register(l, "db_numfields", lua_dbnumfields);
140 /* lua_register(l, "db_getvalue", lua_dbgetvalue);*/
141 }
142
143 #else
144
145 void lua_registerdbcommands(lua_State *l) {
146 }
147
148 #endif