]> jfr.im git - irc/rqf/shadowircd.git/blob - tools/convertklines.c
Add operspy /topic.
[irc/rqf/shadowircd.git] / tools / convertklines.c
1 /************************************************************************
2 * IRC - Internet Relay Chat, tools/convertklines.c
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 1, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * $Id: convertklines.c 6 2005-09-10 01:02:21Z nenolod $
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <string.h>
25
26 #define BUFSIZE 512
27
28 static void ConvertConf(FILE* file,FILE *outkline, FILE *outdline);
29 static void usage(void);
30 static char *getfield(char *);
31 static void ReplaceQuotes(char *out, char *in);
32 static void parse(FILE *outkline, FILE *outdline, char *in);
33
34 int main(int argc,char *argv[])
35 {
36 FILE *in;
37 FILE *outkline;
38 FILE *outdline;
39
40 if(argc < 4)
41 usage();
42
43 if (( in = fopen(argv[1],"r")) == NULL )
44 {
45 fprintf(stderr,"Can't open %s for reading\n", argv[1]);
46 usage();
47 }
48
49 if (( outkline = fopen(argv[2],"w")) == NULL )
50 {
51 fprintf(stderr,"Can't open %s for writing\n", argv[2]);
52 usage();
53 }
54
55 if(( outdline = fopen(argv[3], "w")) == NULL )
56 {
57 fprintf(stderr, "Can't open %s for writing\n", argv[3]);
58 usage();
59 }
60
61 ConvertConf(in, outkline, outdline);
62
63 fprintf(stderr, "The kline file has been converted and should be renamed to\n");
64 fprintf(stderr, "the config.h options (normally kline.conf and dline.conf) and\n");
65 fprintf(stderr, "placed in your etc/ dir\n");
66 return 0;
67 }
68
69 static void usage()
70 {
71 fprintf(stderr, "klines and dlines now go in separate files:\n");
72 fprintf(stderr,"convertklines kline.conf.old kline.conf.new dline.conf.new\n");
73 exit(-1);
74 }
75
76
77 /*
78 * ConvertConf()
79 * Read configuration file.
80 *
81 *
82 * inputs - FILE* to config file to convert
83 * - FILE* to output for new klines
84 * - FILE* to output for new dlines
85 * outputs - -1 if the file cannot be opened
86 * - 0 otherwise
87 */
88
89 static void ConvertConf(FILE* file, FILE *outkline, FILE *outdline)
90 {
91 char line[BUFSIZE];
92 char quotedLine[BUFSIZE];
93 char* p;
94
95 while (fgets(line, sizeof(line), file))
96 {
97 if ((p = strchr(line, '\n')))
98 *p = '\0';
99
100 ReplaceQuotes(quotedLine,line);
101
102 if (!*quotedLine || quotedLine[0] == '#' || quotedLine[0] == '\n' ||
103 quotedLine[0] == ' ' || quotedLine[0] == '\t')
104 continue;
105
106 /* Could we test if it's conf line at all? -Vesa */
107 if (quotedLine[1] == ':')
108 {
109 parse(outkline, outdline, quotedLine);
110 }
111 }
112
113 fclose(file);
114 }
115
116 /*
117 * ReplaceQuotes
118 * Inputs - input line to quote
119 * Output - quoted line
120 * Side Effects - All quoted chars in input are replaced
121 * with quoted values in output, # chars replaced with '\0'
122 * otherwise input is copied to output.
123 */
124 static void ReplaceQuotes(char* quotedLine,char *inputLine)
125 {
126 char *in;
127 char *out;
128 static char quotes[] = {
129 0, /* */
130 0, /* a */
131 '\b', /* b */
132 0, /* c */
133 0, /* d */
134 0, /* e */
135 '\f', /* f */
136 0, /* g */
137 0, /* h */
138 0, /* i */
139 0, /* j */
140 0, /* k */
141 0, /* l */
142 0, /* m */
143 '\n', /* n */
144 0, /* o */
145 0, /* p */
146 0, /* q */
147 '\r', /* r */
148 0, /* s */
149 '\t', /* t */
150 0, /* u */
151 '\v', /* v */
152 0, /* w */
153 0, /* x */
154 0, /* y */
155 0, /* z */
156 0,0,0,0,0,0
157 };
158
159 /*
160 * Do quoting of characters and # detection.
161 */
162 for (out = quotedLine,in = inputLine; *in; out++, in++)
163 {
164 if (*in == '\\')
165 {
166 in++;
167 if(*in == '\\')
168 *out = '\\';
169 else if(*in == '#')
170 *out = '#';
171 else
172 *out = quotes[ (unsigned int) (*in & 0x1F) ];
173 }
174 else if (*in == '#')
175 {
176 *out = '\0';
177 return;
178 }
179 else
180 *out = *in;
181 }
182 *out = '\0';
183 }
184
185 /*
186 * parse()
187 * Inputs - pointer to line to parse
188 * - pointer to output to write
189 * Output -
190 * Side Effects - Parse one old style conf line.
191 */
192
193 static void parse(FILE *outkline, FILE *outdline, char* line)
194 {
195 char conf_letter;
196 char *tmp;
197 const char *user_field = NULL;
198 const char *passwd_field = NULL;
199 const char *host_field = NULL;
200 const char *operpasswd_field = NULL;
201
202 tmp = getfield(line);
203
204 conf_letter = *tmp;
205
206 for (;;) /* Fake loop, that I can use break here --msa */
207 {
208 /* host field */
209 if ((host_field = getfield(NULL)) == NULL)
210 return;
211
212 /* pass field */
213 if ((passwd_field = getfield(NULL)) == NULL)
214 break;
215 else
216 {
217 /* if theres a password, try splitting the operreason out */
218 char *p;
219
220 if((p = strchr(passwd_field, '|')))
221 {
222 *p++ = '\0';
223 operpasswd_field = p;
224 }
225 else
226 operpasswd_field = "";
227 }
228
229 /* user field */
230 if ((user_field = getfield(NULL)) == NULL)
231 break;
232
233 /* what could possibly be after a userfield? */
234 break;
235 }
236
237 if (!passwd_field)
238 {
239 passwd_field = "";
240 operpasswd_field = "";
241 }
242
243 if (!user_field)
244 user_field = "";
245
246 switch( conf_letter )
247 {
248 case 'd':
249 fprintf(stderr, "exempt in old file, ignoring.\n");
250 break;
251
252 case 'D': /* Deny lines (immediate refusal) */
253 if(host_field && passwd_field)
254 fprintf(outdline, "\"%s\",\"%s\",\"%s\",\"\",\"Unknown\",0\n",
255 host_field, passwd_field, operpasswd_field);
256 break;
257
258 case 'K': /* Kill user line on irc.conf */
259 case 'k':
260 if(host_field && passwd_field && user_field)
261 fprintf(outkline, "\"%s\",\"%s\",\"%s\",\"%s\",\"\",\"Unknown\",0\n",
262 user_field, host_field, passwd_field, operpasswd_field);
263 break;
264
265 default:
266 fprintf(stderr, "Error in config file: %s", line);
267 break;
268 }
269 }
270
271
272 /*
273 * field breakup for ircd.conf file.
274 */
275 static char *getfield(char *newline)
276 {
277 static char *line = NULL;
278 char *end, *field;
279
280 if (newline)
281 line = newline;
282
283 if (line == NULL)
284 {
285 fprintf(stderr, "returned null!\n");
286 return NULL;
287 }
288
289 field = line;
290
291 if ((end = strchr(line,':')) == NULL)
292 {
293 line = NULL;
294 if ((end = strchr(field,'\n')) == NULL)
295 end = field + strlen(field);
296 }
297 else
298 line = end + 1;
299
300 *end = '\0';
301
302 return field;
303 }
304
305