]>
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.
34 #include "ircd_defs.h"
39 #define COMMIT_INTERVAL 3 /* seconds */
50 static char bandb_letter
[LAST_BANDB_TYPE
] = {
54 static const char *bandb_table
[LAST_BANDB_TYPE
] = {
55 "kline", "dline", "xline", "resv"
59 static rb_helper
*bandb_helper
;
60 static int in_transaction
;
62 static void check_schema(void);
65 bandb_commit(void *unused
)
67 rsdb_transaction(RSDB_TRANS_END
);
72 parse_ban(bandb_type type
, char *parv
[], int parc
)
74 const char *mask1
= NULL
;
75 const char *mask2
= NULL
;
76 const char *oper
= NULL
;
77 const char *curtime
= NULL
;
78 const char *reason
= NULL
;
79 const char *perm
= NULL
;
82 if(type
== BANDB_KLINE
)
92 if(type
== BANDB_KLINE
)
96 curtime
= parv
[para
++];
98 reason
= parv
[para
++];
102 rsdb_transaction(RSDB_TRANS_START
);
104 rb_event_addonce("bandb_commit", bandb_commit
, NULL
,
109 "INSERT INTO %s (mask1, mask2, oper, time, perm, reason) VALUES('%Q', '%Q', '%Q', %s, %s, '%Q')",
110 bandb_table
[type
], mask1
, mask2
? mask2
: "", oper
, curtime
, perm
, reason
);
114 parse_unban(bandb_type type
, char *parv
[], int parc
)
116 const char *mask1
= NULL
;
117 const char *mask2
= NULL
;
119 if(type
== BANDB_KLINE
)
129 if(type
== BANDB_KLINE
)
134 rsdb_transaction(RSDB_TRANS_START
);
136 rb_event_addonce("bandb_commit", bandb_commit
, NULL
,
140 rsdb_exec(NULL
, "DELETE FROM %s WHERE mask1='%Q' AND mask2='%Q'",
141 bandb_table
[type
], mask1
, mask2
? mask2
: "");
147 static char buf
[512];
148 struct rsdb_table table
;
151 /* schedule a clear of anything already pending */
152 rb_helper_write_queue(bandb_helper
, "C");
154 for(i
= 0; i
< LAST_BANDB_TYPE
; i
++)
156 rsdb_exec_fetch(&table
, "SELECT mask1,mask2,oper,reason FROM %s WHERE 1",
159 for(j
= 0; j
< table
.row_count
; j
++)
162 snprintf(buf
, sizeof(buf
), "%c %s %s %s :%s",
163 bandb_letter
[i
], table
.row
[j
][0],
164 table
.row
[j
][1], table
.row
[j
][2], table
.row
[j
][3]);
166 snprintf(buf
, sizeof(buf
), "%c %s %s :%s",
167 bandb_letter
[i
], table
.row
[j
][0],
168 table
.row
[j
][2], table
.row
[j
][3]);
170 rb_helper_write_queue(bandb_helper
, "%s", buf
);
173 rsdb_exec_fetch_end(&table
);
176 rb_helper_write(bandb_helper
, "F");
180 parse_request(rb_helper
*helper
)
182 static char *parv
[MAXPARA
+ 1];
183 static char readbuf
[READBUF_SIZE
];
188 while((len
= rb_helper_read(helper
, readbuf
, sizeof(readbuf
))) > 0)
190 parc
= rb_string_to_array(readbuf
, parv
, MAXPARA
);
198 parse_ban(BANDB_KLINE
, parv
, parc
);
202 parse_ban(BANDB_DLINE
, parv
, parc
);
206 parse_ban(BANDB_XLINE
, parv
, parc
);
210 parse_ban(BANDB_RESV
, parv
, parc
);
214 parse_unban(BANDB_KLINE
, parv
, parc
);
218 parse_unban(BANDB_DLINE
, parv
, parc
);
222 parse_unban(BANDB_XLINE
, parv
, parc
);
226 parse_unban(BANDB_RESV
, parv
, parc
);
240 error_cb(rb_helper
*helper
)
243 rsdb_transaction(RSDB_TRANS_END
);
249 dummy_handler(int sig
)
259 struct sigaction act
;
262 act
.sa_handler
= SIG_IGN
;
263 sigemptyset(&act
.sa_mask
);
264 sigaddset(&act
.sa_mask
, SIGPIPE
);
265 sigaddset(&act
.sa_mask
, SIGALRM
);
267 sigaddset(&act
.sa_mask
, SIGTRAP
);
271 sigaddset(&act
.sa_mask
, SIGWINCH
);
272 sigaction(SIGWINCH
, &act
, 0);
274 sigaction(SIGPIPE
, &act
, 0);
276 sigaction(SIGTRAP
, &act
, 0);
279 act
.sa_handler
= dummy_handler
;
280 sigaction(SIGALRM
, &act
, 0);
286 db_error_cb(const char *errstr
)
289 snprintf(buf
, sizeof(buf
), "! :%s", errstr
);
290 rb_helper_write(bandb_helper
, "%s", buf
);
291 rb_sleep(2 << 30, 0);
296 main(int argc
, char *argv
[])
299 bandb_helper
= rb_helper_child(parse_request
, error_cb
, NULL
, NULL
, NULL
, 256, 256, 256); /* XXX fix me */
300 if(bandb_helper
== NULL
)
303 "This is the charybdis bandb for internal ircd use.\n");
305 "You aren't supposed to run me directly (did you want bantool?). Exiting.\n");
308 rsdb_init(db_error_cb
);
310 rb_helper_loop(bandb_helper
, 0);
318 struct rsdb_table table
;
321 for(i
= 0; i
< LAST_BANDB_TYPE
; i
++)
323 rsdb_exec_fetch(&table
,
324 "SELECT name FROM sqlite_master WHERE type='table' AND name='%s'",
327 rsdb_exec_fetch_end(&table
);
331 "CREATE TABLE %s (mask1 TEXT, mask2 TEXT, oper TEXT, time INTEGER, perm INTEGER, reason TEXT)",