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)
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.
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
[])
41 if (( in
= fopen(argv
[1],"r")) == NULL
)
43 fprintf(stderr
,"Can't open %s for reading\n", argv
[1]);
47 if (( outkline
= fopen(argv
[2],"w")) == NULL
)
49 fprintf(stderr
,"Can't open %s for writing\n", argv
[2]);
53 if(( outdline
= fopen(argv
[3], "w")) == NULL
)
55 fprintf(stderr
, "Can't open %s for writing\n", argv
[3]);
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");
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");
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
87 static void ConvertConf(FILE* file
, FILE *outkline
, FILE *outdline
)
90 char quotedLine
[BUFSIZE
];
93 while (fgets(line
, sizeof(line
), file
))
95 if ((p
= strchr(line
, '\n')))
98 ReplaceQuotes(quotedLine
,line
);
100 if (!*quotedLine
|| quotedLine
[0] == '#' || quotedLine
[0] == '\n' ||
101 quotedLine
[0] == ' ' || quotedLine
[0] == '\t')
104 /* Could we test if it's conf line at all? -Vesa */
105 if (quotedLine
[1] == ':')
107 parse(outkline
, outdline
, quotedLine
);
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
)
126 static char quotes
[] = {
158 * Do quoting of characters and # detection.
160 for (out
= quotedLine
,in
= inputLine
; *in
; out
++, in
++)
170 *out
= quotes
[ (unsigned int) (*in
& 0x1F) ];
185 * Inputs - pointer to line to parse
186 * - pointer to output to write
188 * Side Effects - Parse one old style conf line.
191 static void parse(FILE *outkline
, FILE *outdline
, char* line
)
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
);
204 for (;;) /* Fake loop, that I can use break here --msa */
207 if ((host_field
= getfield(NULL
)) == NULL
)
211 if ((passwd_field
= getfield(NULL
)) == NULL
)
215 /* if theres a password, try splitting the operreason out */
218 if((p
= strchr(passwd_field
, '|')))
221 operpasswd_field
= p
;
224 operpasswd_field
= "";
228 if ((user_field
= getfield(NULL
)) == NULL
)
231 /* what could possibly be after a userfield? */
238 operpasswd_field
= "";
244 switch( conf_letter
)
247 fprintf(stderr
, "exempt in old file, ignoring.\n");
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
);
256 case 'K': /* Kill user line on irc.conf */
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
);
264 fprintf(stderr
, "Error in config file: %s", line
);
271 * field breakup for ircd.conf file.
273 static char *getfield(char *newline
)
275 static char *line
= NULL
;
283 fprintf(stderr
, "returned null!\n");
289 if ((end
= strchr(line
,':')) == NULL
)
292 if ((end
= strchr(field
,'\n')) == NULL
)
293 end
= field
+ strlen(field
);