Import from http://svn.freenode.net/ircd-seven/private/beu/seven (r196).
[seven-1.x.git] / src / kdparse.c
blobda3d01a31c6d0d2c3424dafab0c24e2ecc869955
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * kdparse.c: Parses K and D lines.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
24 * $Id: kdparse.c 26 2006-09-20 18:02:06Z spb $
27 #include "stdinc.h"
28 #include "tools.h"
29 #include "s_log.h"
30 #include "s_conf.h"
31 #include "s_newconf.h"
32 #include "hostmask.h"
33 #include "client.h"
34 #include "irc_string.h"
35 #include "memory.h"
36 #include "hash.h"
38 /* conf_add_fields()
40 * inputs - pointer to config item, host/pass/user/operreason fields
41 * output - NONE
42 * side effects - update respective fields with pointers
44 static void
45 conf_add_fields(struct ConfItem *aconf, const char *host_field,
46 const char *pass_field, const char *user_field,
47 const char *operreason_field)
49 if(host_field != NULL)
50 DupString(aconf->host, host_field);
51 if(pass_field != NULL)
52 DupString(aconf->passwd, pass_field);
53 if(user_field != NULL)
54 DupString(aconf->user, user_field);
55 if(operreason_field != NULL)
56 DupString(aconf->spasswd, operreason_field);
60 * parse_k_file
61 * Inputs - pointer to line to parse
62 * Output - NONE
63 * Side Effects - Parse one new style K line
66 void
67 parse_k_file(FILE * file)
69 struct ConfItem *aconf;
70 char *user_field = NULL;
71 char *reason_field = NULL;
72 char *operreason_field = NULL;
73 char *host_field = NULL;
74 char line[BUFSIZE];
75 char *p;
77 while (fgets(line, sizeof(line), file))
79 if((p = strchr(line, '\n')) != NULL)
80 *p = '\0';
82 if((*line == '\0') || (*line == '#'))
83 continue;
85 user_field = getfield(line);
86 if(EmptyString(user_field))
87 continue;
89 host_field = getfield(NULL);
90 if(EmptyString(host_field))
91 continue;
93 reason_field = getfield(NULL);
94 if(EmptyString(reason_field))
95 continue;
97 operreason_field = getfield(NULL);
99 aconf = make_conf();
100 aconf->status = CONF_KILL;
101 conf_add_fields(aconf, host_field, reason_field,
102 user_field, operreason_field);
104 if(aconf->host != NULL)
105 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, aconf);
110 * parse_d_file
111 * Inputs - pointer to line to parse
112 * Output - NONE
113 * Side Effects - Parse one new style D line
116 void
117 parse_d_file(FILE * file)
119 struct ConfItem *aconf;
120 char *reason_field = NULL;
121 char *host_field = NULL;
122 char *operreason_field = NULL;
123 char line[BUFSIZE];
124 char *p;
126 while (fgets(line, sizeof(line), file))
128 if((p = strchr(line, '\n')))
129 *p = '\0';
131 if((*line == '\0') || (line[0] == '#'))
132 continue;
134 host_field = getfield(line);
135 if(EmptyString(host_field))
136 continue;
138 reason_field = getfield(NULL);
139 if(EmptyString(reason_field))
140 continue;
142 operreason_field = getfield(NULL);
144 aconf = make_conf();
145 aconf->status = CONF_DLINE;
146 conf_add_fields(aconf, host_field, reason_field, "", operreason_field);
147 conf_add_d_conf(aconf);
151 void
152 parse_x_file(FILE * file)
154 struct ConfItem *aconf;
155 char *gecos_field = NULL;
156 char *reason_field = NULL;
157 char line[BUFSIZE];
158 char *p;
160 while (fgets(line, sizeof(line), file))
162 if((p = strchr(line, '\n')))
163 *p = '\0';
165 if((*line == '\0') || (line[0] == '#'))
166 continue;
168 gecos_field = getfield(line);
169 if(EmptyString(gecos_field))
170 continue;
172 /* field for xline types, which no longer exist */
173 getfield(NULL);
175 reason_field = getfield(NULL);
176 if(EmptyString(reason_field))
177 continue;
179 /* sanity checking */
180 if((find_xline(gecos_field, 0) != NULL) ||
181 (strchr(reason_field, ':') != NULL))
182 continue;
184 aconf = make_conf();
185 aconf->status = CONF_XLINE;
187 DupString(aconf->name, gecos_field);
188 DupString(aconf->passwd, reason_field);
190 dlinkAddAlloc(aconf, &xline_conf_list);
194 void
195 parse_resv_file(FILE * file)
197 struct ConfItem *aconf;
198 char *reason_field;
199 char *host_field;
200 char line[BUFSIZE];
201 char *p;
203 while (fgets(line, sizeof(line), file))
205 if((p = strchr(line, '\n')))
206 *p = '\0';
208 if((*line == '\0') || (line[0] == '#'))
209 continue;
211 host_field = getfield(line);
212 if(EmptyString(host_field))
213 continue;
215 reason_field = getfield(NULL);
216 if(EmptyString(reason_field))
217 continue;
219 if(IsChannelName(host_field))
221 if(hash_find_resv(host_field))
222 continue;
224 aconf = make_conf();
225 aconf->status = CONF_RESV_CHANNEL;
226 aconf->port = 0;
228 DupString(aconf->name, host_field);
229 DupString(aconf->passwd, reason_field);
230 add_to_resv_hash(aconf->name, aconf);
232 else if(clean_resv_nick(host_field))
234 if(find_nick_resv(host_field))
235 continue;
237 aconf = make_conf();
238 aconf->status = CONF_RESV_NICK;
239 aconf->port = 0;
241 DupString(aconf->name, host_field);
242 DupString(aconf->passwd, reason_field);
243 dlinkAddAlloc(aconf, &resv_conf_list);
249 * getfield
251 * inputs - input buffer
252 * output - next field
253 * side effects - field breakup for ircd.conf file.
255 char *
256 getfield(char *newline)
258 static char *line = NULL;
259 char *end, *field;
261 if(newline != NULL)
262 line = newline;
264 if(line == NULL)
265 return (NULL);
267 field = line;
269 /* XXX make this skip to first " if present */
270 if(*field == '"')
271 field++;
272 else
273 return (NULL); /* mal-formed field */
275 end = strchr(line, ',');
277 while(1)
279 /* no trailing , - last field */
280 if(end == NULL)
282 end = line + strlen(line);
283 line = NULL;
285 if(*end == '"')
287 *end = '\0';
288 return field;
290 else
291 return NULL;
293 else
295 /* look for a ", to mark the end of a field.. */
296 if(*(end - 1) == '"')
298 line = end + 1;
299 end--;
300 *end = '\0';
301 return field;
304 /* search for the next ',' */
305 end++;
306 end = strchr(end, ',');
310 return NULL;