2 * ircd-ratbox: A slightly useful ircd.
3 * m_who.c: Shows who is on a channel.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
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 2 of the License, or
12 * (at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
40 #include "s_newconf.h"
41 #include "ratelimit.h"
42 #include "supported.h"
44 #define FIELD_CHANNEL 0x0001
45 #define FIELD_HOP 0x0002
46 #define FIELD_FLAGS 0x0004
47 #define FIELD_HOST 0x0008
48 #define FIELD_IP 0x0010
49 #define FIELD_IDLE 0x0020
50 #define FIELD_NICK 0x0040
51 #define FIELD_INFO 0x0080
52 #define FIELD_SERVER 0x0100
53 #define FIELD_QUERYTYPE 0x0200 /* cookie for client */
54 #define FIELD_USER 0x0400
55 #define FIELD_ACCOUNT 0x0800
56 #define FIELD_OPLEVEL 0x1000 /* meaningless and stupid, but whatever */
58 static const char who_desc
[] =
59 "Provides the WHO command to display information for users on a channel";
64 const char *querytype
;
67 static void m_who(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
69 static void do_who_on_channel(struct Client
*source_p
, struct Channel
*chptr
,
70 int server_oper
, int member
,
71 struct who_format
*fmt
);
72 static void who_global(struct Client
*source_p
, const char *mask
, int server_oper
, int operspy
, struct who_format
*fmt
);
73 static void do_who(struct Client
*source_p
,
74 struct Client
*target_p
, struct membership
*msptr
,
75 struct who_format
*fmt
);
77 struct Message who_msgtab
= {
79 {mg_unreg
, {m_who
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {m_who
, 2}}
85 add_isupport("WHOX", isupport_string
, "");
92 delete_isupport("WHOX");
95 mapi_clist_av1 who_clist
[] = { &who_msgtab
, NULL
};
96 DECLARE_MODULE_AV2(who
, _modinit
, _moddeinit
, who_clist
, NULL
, NULL
, NULL
, NULL
, who_desc
);
100 ** parv[1] = nickname mask list
101 ** parv[2] = additional selection flag and format options
104 m_who(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
106 static time_t last_used
= 0;
107 struct Client
*target_p
;
108 struct membership
*msptr
;
111 struct Channel
*chptr
= NULL
;
112 int server_oper
= parc
> 2 ? (*parv
[2] == 'o') : 0; /* Show OPERS only */
115 struct who_format fmt
;
120 fmt
.querytype
= NULL
;
121 if (parc
> 2 && (s
= strchr(parv
[2], '%')) != NULL
)
124 for (; *s
!= '\0'; s
++)
128 case 'c': fmt
.fields
|= FIELD_CHANNEL
; break;
129 case 'd': fmt
.fields
|= FIELD_HOP
; break;
130 case 'f': fmt
.fields
|= FIELD_FLAGS
; break;
131 case 'h': fmt
.fields
|= FIELD_HOST
; break;
132 case 'i': fmt
.fields
|= FIELD_IP
; break;
133 case 'l': fmt
.fields
|= FIELD_IDLE
; break;
134 case 'n': fmt
.fields
|= FIELD_NICK
; break;
135 case 'r': fmt
.fields
|= FIELD_INFO
; break;
136 case 's': fmt
.fields
|= FIELD_SERVER
; break;
137 case 't': fmt
.fields
|= FIELD_QUERYTYPE
; break;
138 case 'u': fmt
.fields
|= FIELD_USER
; break;
139 case 'a': fmt
.fields
|= FIELD_ACCOUNT
; break;
140 case 'o': fmt
.fields
|= FIELD_OPLEVEL
; break;
149 if (EmptyString(fmt
.querytype
) || strlen(fmt
.querytype
) > 3)
153 rb_strlcpy(maskcopy
, parv
[1], sizeof maskcopy
);
159 if((*(mask
+ 1) == '\0') && (*mask
== '*'))
161 if(source_p
->user
== NULL
)
164 if((lp
= source_p
->user
->channel
.head
) != NULL
)
167 do_who_on_channel(source_p
, msptr
->chptr
, server_oper
, true, &fmt
);
170 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
171 me
.name
, source_p
->name
, "*");
175 if(IsOperSpy(source_p
) && *mask
== '!')
180 if(EmptyString(mask
))
182 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
183 me
.name
, source_p
->name
, parv
[1]);
188 /* '/who #some_channel' */
189 if(IsChannelName(mask
))
191 /* List all users on a given channel */
192 chptr
= find_channel(parv
[1] + operspy
);
196 if (!IsOper(source_p
) && !ratelimit_client_who(source_p
, rb_dlink_list_length(&chptr
->members
)/50))
198 sendto_one(source_p
, form_str(RPL_LOAD2HI
),
199 me
.name
, source_p
->name
, "WHO");
200 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
201 me
.name
, source_p
->name
, "*");
206 report_operspy(source_p
, "WHO", chptr
->chname
);
208 if(IsMember(source_p
, chptr
) || operspy
)
209 do_who_on_channel(source_p
, chptr
, server_oper
, true, &fmt
);
210 else if(!SecretChannel(chptr
))
211 do_who_on_channel(source_p
, chptr
, server_oper
, false, &fmt
);
214 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
215 me
.name
, source_p
->name
, parv
[1] + operspy
);
221 if(((target_p
= find_named_person(mask
)) != NULL
) &&
222 (!server_oper
|| IsOper(target_p
)))
226 isinvis
= IsInvisible(target_p
);
227 RB_DLINK_FOREACH(lp
, target_p
->user
->channel
.head
)
230 chptr
= msptr
->chptr
;
232 member
= IsMember(source_p
, chptr
);
234 if(isinvis
&& !member
)
237 if(member
|| (!isinvis
&& PubChannel(chptr
)))
241 /* if we stopped midlist, lp->data is the membership for
245 do_who(source_p
, target_p
, lp
->data
, &fmt
);
247 do_who(source_p
, target_p
, NULL
, &fmt
);
249 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
250 me
.name
, source_p
->name
, mask
);
254 if(!IsFloodDone(source_p
))
255 flood_endgrace(source_p
);
257 /* it has to be a global who at this point, limit it */
258 if(!IsOper(source_p
))
260 if((last_used
+ ConfigFileEntry
.pace_wait
) > rb_current_time() || !ratelimit_client(source_p
, 1))
262 sendto_one(source_p
, form_str(RPL_LOAD2HI
),
263 me
.name
, source_p
->name
, "WHO");
264 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
265 me
.name
, source_p
->name
, "*");
269 last_used
= rb_current_time();
272 /* Note: operspy_dont_care_user_info does not apply to
274 if(IsOperSpy(source_p
) && ConfigFileEntry
.operspy_dont_care_user_info
)
277 /* '/who 0' for a global list. this forces clients to actually
278 * request a full list. I presume its because of too many typos
279 * with "/who" ;) --fl
281 if((*(mask
+ 1) == '\0') && (*mask
== '0'))
282 who_global(source_p
, NULL
, server_oper
, 0, &fmt
);
284 who_global(source_p
, mask
, server_oper
, operspy
, &fmt
);
286 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
287 me
.name
, source_p
->name
, mask
);
290 /* who_common_channel
291 * inputs - pointer to client requesting who
292 * - pointer to channel member chain.
293 * - char * mask to match
294 * - int if oper on a server or not
295 * - pointer to int maxmatches
298 * side effects - lists matching invisible clients on specified channel,
299 * marks matched clients.
302 who_common_channel(struct Client
*source_p
, struct Channel
*chptr
,
303 const char *mask
, int server_oper
, int *maxmatches
,
304 struct who_format
*fmt
)
306 struct membership
*msptr
;
307 struct Client
*target_p
;
310 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
313 target_p
= msptr
->client_p
;
315 if(!IsInvisible(target_p
) || IsMarked(target_p
))
318 if(server_oper
&& !IsOper(target_p
))
326 match(mask
, target_p
->name
) || match(mask
, target_p
->username
) ||
327 match(mask
, target_p
->host
) || match(mask
, target_p
->servptr
->name
) ||
328 (IsOper(source_p
) && match(mask
, target_p
->orighost
)) ||
329 match(mask
, target_p
->info
))
331 do_who(source_p
, target_p
, NULL
, fmt
);
341 * inputs - pointer to client requesting who
342 * - char * mask to match
343 * - int if oper on a server or not
344 * - int if operspy or not
347 * side effects - do a global scan of all clients looking for match
348 * this is slightly expensive on EFnet ...
349 * marks assumed cleared for all clients initially
350 * and will be left cleared on return
353 who_global(struct Client
*source_p
, const char *mask
, int server_oper
, int operspy
, struct who_format
*fmt
)
355 struct membership
*msptr
;
356 struct Client
*target_p
;
357 rb_dlink_node
*lp
, *ptr
;
358 int maxmatches
= 500;
360 /* first, list all matching INvisible clients on common channels
361 * if this is not an operspy who
365 RB_DLINK_FOREACH(lp
, source_p
->user
->channel
.head
)
368 who_common_channel(source_p
, msptr
->chptr
, mask
, server_oper
, &maxmatches
, fmt
);
371 else if (!ConfigFileEntry
.operspy_dont_care_user_info
)
372 report_operspy(source_p
, "WHO", mask
);
374 /* second, list all matching visible clients and clear all marks
375 * on invisible clients
376 * if this is an operspy who, list all matching clients, no need
379 RB_DLINK_FOREACH(ptr
, global_client_list
.head
)
381 target_p
= ptr
->data
;
382 if(!IsPerson(target_p
))
385 if(IsInvisible(target_p
) && !operspy
)
391 if(server_oper
&& !IsOper(target_p
))
397 match(mask
, target_p
->name
) || match(mask
, target_p
->username
) ||
398 match(mask
, target_p
->host
) || match(mask
, target_p
->servptr
->name
) ||
399 (IsOper(source_p
) && match(mask
, target_p
->orighost
)) ||
400 match(mask
, target_p
->info
))
402 do_who(source_p
, target_p
, NULL
, fmt
);
410 form_str(ERR_TOOMANYMATCHES
),
411 me
.name
, source_p
->name
, "WHO");
417 * inputs - pointer to client requesting who
418 * - pointer to channel to do who on
419 * - The "real name" of this channel
420 * - int if source_p is a server oper or not
421 * - int if client is member or not
424 * side effects - do a who on given channel
427 do_who_on_channel(struct Client
*source_p
, struct Channel
*chptr
,
428 int server_oper
, int member
, struct who_format
*fmt
)
430 struct Client
*target_p
;
431 struct membership
*msptr
;
434 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
437 target_p
= msptr
->client_p
;
439 if(server_oper
&& !IsOper(target_p
))
442 if(member
|| !IsInvisible(target_p
))
443 do_who(source_p
, target_p
, msptr
, fmt
);
450 * inputs - pointer to buffer
452 * - pointer to position
454 * - arguments for format
456 * side effects - position incremented, possibly beyond size of buffer
457 * this allows detecting overflow
460 append_format(char *buf
, size_t bufsize
, size_t *pos
, const char *fmt
, ...)
465 max
= *pos
>= bufsize
? 0 : bufsize
- *pos
;
467 result
= vsnprintf(buf
+ *pos
, max
, fmt
, ap
);
475 * inputs - pointer to client requesting who
476 * - pointer to client to do who on
477 * - channel membership or NULL
480 * side effects - do a who on given person
484 do_who(struct Client
*source_p
, struct Client
*target_p
, struct membership
*msptr
, struct who_format
*fmt
)
487 char str
[510 + 1]; /* linebuf.c will add \r\n */
491 sprintf(status
, "%c%s%s",
492 target_p
->user
->away
? 'G' : 'H', IsOper(target_p
) ? "*" : "", msptr
? find_channel_status(msptr
, fmt
->fields
|| IsCapable(source_p
, CLICAP_MULTI_PREFIX
)) : "");
494 if (fmt
->fields
== 0)
495 sendto_one(source_p
, form_str(RPL_WHOREPLY
), me
.name
,
496 source_p
->name
, msptr
? msptr
->chptr
->chname
: "*",
497 target_p
->username
, target_p
->host
,
498 target_p
->servptr
->name
, target_p
->name
, status
,
499 ConfigServerHide
.flatten_links
&& !IsOper(source_p
) && !IsExemptShide(source_p
) ? 0 : target_p
->hopcount
,
505 append_format(str
, sizeof str
, &pos
, ":%s %d %s",
506 me
.name
, RPL_WHOSPCRPL
, source_p
->name
);
507 if (fmt
->fields
& FIELD_QUERYTYPE
)
508 append_format(str
, sizeof str
, &pos
, " %s", fmt
->querytype
);
509 if (fmt
->fields
& FIELD_CHANNEL
)
510 append_format(str
, sizeof str
, &pos
, " %s", msptr
? msptr
->chptr
->chname
: "*");
511 if (fmt
->fields
& FIELD_USER
)
512 append_format(str
, sizeof str
, &pos
, " %s", target_p
->username
);
513 if (fmt
->fields
& FIELD_IP
)
515 if (show_ip(source_p
, target_p
) && !EmptyString(target_p
->sockhost
) && strcmp(target_p
->sockhost
, "0"))
516 append_format(str
, sizeof str
, &pos
, " %s", target_p
->sockhost
);
518 append_format(str
, sizeof str
, &pos
, " %s", "255.255.255.255");
520 if (fmt
->fields
& FIELD_HOST
)
521 append_format(str
, sizeof str
, &pos
, " %s", target_p
->host
);
522 if (fmt
->fields
& FIELD_SERVER
)
523 append_format(str
, sizeof str
, &pos
, " %s", target_p
->servptr
->name
);
524 if (fmt
->fields
& FIELD_NICK
)
525 append_format(str
, sizeof str
, &pos
, " %s", target_p
->name
);
526 if (fmt
->fields
& FIELD_FLAGS
)
527 append_format(str
, sizeof str
, &pos
, " %s", status
);
528 if (fmt
->fields
& FIELD_HOP
)
529 append_format(str
, sizeof str
, &pos
, " %d", ConfigServerHide
.flatten_links
&& !IsOper(source_p
) && !IsExemptShide(source_p
) ? 0 : target_p
->hopcount
);
530 if (fmt
->fields
& FIELD_IDLE
)
531 append_format(str
, sizeof str
, &pos
, " %d", (int)(MyClient(target_p
) ? rb_current_time() - target_p
->localClient
->last
: 0));
532 if (fmt
->fields
& FIELD_ACCOUNT
)
534 /* display as in whois */
535 q
= target_p
->user
->suser
;
541 q
= target_p
->user
->suser
;
545 append_format(str
, sizeof str
, &pos
, " %s", q
);
547 if (fmt
->fields
& FIELD_OPLEVEL
)
548 append_format(str
, sizeof str
, &pos
, " %s", is_chanop(msptr
) ? "999" : "n/a");
549 if (fmt
->fields
& FIELD_INFO
)
550 append_format(str
, sizeof str
, &pos
, " :%s", target_p
->info
);
552 if (pos
>= sizeof str
)
554 static bool warned
= false;
556 sendto_realops_snomask(SNO_DEBUG
, L_NETWIDE
,
557 "WHOX overflow while sending information about %s to %s",
558 target_p
->name
, source_p
->name
);
561 sendto_one(source_p
, "%s", str
);