ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/src/servtree.C
Revision: 1.8
Committed: Sun Sep 9 20:05:53 2007 UTC (16 years, 10 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.7: +2 -1 lines
Log Message:
- changed configurations to the c++ stdlib
- more #defines to enum
- removed getopt.h and link.h from the system as they were unused
- reworked logstreams
- added an itoa with old syntax
- made klines objects
- moved some global variables into appropriate classes
- fixed boost.foreach's compiler workaround #if's
- allow other files to add exceptions with ADD_EXCEPTION
- changed mynick_t to c++ object
- moved servers.h out of atheme.h
- corrected PING from inspircd 1.2

File Contents

# User Rev Content
1 pippijn 1.1 /*
2 pippijn 1.4 * Copyright © 2005-2006 Atheme Development Group
3 pippijn 1.2 * Rights to this code are documented in doc/pod/license.pod.
4 pippijn 1.1 *
5     * Services binary tree manipulation. (add_service, del_service, et al.)
6     */
7    
8 pippijn 1.8 static char const rcsid[] = "$Id: servtree.C,v 1.7 2007-09-05 11:23:15 pippijn Exp $";
9 pippijn 1.1
10     #include "atheme.h"
11 pippijn 1.8 #include "servers.h"
12 pippijn 1.4 #include <boost/foreach.hpp>
13 pippijn 1.1
14 pippijn 1.4 service_map services;
15 pippijn 1.1
16     service_t *fcmd_agent = NULL;
17    
18     static void
19     dummy_handler (sourceinfo_t *si, int parc, char **parv)
20     {
21     }
22    
23     void
24     servtree_init (void)
25     {
26 pippijn 1.4 #if 0
27 pippijn 1.1 service_heap = BlockHeapCreate (sizeof (service_t), 12);
28 pippijn 1.4 #endif
29 pippijn 1.1 }
30    
31     static void
32     me_me_init (void)
33     {
34     if (me.numeric)
35     init_uid ();
36     me.me = server_add (me.name, 0, NULL, me.numeric ? me.numeric : NULL, me.desc);
37     }
38    
39     service_t *
40 pippijn 1.4 add_service (char const * const name, char const * const user, char const * const host, char const * const real, void (*handler) (sourceinfo_t *si, int parc, char *parv[]), cmdvec &cmdtree)
41 pippijn 1.1 {
42     service_t *sptr;
43     user_t *u;
44    
45     if (me.me == NULL)
46     me_me_init ();
47    
48     if (name == NULL)
49     {
50     slog (LG_INFO, "add_service(): Bad error! We were given a NULL pointer for service name!");
51     return NULL;
52     }
53    
54     if ((sptr = find_service (name)))
55     {
56     slog (LG_DEBUG, "add_service(): Service `%s' already exists.", name);
57     return NULL;
58     }
59    
60 pippijn 1.4 sptr = new service_t;
61 pippijn 1.1
62     sptr->name = sstrdup (name);
63     sptr->user = sstrdup (user);
64     sptr->host = sstrdup (host);
65     sptr->real = sstrdup (real);
66    
67     /* Display name, either <Service> or <Service>@<Server> */
68     sptr->disp = service_name (name);
69    
70     if (me.numeric && *me.numeric)
71     sptr->uid = sstrdup (uid_get ());
72    
73     sptr->handler = handler;
74     sptr->notice_handler = dummy_handler;
75    
76     sptr->cmdtree = &cmdtree;
77 pippijn 1.7 sptr->chanmsg = false;
78 pippijn 1.1
79     if (me.connected)
80     {
81     u = user_find_named (name);
82     if (u != NULL)
83     {
84 pippijn 1.4 phandler->skill (me.name, u->nick, "Nick taken by service");
85 pippijn 1.1 user_delete (u);
86     }
87     }
88    
89     sptr->me = user_add (name, user, host, NULL, NULL, ircd->uses_uid ? sptr->uid : NULL, real, me.me, NOW);
90     sptr->me->flags |= UF_IRCOP;
91    
92     if (me.connected)
93     {
94 pippijn 1.4 phandler->introduce_nick (sptr->me);
95 pippijn 1.1 /* if the snoop channel already exists, join it now */
96     if (config_options.chan != NULL && channel_find (config_options.chan) != NULL)
97     join (config_options.chan, name);
98     }
99    
100 pippijn 1.4 services[sptr->name] = sptr;
101 pippijn 1.1
102     return sptr;
103     }
104    
105     void
106     del_service (service_t *sptr)
107     {
108 pippijn 1.4 services.erase (sptr->name);
109 pippijn 1.1
110 pippijn 1.4 phandler->quit_sts (sptr->me, "Service unloaded.");
111 pippijn 1.1 user_delete (sptr->me);
112     sptr->me = NULL;
113     sptr->handler = NULL;
114 pippijn 1.5 sfree (sptr->disp); /* service_name() does a salloc() */
115 pippijn 1.4 sfree (sptr->name);
116     sfree (sptr->user);
117     sfree (sptr->host);
118     sfree (sptr->real);
119     sfree (sptr->uid);
120 pippijn 1.1
121 pippijn 1.4 delete sptr;
122 pippijn 1.1 }
123    
124     service_t *
125 pippijn 1.4 find_service (char const * const name)
126 pippijn 1.1 {
127     service_t *sptr;
128 pippijn 1.4 service_map::iterator sit;
129 pippijn 1.1 user_t *u;
130     char *p;
131     char name2[NICKLEN];
132    
133     p = strchr (name, '@');
134     if (p != NULL)
135     {
136     /* Make sure it's for us, not for a jupe -- jilles */
137     if (irccasecmp (p + 1, me.name))
138     return NULL;
139     strlcpy (name2, name, sizeof name2);
140     p = strchr (name2, '@');
141     if (p != NULL)
142     *p = '\0';
143 pippijn 1.4 sit = services.find (name2);
144     if (sit != services.end ())
145     return sit->second;
146     foreach (service_pair &sp, services)
147     {
148     sptr = sp.second;
149     if (sptr->me != NULL && !strcasecmp (name2, sptr->user))
150     return sptr;
151     }
152 pippijn 1.1 }
153     else
154     {
155 pippijn 1.4 sit = services.find (name);
156     if (sit != services.end ())
157     return sit->second;
158 pippijn 1.1
159     if (ircd->uses_uid)
160     {
161     /* yuck yuck -- but quite efficient -- jilles */
162     u = user_find (name);
163     if (u != NULL && u->server == me.me)
164 pippijn 1.4 return (sit = services.find (u->nick)) == services.end () ? NULL : sit->second;
165 pippijn 1.1 }
166     }
167    
168     return NULL;
169     }
170    
171     char *
172 pippijn 1.4 service_name (char const * const name)
173 pippijn 1.1 {
174 pippijn 1.5 char *buf = salloc<char> (BUFSIZE);
175 pippijn 1.1
176     snprintf (buf, BUFSIZE, "%s%s%s", name, (config_options.secure) ? "@" : "", (config_options.secure) ? me.name : "");
177    
178     return buf;
179     }