]> jfr.im git - irc/quakenet/snircd.git/blame - ircd/m_whois.c
Update my e-mail address.
[irc/quakenet/snircd.git] / ircd / m_whois.c
CommitLineData
189935b1 1/*
2 * IRC - Internet Relay Chat, ircd/m_whois.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 *
9f8856e9 23 * $Id: m_whois.c,v 1.37.2.1 2006/06/09 02:16:17 entrope Exp $
189935b1 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 "channel.h"
85#include "client.h"
86#include "hash.h"
87#include "ircd.h"
88#include "ircd_features.h"
89#include "ircd_log.h"
90#include "ircd_reply.h"
91#include "ircd_string.h"
92#include "match.h"
93#include "msg.h"
94#include "numeric.h"
95#include "numnicks.h"
96#include "s_user.h"
97#include "send.h"
98#include "whocmds.h"
99
100/* #include <assert.h> -- Now using assert in ircd_log.h */
101#include <string.h>
102
103/*
104 * 2000-07-01: Isomer
105 * * Rewritten to make this understandable
106 * * You can no longer /whois unregistered clients.
107 *
108 *
109 * General rules:
110 * /whois nick always shows the nick.
111 * /whois wild* shows the nick if:
112 * * they aren't +i and aren't on any channels.
113 * * they are on a common channel.
114 * * they aren't +i and are on a public channel. (not +p and not +s)
115 * * they aren't +i and are on a private channel. (+p but not +s)
116 * Or to look at it another way (I think):
117 * * +i users are only shown if your on a common channel.
118 * * users on +s channels only aren't shown.
119 *
120 * whois doesn't show what channels a +k client is on, for the reason that
121 * /whois X or /whois W floods a user off the server. :)
122 *
123 * nb: if the code and this comment disagree, the codes right and I screwed
124 * up.
125 */
126
127/*
128 * Send whois information for acptr to sptr
129 */
130static void do_whois(struct Client* sptr, struct Client *acptr, int parc)
131{
132 struct Client *a2cptr=0;
133 struct Channel *chptr=0;
134 int mlen;
135 int len;
136 static char buf[512];
137
138 const struct User* user = cli_user(acptr);
139 const char* name = (!*(cli_name(acptr))) ? "?" : cli_name(acptr);
140 a2cptr = feature_bool(FEAT_HIS_WHOIS_SERVERNAME) && !IsAnOper(sptr)
141 && sptr != acptr ? &his : user->server;
142 assert(user);
143 send_reply(sptr, RPL_WHOISUSER, name, user->username, user->host,
144 cli_info(acptr));
145
146 /* Display the channels this user is on. */
d8e74551 147 if ((!IsChannelService(acptr) && !IsNoChan(acptr)) || (acptr==sptr))
189935b1 148 {
149 struct Membership* chan;
150 mlen = strlen(cli_name(&me)) + strlen(cli_name(sptr)) + 12 + strlen(name);
151 len = 0;
152 *buf = '\0';
153 for (chan = user->channel; chan; chan = chan->next_channel)
154 {
155 chptr = chan->channel;
156
157 if (!ShowChannel(sptr, chptr)
158 && !(IsOper(sptr) && IsLocalChannel(chptr->chname)))
159 continue;
160
161 if (acptr != sptr && IsZombie(chan))
162 continue;
163
164 /* Don't show local channels when HIS is defined, unless it's a
165 * remote WHOIS --ULtimaTe_
166 */
167 if (IsLocalChannel(chptr->chname) && (acptr != sptr) && (parc == 2)
168 && feature_bool(FEAT_HIS_WHOIS_LOCALCHAN) && !IsAnOper(sptr))
169 continue;
170
171 if (len+strlen(chptr->chname) + mlen > BUFSIZE - 5)
172 {
173 send_reply(sptr, SND_EXPLICIT | RPL_WHOISCHANNELS, "%s :%s", name, buf);
174 *buf = '\0';
175 len = 0;
176 }
177 if (IsDeaf(acptr))
178 *(buf + len++) = '-';
9f8856e9 179 if (!ShowChannel(sptr, chptr))
189935b1 180 *(buf + len++) = '*';
181 if (IsDelayedJoin(chan) && (sptr != acptr))
182 *(buf + len++) = '<';
183 else if (IsChanOp(chan))
184 *(buf + len++) = '@';
185 else if (HasVoice(chan))
186 *(buf + len++) = '+';
187 else if (IsZombie(chan))
188 *(buf + len++) = '!';
189 if (len)
190 *(buf + len) = '\0';
191 strcpy(buf + len, chptr->chname);
192 len += strlen(chptr->chname);
193 strcat(buf + len, " ");
194 len++;
195 }
196 if (buf[0] != '\0')
197 send_reply(sptr, RPL_WHOISCHANNELS, name, buf);
198 }
199
200 send_reply(sptr, RPL_WHOISSERVER, name, cli_name(a2cptr),
201 cli_info(a2cptr));
202
203 if (user)
204 {
205 if (user->away)
206 send_reply(sptr, RPL_AWAY, name, user->away);
207
49f8e96a 208 if (SeeOper(sptr,acptr)) {
189935b1 209 send_reply(sptr, RPL_WHOISOPERATOR, name);
49f8e96a 210 if (IsAnOper(sptr) && user->opername)
211 send_reply(sptr, RPL_WHOISOPERNAME, name, user->opername);
212 }
189935b1 213
214 if (IsAccount(acptr))
215 send_reply(sptr, RPL_WHOISACCOUNT, name, user->account);
216
d8e74551 217 if ((HasHiddenHost(acptr) || HasSetHost(acptr)) && (IsAnOper(sptr) || acptr == sptr))
218 send_reply(sptr, RPL_WHOISACTUALLY, name, user->realusername,
189935b1 219 user->realhost, ircd_ntoa(&cli_ip(acptr)));
220
d8e74551 221 if (!IsAnOper(sptr) && IsParanoid(acptr) && IsAnOper(acptr))
222 sendcmdto_one(&me, CMD_NOTICE, acptr, "%C :whois: %s performed a /WHOIS on you.", acptr, cli_name(sptr));
223
189935b1 224 /* Hint: if your looking to add more flags to a user, eg +h, here's
225 * probably a good place to add them :)
226 */
227
d8e74551 228 if (MyConnect(acptr) &&
229 (IsAnOper(sptr) ||
230 (!IsNoIdle(acptr) && (!feature_bool(FEAT_HIS_WHOIS_IDLETIME) ||
231 sptr == acptr || parc >= 3))))
189935b1 232 send_reply(sptr, RPL_WHOISIDLE, name, CurrentTime - user->last,
233 cli_firsttime(acptr));
234 }
235}
236
237/*
238 * Search and return as many people as matched by the wild 'nick'.
239 * returns the number of people found (or, obviously, 0, if none where
240 * found).
241 */
242static int do_wilds(struct Client* sptr, char *nick, int count, int parc)
243{
244 struct Client *acptr; /* Current client we're considering */
245 struct User *user; /* the user portion of the client */
246 const char *name; /* the name of this client */
247 struct Membership* chan;
248 int invis; /* does +i apply? */
249 int member; /* Is this user on any channels? */
250 int showperson; /* Should we show this person? */
251 int found = 0 ; /* How many were found? */
252
253 /* Ech! This is hideous! */
254 for (acptr = GlobalClientList; (acptr = next_client(acptr, nick));
255 acptr = cli_next(acptr))
256 {
257 if (!IsRegistered(acptr))
258 continue;
259
260 if (IsServer(acptr))
261 continue;
262 /*
263 * I'm always last :-) and acptr->next == 0!!
264 *
265 * Isomer: Does this strike anyone else as being a horrible hideous
266 * hack?
267 */
268 if (IsMe(acptr)) {
269 assert(!cli_next(acptr));
270 break;
271 }
272
273 /*
274 * 'Rules' established for sending a WHOIS reply:
275 *
276 * - if wildcards are being used don't send a reply if
277 * the querier isn't any common channels and the
278 * client in question is invisible.
279 *
280 * - only send replies about common or public channels
281 * the target user(s) are on;
282 */
283 user = cli_user(acptr);
284 name = (!*(cli_name(acptr))) ? "?" : cli_name(acptr);
285 assert(user);
286
287 invis = (acptr != sptr) && IsInvisible(acptr);
288 member = (user && user->channel) ? 1 : 0;
289 showperson = !invis && !member;
290
291 /* Should we show this person now? */
292 if (showperson) {
293 found++;
294 do_whois(sptr, acptr, parc);
295 if (count+found>MAX_WHOIS_LINES)
296 return found;
297 continue;
298 }
299
300 /* Step through the channels this user is on */
301 for (chan = user->channel; chan; chan = chan->next_channel)
302 {
303 struct Channel *chptr = chan->channel;
304
305 /* If this is a public channel, show the person */
306 if (!invis && PubChannel(chptr)) {
307 showperson = 1;
308 break;
309 }
310
311 /* if this channel is +p and not +s, show them */
312 if (!invis && HiddenChannel(chptr) && !SecretChannel(chptr)) {
313 showperson = 1;
314 break;
315 }
316
317 member = find_channel_member(sptr, chptr) ? 1 : 0;
318 if (invis && !member)
319 continue;
320
321 /* If sptr isn't really on this channel, skip it */
322 if (IsZombie(chan))
323 continue;
324
325 /* Is this a common channel? */
326 if (member) {
327 showperson = 1;
328 break;
329 }
330 } /* of for (chan in channels) */
331
332 /* Don't show this person */
333 if (!showperson)
334 continue;
335
336 do_whois(sptr, acptr, parc);
337 found++;
338 if (count+found>MAX_WHOIS_LINES)
339 return found;
340 } /* of global client list */
341
342 return found;
343}
344
345/*
346 * m_whois - generic message handler
347 *
348 * parv[0] = sender prefix
349 * parv[1] = nickname masklist
350 *
351 * or
352 *
353 * parv[1] = target server, or a nickname representing a server to target.
354 * parv[2] = nickname masklist
355 */
356int m_whois(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
357{
358 char* nick;
359 char* tmp;
360 char* p = 0;
361 int found = 0;
362 int total = 0;
363 int wildscount = 0;
364
365 if (parc < 2)
366 {
367 send_reply(sptr, ERR_NONICKNAMEGIVEN);
368 return 0;
369 }
370
371 if (parc > 2)
372 {
373 /* For convenience: Accept a nickname as first parameter, by replacing
374 * it with the correct servername - as is needed by hunt_server().
375 * This is the secret behind the /whois nick nick trick.
376 */
377 if (feature_int(FEAT_HIS_REMOTE))
378 {
379 /* If remote queries are disabled, then use the *second* parameter of
380 * of whois, so /whois nick nick still works.
381 */
382 if (!IsAnOper(sptr))
383 {
384 if (!FindUser(parv[2]))
385 {
386 send_reply(sptr, ERR_NOSUCHNICK, parv[2]);
387 send_reply(sptr, RPL_ENDOFWHOIS, parv[2]);
388 return 0;
389 }
390 parv[1] = parv[2];
391 }
392 }
393
394 if (hunt_server_cmd(sptr, CMD_WHOIS, cptr, 0, "%C :%s", 1, parc, parv) !=
395 HUNTED_ISME)
396 return 0;
397
398 parv[1] = parv[2];
399 }
400
401 for (tmp = parv[1]; (nick = ircd_strtok(&p, tmp, ",")); tmp = 0)
402 {
403 int wilds;
404
405 found = 0;
406
407 collapse(nick);
408
409 wilds = (strchr(nick, '?') || strchr(nick, '*'));
410 if (!wilds) {
411 struct Client *acptr = 0;
412 /* No wildcards */
413 acptr = FindUser(nick);
414 if (acptr && !IsServer(acptr)) {
415 do_whois(sptr, acptr, parc);
416 found = 1;
417 }
418 }
419 else /* wilds */
420 {
421 if (++wildscount > 3) {
422 send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
423 break;
424 }
425 found=do_wilds(sptr, nick, total, parc);
426 }
427
428 if (!found)
429 send_reply(sptr, ERR_NOSUCHNICK, nick);
430 total+=found;
431 if (total >= MAX_WHOIS_LINES) {
432 send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
433 break;
434 }
435 if (p)
436 p[-1] = ',';
437 } /* of tokenised parm[1] */
438 send_reply(sptr, RPL_ENDOFWHOIS, parv[1]);
439
440 return 0;
441}
442
443/*
444 * ms_whois - server message handler
445 *
446 * parv[0] = sender prefix
447 * parv[1] = nickname masklist
448 *
449 * or
450 *
451 * parv[1] = target server, or a nickname representing a server to target.
452 * parv[2] = nickname masklist
453 */
454int ms_whois(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
455{
456 char* nick;
457 char* tmp;
458 char* p = 0;
459 int found = 0;
460 int total = 0;
461
462 if (parc < 2)
463 {
464 send_reply(sptr, ERR_NONICKNAMEGIVEN);
465 return 0;
466 }
467
468 if (parc > 2)
469 {
470 if (hunt_server_cmd(sptr, CMD_WHOIS, cptr, 0, "%C :%s", 1, parc, parv) !=
471 HUNTED_ISME)
472 return 0;
473 parv[1] = parv[2];
474 }
475
476 total = 0;
477
478 for (tmp = parv[1]; (nick = ircd_strtok(&p, tmp, ",")); tmp = 0)
479 {
480 struct Client *acptr = 0;
481
482 found = 0;
483
484 collapse(nick);
485
486
487 acptr = FindUser(nick);
488 if (acptr && !IsServer(acptr)) {
489 found++;
490 do_whois(sptr, acptr, parc);
491 }
492
493 if (!found)
494 send_reply(sptr, ERR_NOSUCHNICK, nick);
495
496 total+=found;
497
498 if (total >= MAX_WHOIS_LINES) {
499 send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
500 break;
501 }
502
503 if (p)
504 p[-1] = ',';
505 } /* of tokenised parm[1] */
506 send_reply(sptr, RPL_ENDOFWHOIS, parv[1]);
507
508 return 0;
509}
510