ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/servtree.C
Revision: 1.9
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.8: +10 -5 lines
Log Message:
#defines to enum

File Contents

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