Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / class.c
1 /*
2  * IRC - Internet Relay Chat, ircd/class.c
3  * Copyright (C) 1990 Darren Reed
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 1, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * $Id$
20  */
21 #include "class.h"
22 #include "client.h"
23 #include "ircd.h"
24 #include "list.h"
25 #include "numeric.h"
26 #include "s_conf.h"
27 #include "s_debug.h"
28 #include "send.h"
29 #include "struct.h"
30
31 #include <assert.h>
32
33 #define BAD_CONF_CLASS          ((unsigned int)-1)
34 #define BAD_PING                ((unsigned int)-2)
35 #define BAD_CLIENT_CLASS        ((unsigned int)-3)
36
37 struct ConfClass *classes;
38
39 unsigned int get_conf_class(struct ConfItem *aconf)
40 {
41   if ((aconf) && (aconf->confClass))
42     return (ConfClass(aconf));
43
44   Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
45
46   return (BAD_CONF_CLASS);
47
48 }
49
50 static unsigned int get_conf_ping(struct ConfItem *aconf)
51 {
52   if ((aconf) && (aconf->confClass))
53     return (ConfPingFreq(aconf));
54
55   Debug((DEBUG_DEBUG, "No Ping For %s", (aconf) ? aconf->name : "*No Conf*"));
56
57   return (BAD_PING);
58 }
59
60 unsigned int get_client_class(struct Client *acptr)
61 {
62   struct SLink *tmp;
63   struct ConfClass *cl;
64   unsigned int retc = BAD_CLIENT_CLASS;
65
66   if (acptr && !IsMe(acptr) && (acptr->confs))
67     for (tmp = acptr->confs; tmp; tmp = tmp->next)
68     {
69       if (!tmp->value.aconf || !(cl = tmp->value.aconf->confClass))
70         continue;
71       if (ConClass(cl) > retc || retc == BAD_CLIENT_CLASS)
72         retc = ConClass(cl);
73     }
74
75   Debug((DEBUG_DEBUG, "Returning Class %d For %s", retc, acptr->name));
76
77   return (retc);
78 }
79
80 unsigned int get_client_ping(struct Client *acptr)
81 {
82   unsigned int ping = 0;
83   unsigned int ping2;
84   struct ConfItem *aconf;
85   struct SLink *link;
86
87   link = acptr->confs;
88
89   if (link) {
90     while (link) {
91       aconf = link->value.aconf;
92       if (aconf->status & (CONF_CLIENT | CONF_SERVER)) {
93         ping2 = get_conf_ping(aconf);
94         if ((ping2 != BAD_PING) && ((ping > ping2) || !ping))
95           ping = ping2;
96       }
97       link = link->next;
98     }
99   }
100   else {
101     ping = PINGFREQUENCY;
102     Debug((DEBUG_DEBUG, "No Attached Confs for: %s", acptr->name));
103   }
104   if (ping <= 0)
105     ping = PINGFREQUENCY;
106   Debug((DEBUG_DEBUG, "Client %s Ping %d", acptr->name, ping));
107   return (ping);
108 }
109
110 unsigned int get_con_freq(struct ConfClass * clptr)
111 {
112   if (clptr)
113     return (ConFreq(clptr));
114   else
115     return (CONNECTFREQUENCY);
116 }
117
118 /*
119  * When adding a class, check to see if it is already present first.
120  * if so, then update the information for that class, rather than create
121  * a new entry for it and later delete the old entry.
122  * if no present entry is found, then create a new one and add it in
123  * immeadiately after the first one (class 0).
124  */
125 void add_class(unsigned int conClass, unsigned int ping, unsigned int confreq,
126     unsigned int maxli, size_t sendq)
127 {
128   struct ConfClass *t, *p;
129
130   t = find_class(conClass);
131   if ((t == classes) && (conClass != 0))
132   {
133     p = (struct ConfClass *) make_class();
134     NextClass(p) = NextClass(t);
135     NextClass(t) = p;
136   }
137   else
138     p = t;
139   Debug((DEBUG_DEBUG, "Add Class %u: p %p t %p - cf: %u pf: %u ml: %u sq: %d",
140       conClass, p, t, confreq, ping, maxli, sendq));
141   ConClass(p) = conClass;
142   ConFreq(p) = confreq;
143   PingFreq(p) = ping;
144   MaxLinks(p) = maxli;
145   MaxSendq(p) = (sendq > 0) ? sendq : DEFAULTMAXSENDQLENGTH;
146   if (p != t)
147     Links(p) = 0;
148 }
149
150 struct ConfClass *find_class(unsigned int cclass)
151 {
152   struct ConfClass *cltmp;
153
154   for (cltmp = FirstClass(); cltmp; cltmp = NextClass(cltmp))
155     if (ConClass(cltmp) == cclass)
156       return cltmp;
157   return classes;
158 }
159
160 void check_class(void)
161 {
162   struct ConfClass *cltmp, *cltmp2;
163
164   Debug((DEBUG_DEBUG, "Class check:"));
165
166   for (cltmp2 = cltmp = FirstClass(); cltmp; cltmp = NextClass(cltmp2))
167   {
168     Debug((DEBUG_DEBUG,
169         "Class %d : CF: %d PF: %d ML: %d LI: %d SQ: %d",
170         ConClass(cltmp), ConFreq(cltmp), PingFreq(cltmp),
171         MaxLinks(cltmp), Links(cltmp), MaxSendq(cltmp)));
172     if (IsMarkedDelete(cltmp))
173     {
174       NextClass(cltmp2) = NextClass(cltmp);
175       if (Links(cltmp) == 0)
176         free_class(cltmp);
177     }
178     else
179       cltmp2 = cltmp;
180   }
181 }
182
183 void initclass(void)
184 {
185   classes = (struct ConfClass *) make_class();
186
187   ConClass(FirstClass()) = 0;
188   ConFreq(FirstClass()) = CONNECTFREQUENCY;
189   PingFreq(FirstClass()) = PINGFREQUENCY;
190   MaxLinks(FirstClass()) = MAXIMUM_LINKS;
191   MaxSendq(FirstClass()) = DEFAULTMAXSENDQLENGTH;
192   Links(FirstClass()) = 0;
193   NextClass(FirstClass()) = NULL;
194 }
195
196 void report_classes(struct Client *sptr)
197 {
198   struct ConfClass *cltmp;
199
200   for (cltmp = FirstClass(); cltmp; cltmp = NextClass(cltmp))
201     sendto_one(sptr, rpl_str(RPL_STATSYLINE), me.name, sptr->name,
202         'Y', ConClass(cltmp), PingFreq(cltmp), ConFreq(cltmp),
203         MaxLinks(cltmp), MaxSendq(cltmp));
204 }
205
206 size_t get_sendq(struct Client *cptr)
207 {
208   assert(0 != cptr);
209   assert(0 != cptr->local);
210
211   if (cptr->max_sendq)
212     return cptr->max_sendq;
213
214   else if (cptr->confs) {
215     struct SLink*     tmp;
216     struct ConfClass* cl;
217
218     for (tmp = cptr->confs; tmp; tmp = tmp->next) {
219       if (!tmp->value.aconf || !(cl = tmp->value.aconf->confClass))
220         continue;
221       if (ConClass(cl) != BAD_CLIENT_CLASS) {
222         cptr->max_sendq = MaxSendq(cl);
223         return cptr->max_sendq;
224       }
225     }
226   }
227   return DEFAULTMAXSENDQLENGTH;
228 }
229