1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/*
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
*
* Copyright (c) 1997-2018 ircd-hybrid development team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
/*! \file m_who.c
* \brief Includes required functions for processing the WHO command.
* \version $Id: m_who.c 8436 2018-03-29 09:04:53Z michael $
*/
#include "stdinc.h"
#include "list.h"
#include "client.h"
#include "channel.h"
#include "channel_mode.h"
#include "hash.h"
#include "ircd.h"
#include "numeric.h"
#include "server.h"
#include "send.h"
#include "irc_string.h"
#include "conf.h"
#include "parse.h"
#include "modules.h"
enum { WHO_MAX_REPLIES = 500 };
/* do_who()
*
* inputs - pointer to client requesting who
* - pointer to client to do who on
* - The reported name
* - channel flags
* output - NONE
* side effects - do a who on given person
*/
static void
do_who(struct Client *source_p, const struct Client *target_p,
const char *name, const char *op_flags)
{
char status[IRCD_BUFSIZE] = "";
if (HasUMode(source_p, UMODE_OPER))
snprintf(status, sizeof(status), "%c%s%s%s", target_p->away[0] ? 'G' : 'H',
HasUMode(target_p, UMODE_REGISTERED) ? "r" : "",
HasUMode(target_p, UMODE_OPER) ? "*" : "", op_flags);
else
snprintf(status, sizeof(status), "%c%s%s%s", target_p->away[0] ? 'G' : 'H',
HasUMode(target_p, UMODE_REGISTERED) ? "r" : "",
HasUMode(target_p, UMODE_OPER) &&
!HasUMode(target_p, UMODE_HIDDEN) ? "*" : "", op_flags);
if (ConfigServerHide.hide_servers || IsHidden(target_p->servptr))
sendto_one_numeric(source_p, &me, RPL_WHOREPLY,
(name) ? (name) : "*",
target_p->username, target_p->host,
HasUMode(source_p, UMODE_OPER) ? target_p->servptr->name : "*",
target_p->name, status,
HasUMode(source_p, UMODE_OPER) ? target_p->hopcount : 0, target_p->info);
else
sendto_one_numeric(source_p, &me, RPL_WHOREPLY,
(name) ? (name) : "*", target_p->username,
target_p->host, target_p->servptr->name, target_p->name,
status, target_p->hopcount, target_p->info);
}
/*!
* \param source_p Pointer to client requesting who
* \param target_p Pointer to client to do who on
* \param mask Mask to match
* \return 1 if mask matches, 0 otherwise
*/
static int
who_matches(struct Client *source_p, struct Client *target_p, const char *mask)
{
if (!mask)
return 1;
if (!match(mask, target_p->name))
return 1;
if (!match(mask, target_p->username))
return 1;
if (!match(mask, target_p->host))
return 1;
if (!match(mask, target_p->info))
return 1;
if (HasUMode(source_p, UMODE_OPER))
if (!match(mask, target_p->sockhost))
return 1;
if (HasUMode(source_p, UMODE_OPER) ||
(!ConfigServerHide.hide_servers && !IsHidden(target_p->servptr)))
if (!match(mask, target_p->servptr->name))
return 1;
return 0;
}
/* who_common_channel
* inputs - pointer to client requesting who
* - pointer to channel member chain.
* - char * mask to match
* - int if oper on a server or not
* - pointer to int maxmatches
* output - NONE
* side effects - lists matching clients on specified channel,
* marks matched clients.
*
*/
static void
who_common_channel(struct Client *source_p, struct Channel *chptr, const char *mask,
int server_oper, unsigned int *maxmatches)
{
dlink_node *node;
DLINK_FOREACH(node, chptr->members.head)
{
struct Client *target_p = ((struct Membership *)node->data)->client_p;
if (!HasUMode(target_p, UMODE_INVISIBLE) || HasFlag(target_p, FLAGS_MARK))
continue;
if (server_oper)
if (!HasUMode(target_p, UMODE_OPER) ||
(HasUMode(target_p, UMODE_HIDDEN) && !HasUMode(source_p, UMODE_OPER)))
continue;
AddFlag(target_p, FLAGS_MARK);
if (who_matches(source_p, target_p, mask))
{
do_who(source_p, target_p, NULL, "");
if (*maxmatches)
{
if (--(*maxmatches) == 0)
{
sendto_one_numeric(source_p, &me, ERR_WHOLIMEXCEED, WHO_MAX_REPLIES, "WHO");
return;
}
}
}
}
}
/* who_global()
*
* inputs - pointer to client requesting who
* - char * mask to match
* - int if oper on a server or not
* output - NONE
* side effects - do a global scan of all clients looking for match
* this is slightly expensive on EFnet ...
*/
static void
who_global(struct Client *source_p, const char *mask, int server_oper)
{
dlink_node *node;
unsigned int maxmatches = WHO_MAX_REPLIES;
static uintmax_t last_used = 0;
if (!HasUMode(source_p, UMODE_OPER))
{
if ((last_used + ConfigGeneral.pace_wait) > CurrentTime)
{
sendto_one_numeric(source_p, &me, RPL_LOAD2HI, "WHO");
return;
}
last_used = CurrentTime;
}
/* First, list all matching invisible clients on common channels */
DLINK_FOREACH(node, source_p->channel.head)
{
struct Channel *chptr = ((struct Membership *)node->data)->chptr;
who_common_channel(source_p, chptr, mask, server_oper, &maxmatches);
}
/* Second, list all matching visible clients */
DLINK_FOREACH(node, global_client_list.head)
{
struct Client *target_p = node->data;
assert(IsClient(target_p));
if (HasUMode(target_p, UMODE_INVISIBLE))
{
DelFlag(target_p, FLAGS_MARK);
continue;
}
if (server_oper)
if (!HasUMode(target_p, UMODE_OPER) ||
(HasUMode(target_p, UMODE_HIDDEN) && !HasUMode(source_p, UMODE_OPER)))
continue;
if (who_matches(source_p, target_p, mask))
{
do_who(source_p, target_p, NULL, "");
if (maxmatches)
{
if (--maxmatches == 0)
{
sendto_one_numeric(source_p, &me, ERR_WHOLIMEXCEED, WHO_MAX_REPLIES, "WHO");
return;
}
}
}
}
}
/* do_who_on_channel()
*
* inputs - pointer to client requesting who
* - pointer to channel to do who on
* - The "real name" of this channel
* - int if source_p is a server oper or not
* - int if client is member or not
* - int server_op flag
* output - NONE
* side effects - do a who on given channel
*/
static void
do_who_on_channel(struct Client *source_p, struct Channel *chptr,
int is_member, int server_oper)
{
dlink_node *node;
DLINK_FOREACH(node, chptr->members.head)
{
struct Membership *member = node->data;
struct Client *target_p = member->client_p;
if (is_member || !HasUMode(target_p, UMODE_INVISIBLE))
{
if (server_oper)
if (!HasUMode(target_p, UMODE_OPER) ||
(HasUMode(target_p, UMODE_HIDDEN) && !HasUMode(source_p, UMODE_OPER)))
continue;
do_who(source_p, target_p, chptr->name, get_member_status(member, !!HasCap(source_p, CAP_MULTI_PREFIX)));
}
}
}
/*! \brief WHO command handler
*
* \param source_p Pointer to allocated Client struct from which the message
* originally comes from. This can be a local or remote client.
* \param parc Integer holding the number of supplied arguments.
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL
* pointers.
* \note Valid arguments for this command are:
* - parv[0] = command
* - parv[1] = nickname/channelname
* - parv[2] = additional selection flag, only 'o' for now
*/
static int
m_who(struct Client *source_p, int parc, char *parv[])
{
dlink_node *node;
struct Client *target_p = NULL;
struct Channel *chptr = NULL;
char *mask = parv[1];
const int server_oper = parc > 2 && *parv[2] == 'o'; /* Show OPERS only */
/* See if mask is there, collapse it or return if not there */
if (EmptyString(mask))
{
who_global(source_p, NULL, server_oper);
sendto_one_numeric(source_p, &me, RPL_ENDOFWHO, "*");
return 0;
}
/* Mask isn't NULL at this point. repeat after me... -db */
collapse(mask);
/* '/who #some_channel' */
if (IsChanPrefix(*mask))
{
/* List all users on a given channel */
if ((chptr = hash_find_channel(mask)))
{
if (HasUMode(source_p, UMODE_ADMIN) || IsMember(source_p, chptr))
do_who_on_channel(source_p, chptr, 1, server_oper);
else if (!SecretChannel(chptr))
do_who_on_channel(source_p, chptr, 0, server_oper);
}
sendto_one_numeric(source_p, &me, RPL_ENDOFWHO, mask);
return 0;
}
/* '/who nick' */
if ((target_p = find_person(source_p, mask)) &&
(!server_oper || HasUMode(target_p, UMODE_OPER)))
{
DLINK_FOREACH(node, target_p->channel.head)
{
chptr = ((struct Membership *)node->data)->chptr;
if (PubChannel(chptr) || IsMember(source_p, chptr))
break;
}
if (node)
do_who(source_p, target_p, chptr->name,
get_member_status(node->data, !!HasCap(source_p, CAP_MULTI_PREFIX)));
else
do_who(source_p, target_p, NULL, "");
sendto_one_numeric(source_p, &me, RPL_ENDOFWHO, mask);
return 0;
}
/* '/who *' */
if (!strcmp(mask, "*"))
{
if ((node = source_p->channel.head))
{
chptr = ((struct Membership *)node->data)->chptr;
do_who_on_channel(source_p, chptr, 1, server_oper);
}
sendto_one_numeric(source_p, &me, RPL_ENDOFWHO, "*");
return 0;
}
/* '/who 0' */
if (!strcmp(mask, "0"))
who_global(source_p, NULL, server_oper);
else
who_global(source_p, mask, server_oper);
/* Wasn't a nick, wasn't a channel, wasn't a '*' so ... */
sendto_one_numeric(source_p, &me, RPL_ENDOFWHO, mask);
return 0;
}
static struct Message who_msgtab =
{
.cmd = "WHO",
.args_max = MAXPARA,
.handlers[UNREGISTERED_HANDLER] = m_unregistered,
.handlers[CLIENT_HANDLER] = m_who,
.handlers[SERVER_HANDLER] = m_ignore,
.handlers[ENCAP_HANDLER] = m_ignore,
.handlers[OPER_HANDLER] = m_who
};
static void
module_init(void)
{
mod_add_cmd(&who_msgtab);
}
static void
module_exit(void)
{
mod_del_cmd(&who_msgtab);
}
struct module module_entry =
{
.version = "$Revision: 8436 $",
.modinit = module_init,
.modexit = module_exit,
};
|