]> jfr.im git - irc/rqf/shadowircd.git/blob - src/class.c
Add extensions/m_roleplay, a module that provides various roleplaying commands.
[irc/rqf/shadowircd.git] / src / class.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * class.c: Controls connection classes.
4 *
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
8 *
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.
13 *
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.
18 *
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
23 *
24 */
25
26 #include "stdinc.h"
27 #include "config.h"
28
29 #include "class.h"
30 #include "client.h"
31 #include "common.h"
32 #include "ircd.h"
33 #include "numeric.h"
34 #include "s_conf.h"
35 #include "s_newconf.h"
36 #include "send.h"
37 #include "match.h"
38
39 #define BAD_CONF_CLASS -1
40 #define BAD_PING -2
41 #define BAD_CLIENT_CLASS -3
42
43 rb_dlink_list class_list;
44 struct Class *default_class;
45
46 struct Class *
47 make_class(void)
48 {
49 struct Class *tmp;
50
51 tmp = rb_malloc(sizeof(struct Class));
52
53 ConFreq(tmp) = DEFAULT_CONNECTFREQUENCY;
54 PingFreq(tmp) = DEFAULT_PINGFREQUENCY;
55 MaxUsers(tmp) = 1;
56 MaxSendq(tmp) = DEFAULT_SENDQ;
57
58 tmp->ip_limits = rb_new_patricia(PATRICIA_BITS);
59 return tmp;
60 }
61
62 void
63 free_class(struct Class *tmp)
64 {
65 if(tmp->ip_limits)
66 rb_destroy_patricia(tmp->ip_limits, NULL);
67
68 rb_free(tmp->class_name);
69 rb_free(tmp);
70
71 }
72
73 /*
74 * get_conf_ping
75 *
76 * inputs - pointer to struct ConfItem
77 * output - ping frequency
78 * side effects - NONE
79 */
80 static int
81 get_conf_ping(struct ConfItem *aconf)
82 {
83 if((aconf) && ClassPtr(aconf))
84 return (ConfPingFreq(aconf));
85
86 return (BAD_PING);
87 }
88
89 /*
90 * get_client_class
91 *
92 * inputs - pointer to client struct
93 * output - pointer to name of class
94 * side effects - NONE
95 */
96 const char *
97 get_client_class(struct Client *target_p)
98 {
99 const char *retc = "unknown";
100
101 if(target_p == NULL || IsMe(target_p))
102 return retc;
103
104 if(IsServer(target_p))
105 {
106 struct server_conf *server_p = target_p->localClient->att_sconf;
107 return server_p->class_name;
108 }
109 else
110 {
111 struct ConfItem *aconf;
112 aconf = target_p->localClient->att_conf;
113
114 if((aconf == NULL) || (aconf->className == NULL))
115 retc = "default";
116 else
117 retc = aconf->className;
118 }
119
120 return (retc);
121 }
122
123 /*
124 * get_client_ping
125 *
126 * inputs - pointer to client struct
127 * output - ping frequency
128 * side effects - NONE
129 */
130 int
131 get_client_ping(struct Client *target_p)
132 {
133 int ping = 0;
134
135 if(IsServer(target_p))
136 {
137 struct server_conf *server_p = target_p->localClient->att_sconf;
138 ping = PingFreq(server_p->class);
139 }
140 else
141 {
142 struct ConfItem *aconf;
143
144 aconf = target_p->localClient->att_conf;
145
146 if(aconf != NULL)
147 ping = get_conf_ping(aconf);
148 else
149 ping = DEFAULT_PINGFREQUENCY;
150 }
151
152 if(ping <= 0)
153 ping = DEFAULT_PINGFREQUENCY;
154
155 return ping;
156 }
157
158 /*
159 * get_con_freq
160 *
161 * inputs - pointer to class struct
162 * output - connection frequency
163 * side effects - NONE
164 */
165 int
166 get_con_freq(struct Class *clptr)
167 {
168 if(clptr)
169 return (ConFreq(clptr));
170 return (DEFAULT_CONNECTFREQUENCY);
171 }
172
173 /* add_class()
174 *
175 * input - class to add
176 * output -
177 * side effects - class is added to class_list if new, else old class
178 * is updated with new values.
179 */
180 void
181 add_class(struct Class *classptr)
182 {
183 struct Class *tmpptr;
184
185 tmpptr = find_class(classptr->class_name);
186
187 if(tmpptr == default_class)
188 {
189 rb_dlinkAddAlloc(classptr, &class_list);
190 CurrUsers(classptr) = 0;
191 }
192 else
193 {
194 MaxUsers(tmpptr) = MaxUsers(classptr);
195 MaxLocal(tmpptr) = MaxLocal(classptr);
196 MaxGlobal(tmpptr) = MaxGlobal(classptr);
197 MaxIdent(tmpptr) = MaxIdent(classptr);
198 PingFreq(tmpptr) = PingFreq(classptr);
199 MaxSendq(tmpptr) = MaxSendq(classptr);
200 ConFreq(tmpptr) = ConFreq(classptr);
201 CidrIpv4Bitlen(tmpptr) = CidrIpv4Bitlen(classptr);
202 CidrIpv6Bitlen(tmpptr) = CidrIpv6Bitlen(classptr);
203 CidrAmount(tmpptr) = CidrAmount(classptr);
204
205 free_class(classptr);
206 }
207 }
208
209
210 /*
211 * find_class
212 *
213 * inputs - string name of class
214 * output - corresponding class pointer
215 * side effects - NONE
216 */
217 struct Class *
218 find_class(const char *classname)
219 {
220 struct Class *cltmp;
221 rb_dlink_node *ptr;
222
223 if(classname == NULL)
224 return default_class;
225
226 RB_DLINK_FOREACH(ptr, class_list.head)
227 {
228 cltmp = ptr->data;
229
230 if(!strcmp(ClassName(cltmp), classname))
231 return cltmp;
232 }
233
234 return default_class;
235 }
236
237 /*
238 * check_class
239 *
240 * inputs - NONE
241 * output - NONE
242 * side effects -
243 */
244 void
245 check_class()
246 {
247 struct Class *cltmp;
248 rb_dlink_node *ptr;
249 rb_dlink_node *next_ptr;
250
251 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, class_list.head)
252 {
253 cltmp = ptr->data;
254
255 if(MaxUsers(cltmp) < 0)
256 {
257 rb_dlinkDestroy(ptr, &class_list);
258 if(CurrUsers(cltmp) <= 0)
259 free_class(cltmp);
260 }
261 }
262 }
263
264 /*
265 * initclass
266 *
267 * inputs - NONE
268 * output - NONE
269 * side effects -
270 */
271 void
272 initclass()
273 {
274 default_class = make_class();
275 ClassName(default_class) = rb_strdup("default");
276 }
277
278 /*
279 * report_classes
280 *
281 * inputs - pointer to client to report to
282 * output - NONE
283 * side effects - class report is done to this client
284 */
285 void
286 report_classes(struct Client *source_p)
287 {
288 struct Class *cltmp;
289 rb_dlink_node *ptr;
290
291 RB_DLINK_FOREACH(ptr, class_list.head)
292 {
293 cltmp = ptr->data;
294
295 sendto_one_numeric(source_p, RPL_STATSYLINE,
296 form_str(RPL_STATSYLINE),
297 ClassName(cltmp), PingFreq(cltmp),
298 ConFreq(cltmp), MaxUsers(cltmp),
299 MaxSendq(cltmp),
300 MaxLocal(cltmp), MaxIdent(cltmp),
301 MaxGlobal(cltmp), MaxIdent(cltmp),
302 CurrUsers(cltmp));
303 }
304
305 /* also output the default class */
306 sendto_one_numeric(source_p, RPL_STATSYLINE, form_str(RPL_STATSYLINE),
307 ClassName(default_class), PingFreq(default_class),
308 ConFreq(default_class), MaxUsers(default_class),
309 MaxSendq(default_class),
310 MaxLocal(default_class), MaxIdent(default_class),
311 MaxGlobal(default_class), MaxIdent(default_class),
312 CurrUsers(default_class));
313 }
314
315 /*
316 * get_sendq
317 *
318 * inputs - pointer to client
319 * output - sendq for this client as found from its class
320 * side effects - NONE
321 */
322 long
323 get_sendq(struct Client *client_p)
324 {
325 if(client_p == NULL || IsMe(client_p))
326 return DEFAULT_SENDQ;
327
328 if(IsServer(client_p))
329 {
330 struct server_conf *server_p;
331 server_p = client_p->localClient->att_sconf;
332 return MaxSendq(server_p->class);
333 }
334 else
335 {
336 struct ConfItem *aconf = client_p->localClient->att_conf;
337
338 if(aconf != NULL && aconf->status & CONF_CLIENT)
339 return ConfMaxSendq(aconf);
340 }
341
342 return DEFAULT_SENDQ;
343 }