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
39 #include "s_newconf.h"
40 #include "ratelimit.h"
41 #include "supported.h"
43 #define FIELD_CHANNEL 0x0001
44 #define FIELD_HOP 0x0002
45 #define FIELD_FLAGS 0x0004
46 #define FIELD_HOST 0x0008
47 #define FIELD_IP 0x0010
48 #define FIELD_IDLE 0x0020
49 #define FIELD_NICK 0x0040
50 #define FIELD_INFO 0x0080
51 #define FIELD_SERVER 0x0100
52 #define FIELD_QUERYTYPE 0x0200 /* cookie for client */
53 #define FIELD_USER 0x0400
54 #define FIELD_ACCOUNT 0x0800
55 #define FIELD_OPLEVEL 0x1000 /* meaningless and stupid, but whatever */
57 static const char who_desc
[] =
58 "Provides the WHO command to display information for users on a channel";
63 const char *querytype
;
66 static void m_who(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
68 static void do_who_on_channel(struct Client
*source_p
, struct Channel
*chptr
,
69 int server_oper
, int member
,
70 struct who_format
*fmt
);
71 static void who_global(struct Client
*source_p
, const char *mask
, int server_oper
, int operspy
, struct who_format
*fmt
);
72 static void do_who(struct Client
*source_p
,
73 struct Client
*target_p
, struct membership
*msptr
,
74 struct who_format
*fmt
);
76 struct Message who_msgtab
= {
78 {mg_unreg
, {m_who
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {m_who
, 2}}
84 add_isupport("WHOX", isupport_string
, "");
91 delete_isupport("WHOX");
94 mapi_clist_av1 who_clist
[] = { &who_msgtab
, NULL
};
95 DECLARE_MODULE_AV2(who
, _modinit
, _moddeinit
, who_clist
, NULL
, NULL
, NULL
, NULL
, who_desc
);
99 ** parv[1] = nickname mask list
100 ** parv[2] = additional selection flag and format options
103 m_who(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
105 static time_t last_used
= 0;
106 struct Client
*target_p
;
107 struct membership
*msptr
;
110 struct Channel
*chptr
= NULL
;
111 int server_oper
= parc
> 2 ? (*parv
[2] == 'o') : 0; /* Show OPERS only */
114 struct who_format fmt
;
119 fmt
.querytype
= NULL
;
120 if (parc
> 2 && (s
= strchr(parv
[2], '%')) != NULL
)
123 for (; *s
!= '\0'; s
++)
127 case 'c': fmt
.fields
|= FIELD_CHANNEL
; break;
128 case 'd': fmt
.fields
|= FIELD_HOP
; break;
129 case 'f': fmt
.fields
|= FIELD_FLAGS
; break;
130 case 'h': fmt
.fields
|= FIELD_HOST
; break;
131 case 'i': fmt
.fields
|= FIELD_IP
; break;
132 case 'l': fmt
.fields
|= FIELD_IDLE
; break;
133 case 'n': fmt
.fields
|= FIELD_NICK
; break;
134 case 'r': fmt
.fields
|= FIELD_INFO
; break;
135 case 's': fmt
.fields
|= FIELD_SERVER
; break;
136 case 't': fmt
.fields
|= FIELD_QUERYTYPE
; break;
137 case 'u': fmt
.fields
|= FIELD_USER
; break;
138 case 'a': fmt
.fields
|= FIELD_ACCOUNT
; break;
139 case 'o': fmt
.fields
|= FIELD_OPLEVEL
; break;
148 if (EmptyString(fmt
.querytype
) || strlen(fmt
.querytype
) > 3)
152 rb_strlcpy(maskcopy
, parv
[1], sizeof maskcopy
);
158 if((*(mask
+ 1) == '\0') && (*mask
== '*'))
160 if(source_p
->user
== NULL
)
163 if((lp
= source_p
->user
->channel
.head
) != NULL
)
166 do_who_on_channel(source_p
, msptr
->chptr
, server_oper
, true, &fmt
);
169 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
170 me
.name
, source_p
->name
, "*");
174 if(IsOperSpy(source_p
) && *mask
== '!')
179 if(EmptyString(mask
))
181 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
182 me
.name
, source_p
->name
, parv
[1]);
187 /* '/who #some_channel' */
188 if(IsChannelName(mask
))
190 /* List all users on a given channel */
191 chptr
= find_channel(parv
[1] + operspy
);
195 if (!IsOper(source_p
) && !ratelimit_client_who(source_p
, rb_dlink_list_length(&chptr
->members
)/50))
197 sendto_one(source_p
, form_str(RPL_LOAD2HI
),
198 me
.name
, source_p
->name
, "WHO");
199 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
200 me
.name
, source_p
->name
, "*");
205 report_operspy(source_p
, "WHO", chptr
->chname
);
207 if(IsMember(source_p
, chptr
) || operspy
)
208 do_who_on_channel(source_p
, chptr
, server_oper
, true, &fmt
);
209 else if(!SecretChannel(chptr
))
210 do_who_on_channel(source_p
, chptr
, server_oper
, false, &fmt
);
213 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
214 me
.name
, source_p
->name
, parv
[1] + operspy
);
220 if(((target_p
= find_named_person(mask
)) != NULL
) &&
221 (!server_oper
|| IsOper(target_p
)))
225 isinvis
= IsInvisible(target_p
);
226 RB_DLINK_FOREACH(lp
, target_p
->user
->channel
.head
)
229 chptr
= msptr
->chptr
;
231 member
= IsMember(source_p
, chptr
);
233 if(isinvis
&& !member
)
236 if(member
|| (!isinvis
&& PubChannel(chptr
)))
240 /* if we stopped midlist, lp->data is the membership for
244 do_who(source_p
, target_p
, lp
->data
, &fmt
);
246 do_who(source_p
, target_p
, NULL
, &fmt
);
248 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
249 me
.name
, source_p
->name
, mask
);
253 if(!IsFloodDone(source_p
))
254 flood_endgrace(source_p
);
256 /* it has to be a global who at this point, limit it */
257 if(!IsOper(source_p
))
259 if((last_used
+ ConfigFileEntry
.pace_wait
) > rb_current_time() || !ratelimit_client(source_p
, 1))
261 sendto_one(source_p
, form_str(RPL_LOAD2HI
),
262 me
.name
, source_p
->name
, "WHO");
263 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
264 me
.name
, source_p
->name
, "*");
268 last_used
= rb_current_time();
271 /* Note: operspy_dont_care_user_info does not apply to
273 if(IsOperSpy(source_p
) && ConfigFileEntry
.operspy_dont_care_user_info
)
276 /* '/who 0' for a global list. this forces clients to actually
277 * request a full list. I presume its because of too many typos
278 * with "/who" ;) --fl
280 if((*(mask
+ 1) == '\0') && (*mask
== '0'))
281 who_global(source_p
, NULL
, server_oper
, 0, &fmt
);
283 who_global(source_p
, mask
, server_oper
, operspy
, &fmt
);
285 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
286 me
.name
, source_p
->name
, mask
);
289 /* who_common_channel
290 * inputs - pointer to client requesting who
291 * - pointer to channel member chain.
292 * - char * mask to match
293 * - int if oper on a server or not
294 * - pointer to int maxmatches
297 * side effects - lists matching invisible clients on specified channel,
298 * marks matched clients.
301 who_common_channel(struct Client
*source_p
, struct Channel
*chptr
,
302 const char *mask
, int server_oper
, int *maxmatches
,
303 struct who_format
*fmt
)
305 struct membership
*msptr
;
306 struct Client
*target_p
;
309 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
312 target_p
= msptr
->client_p
;
314 if(!IsInvisible(target_p
) || IsMarked(target_p
))
317 if(server_oper
&& !IsOper(target_p
))
325 match(mask
, target_p
->name
) || match(mask
, target_p
->username
) ||
326 match(mask
, target_p
->host
) || match(mask
, target_p
->servptr
->name
) ||
327 (IsOper(source_p
) && match(mask
, target_p
->orighost
)) ||
328 match(mask
, target_p
->info
))
330 do_who(source_p
, target_p
, NULL
, fmt
);
340 * inputs - pointer to client requesting who
341 * - char * mask to match
342 * - int if oper on a server or not
343 * - int if operspy or not
346 * side effects - do a global scan of all clients looking for match
347 * this is slightly expensive on EFnet ...
348 * marks assumed cleared for all clients initially
349 * and will be left cleared on return
352 who_global(struct Client
*source_p
, const char *mask
, int server_oper
, int operspy
, struct who_format
*fmt
)
354 struct membership
*msptr
;
355 struct Client
*target_p
;
356 rb_dlink_node
*lp
, *ptr
;
357 int maxmatches
= 500;
359 /* first, list all matching INvisible clients on common channels
360 * if this is not an operspy who
364 RB_DLINK_FOREACH(lp
, source_p
->user
->channel
.head
)
367 who_common_channel(source_p
, msptr
->chptr
, mask
, server_oper
, &maxmatches
, fmt
);
370 else if (!ConfigFileEntry
.operspy_dont_care_user_info
)
371 report_operspy(source_p
, "WHO", mask
);
373 /* second, list all matching visible clients and clear all marks
374 * on invisible clients
375 * if this is an operspy who, list all matching clients, no need
378 RB_DLINK_FOREACH(ptr
, global_client_list
.head
)
380 target_p
= ptr
->data
;
381 if(!IsPerson(target_p
))
384 if(IsInvisible(target_p
) && !operspy
)
390 if(server_oper
&& !IsOper(target_p
))
396 match(mask
, target_p
->name
) || match(mask
, target_p
->username
) ||
397 match(mask
, target_p
->host
) || match(mask
, target_p
->servptr
->name
) ||
398 (IsOper(source_p
) && match(mask
, target_p
->orighost
)) ||
399 match(mask
, target_p
->info
))
401 do_who(source_p
, target_p
, NULL
, fmt
);
409 form_str(ERR_TOOMANYMATCHES
),
410 me
.name
, source_p
->name
, "WHO");
416 * inputs - pointer to client requesting who
417 * - pointer to channel to do who on
418 * - The "real name" of this channel
419 * - int if source_p is a server oper or not
420 * - int if client is member or not
423 * side effects - do a who on given channel
426 do_who_on_channel(struct Client
*source_p
, struct Channel
*chptr
,
427 int server_oper
, int member
, struct who_format
*fmt
)
429 struct Client
*target_p
;
430 struct membership
*msptr
;
433 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
436 target_p
= msptr
->client_p
;
438 if(server_oper
&& !IsOper(target_p
))
441 if(member
|| !IsInvisible(target_p
))
442 do_who(source_p
, target_p
, msptr
, fmt
);
449 * inputs - pointer to buffer
451 * - pointer to position
453 * - arguments for format
455 * side effects - position incremented, possibly beyond size of buffer
456 * this allows detecting overflow
459 append_format(char *buf
, size_t bufsize
, size_t *pos
, const char *fmt
, ...)
464 max
= *pos
>= bufsize
? 0 : bufsize
- *pos
;
466 result
= vsnprintf(buf
+ *pos
, max
, fmt
, ap
);
474 * inputs - pointer to client requesting who
475 * - pointer to client to do who on
476 * - channel membership or NULL
479 * side effects - do a who on given person
483 do_who(struct Client
*source_p
, struct Client
*target_p
, struct membership
*msptr
, struct who_format
*fmt
)
486 char str
[510 + 1]; /* linebuf.c will add \r\n */
490 sprintf(status
, "%c%s%s",
491 target_p
->user
->away
? 'G' : 'H', IsOper(target_p
) ? "*" : "", msptr
? find_channel_status(msptr
, fmt
->fields
|| IsCapable(source_p
, CLICAP_MULTI_PREFIX
)) : "");
493 if (fmt
->fields
== 0)
494 sendto_one(source_p
, form_str(RPL_WHOREPLY
), me
.name
,
495 source_p
->name
, msptr
? msptr
->chptr
->chname
: "*",
496 target_p
->username
, target_p
->host
,
497 target_p
->servptr
->name
, target_p
->name
, status
,
498 ConfigServerHide
.flatten_links
&& !IsOper(source_p
) && !IsExemptShide(source_p
) ? 0 : target_p
->hopcount
,
504 append_format(str
, sizeof str
, &pos
, ":%s %d %s",
505 me
.name
, RPL_WHOSPCRPL
, source_p
->name
);
506 if (fmt
->fields
& FIELD_QUERYTYPE
)
507 append_format(str
, sizeof str
, &pos
, " %s", fmt
->querytype
);
508 if (fmt
->fields
& FIELD_CHANNEL
)
509 append_format(str
, sizeof str
, &pos
, " %s", msptr
? msptr
->chptr
->chname
: "*");
510 if (fmt
->fields
& FIELD_USER
)
511 append_format(str
, sizeof str
, &pos
, " %s", target_p
->username
);
512 if (fmt
->fields
& FIELD_IP
)
514 if (show_ip(source_p
, target_p
) && !EmptyString(target_p
->sockhost
) && strcmp(target_p
->sockhost
, "0"))
515 append_format(str
, sizeof str
, &pos
, " %s", target_p
->sockhost
);
517 append_format(str
, sizeof str
, &pos
, " %s", "255.255.255.255");
519 if (fmt
->fields
& FIELD_HOST
)
520 append_format(str
, sizeof str
, &pos
, " %s", target_p
->host
);
521 if (fmt
->fields
& FIELD_SERVER
)
522 append_format(str
, sizeof str
, &pos
, " %s", target_p
->servptr
->name
);
523 if (fmt
->fields
& FIELD_NICK
)
524 append_format(str
, sizeof str
, &pos
, " %s", target_p
->name
);
525 if (fmt
->fields
& FIELD_FLAGS
)
526 append_format(str
, sizeof str
, &pos
, " %s", status
);
527 if (fmt
->fields
& FIELD_HOP
)
528 append_format(str
, sizeof str
, &pos
, " %d", ConfigServerHide
.flatten_links
&& !IsOper(source_p
) && !IsExemptShide(source_p
) ? 0 : target_p
->hopcount
);
529 if (fmt
->fields
& FIELD_IDLE
)
530 append_format(str
, sizeof str
, &pos
, " %d", (int)(MyClient(target_p
) ? rb_current_time() - target_p
->localClient
->last
: 0));
531 if (fmt
->fields
& FIELD_ACCOUNT
)
533 /* display as in whois */
534 q
= target_p
->user
->suser
;
540 q
= target_p
->user
->suser
;
544 append_format(str
, sizeof str
, &pos
, " %s", q
);
546 if (fmt
->fields
& FIELD_OPLEVEL
)
547 append_format(str
, sizeof str
, &pos
, " %s", is_chanop(msptr
) ? "999" : "n/a");
548 if (fmt
->fields
& FIELD_INFO
)
549 append_format(str
, sizeof str
, &pos
, " :%s", target_p
->info
);
551 if (pos
>= sizeof str
)
553 static bool warned
= false;
555 sendto_realops_snomask(SNO_DEBUG
, L_NETWIDE
,
556 "WHOX overflow while sending information about %s to %s",
557 target_p
->name
, source_p
->name
);
560 sendto_one(source_p
, "%s", str
);