2 * IRC - Internet Relay Chat, ircd/m_whois.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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)
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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * m_functions execute protocol messages on this server:
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...).
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.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
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 ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
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*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
86 * No need to include handlers.h here the signatures must match
87 * and we don't need to force a rebuild of all the handlers everytime
88 * we add a new one to the list. --Bleep
96 #include "ircd_policy.h"
97 #include "ircd_reply.h"
98 #include "ircd_string.h"
102 #include "numnicks.h"
112 * * Rewritten to make this understandable
113 * * You can nolonger /whois unregistered clients.
117 * /whois nick always shows the nick.
118 * /whois wild* shows the nick if:
119 * * they aren't +i and aren't on any channels.
120 * * they are on a common channel.
121 * * they aren't +i and are on a public channel. (not +p and not +s)
122 * * they aren't +i and are on a private channel. (+p but not +s)
123 * Or to look at it another way (I think):
124 * * +i users are only shown if your on a common channel.
125 * * users on +s channels only aren't shown.
127 * whois doesn't show what channels a +k client is on, for the reason that
128 * /whois X or /whois W floods a user off the server. :)
130 * nb: if the code and this comment disagree, the codes right and I screwed
135 * Send whois information for acptr to sptr
137 static void do_whois(struct Client* sptr, struct Client *acptr)
139 struct Client *a2cptr=0;
140 struct Channel *chptr=0;
143 static char buf[512];
145 const struct User* user = cli_user(acptr);
146 const char* name = (!*(cli_name(acptr))) ? "?" : cli_name(acptr);
147 a2cptr = user->server;
149 send_reply(sptr, RPL_WHOISUSER, name, user->username, user->host,
152 /* Display the channels this user is on. */
153 if (!IsChannelService(acptr))
155 struct Membership* chan;
156 mlen = strlen(cli_name(&me)) + strlen(cli_name(sptr)) + 12 + strlen(name);
159 for (chan = user->channel; chan; chan = chan->next_channel)
161 chptr = chan->channel;
163 if (!ShowChannel(sptr, chptr))
166 if (acptr != sptr && IsZombie(chan))
169 if (len+strlen(chptr->chname) + mlen > BUFSIZE - 5)
171 send_reply(sptr, SND_EXPLICIT | RPL_WHOISCHANNELS, "%s :%s", name, buf);
176 *(buf + len++) = '-';
177 if (is_chan_op(acptr, chptr))
178 *(buf + len++) = '@';
179 else if (has_voice(acptr, chptr))
180 *(buf + len++) = '+';
181 else if (IsZombie(chan))
182 *(buf + len++) = '!';
185 strcpy(buf + len, chptr->chname);
186 len += strlen(chptr->chname);
187 strcat(buf + len, " ");
191 send_reply(sptr, RPL_WHOISCHANNELS, name, buf);
194 #ifdef HEAD_IN_SAND_WHOIS_SERVERNAME
195 if (!IsAnOper(sptr) && sptr != acptr)
196 send_reply(sptr, RPL_WHOISSERVER, name, "*.undernet.org",
197 "The Undernet Underworld");
200 send_reply(sptr, RPL_WHOISSERVER, name, cli_name(a2cptr),
206 send_reply(sptr, RPL_AWAY, name, user->away);
208 if (IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
209 HasPriv(sptr, PRIV_SEE_OPERS)))
210 send_reply(sptr, RPL_WHOISOPERATOR, name);
212 /* Hint: if your looking to add more flags to a user, eg +h, here's
213 * probably a good place to add them :)
216 if (MyConnect(acptr))
217 send_reply(sptr, RPL_WHOISIDLE, name, CurrentTime - user->last,
218 cli_firsttime(acptr));
223 * Search and return as many people as matched by the wild 'nick'.
224 * returns the number of people found (or, obviously, 0, if none where
227 static int do_wilds(struct Client* sptr,char *nick,int count)
229 struct Client *acptr; /* Current client we're concidering */
230 struct User *user; /* the user portion of the client */
231 char *name; /* the name of this client */
232 struct Membership* chan;
233 int invis; /* does +i apply? */
234 int member; /* Is this user on any channels? */
235 int showperson; /* Should we show this person? */
236 int found = 0 ; /* How many were found? */
238 /* Ech! This is hidious! */
239 for (acptr = GlobalClientList; (acptr = next_client(acptr, nick));
240 acptr = cli_next(acptr))
242 if (!IsRegistered(acptr))
248 * I'm always last :-) and acptr->next == 0!!
250 * Isomer: Does this strike anyone else as being a horrible hidious
254 assert(!cli_next(acptr));
259 * 'Rules' established for sending a WHOIS reply:
261 * - if wildcards are being used dont send a reply if
262 * the querier isnt any common channels and the
263 * client in question is invisible.
265 * - only send replies about common or public channels
266 * the target user(s) are on;
268 user = cli_user(acptr);
269 name = (!*(cli_name(acptr))) ? "?" : cli_name(acptr);
272 invis = (acptr != sptr) && IsInvisible(acptr);
273 member = (user && user->channel) ? 1 : 0;
274 showperson = !invis && !member;
276 /* Should we show this person now? */
279 do_whois(sptr,acptr);
280 if (count+found>MAX_WHOIS_LINES)
285 /* Step through the channels this user is on */
286 for (chan = user->channel; chan; chan = chan->next_channel)
288 struct Channel *chptr = chan->channel;
290 /* If this is a public channel, show the person */
291 if (!invis && PubChannel(chptr)) {
296 /* if this channel is +p and not +s, show them */
297 if (!invis && HiddenChannel(chptr) && !SecretChannel(chptr)) {
302 member = find_channel_member(sptr, chptr) ? 1 : 0;
303 if (invis && !member)
306 /* If sptr isn't really on this channel, skip it */
310 /* Is this a common channel? */
315 } /* of for (chan in channels) */
317 /* Don't show this person */
321 do_whois(sptr,acptr);
323 if (count+found>MAX_WHOIS_LINES)
325 } /* of global client list */
331 * m_whois - generic message handler
333 * parv[0] = sender prefix
334 * parv[1] = nickname masklist
338 * parv[1] = target server, or a nickname representing a server to target.
339 * parv[2] = nickname masklist
341 int m_whois(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
351 send_reply(sptr, ERR_NONICKNAMEGIVEN);
357 struct Client *acptr;
358 /* For convenience: Accept a nickname as first parameter, by replacing
359 * it with the correct servername - as is needed by hunt_server().
360 * This is the secret behind the /whois nick nick trick.
362 #if HEAD_IN_SAND_REMOTE
363 /* If remote queries are disabled, then use the *second* parameter of
364 * of whois, so /whois nick nick still works.
366 acptr = FindUser(parv[2]);
368 acptr = FindUser(parv[1]);
371 parv[1] = cli_name(cli_user(acptr)->server);
372 if (hunt_server_cmd(sptr, CMD_WHOIS, cptr, 0, "%C :%s", 1, parc, parv) !=
378 for (tmp = parv[1]; (nick = ircd_strtok(&p, tmp, ",")); tmp = 0)
386 wilds = (strchr(nick, '?') || strchr(nick, '*'));
388 struct Client *acptr = 0;
390 acptr = FindUser(nick);
391 if (acptr && !IsServer(acptr)) {
392 do_whois(sptr,acptr);
397 found=do_wilds(sptr,nick,total);
400 send_reply(sptr, ERR_NOSUCHNICK, nick);
402 if (total >= MAX_WHOIS_LINES) {
403 send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
408 } /* of tokenised parm[1] */
409 send_reply(sptr, RPL_ENDOFWHOIS, parv[1]);
415 * ms_whois - server message handler
417 * parv[0] = sender prefix
418 * parv[1] = nickname masklist
422 * parv[1] = target server, or a nickname representing a server to target.
423 * parv[2] = nickname masklist
425 int ms_whois(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
435 send_reply(sptr, ERR_NONICKNAMEGIVEN);
441 struct Client *acptr;
442 /* For convenience: Accept a nickname as first parameter, by replacing
443 * it with the correct servername - as is needed by hunt_server().
444 * This is the secret behind the /whois nick nick trick.
446 acptr = FindUser(parv[1]);
448 parv[1] = cli_name(cli_user(acptr)->server);
449 if (hunt_server_cmd(sptr, CMD_WHOIS, cptr, 0, "%C :%s", 1, parc, parv) !=
457 for (tmp = parv[1]; (nick = ircd_strtok(&p, tmp, ",")); tmp = 0)
459 struct Client *acptr = 0;
466 acptr = FindUser(nick);
467 if (acptr && !IsServer(acptr)) {
469 do_whois(sptr,acptr);
473 send_reply(sptr, ERR_NOSUCHNICK, nick);
477 if (total >= MAX_WHOIS_LINES) {
478 send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
484 } /* of tokenised parm[1] */
485 send_reply(sptr, RPL_ENDOFWHOIS, parv[1]);