ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/src/servtree.C
Revision: 1.7
Committed: Wed Sep 5 11:23:15 2007 UTC (16 years, 10 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.6: +2 -1 lines
Log Message:
removed GPLed code and put license back to BSD

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.7 static char const rcsid[] = "$Id: servtree.C,v 1.6 2007-09-04 11:13: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 pippijn 1.7 sptr->chanmsg = false;
77 pippijn 1.1
78     if (me.connected)
79     {
80     u = user_find_named (name);
81     if (u != NULL)
82     {
83 pippijn 1.4 phandler->skill (me.name, u->nick, "Nick taken by service");
84 pippijn 1.1 user_delete (u);
85     }
86     }
87    
88     sptr->me = user_add (name, user, host, NULL, NULL, ircd->uses_uid ? sptr->uid : NULL, real, me.me, NOW);
89     sptr->me->flags |= UF_IRCOP;
90    
91     if (me.connected)
92     {
93 pippijn 1.4 phandler->introduce_nick (sptr->me);
94 pippijn 1.1 /* if the snoop channel already exists, join it now */
95     if (config_options.chan != NULL && channel_find (config_options.chan) != NULL)
96     join (config_options.chan, name);
97     }
98    
99 pippijn 1.4 services[sptr->name] = sptr;
100 pippijn 1.1
101     return sptr;
102     }
103    
104     void
105     del_service (service_t *sptr)
106     {
107 pippijn 1.4 services.erase (sptr->name);
108 pippijn 1.1
109 pippijn 1.4 phandler->quit_sts (sptr->me, "Service unloaded.");
110 pippijn 1.1 user_delete (sptr->me);
111     sptr->me = NULL;
112     sptr->handler = NULL;
113 pippijn 1.5 sfree (sptr->disp); /* service_name() does a salloc() */
114 pippijn 1.4 sfree (sptr->name);
115     sfree (sptr->user);
116     sfree (sptr->host);
117     sfree (sptr->real);
118     sfree (sptr->uid);
119 pippijn 1.1
120 pippijn 1.4 delete sptr;
121 pippijn 1.1 }
122    
123     service_t *
124 pippijn 1.4 find_service (char const * const name)
125 pippijn 1.1 {
126     service_t *sptr;
127 pippijn 1.4 service_map::iterator sit;
128 pippijn 1.1 user_t *u;
129     char *p;
130     char name2[NICKLEN];
131    
132     p = strchr (name, '@');
133     if (p != NULL)
134     {
135     /* Make sure it's for us, not for a jupe -- jilles */
136     if (irccasecmp (p + 1, me.name))
137     return NULL;
138     strlcpy (name2, name, sizeof name2);
139     p = strchr (name2, '@');
140     if (p != NULL)
141     *p = '\0';
142 pippijn 1.4 sit = services.find (name2);
143     if (sit != services.end ())
144     return sit->second;
145     foreach (service_pair &sp, services)
146     {
147     sptr = sp.second;
148     if (sptr->me != NULL && !strcasecmp (name2, sptr->user))
149     return sptr;
150     }
151 pippijn 1.1 }
152     else
153     {
154 pippijn 1.4 sit = services.find (name);
155     if (sit != services.end ())
156     return sit->second;
157 pippijn 1.1
158     if (ircd->uses_uid)
159     {
160     /* yuck yuck -- but quite efficient -- jilles */
161     u = user_find (name);
162     if (u != NULL && u->server == me.me)
163 pippijn 1.4 return (sit = services.find (u->nick)) == services.end () ? NULL : sit->second;
164 pippijn 1.1 }
165     }
166    
167     return NULL;
168     }
169    
170     char *
171 pippijn 1.4 service_name (char const * const name)
172 pippijn 1.1 {
173 pippijn 1.5 char *buf = salloc<char> (BUFSIZE);
174 pippijn 1.1
175     snprintf (buf, BUFSIZE, "%s%s%s", name, (config_options.secure) ? "@" : "", (config_options.secure) ? me.name : "");
176    
177     return buf;
178     }