ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/servers.C
Revision: 1.6
Committed: Sun Sep 16 18:54:45 2007 UTC (16 years, 8 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.5: +7 -2 lines
Log Message:
#defines to enum

File Contents

# User Rev Content
1 pippijn 1.1 /*
2     * servers.C: Server and network state tracking.
3 pippijn 1.6 *
4     * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
5     * Rights to this code are as documented in COPYING.
6     *
7     *
8     * Portions of this file were derived from sources bearing the following license:
9 pippijn 1.2 * Rights to this code are documented in doc/pod/license.pod.
10 pippijn 1.4 * Copyright © 2005-2007 Atheme Project (http://www.atheme.org)
11 pippijn 1.1 */
12    
13 pippijn 1.6 static char const rcsid[] = "$Id: servers.C,v 1.5 2007-09-09 20:05:52 pippijn Exp $";
14 pippijn 1.1
15     #include "atheme.h"
16 pippijn 1.5 #include "servers.h"
17 pippijn 1.1 #include <account/myuser.h>
18    
19 pippijn 1.4 typedef std::pair<char const * const, server_t *> server_pair;
20     typedef std::map<char const * const, server_t *, irccase_lt> server_map;
21     server_map sidlist;
22     server_map servlist;
23 pippijn 1.1 list_t tldlist;
24 pippijn 1.4 server_t::callbacks server_t::callback;
25 pippijn 1.1
26     /*
27     * init_servers()
28     *
29     * Initializes the server heap and server/sid DTree structures.
30     *
31     * Inputs:
32     * - nothing
33     *
34     * Outputs:
35     * - nothing
36     *
37     * Side Effects:
38     * - if the heap or dtrees fail to initialize, the program
39     * will abort.
40     */
41     void
42     init_servers (void)
43     {
44 pippijn 1.4 #if 0
45 pippijn 1.1 serv_heap = BlockHeapCreate (sizeof (server_t), HEAP_SERVER);
46     tld_heap = BlockHeapCreate (sizeof (tld_t), 4);
47 pippijn 1.4 #endif
48 pippijn 1.1 }
49    
50     /*
51 pippijn 1.4 * server_add(char const * const name, unsigned int hops, char const * const uplink,
52     * char const * const id, char const * const desc)
53 pippijn 1.1 *
54     * Server object factory.
55     *
56     * Inputs:
57     * - name of server object to create
58     * - amount of hops server has from services
59     * - name of server's uplink or NULL if it's us
60     * - SID of uplink if applicable otherwise NULL
61     * - server's description
62     *
63     * Outputs:
64     * - on success, a new server object
65     *
66     * Side Effects:
67     * - the new server object is added to the server and sid DTree.
68     */
69     server_t *
70 pippijn 1.4 server_add (char const * const name, unsigned int hops, char const * const uplink, char const * const id, char const * const description)
71 pippijn 1.1 {
72     server_t *s, *u = NULL;
73 pippijn 1.4 char const *tld;
74     char const *desc = description;
75 pippijn 1.1
76     if (uplink)
77     {
78     if (id != NULL)
79     slog (LG_NETWORK, "server_add(): %s (%s), uplink %s", name, id, uplink);
80     else
81     slog (LG_NETWORK, "server_add(): %s, uplink %s", name, uplink);
82     u = server_find (uplink);
83     }
84     else
85     slog (LG_DEBUG, "server_add(): %s, root", name);
86    
87 pippijn 1.4 s = new server_t;
88 pippijn 1.1
89     if (id != NULL)
90     {
91     s->sid = sstrdup (id);
92 pippijn 1.4 sidlist[s->sid] = s;
93 pippijn 1.1 }
94    
95     /* check to see if it's hidden */
96     if (!strncmp (desc, "(H)", 3))
97     {
98     s->flags |= SF_HIDE;
99     desc += 3;
100     if (*desc == ' ')
101     desc++;
102     }
103    
104     s->name = sstrdup (name);
105     s->desc = sstrdup (desc);
106     s->hops = hops;
107     s->connected_since = NOW;
108    
109 pippijn 1.4 servlist[s->name] = s;
110 pippijn 1.1
111     if (u)
112     {
113     s->uplink = u;
114     node_add (s, node_create (), &u->children);
115     }
116    
117     /* tld list for global noticer */
118     tld = strrchr (name, '.');
119    
120     if (tld != NULL)
121     {
122     if (!tld_find (tld))
123     tld_add (tld);
124     }
125    
126     cnt.server++;
127    
128     return s;
129     }
130    
131 pippijn 1.5 void
132     server_delete (char const * const name)
133     {
134     server_t *s = server_find (name);
135     server_delete (s);
136    
137     if (!s)
138     {
139     slog (LG_DEBUG, "server_delete(): called for nonexistant server: %s", name);
140    
141     return;
142     }
143     }
144    
145 pippijn 1.1 /*
146 pippijn 1.4 * server_delete(char const * const name)
147 pippijn 1.1 *
148     * Finds and recursively destroys a server object.
149     *
150     * Inputs:
151     * - name of server to find and destroy
152     *
153     * Outputs:
154     * - nothing
155     *
156     * Side Effects:
157     * - all users and servers attached to the target are recursively deleted
158     */
159     void
160 pippijn 1.5 server_delete (server_t *s, bool force)
161 pippijn 1.1 {
162     server_t *child;
163     user_t *u;
164     node_t *n, *tn;
165    
166 pippijn 1.5 if (!force && s == me.me)
167 pippijn 1.1 {
168     /* Deleting this would cause confusion, so let's not do it.
169     * Some ircds send SQUIT <myname> when we are squitted.
170     * -- jilles
171     */
172     slog (LG_DEBUG, "server_delete(): tried to delete myself");
173     return;
174     }
175    
176     slog (me.connected ? LG_NETWORK : LG_DEBUG, "server_delete(): %s, uplink %s (%d users)", s->name, s->uplink != NULL ? s->uplink->name : "<none>", s->users);
177    
178     /* first go through it's users and kill all of them */
179     LIST_FOREACH_SAFE (n, tn, s->userlist.head)
180     {
181     u = (user_t *) n->data;
182     /* This user split, allow bursted logins for the account.
183     * XXX should we do this here?
184     * -- jilles */
185     if (u->myuser != NULL)
186     u->myuser->flags &= ~MU_NOBURSTLOGIN;
187     user_delete (u);
188     }
189    
190     LIST_FOREACH_SAFE (n, tn, s->children.head)
191     {
192     child = static_cast<server_t *> (n->data);
193     server_delete (child->name);
194     }
195    
196     /* now remove the server */
197 pippijn 1.4 servlist.erase (s->name);
198 pippijn 1.1
199     if (s->sid)
200 pippijn 1.4 sidlist.erase (s->sid);
201 pippijn 1.1
202     if (s->uplink)
203     {
204     n = node_find (s, &s->uplink->children);
205     node_del (n, &s->uplink->children);
206     node_free (n);
207     }
208    
209     /* If unconnect semantics SQUIT was confirmed, introduce the jupe
210     * now. This must be after removing the server from the dtrees.
211     * -- jilles */
212     if (s->flags & SF_JUPE_PENDING)
213 pippijn 1.4 phandler->jupe (s->name, "Juped");
214 pippijn 1.1
215 pippijn 1.4 sfree (s->name);
216     sfree (s->desc);
217 pippijn 1.1 if (s->sid)
218 pippijn 1.4 sfree (s->sid);
219 pippijn 1.1
220 pippijn 1.4 delete s;
221 pippijn 1.1
222     cnt.server--;
223     }
224    
225     /*
226 pippijn 1.4 * server_find(char const * const name)
227 pippijn 1.1 *
228     * Finds a server object.
229     *
230     * Inputs:
231     * - name of server to find
232     *
233     * Outputs:
234     * - on success, the server object
235     * - on failure, NULL.
236     *
237     * Side Effects:
238     * - none
239     */
240     server_t *
241 pippijn 1.4 server_find (char const * const name)
242 pippijn 1.1 {
243 pippijn 1.4 server_map::iterator it;
244 pippijn 1.1
245     if (ircd->uses_uid)
246     {
247 pippijn 1.4 it = sidlist.find (name);
248     if (it != sidlist.end ())
249     return it->second;
250 pippijn 1.1 }
251    
252 pippijn 1.4 return ((it = servlist.find (name)) == servlist.end ()
253     ? NULL
254     : it->second);
255 pippijn 1.1 }
256    
257     /*
258 pippijn 1.4 * tld_add(char const * const name)
259 pippijn 1.1 *
260     * TLD object factory.
261     *
262     * Inputs:
263     * - name of TLD to cache as an object
264     *
265     * Outputs:
266     * - on success, a TLD object
267     * - on failure, NULL
268     *
269     * Side Effects:
270     * - the TLD object is registered with the TLD list.
271     */
272     tld_t *
273 pippijn 1.4 tld_add (char const * const name)
274 pippijn 1.1 {
275     tld_t *tld;
276     node_t *n = node_create ();
277    
278     slog (LG_DEBUG, "tld_add(): %s", name);
279    
280 pippijn 1.4 tld = new tld_t;
281 pippijn 1.1
282     node_add (tld, n, &tldlist);
283    
284     tld->name = sstrdup (name);
285    
286     cnt.tld++;
287    
288     return tld;
289     }
290    
291     /*
292 pippijn 1.4 * tld_delete(char const * const name)
293 pippijn 1.1 *
294     * Destroys a TLD object.
295     *
296     * Inputs:
297     * - name of TLD object to destroy
298     *
299     * Outputs:
300     * - nothing
301     *
302     * Side Effects:
303     * - the TLD object is removed and deregistered from the TLD list.
304     */
305     void
306 pippijn 1.4 tld_delete (char const * const name)
307 pippijn 1.1 {
308     tld_t *tld = tld_find (name);
309     node_t *n;
310    
311     if (!tld)
312     {
313     slog (LG_DEBUG, "tld_delete(): called for nonexistant tld: %s", name);
314    
315     return;
316     }
317    
318     slog (LG_DEBUG, "tld_delete(): %s", tld->name);
319    
320     n = node_find (tld, &tldlist);
321     node_del (n, &tldlist);
322     node_free (n);
323    
324 pippijn 1.4 sfree (tld->name);
325     delete tld;
326 pippijn 1.1
327     cnt.tld--;
328     }
329    
330     /*
331 pippijn 1.4 * tld_find(char const * const name)
332 pippijn 1.1 *
333     * Looks up a TLD object.
334     *
335     * Inputs:
336     * - name of TLD object to look up
337     *
338     * Outputs:
339     * - on success, the TLD object
340     * - on failure, NULL
341     *
342     * Side Effects:
343     * - none
344     */
345     tld_t *
346 pippijn 1.4 tld_find (char const * const name)
347 pippijn 1.1 {
348     tld_t *tld;
349     node_t *n;
350    
351     if (name == NULL)
352     return NULL;
353    
354     LIST_FOREACH (n, tldlist.head)
355     {
356     tld = (tld_t *) n->data;
357    
358     if (!strcasecmp (name, tld->name))
359     return tld;
360     }
361    
362     return NULL;
363     }
364    
365     /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
366     * vim:ts=8
367     * vim:sw=8
368     * vim:noexpandtab
369     */