]>
jfr.im git - solanum.git/blob - bandb/bandb.c
3 * Copyright (C) 2006 Lee Hardy <lee -at- leeh.co.uk>
4 * Copyright (C) 2006-2008 ircd-ratbox development team
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * 1.Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2.Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3.The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * $Id: bandb.c 26094 2008-09-19 15:33:46Z androsyn $
33 #include <ratbox_lib.h>
41 #define COMMIT_INTERVAL 3 /* seconds */
52 static char bandb_letter
[LAST_BANDB_TYPE
] = {
56 static const char *bandb_table
[LAST_BANDB_TYPE
] = {
57 "kline", "dline", "xline", "resv"
61 static rb_helper
*bandb_helper
;
62 static int in_transaction
;
64 static void check_schema(void);
67 bandb_commit(void *unused
)
69 rsdb_transaction(RSDB_TRANS_END
);
74 parse_ban(bandb_type type
, char *parv
[], int parc
)
76 const char *mask1
= NULL
;
77 const char *mask2
= NULL
;
78 const char *oper
= NULL
;
79 const char *curtime
= NULL
;
80 const char *reason
= NULL
;
81 const char *perm
= NULL
;
84 if(type
== BANDB_KLINE
)
94 if(type
== BANDB_KLINE
)
98 curtime
= parv
[para
++];
100 reason
= parv
[para
++];
104 rsdb_transaction(RSDB_TRANS_START
);
106 rb_event_addonce("bandb_commit", bandb_commit
, NULL
,
111 "INSERT INTO %s (mask1, mask2, oper, time, perm, reason) VALUES('%Q', '%Q', '%Q', %s, %s, '%Q')",
112 bandb_table
[type
], mask1
, mask2
? mask2
: "", oper
, curtime
, perm
, reason
);
116 parse_unban(bandb_type type
, char *parv
[], int parc
)
118 const char *mask1
= NULL
;
119 const char *mask2
= NULL
;
121 if(type
== BANDB_KLINE
)
131 if(type
== BANDB_KLINE
)
136 rsdb_transaction(RSDB_TRANS_START
);
138 rb_event_addonce("bandb_commit", bandb_commit
, NULL
,
142 rsdb_exec(NULL
, "DELETE FROM %s WHERE mask1='%Q' AND mask2='%Q'",
143 bandb_table
[type
], mask1
, mask2
? mask2
: "");
149 static char buf
[512];
150 struct rsdb_table table
;
153 /* schedule a clear of anything already pending */
154 rb_helper_write_queue(bandb_helper
, "C");
156 for(i
= 0; i
< LAST_BANDB_TYPE
; i
++)
158 rsdb_exec_fetch(&table
, "SELECT mask1,mask2,oper,reason FROM %s WHERE 1",
161 for(j
= 0; j
< table
.row_count
; j
++)
164 rb_snprintf(buf
, sizeof(buf
), "%c %s %s %s :%s",
165 bandb_letter
[i
], table
.row
[j
][0],
166 table
.row
[j
][1], table
.row
[j
][2], table
.row
[j
][3]);
168 rb_snprintf(buf
, sizeof(buf
), "%c %s %s :%s",
169 bandb_letter
[i
], table
.row
[j
][0],
170 table
.row
[j
][2], table
.row
[j
][3]);
172 rb_helper_write_queue(bandb_helper
, "%s", buf
);
175 rsdb_exec_fetch_end(&table
);
178 rb_helper_write(bandb_helper
, "F");
182 parse_request(rb_helper
*helper
)
184 static char *parv
[MAXPARA
+ 1];
185 static char readbuf
[READBUF_SIZE
];
190 while((len
= rb_helper_read(helper
, readbuf
, sizeof(readbuf
))) > 0)
192 parc
= rb_string_to_array(readbuf
, parv
, MAXPARA
);
200 parse_ban(BANDB_KLINE
, parv
, parc
);
204 parse_ban(BANDB_DLINE
, parv
, parc
);
208 parse_ban(BANDB_XLINE
, parv
, parc
);
212 parse_ban(BANDB_RESV
, parv
, parc
);
216 parse_unban(BANDB_KLINE
, parv
, parc
);
220 parse_unban(BANDB_DLINE
, parv
, parc
);
224 parse_unban(BANDB_XLINE
, parv
, parc
);
228 parse_unban(BANDB_RESV
, parv
, parc
);
242 error_cb(rb_helper
*helper
)
245 rsdb_transaction(RSDB_TRANS_END
);
251 dummy_handler(int sig
)
261 struct sigaction act
;
264 act
.sa_handler
= SIG_IGN
;
265 sigemptyset(&act
.sa_mask
);
266 sigaddset(&act
.sa_mask
, SIGPIPE
);
267 sigaddset(&act
.sa_mask
, SIGALRM
);
269 sigaddset(&act
.sa_mask
, SIGTRAP
);
273 sigaddset(&act
.sa_mask
, SIGWINCH
);
274 sigaction(SIGWINCH
, &act
, 0);
276 sigaction(SIGPIPE
, &act
, 0);
278 sigaction(SIGTRAP
, &act
, 0);
281 act
.sa_handler
= dummy_handler
;
282 sigaction(SIGALRM
, &act
, 0);
288 db_error_cb(const char *errstr
)
291 rb_snprintf(buf
, sizeof(buf
), "! :%s", errstr
);
292 rb_helper_write(bandb_helper
, buf
);
293 rb_sleep(2 << 30, 0);
298 main(int argc
, char *argv
[])
301 bandb_helper
= rb_helper_child(parse_request
, error_cb
, NULL
, NULL
, NULL
, 256, 256, 256, 256); /* XXX fix me */
302 if(bandb_helper
== NULL
)
305 "This is ircd-ratbox bandb. You aren't supposed to run me directly. Maybe you want bantool?\n");
307 "However I will print my Id tag $Id: bandb.c 26094 2008-09-19 15:33:46Z androsyn $\n");
308 fprintf(stderr
, "Have a nice day\n");
311 rsdb_init(db_error_cb
);
313 rb_helper_loop(bandb_helper
, 0);
321 struct rsdb_table table
;
324 for(i
= 0; i
< LAST_BANDB_TYPE
; i
++)
326 rsdb_exec_fetch(&table
,
327 "SELECT name FROM sqlite_master WHERE type='table' AND name='%s'",
330 rsdb_exec_fetch_end(&table
);
334 "CREATE TABLE %s (mask1 TEXT, mask2 TEXT, oper TEXT, time INTEGER, perm INTEGER, reason TEXT)",