ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/src/servtree.C
Revision: 1.6
Committed: Tue Sep 4 11:13:26 2007 UTC (16 years, 10 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.5: +1 -4 lines
Log Message:
- fixed bug in crypto
- merged with upstream

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