More RCS keyword removal...
[seven-1.x.git] / tools / convertklines.c
blob9ab8b7efb4f5d4165e8f824f323e23040013860a
1 /************************************************************************
2 * IRC - Internet Relay Chat, tools/convertklines.c
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.
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.
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <string.h>
24 #define BUFSIZE 512
26 static void ConvertConf(FILE* file,FILE *outkline, FILE *outdline);
27 static void usage(void);
28 static char *getfield(char *);
29 static void ReplaceQuotes(char *out, char *in);
30 static void parse(FILE *outkline, FILE *outdline, char *in);
32 int main(int argc,char *argv[])
34 FILE *in;
35 FILE *outkline;
36 FILE *outdline;
38 if(argc < 4)
39 usage();
41 if (( in = fopen(argv[1],"r")) == NULL )
43 fprintf(stderr,"Can't open %s for reading\n", argv[1]);
44 usage();
47 if (( outkline = fopen(argv[2],"w")) == NULL )
49 fprintf(stderr,"Can't open %s for writing\n", argv[2]);
50 usage();
53 if(( outdline = fopen(argv[3], "w")) == NULL )
55 fprintf(stderr, "Can't open %s for writing\n", argv[3]);
56 usage();
59 ConvertConf(in, outkline, outdline);
61 fprintf(stderr, "The kline file has been converted and should be renamed to\n");
62 fprintf(stderr, "the config.h options (normally kline.conf and dline.conf) and\n");
63 fprintf(stderr, "placed in your etc/ dir\n");
64 return 0;
67 static void usage()
69 fprintf(stderr, "klines and dlines now go in separate files:\n");
70 fprintf(stderr,"convertklines kline.conf.old kline.conf.new dline.conf.new\n");
71 exit(-1);
76 * ConvertConf()
77 * Read configuration file.
80 * inputs - FILE* to config file to convert
81 * - FILE* to output for new klines
82 * - FILE* to output for new dlines
83 * outputs - -1 if the file cannot be opened
84 * - 0 otherwise
87 static void ConvertConf(FILE* file, FILE *outkline, FILE *outdline)
89 char line[BUFSIZE];
90 char quotedLine[BUFSIZE];
91 char* p;
93 while (fgets(line, sizeof(line), file))
95 if ((p = strchr(line, '\n')))
96 *p = '\0';
98 ReplaceQuotes(quotedLine,line);
100 if (!*quotedLine || quotedLine[0] == '#' || quotedLine[0] == '\n' ||
101 quotedLine[0] == ' ' || quotedLine[0] == '\t')
102 continue;
104 /* Could we test if it's conf line at all? -Vesa */
105 if (quotedLine[1] == ':')
107 parse(outkline, outdline, quotedLine);
111 fclose(file);
115 * ReplaceQuotes
116 * Inputs - input line to quote
117 * Output - quoted line
118 * Side Effects - All quoted chars in input are replaced
119 * with quoted values in output, # chars replaced with '\0'
120 * otherwise input is copied to output.
122 static void ReplaceQuotes(char* quotedLine,char *inputLine)
124 char *in;
125 char *out;
126 static char quotes[] = {
127 0, /* */
128 0, /* a */
129 '\b', /* b */
130 0, /* c */
131 0, /* d */
132 0, /* e */
133 '\f', /* f */
134 0, /* g */
135 0, /* h */
136 0, /* i */
137 0, /* j */
138 0, /* k */
139 0, /* l */
140 0, /* m */
141 '\n', /* n */
142 0, /* o */
143 0, /* p */
144 0, /* q */
145 '\r', /* r */
146 0, /* s */
147 '\t', /* t */
148 0, /* u */
149 '\v', /* v */
150 0, /* w */
151 0, /* x */
152 0, /* y */
153 0, /* z */
154 0,0,0,0,0,0
158 * Do quoting of characters and # detection.
160 for (out = quotedLine,in = inputLine; *in; out++, in++)
162 if (*in == '\\')
164 in++;
165 if(*in == '\\')
166 *out = '\\';
167 else if(*in == '#')
168 *out = '#';
169 else
170 *out = quotes[ (unsigned int) (*in & 0x1F) ];
172 else if (*in == '#')
174 *out = '\0';
175 return;
177 else
178 *out = *in;
180 *out = '\0';
184 * parse()
185 * Inputs - pointer to line to parse
186 * - pointer to output to write
187 * Output -
188 * Side Effects - Parse one old style conf line.
191 static void parse(FILE *outkline, FILE *outdline, char* line)
193 char conf_letter;
194 char *tmp;
195 const char *user_field = NULL;
196 const char *passwd_field = NULL;
197 const char *host_field = NULL;
198 const char *operpasswd_field = NULL;
200 tmp = getfield(line);
202 conf_letter = *tmp;
204 for (;;) /* Fake loop, that I can use break here --msa */
206 /* host field */
207 if ((host_field = getfield(NULL)) == NULL)
208 return;
210 /* pass field */
211 if ((passwd_field = getfield(NULL)) == NULL)
212 break;
213 else
215 /* if theres a password, try splitting the operreason out */
216 char *p;
218 if((p = strchr(passwd_field, '|')))
220 *p++ = '\0';
221 operpasswd_field = p;
223 else
224 operpasswd_field = "";
227 /* user field */
228 if ((user_field = getfield(NULL)) == NULL)
229 break;
231 /* what could possibly be after a userfield? */
232 break;
235 if (!passwd_field)
237 passwd_field = "";
238 operpasswd_field = "";
241 if (!user_field)
242 user_field = "";
244 switch( conf_letter )
246 case 'd':
247 fprintf(stderr, "exempt in old file, ignoring.\n");
248 break;
250 case 'D': /* Deny lines (immediate refusal) */
251 if(host_field && passwd_field)
252 fprintf(outdline, "\"%s\",\"%s\",\"%s\",\"\",\"Unknown\",0\n",
253 host_field, passwd_field, operpasswd_field);
254 break;
256 case 'K': /* Kill user line on irc.conf */
257 case 'k':
258 if(host_field && passwd_field && user_field)
259 fprintf(outkline, "\"%s\",\"%s\",\"%s\",\"%s\",\"\",\"Unknown\",0\n",
260 user_field, host_field, passwd_field, operpasswd_field);
261 break;
263 default:
264 fprintf(stderr, "Error in config file: %s", line);
265 break;
271 * field breakup for ircd.conf file.
273 static char *getfield(char *newline)
275 static char *line = NULL;
276 char *end, *field;
278 if (newline)
279 line = newline;
281 if (line == NULL)
283 fprintf(stderr, "returned null!\n");
284 return NULL;
287 field = line;
289 if ((end = strchr(line,':')) == NULL)
291 line = NULL;
292 if ((end = strchr(field,'\n')) == NULL)
293 end = field + strlen(field);
295 else
296 line = end + 1;
298 *end = '\0';
300 return field;