]>
jfr.im git - irc/rqf/shadowircd.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>
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
;
61 static void check_schema(void);
64 parse_ban(bandb_type type
, char *parv
[], int parc
)
66 const char *mask1
= NULL
;
67 const char *mask2
= NULL
;
68 const char *oper
= NULL
;
69 const char *curtime
= NULL
;
70 const char *reason
= NULL
;
71 const char *perm
= NULL
;
74 if(type
== BANDB_KLINE
)
84 if(type
== BANDB_KLINE
)
88 curtime
= parv
[para
++];
90 reason
= parv
[para
++];
93 "INSERT INTO %s (mask1, mask2, oper, time, perm, reason) VALUES('%Q', '%Q', '%Q', %s, %s, '%Q')",
94 bandb_table
[type
], mask1
, mask2
? mask2
: "", oper
, curtime
, perm
, reason
);
98 parse_unban(bandb_type type
, char *parv
[], int parc
)
100 const char *mask1
= NULL
;
101 const char *mask2
= NULL
;
103 if(type
== BANDB_KLINE
)
113 if(type
== BANDB_KLINE
)
116 rsdb_exec(NULL
, "DELETE FROM %s WHERE mask1='%Q' AND mask2='%Q'",
117 bandb_table
[type
], mask1
, mask2
? mask2
: "");
123 static char buf
[512];
124 struct rsdb_table table
;
127 /* schedule a clear of anything already pending */
128 rb_helper_write_queue(bandb_helper
, "C");
130 for(i
= 0; i
< LAST_BANDB_TYPE
; i
++)
132 rsdb_exec_fetch(&table
, "SELECT mask1,mask2,oper,reason FROM %s WHERE 1",
135 for(j
= 0; j
< table
.row_count
; j
++)
138 rb_snprintf(buf
, sizeof(buf
), "%c %s %s %s :%s",
139 bandb_letter
[i
], table
.row
[j
][0],
140 table
.row
[j
][1], table
.row
[j
][2], table
.row
[j
][3]);
142 rb_snprintf(buf
, sizeof(buf
), "%c %s %s :%s",
143 bandb_letter
[i
], table
.row
[j
][0],
144 table
.row
[j
][2], table
.row
[j
][3]);
146 rb_helper_write_queue(bandb_helper
, "%s", buf
);
149 rsdb_exec_fetch_end(&table
);
152 rb_helper_write(bandb_helper
, "F");
156 parse_request(rb_helper
*helper
)
158 static char *parv
[MAXPARA
+ 1];
159 static char readbuf
[READBUF_SIZE
];
164 while((len
= rb_helper_read(helper
, readbuf
, sizeof(readbuf
))) > 0)
166 parc
= rb_string_to_array(readbuf
, parv
, MAXPARA
);
174 parse_ban(BANDB_KLINE
, parv
, parc
);
178 parse_ban(BANDB_DLINE
, parv
, parc
);
182 parse_ban(BANDB_XLINE
, parv
, parc
);
186 parse_ban(BANDB_RESV
, parv
, parc
);
190 parse_unban(BANDB_KLINE
, parv
, parc
);
194 parse_unban(BANDB_DLINE
, parv
, parc
);
198 parse_unban(BANDB_XLINE
, parv
, parc
);
202 parse_unban(BANDB_RESV
, parv
, parc
);
216 error_cb(rb_helper
*helper
)
223 dummy_handler(int sig
)
233 struct sigaction act
;
236 act
.sa_handler
= SIG_IGN
;
237 sigemptyset(&act
.sa_mask
);
238 sigaddset(&act
.sa_mask
, SIGPIPE
);
239 sigaddset(&act
.sa_mask
, SIGALRM
);
241 sigaddset(&act
.sa_mask
, SIGTRAP
);
245 sigaddset(&act
.sa_mask
, SIGWINCH
);
246 sigaction(SIGWINCH
, &act
, 0);
248 sigaction(SIGPIPE
, &act
, 0);
250 sigaction(SIGTRAP
, &act
, 0);
253 act
.sa_handler
= dummy_handler
;
254 sigaction(SIGALRM
, &act
, 0);
260 db_error_cb(const char *errstr
)
263 rb_snprintf(buf
, sizeof(buf
), "! :%s", errstr
);
264 rb_helper_write(bandb_helper
, buf
);
265 rb_sleep(2 << 30, 0);
270 main(int argc
, char *argv
[])
273 bandb_helper
= rb_helper_child(parse_request
, error_cb
, NULL
, NULL
, NULL
, 256, 256, 256, 256); /* XXX fix me */
274 if(bandb_helper
== NULL
)
277 "This is ircd-ratbox bandb. You aren't supposed to run me directly. Maybe you want bantool?\n");
279 "However I will print my Id tag $Id: bandb.c 26094 2008-09-19 15:33:46Z androsyn $\n");
280 fprintf(stderr
, "Have a nice day\n");
283 rsdb_init(db_error_cb
);
285 rb_helper_loop(bandb_helper
, 0);
293 struct rsdb_table table
;
296 for(i
= 0; i
< LAST_BANDB_TYPE
; i
++)
298 rsdb_exec_fetch(&table
,
299 "SELECT name FROM sqlite_master WHERE type='table' AND name='%s'",
302 rsdb_exec_fetch_end(&table
);
306 "CREATE TABLE %s (mask1 TEXT, mask2 TEXT, oper TEXT, time INTEGER, perm INTEGER, reason TEXT)",