fixed ircd crashing when using /whowas
[ircu2.10.12-pk.git] / ircd / m_whowas.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_whowas.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: m_whowas.c 1599 2006-01-01 04:39:07Z entrope $
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 #include "config.h"
83
84 #include "client.h"
85 #include "hash.h"
86 #include "ircd.h"
87 #include "ircd_features.h"
88 #include "ircd_log.h"
89 #include "ircd_reply.h"
90 #include "ircd_string.h"
91 #include "msg.h"
92 #include "numeric.h"
93 #include "numnicks.h"
94 #include "s_user.h"
95 #include "s_misc.h"
96 #include "send.h"
97 #include "whowas.h"
98
99 /* #include <assert.h> -- Now using assert in ircd_log.h */
100 #include <stdlib.h>
101
102 /*
103  * m_whowas - generic message handler
104  *
105  * parv[0] = sender prefix
106  * parv[1] = nickname queried
107  * parv[2] = maximum returned items (optional, default is unlimited)
108  * parv[3] = remote server target (Opers only, max returned items 20)
109  */
110 int m_whowas(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
111 {
112   struct Whowas *temp;
113   int cur = 0;
114   int max = -1, found = 0;
115   char *p, *nick, *s;
116
117   if (parc < 2)
118   {
119     send_reply(sptr, ERR_NONICKNAMEGIVEN);
120     return 0;
121   }
122   if (parc > 2)
123     max = atoi(parv[2]);
124   if (parc > 3)
125     if (hunt_server_cmd(sptr, CMD_WHOWAS, cptr, 1, "%s %s :%C", 3, parc, parv))
126       return 0;
127
128   parv[1] = canonize(parv[1]);
129   if (!MyConnect(sptr) && (max > 20))
130     max = 20;                   /* Set max replies at 20 */
131   for (s = parv[1]; (nick = ircd_strtok(&p, s, ",")); s = 0)
132   {
133     /* Search through bucket, finding all nicknames that match */
134     found = 0;
135     for (temp = whowashash[hash_whowas_name(nick)]; temp; temp = temp->hnext)
136     {
137       if (0 == ircd_strcmp(nick, temp->name))
138       {
139         send_reply(sptr, RPL_WHOWASUSER, temp->name, temp->username,
140                    temp->hostname, temp->realname);
141         if (IsAnOper(sptr) && temp->realhost)
142           send_reply(sptr, RPL_WHOISACTUALLY, temp->name, temp->username, temp->realhost, "<untracked>");
143         send_reply(sptr, RPL_WHOISSERVER, temp->name,
144                    (feature_bool(FEAT_HIS_WHOIS_SERVERNAME) && !IsOper(sptr)) ?
145                      feature_str(FEAT_HIS_SERVERNAME) :
146                      temp->servername,
147                    myctime(temp->logoff),
148            "","*");
149         if (temp->away)
150           send_reply(sptr, RPL_AWAY, temp->name, temp->away);
151         cur++;
152         found++;
153       }
154       if (max >= 0 && cur >= max)
155         break;
156     }
157     if (!found)
158       send_reply(sptr, ERR_WASNOSUCHNICK, nick);
159     /* To keep parv[1] intact for ENDOFWHOWAS */
160     if (p)
161       p[-1] = ',';
162   }
163   send_reply(sptr, RPL_ENDOFWHOWAS, parv[1]);
164   return 0;
165 }