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
88 #include "ircd_features.h"
89 #include "ircd_reply.h"
90 #include "ircd_string.h"
104 * * Rewritten to make this understandable
105 * * You can nolonger /whois unregistered clients.
109 * /whois nick always shows the nick.
110 * /whois wild* shows the nick if:
111 * * they aren't +i and aren't on any channels.
112 * * they are on a common channel.
113 * * they aren't +i and are on a public channel. (not +p and not +s)
114 * * they aren't +i and are on a private channel. (+p but not +s)
115 * Or to look at it another way (I think):
116 * * +i users are only shown if your on a common channel.
117 * * users on +s channels only aren't shown.
119 * whois doesn't show what channels a +k client is on, for the reason that
120 * /whois X or /whois W floods a user off the server. :)
122 * nb: if the code and this comment disagree, the codes right and I screwed
127 * Send whois information for acptr to sptr
129 static void do_whois(struct Client* sptr, struct Client *acptr, int parc)
131 struct Client *a2cptr=0;
132 struct Channel *chptr=0;
135 static char buf[512];
137 const struct User* user = cli_user(acptr);
138 const char* name = (!*(cli_name(acptr))) ? "?" : cli_name(acptr);
139 a2cptr = user->server;
141 send_reply(sptr, RPL_WHOISUSER, name, user->username, user->host,
144 /* Display the channels this user is on. */
145 if (!IsChannelService(acptr))
147 struct Membership* chan;
148 mlen = strlen(cli_name(&me)) + strlen(cli_name(sptr)) + 12 + strlen(name);
151 for (chan = user->channel; chan; chan = chan->next_channel)
153 chptr = chan->channel;
155 if (!ShowChannel(sptr, chptr))
158 if (acptr != sptr && IsZombie(chan))
161 if (len+strlen(chptr->chname) + mlen > BUFSIZE - 5)
163 send_reply(sptr, SND_EXPLICIT | RPL_WHOISCHANNELS, "%s :%s", name, buf);
168 *(buf + len++) = '-';
169 if (is_chan_op(acptr, chptr))
170 *(buf + len++) = '@';
171 else if (has_voice(acptr, chptr))
172 *(buf + len++) = '+';
173 else if (IsZombie(chan))
174 *(buf + len++) = '!';
177 strcpy(buf + len, chptr->chname);
178 len += strlen(chptr->chname);
179 strcat(buf + len, " ");
183 send_reply(sptr, RPL_WHOISCHANNELS, name, buf);
186 if (feature_bool(FEAT_HIS_WHOIS_SERVERNAME) && !IsAnOper(sptr) &&
188 send_reply(sptr, RPL_WHOISSERVER, name, feature_str(FEAT_HIS_SERVERNAME),
189 feature_str(FEAT_HIS_SERVERINFO));
191 send_reply(sptr, RPL_WHOISSERVER, name, cli_name(a2cptr),
197 send_reply(sptr, RPL_AWAY, name, user->away);
199 if (SeeOper(sptr,acptr))
200 send_reply(sptr, RPL_WHOISOPERATOR, name);
202 if (IsAccount(acptr))
203 send_reply(sptr, RPL_WHOISACCOUNT, name, user->account);
205 if (HasHiddenHost(acptr) && (IsAnOper(sptr) || acptr == sptr))
206 send_reply(sptr, RPL_WHOISACTUALLY, name, user->username,
207 user->realhost, ircd_ntoa((const char*) &(cli_ip(acptr))));
209 /* Hint: if your looking to add more flags to a user, eg +h, here's
210 * probably a good place to add them :)
213 if (MyConnect(acptr) && (!feature_bool(FEAT_HIS_WHOIS_IDLETIME) ||
214 (sptr == acptr || IsAnOper(sptr) || parc >= 3)))
215 send_reply(sptr, RPL_WHOISIDLE, name, CurrentTime - user->last,
216 cli_firsttime(acptr));
221 * Search and return as many people as matched by the wild 'nick'.
222 * returns the number of people found (or, obviously, 0, if none where
225 static int do_wilds(struct Client* sptr, char *nick, int count, int parc)
227 struct Client *acptr; /* Current client we're concidering */
228 struct User *user; /* the user portion of the client */
229 const char *name; /* the name of this client */
230 struct Membership* chan;
231 int invis; /* does +i apply? */
232 int member; /* Is this user on any channels? */
233 int showperson; /* Should we show this person? */
234 int found = 0 ; /* How many were found? */
236 /* Ech! This is hidious! */
237 for (acptr = GlobalClientList; (acptr = next_client(acptr, nick));
238 acptr = cli_next(acptr))
240 if (!IsRegistered(acptr))
246 * I'm always last :-) and acptr->next == 0!!
248 * Isomer: Does this strike anyone else as being a horrible hidious
252 assert(!cli_next(acptr));
257 * 'Rules' established for sending a WHOIS reply:
259 * - if wildcards are being used dont send a reply if
260 * the querier isnt any common channels and the
261 * client in question is invisible.
263 * - only send replies about common or public channels
264 * the target user(s) are on;
266 user = cli_user(acptr);
267 name = (!*(cli_name(acptr))) ? "?" : cli_name(acptr);
270 invis = (acptr != sptr) && IsInvisible(acptr);
271 member = (user && user->channel) ? 1 : 0;
272 showperson = !invis && !member;
274 /* Should we show this person now? */
277 do_whois(sptr, acptr, parc);
278 if (count+found>MAX_WHOIS_LINES)
283 /* Step through the channels this user is on */
284 for (chan = user->channel; chan; chan = chan->next_channel)
286 struct Channel *chptr = chan->channel;
288 /* If this is a public channel, show the person */
289 if (!invis && PubChannel(chptr)) {
294 /* if this channel is +p and not +s, show them */
295 if (!invis && HiddenChannel(chptr) && !SecretChannel(chptr)) {
300 member = find_channel_member(sptr, chptr) ? 1 : 0;
301 if (invis && !member)
304 /* If sptr isn't really on this channel, skip it */
308 /* Is this a common channel? */
313 } /* of for (chan in channels) */
315 /* Don't show this person */
319 do_whois(sptr, acptr, parc);
321 if (count+found>MAX_WHOIS_LINES)
323 } /* of global client list */
329 * m_whois - generic message handler
331 * parv[0] = sender prefix
332 * parv[1] = nickname masklist
336 * parv[1] = target server, or a nickname representing a server to target.
337 * parv[2] = nickname masklist
339 int m_whois(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
349 send_reply(sptr, ERR_NONICKNAMEGIVEN);
355 /* For convenience: Accept a nickname as first parameter, by replacing
356 * it with the correct servername - as is needed by hunt_server().
357 * This is the secret behind the /whois nick nick trick.
359 if (feature_int(FEAT_HIS_REMOTE))
361 /* If remote queries are disabled, then use the *second* parameter of
362 * of whois, so /whois nick nick still works.
366 if (!FindUser(parv[2]))
368 send_reply(sptr, ERR_NOSUCHNICK, parv[2]);
369 send_reply(sptr, RPL_ENDOFWHOIS, parv[2]);
376 if (hunt_server_cmd(sptr, CMD_WHOIS, cptr, 0, "%C :%s", 1, parc, parv) !=
383 for (tmp = parv[1]; (nick = ircd_strtok(&p, tmp, ",")); tmp = 0)
391 wilds = (strchr(nick, '?') || strchr(nick, '*'));
393 struct Client *acptr = 0;
395 acptr = FindUser(nick);
396 if (acptr && !IsServer(acptr)) {
397 do_whois(sptr, acptr, parc);
402 found=do_wilds(sptr, nick, total, parc);
405 send_reply(sptr, ERR_NOSUCHNICK, nick);
407 if (total >= MAX_WHOIS_LINES) {
408 send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
413 } /* of tokenised parm[1] */
414 send_reply(sptr, RPL_ENDOFWHOIS, parv[1]);
420 * ms_whois - server message handler
422 * parv[0] = sender prefix
423 * parv[1] = nickname masklist
427 * parv[1] = target server, or a nickname representing a server to target.
428 * parv[2] = nickname masklist
430 int ms_whois(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
440 send_reply(sptr, ERR_NONICKNAMEGIVEN);
446 if (hunt_server_cmd(sptr, CMD_WHOIS, cptr, 0, "%C :%s", 1, parc, parv) !=
454 for (tmp = parv[1]; (nick = ircd_strtok(&p, tmp, ",")); tmp = 0)
456 struct Client *acptr = 0;
463 acptr = FindUser(nick);
464 if (acptr && !IsServer(acptr)) {
466 do_whois(sptr, acptr, parc);
470 send_reply(sptr, ERR_NOSUCHNICK, nick);
474 if (total >= MAX_WHOIS_LINES) {
475 send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
481 } /* of tokenised parm[1] */
482 send_reply(sptr, RPL_ENDOFWHOIS, parv[1]);