Author: Kev <klmitch@mit.edu>
[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 "ircd_reply.h"
25 #include "list.h"
26 #include "numeric.h"
27 #include "s_conf.h"
28 #include "s_debug.h"
29 #include "send.h"
30 #include "struct.h"
31
32 #include <assert.h>
33
34 #define BAD_CONF_CLASS          ((unsigned int)-1)
35 #define BAD_PING                ((unsigned int)-2)
36 #define BAD_CLIENT_CLASS        ((unsigned int)-3)
37
38 struct ConfClass *classes;
39
40 unsigned int get_conf_class(struct ConfItem *aconf)
41 {
42   if ((aconf) && (aconf->confClass))
43     return (ConfClass(aconf));
44
45   Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
46
47   return (BAD_CONF_CLASS);
48
49 }
50
51 static unsigned int get_conf_ping(struct ConfItem *aconf)
52 {
53   if ((aconf) && (aconf->confClass))
54     return (ConfPingFreq(aconf));
55
56   Debug((DEBUG_DEBUG, "No Ping For %s", (aconf) ? aconf->name : "*No Conf*"));
57
58   return (BAD_PING);
59 }
60
61 unsigned int get_client_class(struct Client *acptr)
62 {
63   struct SLink *tmp;
64   struct ConfClass *cl;
65   unsigned int retc = BAD_CLIENT_CLASS;
66
67   if (acptr && !IsMe(acptr) && (acptr->confs))
68     for (tmp = acptr->confs; tmp; tmp = tmp->next)
69     {
70       if (!tmp->value.aconf || !(cl = tmp->value.aconf->confClass))
71         continue;
72       if (ConClass(cl) > retc || retc == BAD_CLIENT_CLASS)
73         retc = ConClass(cl);
74     }
75
76   Debug((DEBUG_DEBUG, "Returning Class %d For %s", retc, acptr->name));
77
78   return (retc);
79 }
80
81 unsigned int get_client_ping(struct Client *acptr)
82 {
83   unsigned int ping = 0;
84   unsigned int ping2;
85   struct ConfItem *aconf;
86   struct SLink *link;
87
88   link = acptr->confs;
89
90   if (link) {
91     while (link) {
92       aconf = link->value.aconf;
93       if (aconf->status & (CONF_CLIENT | CONF_SERVER)) {
94         ping2 = get_conf_ping(aconf);
95         if ((ping2 != BAD_PING) && ((ping > ping2) || !ping))
96           ping = ping2;
97       }
98       link = link->next;
99     }
100   }
101   else {
102     ping = PINGFREQUENCY;
103     Debug((DEBUG_DEBUG, "No Attached Confs for: %s", acptr->name));
104   }
105   if (ping <= 0)
106     ping = PINGFREQUENCY;
107   Debug((DEBUG_DEBUG, "Client %s Ping %d", acptr->name, ping));
108   return (ping);
109 }
110
111 unsigned int get_con_freq(struct ConfClass * clptr)
112 {
113   if (clptr)
114     return (ConFreq(clptr));
115   else
116     return (CONNECTFREQUENCY);
117 }
118
119 /*
120  * When adding a class, check to see if it is already present first.
121  * if so, then update the information for that class, rather than create
122  * a new entry for it and later delete the old entry.
123  * if no present entry is found, then create a new one and add it in
124  * immeadiately after the first one (class 0).
125  */
126 void add_class(unsigned int conClass, unsigned int ping, unsigned int confreq,
127     unsigned int maxli, unsigned int sendq)
128 {
129   struct ConfClass *t, *p;
130
131   t = find_class(conClass);
132   if ((t == classes) && (conClass != 0))
133   {
134     p = (struct ConfClass *) make_class();
135     NextClass(p) = NextClass(t);
136     NextClass(t) = p;
137   }
138   else
139     p = t;
140   Debug((DEBUG_DEBUG, "Add Class %u: p %p t %p - cf: %u pf: %u ml: %u sq: %d",
141       conClass, p, t, confreq, ping, maxli, sendq));
142   ConClass(p) = conClass;
143   ConFreq(p) = confreq;
144   PingFreq(p) = ping;
145   MaxLinks(p) = maxli;
146   MaxSendq(p) = (sendq > 0) ? sendq : DEFAULTMAXSENDQLENGTH;
147   if (p != t)
148     Links(p) = 0;
149 }
150
151 struct ConfClass *find_class(unsigned int cclass)
152 {
153   struct ConfClass *cltmp;
154
155   for (cltmp = FirstClass(); cltmp; cltmp = NextClass(cltmp))
156     if (ConClass(cltmp) == cclass)
157       return cltmp;
158   return classes;
159 }
160
161 void check_class(void)
162 {
163   struct ConfClass *cltmp, *cltmp2;
164
165   Debug((DEBUG_DEBUG, "Class check:"));
166
167   for (cltmp2 = cltmp = FirstClass(); cltmp; cltmp = NextClass(cltmp2))
168   {
169     Debug((DEBUG_DEBUG,
170         "Class %d : CF: %d PF: %d ML: %d LI: %d SQ: %d",
171         ConClass(cltmp), ConFreq(cltmp), PingFreq(cltmp),
172         MaxLinks(cltmp), Links(cltmp), MaxSendq(cltmp)));
173     if (IsMarkedDelete(cltmp))
174     {
175       NextClass(cltmp2) = NextClass(cltmp);
176       if (Links(cltmp) == 0)
177         free_class(cltmp);
178     }
179     else
180       cltmp2 = cltmp;
181   }
182 }
183
184 void initclass(void)
185 {
186   classes = (struct ConfClass *) make_class();
187
188   ConClass(FirstClass()) = 0;
189   ConFreq(FirstClass()) = CONNECTFREQUENCY;
190   PingFreq(FirstClass()) = PINGFREQUENCY;
191   MaxLinks(FirstClass()) = MAXIMUM_LINKS;
192   MaxSendq(FirstClass()) = DEFAULTMAXSENDQLENGTH;
193   Links(FirstClass()) = 0;
194   NextClass(FirstClass()) = NULL;
195 }
196
197 void report_classes(struct Client *sptr)
198 {
199   struct ConfClass *cltmp;
200
201   for (cltmp = FirstClass(); cltmp; cltmp = NextClass(cltmp))
202     send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
203                ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp));
204 }
205
206 unsigned int 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