Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / m_trace.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_trace.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
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 1, or (at your option)
12  * any later version.
13  *
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.
18  *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #if 0
83 /*
84  * No need to include handlers.h here the signatures must match
85  * and we don't need to force a rebuild of all the handlers everytime
86  * we add a new one to the list. --Bleep
87  */
88 #include "handlers.h"
89 #endif /* 0 */
90 #include "class.h"
91 #include "client.h"
92 #include "hash.h"
93 #include "ircd.h"
94 #include "ircd_reply.h"
95 #include "ircd_string.h"
96 #include "match.h"
97 #include "msg.h"
98 #include "numeric.h"
99 #include "numnicks.h"
100 #include "s_bsd.h"
101 #include "s_conf.h"
102 #include "s_user.h"
103 #include "send.h"
104 #include "version.h"
105
106 #include <assert.h>
107 #include <string.h>
108
109 /*
110  * m_trace - generic message handler
111  *
112  * parv[0] = sender prefix
113  * parv[1] = nick or servername
114  * parv[2] = 'target' servername
115  */
116 int m_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
117 {
118   int i;
119   struct Client *acptr;
120   struct ConfClass *cltmp;
121   char *tname;
122   int doall, link_s[MAXCONNECTIONS], link_u[MAXCONNECTIONS];
123   int cnt = 0, wilds, dow;
124
125   if (parc < 2 || BadPtr(parv[1])) {
126     /* just "TRACE" without parameters. Must be from local client */
127     parc = 1;
128     acptr = &me;
129     tname = me.name;
130     i = HUNTED_ISME;
131   } else if (parc < 3 || BadPtr(parv[2])) {
132     /* No target specified. Make one before propagating. */
133     parc = 2;
134     tname = parv[1];
135     if ((acptr = find_match_server(parv[1])) ||
136         ((acptr = FindClient(parv[1])) && !MyUser(acptr))) {
137       if (IsUser(acptr))
138         parv[2] = acptr->user->server->name;
139       else
140         parv[2] = acptr->name;
141       parc = 3;
142       parv[3] = 0;
143       if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, IsServer(acptr),
144                                "%s :%C", 2, parc, parv)) == HUNTED_NOSUCH)
145         return 0;
146     } else
147       i = HUNTED_ISME;
148   } else {
149     /* Got "TRACE <tname> :<target>" */
150     parc = 3;
151     if (MyUser(sptr) || Protocol(cptr) < 10)
152       acptr = find_match_server(parv[2]);
153     else
154       acptr = FindNServer(parv[2]);
155     if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, 0, "%s :%C", 2, parc,
156                              parv)) == HUNTED_NOSUCH)
157       return 0;
158     tname = parv[1];
159   }
160
161   if (i == HUNTED_PASS) {
162     if (!acptr)
163       acptr = next_client(GlobalClientList, tname);
164     else
165       acptr = acptr->from;
166     send_reply(sptr, RPL_TRACELINK,
167 #ifndef GODMODE
168         version, debugmode, tname, acptr ? acptr->from->name : "<No_match>"
169 #else /* GODMODE */
170         version, debugmode, tname, acptr ? acptr->from->name : "<No_match>",
171         (acptr && acptr->from->serv) ? acptr->from->serv->timestamp : 0
172 #endif /* GODMODE */
173                ); /* I really could do without GODMODE */
174     return 0;
175   }
176
177   doall = (parv[1] && (parc > 1)) ? !match(tname, me.name) : 1;
178   wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
179   dow = wilds || doall;
180
181   /* Don't give (long) remote listings to lusers */
182   if (dow && !MyConnect(sptr) && !IsAnOper(sptr))
183     return 0;
184
185   for (i = 0; i < MAXCONNECTIONS; i++)
186     link_s[i] = 0, link_u[i] = 0;
187
188   if (doall) {
189     for (acptr = GlobalClientList; acptr; acptr = acptr->next) {
190       if (IsUser(acptr))
191         link_u[acptr->from->fd]++;
192       else if (IsServer(acptr))
193         link_s[acptr->from->fd]++;
194     }
195   }
196
197   /* report all direct connections */
198
199   for (i = 0; i <= HighestFd; i++) {
200     unsigned int conClass;
201
202     if (!(acptr = LocalClientArray[i])) /* Local Connection? */
203       continue;
204     if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
205         !IsAnOper(acptr) && (acptr != sptr))
206       continue;
207     if (!doall && wilds && match(tname, acptr->name))
208       continue;
209     if (!dow && 0 != ircd_strcmp(tname, acptr->name))
210       continue;
211
212     conClass = get_client_class(acptr);
213
214     switch (acptr->status) {
215       case STAT_CONNECTING:
216         send_reply(sptr, RPL_TRACECONNECTING, conClass, acptr->name);
217         cnt++;
218         break;
219       case STAT_HANDSHAKE:
220         send_reply(sptr, RPL_TRACEHANDSHAKE, conClass, acptr->name);
221         cnt++;
222         break;
223       case STAT_ME:
224         break;
225       case STAT_UNKNOWN:
226       case STAT_UNKNOWN_USER:
227         send_reply(sptr, RPL_TRACEUNKNOWN, conClass,
228                    get_client_name(acptr, HIDE_IP));
229         cnt++;
230         break;
231       case STAT_UNKNOWN_SERVER:
232         send_reply(sptr, RPL_TRACEUNKNOWN, conClass, "Unknown Server");
233         cnt++;
234         break;
235       case STAT_USER:
236         /* Only opers see users if there is a wildcard
237            but anyone can see all the opers. */
238         if ((IsAnOper(sptr) && (MyUser(sptr) ||
239             !(dow && IsInvisible(acptr)))) || !dow || IsAnOper(acptr)) {
240           if (IsAnOper(acptr))
241             send_reply(sptr, RPL_TRACEOPERATOR, conClass,
242                        get_client_name(acptr, HIDE_IP),
243                        CurrentTime - acptr->lasttime);
244           else
245             send_reply(sptr, RPL_TRACEUSER, conClass,
246                        get_client_name(acptr, HIDE_IP),
247                        CurrentTime - acptr->lasttime);
248           cnt++;
249         }
250         break;
251         /*
252          * Connection is a server
253          *
254          * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
255          *
256          * class        Class the server is in
257          * nS           Number of servers reached via this link
258          * nC           Number of clients reached via this link
259          * name         Name of the server linked
260          * ConnBy       Who established this link
261          * last         Seconds since we got something from this link
262          * age          Seconds this link has been alive
263          *
264          * Additional comments etc......        -Cym-<cym@acrux.net>
265          */
266
267       case STAT_SERVER:
268         if (acptr->serv->user)
269           send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
270                      link_u[i], acptr->name,
271                      (*acptr->serv->by) ? acptr->serv->by : "*",
272                      acptr->serv->user->username, acptr->serv->user->host,
273                      CurrentTime - acptr->lasttime,
274                      CurrentTime - acptr->serv->timestamp);
275         else
276           send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
277                      link_u[i], acptr->name,
278                      (*acptr->serv->by) ?  acptr->serv->by : "*", "*",
279                      me.name, CurrentTime - acptr->lasttime,
280                      CurrentTime - acptr->serv->timestamp);
281         cnt++;
282         break;
283       default:                  /* We actually shouldn't come here, -msa */
284         send_reply(sptr, RPL_TRACENEWTYPE, get_client_name(acptr, HIDE_IP));
285         cnt++;
286         break;
287     }
288   }
289   /*
290    * Add these lines to summarize the above which can get rather long
291    * and messy when done remotely - Avalon
292    */
293   if (!IsAnOper(sptr) || !cnt) {
294     if (!cnt)
295       /* let the user have some idea that its at the end of the trace */
296       send_reply(sptr, RPL_TRACESERVER, 0, link_s[me.fd],
297                  link_u[me.fd], "<No_match>", *(me.serv->by) ?
298                  me.serv->by : "*", "*", me.name, 0, 0);
299     return 0;
300   }
301   for (cltmp = FirstClass(); doall && cltmp; cltmp = NextClass(cltmp))
302     if (Links(cltmp) > 0)
303      send_reply(sptr, RPL_TRACECLASS, ConClass(cltmp), Links(cltmp));
304   return 0;
305 }
306
307 /*
308  * ms_trace - server message handler
309  *
310  * parv[0] = sender prefix
311  * parv[1] = nick or servername
312  * parv[2] = 'target' servername
313  */
314 int ms_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
315 {
316   int i;
317   struct Client *acptr;
318   struct ConfClass *cltmp;
319   char *tname;
320   int doall, link_s[MAXCONNECTIONS], link_u[MAXCONNECTIONS];
321   int cnt = 0, wilds, dow;
322
323   if (parc < 2 || BadPtr(parv[1])) {
324     /* just "TRACE" without parameters. Must be from local client */
325     parc = 1;
326     acptr = &me;
327     tname = me.name;
328     i = HUNTED_ISME;
329   } else if (parc < 3 || BadPtr(parv[2])) {
330     /* No target specified. Make one before propagating. */
331     parc = 2;
332     tname = parv[1];
333     if ((acptr = find_match_server(parv[1])) ||
334         ((acptr = FindClient(parv[1])) && !MyUser(acptr))) {
335       if (IsUser(acptr))
336         parv[2] = acptr->user->server->name;
337       else
338         parv[2] = acptr->name;
339       parc = 3;
340       parv[3] = 0;
341
342       if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, IsServer(acptr),
343                                "%s :%C", 2, parc, parv)) == HUNTED_NOSUCH)
344         return 0;
345     } else
346       i = HUNTED_ISME;
347   } else {
348     /* Got "TRACE <tname> :<target>" */
349     parc = 3;
350     if (MyUser(sptr) || Protocol(cptr) < 10)
351       acptr = find_match_server(parv[2]);
352     else
353       acptr = FindNServer(parv[2]);
354     if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, 0, "%s :%C", 2, parc,
355                              parv)) == HUNTED_NOSUCH)
356       return 0;
357     tname = parv[1];
358   }
359
360   if (i == HUNTED_PASS) {
361     if (!acptr)
362       acptr = next_client(GlobalClientList, tname);
363     else
364       acptr = acptr->from;
365     send_reply(sptr, RPL_TRACELINK,
366 #ifndef GODMODE
367         version, debugmode, tname, acptr ? acptr->from->name : "<No_match>"
368 #else /* GODMODE */
369         version, debugmode, tname, acptr ? acptr->from->name : "<No_match>",
370         (acptr && acptr->from->serv) ? acptr->from->serv->timestamp : 0
371 #endif /* GODMODE */
372                ); /* I really could do without GODMODE */
373     return 0;
374   }
375
376   doall = (parv[1] && (parc > 1)) ? !match(tname, me.name) : 1;
377   wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
378   dow = wilds || doall;
379
380   /* Don't give (long) remote listings to lusers */
381   if (dow && !MyConnect(sptr) && !IsAnOper(sptr))
382     return 0;
383
384   for (i = 0; i < MAXCONNECTIONS; i++)
385     link_s[i] = 0, link_u[i] = 0;
386
387   if (doall) {
388     for (acptr = GlobalClientList; acptr; acptr = acptr->next) {
389       if (IsUser(acptr))
390         link_u[acptr->from->fd]++;
391       else if (IsServer(acptr))
392         link_s[acptr->from->fd]++;
393     }
394   }
395
396   /* report all direct connections */
397
398   for (i = 0; i <= HighestFd; i++) {
399     unsigned int conClass;
400
401     if (!(acptr = LocalClientArray[i])) /* Local Connection? */
402       continue;
403     if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
404         !IsAnOper(acptr) && (acptr != sptr))
405       continue;
406     if (!doall && wilds && match(tname, acptr->name))
407       continue;
408     if (!dow && 0 != ircd_strcmp(tname, acptr->name))
409       continue;
410     conClass = get_client_class(acptr);
411
412     switch (acptr->status) {
413       case STAT_CONNECTING:
414         send_reply(sptr, RPL_TRACECONNECTING, conClass, acptr->name);
415         cnt++;
416         break;
417       case STAT_HANDSHAKE:
418         send_reply(sptr, RPL_TRACEHANDSHAKE, conClass, acptr->name);
419         cnt++;
420         break;
421       case STAT_ME:
422         break;
423       case STAT_UNKNOWN:
424       case STAT_UNKNOWN_USER:
425         send_reply(sptr, RPL_TRACEUNKNOWN, conClass,
426                    get_client_name(acptr, HIDE_IP));
427         cnt++;
428         break;
429       case STAT_UNKNOWN_SERVER:
430         send_reply(sptr, RPL_TRACEUNKNOWN, conClass, "Unknown Server");
431         cnt++;
432         break;
433       case STAT_USER:
434         /* Only opers see users if there is a wildcard
435            but anyone can see all the opers. */
436         if ((IsAnOper(sptr) && (MyUser(sptr) ||
437             !(dow && IsInvisible(acptr)))) || !dow || IsAnOper(acptr)) {
438           if (IsAnOper(acptr))
439             send_reply(sptr, RPL_TRACEOPERATOR, conClass,
440                        get_client_name(acptr, HIDE_IP),
441                        CurrentTime - acptr->lasttime);
442           else
443             send_reply(sptr, RPL_TRACEUSER, conClass,
444                        get_client_name(acptr, HIDE_IP),
445                        CurrentTime - acptr->lasttime);
446           cnt++;
447         }
448         break;
449         /*
450          * Connection is a server
451          *
452          * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
453          *
454          * class        Class the server is in
455          * nS           Number of servers reached via this link
456          * nC           Number of clients reached via this link
457          * name         Name of the server linked
458          * ConnBy       Who established this link
459          * last         Seconds since we got something from this link
460          * age          Seconds this link has been alive
461          *
462          * Additional comments etc......        -Cym-<cym@acrux.net>
463          */
464
465       case STAT_SERVER:
466         if (acptr->serv->user)
467           send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
468                      link_u[i], acptr->name,
469                      (*acptr->serv->by) ? acptr->serv->by : "*",
470                      acptr->serv->user->username, acptr->serv->user->host,
471                      CurrentTime - acptr->lasttime,
472                      CurrentTime - acptr->serv->timestamp);
473         else
474           send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
475                      link_u[i], acptr->name,
476                      (*acptr->serv->by) ?  acptr->serv->by : "*", "*",
477                      me.name, CurrentTime - acptr->lasttime,
478                      CurrentTime - acptr->serv->timestamp);
479         cnt++;
480         break;
481       default:                  /* We actually shouldn't come here, -msa */
482         send_reply(sptr, RPL_TRACENEWTYPE, get_client_name(acptr, HIDE_IP));
483         cnt++;
484         break;
485     }
486   }
487   /*
488    * Add these lines to summarize the above which can get rather long
489    * and messy when done remotely - Avalon
490    */
491   if (!IsAnOper(sptr) || !cnt) {
492     if (!cnt)
493       /* let the user have some idea that its at the end of the trace */
494       send_reply(sptr, RPL_TRACESERVER, 0, link_s[me.fd],
495           link_u[me.fd], "<No_match>", *(me.serv->by) ?
496           me.serv->by : "*", "*", me.name, 0, 0);
497     return 0;
498   }
499   for (cltmp = FirstClass(); doall && cltmp; cltmp = NextClass(cltmp))
500     if (Links(cltmp) > 0)
501       send_reply(sptr, RPL_TRACECLASS, ConClass(cltmp), Links(cltmp));
502   return 0;
503 }
504
505 /*
506  * mo_trace - oper message handler
507  *
508  * parv[0] = sender prefix
509  * parv[1] = nick or servername
510  * parv[2] = 'target' servername
511  */
512 int mo_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
513 {
514   int i;
515   struct Client *acptr;
516   struct ConfClass *cltmp;
517   char *tname;
518   int doall, link_s[MAXCONNECTIONS], link_u[MAXCONNECTIONS];
519   int cnt = 0, wilds, dow;
520
521   if (parc < 2 || BadPtr(parv[1])) {
522     /* just "TRACE" without parameters. Must be from local client */
523     parc = 1;
524     acptr = &me;
525     tname = me.name;
526     i = HUNTED_ISME;
527   } else if (parc < 3 || BadPtr(parv[2])) {
528     /* No target specified. Make one before propagating. */
529     parc = 2;
530     tname = parv[1];
531     if ((acptr = find_match_server(parv[1])) ||
532         ((acptr = FindClient(parv[1])) && !MyUser(acptr))) {
533       if (IsUser(acptr))
534         parv[2] = acptr->user->server->name;
535       else
536         parv[2] = acptr->name;
537       parc = 3;
538       parv[3] = 0;
539       if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, IsServer(acptr),
540                                "%s :%C", 2, parc, parv)) == HUNTED_NOSUCH)
541         return 0;
542     } else
543       i = HUNTED_ISME;
544   } else {
545     /* Got "TRACE <tname> :<target>" */
546     parc = 3;
547     if (MyUser(sptr) || Protocol(cptr) < 10)
548       acptr = find_match_server(parv[2]);
549     else
550       acptr = FindNServer(parv[2]);
551     if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, 0, "%s :%C", 2, parc,
552                              parv)) == HUNTED_NOSUCH)
553       return 0;
554     tname = parv[1];
555   }
556
557   if (i == HUNTED_PASS) {
558     if (!acptr)
559       acptr = next_client(GlobalClientList, tname);
560     else
561       acptr = acptr->from;
562     send_reply(sptr, RPL_TRACELINK,
563 #ifndef GODMODE
564         version, debugmode, tname, acptr ? acptr->from->name : "<No_match>"
565 #else /* GODMODE */
566         version, debugmode, tname, acptr ? acptr->from->name : "<No_match>",
567         (acptr && acptr->from->serv) ? acptr->from->serv->timestamp : 0
568 #endif /* GODMODE */
569                ); /* I really could do without GODMODE */
570     return 0;
571   }
572
573   doall = (parv[1] && (parc > 1)) ? !match(tname, me.name) : 1;
574   wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
575   dow = wilds || doall;
576
577   /* Don't give (long) remote listings to lusers */
578   if (dow && !MyConnect(sptr) && !IsAnOper(sptr))
579     return 0;
580
581   for (i = 0; i < MAXCONNECTIONS; i++)
582     link_s[i] = 0, link_u[i] = 0;
583
584   if (doall) {
585     for (acptr = GlobalClientList; acptr; acptr = acptr->next) {
586       if (IsUser(acptr))
587         link_u[acptr->from->fd]++;
588       else if (IsServer(acptr))
589         link_s[acptr->from->fd]++;
590     }
591   }
592
593   /* report all direct connections */
594
595   for (i = 0; i <= HighestFd; i++) {
596     unsigned int conClass;
597
598     if (!(acptr = LocalClientArray[i])) /* Local Connection? */
599       continue;
600     if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
601         !IsAnOper(acptr) && (acptr != sptr))
602       continue;
603     if (!doall && wilds && match(tname, acptr->name))
604       continue;
605     if (!dow && 0 != ircd_strcmp(tname, acptr->name))
606       continue;
607     conClass = get_client_class(acptr);
608
609     switch (acptr->status) {
610       case STAT_CONNECTING:
611         send_reply(sptr, RPL_TRACECONNECTING, conClass, acptr->name);
612         cnt++;
613         break;
614       case STAT_HANDSHAKE:
615         send_reply(sptr, RPL_TRACEHANDSHAKE, conClass, acptr->name);
616         cnt++;
617         break;
618       case STAT_ME:
619         break;
620       case STAT_UNKNOWN:
621       case STAT_UNKNOWN_USER:
622         send_reply(sptr, RPL_TRACEUNKNOWN, conClass,
623                    get_client_name(acptr, HIDE_IP));
624         cnt++;
625         break;
626       case STAT_UNKNOWN_SERVER:
627         send_reply(sptr, RPL_TRACEUNKNOWN, conClass, "Unknown Server");
628         cnt++;
629         break;
630       case STAT_USER:
631         /* Only opers see users if there is a wildcard
632            but anyone can see all the opers. */
633         if ((IsAnOper(sptr) && (MyUser(sptr) ||
634             !(dow && IsInvisible(acptr)))) || !dow || IsAnOper(acptr)) {
635           if (IsAnOper(acptr))
636             send_reply(sptr, RPL_TRACEOPERATOR, conClass,
637                        get_client_name(acptr, HIDE_IP),
638                        CurrentTime - acptr->lasttime);
639           else
640             send_reply(sptr, RPL_TRACEUSER, conClass,
641                        get_client_name(acptr, HIDE_IP),
642                        CurrentTime - acptr->lasttime);
643           cnt++;
644         }
645         break;
646         /*
647          * Connection is a server
648          *
649          * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
650          *
651          * class        Class the server is in
652          * nS           Number of servers reached via this link
653          * nC           Number of clients reached via this link
654          * name         Name of the server linked
655          * ConnBy       Who established this link
656          * last         Seconds since we got something from this link
657          * age          Seconds this link has been alive
658          *
659          * Additional comments etc......        -Cym-<cym@acrux.net>
660          */
661
662       case STAT_SERVER:
663         if (acptr->serv->user)
664           send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
665                      link_u[i], acptr->name,
666                      (*acptr->serv->by) ? acptr->serv->by : "*",
667                      acptr->serv->user->username, acptr->serv->user->host,
668                      CurrentTime - acptr->lasttime,
669                      CurrentTime - acptr->serv->timestamp);
670         else
671           send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
672                      link_u[i], acptr->name,
673                      (*acptr->serv->by) ?  acptr->serv->by : "*", "*",
674                      me.name, CurrentTime - acptr->lasttime,
675                      CurrentTime - acptr->serv->timestamp);
676         cnt++;
677         break;
678       default:                  /* We actually shouldn't come here, -msa */
679         send_reply(sptr, RPL_TRACENEWTYPE, get_client_name(acptr, HIDE_IP));
680         cnt++;
681         break;
682     }
683   }
684   /*
685    * Add these lines to summarize the above which can get rather long
686    * and messy when done remotely - Avalon
687    */
688   if (!IsAnOper(sptr) || !cnt) {
689     if (!cnt)
690       /* let the user have some idea that its at the end of the trace */
691       send_reply(sptr, RPL_TRACESERVER, 0, link_s[me.fd],
692                  link_u[me.fd], "<No_match>", *(me.serv->by) ?
693                  me.serv->by : "*", "*", me.name, 0, 0);
694     return 0;
695   }
696   for (cltmp = FirstClass(); doall && cltmp; cltmp = NextClass(cltmp))
697     if (Links(cltmp) > 0)
698       send_reply(sptr, RPL_TRACECLASS, ConClass(cltmp), Links(cltmp));
699   return 0;
700 }
701
702   
703 #if 0
704 /*
705  * m_trace
706  *
707  * parv[0] = sender prefix
708  * parv[1] = nick or servername
709  * parv[2] = 'target' servername
710  */
711 int m_trace(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
712 {
713   int i;
714   struct Client *acptr;
715   struct ConfClass *cltmp;
716   char *tname;
717   int doall, link_s[MAXCONNECTIONS], link_u[MAXCONNECTIONS];
718   int cnt = 0, wilds, dow;
719
720   if (parc < 2 || BadPtr(parv[1]))
721   {
722     /* just "TRACE" without parameters. Must be from local client */
723     parc = 1;
724     acptr = &me;
725     tname = me.name;
726     i = HUNTED_ISME;
727   }
728   else if (parc < 3 || BadPtr(parv[2]))
729   {
730     /* No target specified. Make one before propagating. */
731     parc = 2;
732     tname = parv[1];
733     if ((acptr = find_match_server(parv[1])) ||
734         ((acptr = FindClient(parv[1])) && !MyUser(acptr)))
735     {
736       if (IsUser(acptr))
737         parv[2] = acptr->user->server->name;
738       else
739         parv[2] = acptr->name;
740       parc = 3;
741       parv[3] = 0;
742       if ((i = hunt_server(IsServer(acptr), cptr, sptr, /* XXX DEAD */
743           "%s%s " TOK_TRACE " %s :%s", 2, parc, parv)) == HUNTED_NOSUCH)
744         return 0;
745     }
746     else
747       i = HUNTED_ISME;
748   }
749   else
750   {
751     /* Got "TRACE <tname> :<target>" */
752     parc = 3;
753     if (MyUser(sptr) || Protocol(cptr) < 10)
754       acptr = find_match_server(parv[2]);
755     else
756       acptr = FindNServer(parv[2]);
757     if ((i = hunt_server(0, cptr, sptr, /* XXX DEAD */
758         "%s%s " TOK_TRACE " %s :%s", 2, parc, parv)) == HUNTED_NOSUCH)
759       return 0;
760     tname = parv[1];
761   }
762
763   if (i == HUNTED_PASS)
764   {
765     if (!acptr)
766       acptr = next_client(GlobalClientList, tname);
767     else
768       acptr = acptr->from;
769     sendto_one(sptr, rpl_str(RPL_TRACELINK), me.name, parv[0], /* XXX DEAD */
770 #ifndef GODMODE
771         version, debugmode, tname, acptr ? acptr->from->name : "<No_match>");
772 #else /* GODMODE */
773         version, debugmode, tname, acptr ? acptr->from->name : "<No_match>",
774         (acptr && acptr->from->serv) ? acptr->from->serv->timestamp : 0);
775 #endif /* GODMODE */
776     return 0;
777   }
778
779   doall = (parv[1] && (parc > 1)) ? !match(tname, me.name) : TRUE;
780   wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
781   dow = wilds || doall;
782
783   /* Don't give (long) remote listings to lusers */
784   if (dow && !MyConnect(sptr) && !IsAnOper(sptr))
785     return 0;
786
787   for (i = 0; i < MAXCONNECTIONS; i++)
788     link_s[i] = 0, link_u[i] = 0;
789
790   if (doall)
791   {
792     for (acptr = GlobalClientList; acptr; acptr = acptr->next) {
793       if (IsUser(acptr))
794         link_u[acptr->from->fd]++;
795       else if (IsServer(acptr))
796         link_s[acptr->from->fd]++;
797     }
798   }
799
800   /* report all direct connections */
801
802   for (i = 0; i <= HighestFd; i++)
803   {
804     const char* name;
805     unsigned int conClass;
806
807     if (!(acptr = LocalClientArray[i])) /* Local Connection? */
808       continue;
809     if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
810         !IsAnOper(acptr) && (acptr != sptr))
811       continue;
812     if (!doall && wilds && match(tname, acptr->name))
813       continue;
814     if (!dow && 0 != ircd_strcmp(tname, acptr->name))
815       continue;
816     conClass = get_client_class(acptr);
817
818     switch (acptr->status)
819     {
820       case STAT_CONNECTING:
821         sendto_one(sptr, rpl_str(RPL_TRACECONNECTING), /* XXX DEAD */
822             me.name, parv[0], conClass, name);
823         cnt++;
824         break;
825       case STAT_HANDSHAKE:
826         sendto_one(sptr, rpl_str(RPL_TRACEHANDSHAKE), /* XXX DEAD */
827             me.name, parv[0], conClass, name);
828         cnt++;
829         break;
830       case STAT_ME:
831         break;
832       case STAT_UNKNOWN:
833       case STAT_UNKNOWN_USER:
834       case STAT_UNKNOWN_SERVER:
835         sendto_one(sptr, rpl_str(RPL_TRACEUNKNOWN), /* XXX DEAD */
836             me.name, parv[0], conClass, name);
837         cnt++;
838         break;
839       case STAT_USER:
840         /* Only opers see users if there is a wildcard
841            but anyone can see all the opers. */
842         if ((IsAnOper(sptr) && (MyUser(sptr) ||
843             !(dow && IsInvisible(acptr)))) || !dow || IsAnOper(acptr))
844         {
845           if (IsAnOper(acptr))
846             sendto_one(sptr, rpl_str(RPL_TRACEOPERATOR), /* XXX DEAD */
847                 me.name, parv[0], conClass, name, CurrentTime - acptr->lasttime);
848           else
849             sendto_one(sptr, rpl_str(RPL_TRACEUSER), /* XXX DEAD */
850                 me.name, parv[0], conClass, name, CurrentTime - acptr->lasttime);
851           cnt++;
852         }
853         break;
854         /*
855          * Connection is a server
856          *
857          * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
858          *
859          * class        Class the server is in
860          * nS           Number of servers reached via this link
861          * nC           Number of clients reached via this link
862          * name         Name of the server linked
863          * ConnBy       Who established this link
864          * last         Seconds since we got something from this link
865          * age          Seconds this link has been alive
866          *
867          * Additional comments etc......        -Cym-<cym@acrux.net>
868          */
869
870       case STAT_SERVER:
871         if (acptr->serv->user)
872           sendto_one(sptr, rpl_str(RPL_TRACESERVER), /* XXX DEAD */
873               me.name, parv[0], conClass, link_s[i],
874               link_u[i], name, (*acptr->serv->by) ? acptr->serv->by : "*",
875               acptr->serv->user->username, acptr->serv->user->host,
876               CurrentTime - acptr->lasttime,
877               CurrentTime - acptr->serv->timestamp);
878         else
879           sendto_one(sptr, rpl_str(RPL_TRACESERVER), /* XXX DEAD */
880               me.name, parv[0], conClass, link_s[i],
881               link_u[i], name, (*acptr->serv->by) ?  acptr->serv->by : "*", "*",
882               me.name, CurrentTime - acptr->lasttime,
883               CurrentTime - acptr->serv->timestamp);
884         cnt++;
885         break;
886       default:                  /* We actually shouldn't come here, -msa */
887         sendto_one(sptr, rpl_str(RPL_TRACENEWTYPE), me.name, parv[0], name); /* XXX DEAD */
888         cnt++;
889         break;
890     }
891   }
892   /*
893    * Add these lines to summarize the above which can get rather long
894    * and messy when done remotely - Avalon
895    */
896   if (!IsAnOper(sptr) || !cnt)
897   {
898     if (!cnt)
899       /* let the user have some idea that its at the end of the trace */
900       sendto_one(sptr, rpl_str(RPL_TRACESERVER), /* XXX DEAD */
901           me.name, parv[0], 0, link_s[me.fd],
902           link_u[me.fd], "<No_match>", *(me.serv->by) ?
903           me.serv->by : "*", "*", me.name, 0, 0);
904     return 0;
905   }
906   for (cltmp = FirstClass(); doall && cltmp; cltmp = NextClass(cltmp))
907     if (Links(cltmp) > 0)
908       sendto_one(sptr, rpl_str(RPL_TRACECLASS), me.name, /* XXX DEAD */
909           parv[0], ConClass(cltmp), Links(cltmp));
910   return 0;
911 }
912 #endif /* 0 */
913