Remove cruft left behind by removed stats_p_hook.
[seven-1.x.git] / modules / m_trace.c
blobe11cb8670c4d8282522b9f90cca7f03166da38cc
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_trace.c: Traces a path to a client/server.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
25 #include "stdinc.h"
26 #include "class.h"
27 #include "hook.h"
28 #include "client.h"
29 #include "hash.h"
30 #include "common.h"
31 #include "hash.h"
32 #include "irc_string.h"
33 #include "ircd.h"
34 #include "numeric.h"
35 #include "commio.h"
36 #include "s_serv.h"
37 #include "s_conf.h"
38 #include "s_newconf.h"
39 #include "send.h"
40 #include "msg.h"
41 #include "parse.h"
42 #include "modules.h"
44 static int m_trace(struct Client *, struct Client *, int, const char **);
46 struct Message trace_msgtab = {
47 "TRACE", 0, 0, 0, MFLG_SLOW,
48 {mg_unreg, {m_trace, 0}, {m_trace, 0}, mg_ignore, mg_ignore, {m_trace, 0}}
51 int doing_trace_hook;
53 mapi_clist_av1 trace_clist[] = { &trace_msgtab, NULL };
54 mapi_hlist_av1 trace_hlist[] = {
55 { "doing_trace", &doing_trace_hook },
56 { NULL, NULL }
58 DECLARE_MODULE_AV1(trace, NULL, NULL, trace_clist, trace_hlist, NULL, "$Revision: 147 $");
60 static void count_downlinks(struct Client *server_p, int *pservcount, int *pusercount);
61 static int report_this_status(struct Client *source_p, struct Client *target_p, int dow);
64 * m_trace
65 * parv[0] = sender prefix
66 * parv[1] = servername
68 static int
69 m_trace(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
71 struct Client *target_p = NULL;
72 struct Class *cltmp;
73 const char *tname;
74 int doall = 0;
75 int cnt = 0, wilds, dow;
76 dlink_node *ptr;
78 if(parc > 1)
80 tname = parv[1];
82 if(parc > 2)
84 if(hunt_server(client_p, source_p, ":%s TRACE %s :%s", 2, parc, parv) !=
85 HUNTED_ISME)
86 return 0;
89 else
90 tname = me.name;
92 /* if we have 3 parameters, then the command is directed at us. So
93 * we shouldnt be forwarding it anywhere.
95 if(parc < 3)
97 switch (hunt_server(client_p, source_p, ":%s TRACE :%s", 1, parc, parv))
99 case HUNTED_PASS: /* note: gets here only if parv[1] exists */
101 struct Client *ac2ptr;
103 if(MyClient(source_p))
104 ac2ptr = find_named_client(tname);
105 else
106 ac2ptr = find_client(tname);
108 if(ac2ptr == NULL)
110 DLINK_FOREACH(ptr, global_client_list.head)
112 ac2ptr = ptr->data;
114 if(match(tname, ac2ptr->name) || match(ac2ptr->name, tname))
115 break;
116 else
117 ac2ptr = NULL;
121 /* giving this out with flattened links defeats the
122 * object --fl
124 if(IsOperRouting(source_p) || IsExemptShide(source_p) ||
125 !ConfigServerHide.flatten_links)
126 sendto_one_numeric(source_p, RPL_TRACELINK,
127 form_str(RPL_TRACELINK),
128 ircd_version,
129 ac2ptr ? ac2ptr->name : tname,
130 ac2ptr ? ac2ptr->from->name : "EEK!");
132 return 0;
135 case HUNTED_ISME:
136 break;
138 default:
139 return 0;
143 if(match(tname, me.name))
145 doall = 1;
147 /* if theyre tracing our SID, we need to move tname to our name so
148 * we dont give the sid in ENDOFTRACE
150 else if(!MyClient(source_p) && !strcmp(tname, me.id))
152 doall = 1;
153 tname = me.name;
156 wilds = strchr(tname, '*') || strchr(tname, '?');
157 dow = wilds || doall;
159 /* specific trace */
160 if(dow == 0)
162 if(MyClient(source_p) || parc > 2)
163 target_p = find_named_person(tname);
164 else
165 target_p = find_person(tname);
167 /* tname could be pointing to an ID at this point, so reset
168 * it to target_p->name if we have a target --fl
170 if(target_p != NULL)
172 report_this_status(source_p, target_p, 0);
173 tname = target_p->name;
176 sendto_one_numeric(source_p, RPL_ENDOFTRACE,
177 form_str(RPL_ENDOFTRACE), tname);
178 return 0;
181 /* give non-opers a limited trace output of themselves (if local),
182 * opers and servers (if no shide) --fl
184 if(!IsOperRouting(source_p))
186 if(MyClient(source_p))
188 if(doall || (wilds && match(tname, source_p->name)))
189 report_this_status(source_p, source_p, 0);
192 DLINK_FOREACH(ptr, local_oper_list.head)
194 target_p = ptr->data;
196 if(!doall && wilds && (match(tname, target_p->name) == 0))
197 continue;
199 report_this_status(source_p, target_p, 0);
202 if (IsExemptShide(source_p) || !ConfigServerHide.flatten_links)
204 DLINK_FOREACH(ptr, serv_list.head)
206 target_p = ptr->data;
208 if(!doall && wilds && !match(tname, target_p->name))
209 continue;
211 report_this_status(source_p, target_p, 0);
215 sendto_one_numeric(source_p, RPL_ENDOFTRACE,
216 form_str(RPL_ENDOFTRACE), tname);
217 return 0;
220 /* source_p is opered */
222 /* report all direct connections */
223 DLINK_FOREACH(ptr, lclient_list.head)
225 target_p = ptr->data;
227 /* dont show invisible users to remote opers */
228 if(IsInvisible(target_p) && dow && !MyConnect(source_p) && !IsOper(target_p))
229 continue;
231 if(!doall && wilds && !match(tname, target_p->name))
232 continue;
234 cnt = report_this_status(source_p, target_p, dow);
237 DLINK_FOREACH(ptr, serv_list.head)
239 target_p = ptr->data;
241 if(!doall && wilds && !match(tname, target_p->name))
242 continue;
244 cnt = report_this_status(source_p, target_p, dow);
247 if(MyConnect(source_p))
249 DLINK_FOREACH(ptr, unknown_list.head)
251 target_p = ptr->data;
253 if(!doall && wilds && !match(tname, target_p->name))
254 continue;
256 cnt = report_this_status(source_p, target_p, dow);
260 if(!cnt)
262 sendto_one_numeric(source_p, ERR_NOSUCHSERVER, form_str(ERR_NOSUCHSERVER),
263 tname);
265 /* let the user have some idea that its at the end of the
266 * trace
268 sendto_one_numeric(source_p, RPL_ENDOFTRACE,
269 form_str(RPL_ENDOFTRACE), tname);
270 return 0;
273 if(doall)
275 DLINK_FOREACH(ptr, class_list.head)
277 cltmp = ptr->data;
279 if(CurrUsers(cltmp) > 0)
280 sendto_one_numeric(source_p, RPL_TRACECLASS,
281 form_str(RPL_TRACECLASS),
282 ClassName(cltmp), CurrUsers(cltmp));
286 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), tname);
288 return 0;
292 * count_downlinks
294 * inputs - pointer to server to count
295 * - pointers to server and user count
296 * output - NONE
297 * side effects - server and user counts are added to given values
299 static void
300 count_downlinks(struct Client *server_p, int *pservcount, int *pusercount)
302 dlink_node *ptr;
304 (*pservcount)++;
305 *pusercount += dlink_list_length(&server_p->serv->users);
306 DLINK_FOREACH(ptr, server_p->serv->servers.head)
308 count_downlinks(ptr->data, pservcount, pusercount);
313 * report_this_status
315 * inputs - pointer to client to report to
316 * - pointer to client to report about
317 * output - counter of number of hits
318 * side effects - NONE
320 static int
321 report_this_status(struct Client *source_p, struct Client *target_p,
322 int dow)
324 const char *name;
325 const char *class_name;
326 char ip[HOSTIPLEN];
327 int cnt = 0;
329 /* sanity check - should never happen */
330 if(!MyConnect(target_p))
331 return 0;
333 inetntop_sock((struct sockaddr *)&target_p->localClient->ip, ip, sizeof(ip));
334 class_name = get_client_class(target_p);
336 if(IsAnyServer(target_p))
337 name = get_server_name(target_p, HIDE_IP);
338 else
339 name = get_client_name(target_p, HIDE_IP);
341 switch (target_p->status)
343 case STAT_CONNECTING:
344 sendto_one_numeric(source_p, RPL_TRACECONNECTING,
345 form_str(RPL_TRACECONNECTING),
346 class_name, name);
347 cnt++;
348 break;
350 case STAT_HANDSHAKE:
351 sendto_one_numeric(source_p, RPL_TRACEHANDSHAKE,
352 form_str(RPL_TRACEHANDSHAKE),
353 class_name, name);
354 cnt++;
355 break;
357 case STAT_ME:
358 break;
360 case STAT_UNKNOWN:
361 /* added time -Taner */
362 sendto_one_numeric(source_p, RPL_TRACEUNKNOWN,
363 form_str(RPL_TRACEUNKNOWN),
364 class_name, name, ip,
365 CurrentTime - target_p->localClient->firsttime);
366 cnt++;
367 break;
369 case STAT_CLIENT:
370 /* Only opers see users if there is a wildcard
371 * but anyone can see all the opers.
373 if((IsOper(source_p) &&
374 (MyClient(source_p) || !(dow && IsInvisible(target_p))))
375 || !dow || IsOper(target_p) || (source_p == target_p))
377 if(IsOper(target_p))
378 sendto_one_numeric(source_p, RPL_TRACEOPERATOR,
379 form_str(RPL_TRACEOPERATOR),
380 class_name, name,
381 show_ip(source_p, target_p) ? ip : "255.255.255.255",
382 CurrentTime - target_p->localClient->lasttime,
383 CurrentTime - target_p->localClient->last);
385 else
386 sendto_one_numeric(source_p, RPL_TRACEUSER,
387 form_str(RPL_TRACEUSER),
388 class_name, name,
389 show_ip(source_p, target_p) ? ip : "255.255.255.255",
390 CurrentTime - target_p->localClient->lasttime,
391 CurrentTime - target_p->localClient->last);
392 cnt++;
394 break;
396 case STAT_SERVER:
398 int usercount = 0;
399 int servcount = 0;
401 count_downlinks(target_p, &servcount, &usercount);
403 sendto_one_numeric(source_p, RPL_TRACESERVER, form_str(RPL_TRACESERVER),
404 class_name, servcount, usercount, name,
405 *(target_p->serv->by) ? target_p->serv->by : "*", "*",
406 me.name, CurrentTime - target_p->localClient->lasttime);
407 cnt++;
410 break;
412 default: /* ...we actually shouldn't come here... --msa */
413 sendto_one_numeric(source_p, RPL_TRACENEWTYPE,
414 form_str(RPL_TRACENEWTYPE),
415 me.name, source_p->name, name);
416 cnt++;
417 break;
420 return (cnt);