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
24 * $Id: m_who.c 3350 2007-04-02 22:03:08Z jilles $
42 #include "s_newconf.h"
43 #include "ratelimit.h"
44 #include "supported.h"
46 #define FIELD_CHANNEL 0x0001
47 #define FIELD_HOP 0x0002
48 #define FIELD_FLAGS 0x0004
49 #define FIELD_HOST 0x0008
50 #define FIELD_IP 0x0010
51 #define FIELD_IDLE 0x0020
52 #define FIELD_NICK 0x0040
53 #define FIELD_INFO 0x0080
54 #define FIELD_SERVER 0x0100
55 #define FIELD_QUERYTYPE 0x0200 /* cookie for client */
56 #define FIELD_USER 0x0400
57 #define FIELD_ACCOUNT 0x0800
58 #define FIELD_OPLEVEL 0x1000 /* meaningless and stupid, but whatever */
63 const char *querytype
;
66 static int m_who(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
68 struct Message who_msgtab
= {
69 "WHO", 0, 0, 0, MFLG_SLOW
,
70 {mg_unreg
, {m_who
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {m_who
, 2}}
76 add_isupport("WHOX", isupport_string
, "");
83 delete_isupport("WHOX");
86 mapi_clist_av1 who_clist
[] = { &who_msgtab
, NULL
};
87 DECLARE_MODULE_AV1(who
, _modinit
, _moddeinit
, who_clist
, NULL
, NULL
, "$Revision: 3350 $");
89 static void do_who_on_channel(struct Client
*source_p
, struct Channel
*chptr
,
90 int server_oper
, int member
,
91 struct who_format
*fmt
);
93 static void who_global(struct Client
*source_p
, const char *mask
, int server_oper
, int operspy
, struct who_format
*fmt
);
95 static void do_who(struct Client
*source_p
,
96 struct Client
*target_p
, struct membership
*msptr
,
97 struct who_format
*fmt
);
102 ** parv[1] = nickname mask list
103 ** parv[2] = additional selection flag and format options
106 m_who(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
108 static time_t last_used
= 0;
109 struct Client
*target_p
;
110 struct membership
*msptr
;
113 struct Channel
*chptr
= NULL
;
114 int server_oper
= parc
> 2 ? (*parv
[2] == 'o') : 0; /* Show OPERS only */
117 struct who_format fmt
;
122 fmt
.querytype
= NULL
;
123 if (parc
> 2 && (s
= strchr(parv
[2], '%')) != NULL
)
126 for (; *s
!= '\0'; s
++)
130 case 'c': fmt
.fields
|= FIELD_CHANNEL
; break;
131 case 'd': fmt
.fields
|= FIELD_HOP
; break;
132 case 'f': fmt
.fields
|= FIELD_FLAGS
; break;
133 case 'h': fmt
.fields
|= FIELD_HOST
; break;
134 case 'i': fmt
.fields
|= FIELD_IP
; break;
135 case 'l': fmt
.fields
|= FIELD_IDLE
; break;
136 case 'n': fmt
.fields
|= FIELD_NICK
; break;
137 case 'r': fmt
.fields
|= FIELD_INFO
; break;
138 case 's': fmt
.fields
|= FIELD_SERVER
; break;
139 case 't': fmt
.fields
|= FIELD_QUERYTYPE
; break;
140 case 'u': fmt
.fields
|= FIELD_USER
; break;
141 case 'a': fmt
.fields
|= FIELD_ACCOUNT
; break;
142 case 'o': fmt
.fields
|= FIELD_OPLEVEL
; break;
151 if (EmptyString(fmt
.querytype
) || strlen(fmt
.querytype
) > 3)
155 rb_strlcpy(maskcopy
, parv
[1], sizeof maskcopy
);
161 if((*(mask
+ 1) == '\0') && (*mask
== '*'))
163 if(source_p
->user
== NULL
)
166 if((lp
= source_p
->user
->channel
.head
) != NULL
)
169 do_who_on_channel(source_p
, msptr
->chptr
, server_oper
, YES
, &fmt
);
172 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
173 me
.name
, source_p
->name
, "*");
177 if(IsOperSpy(source_p
) && *mask
== '!')
182 if(EmptyString(mask
))
184 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
185 me
.name
, source_p
->name
, parv
[1]);
190 /* '/who #some_channel' */
191 if(IsChannelName(mask
))
193 /* List all users on a given channel */
194 chptr
= find_channel(parv
[1] + operspy
);
198 if (!IsOper(source_p
) && !ratelimit_client_who(source_p
, rb_dlink_list_length(&chptr
->members
)/50))
200 sendto_one(source_p
, form_str(RPL_LOAD2HI
),
201 me
.name
, source_p
->name
, "WHO");
202 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
203 me
.name
, source_p
->name
, "*");
208 report_operspy(source_p
, "WHO", chptr
->chname
);
210 if(IsMember(source_p
, chptr
) || operspy
)
211 do_who_on_channel(source_p
, chptr
, server_oper
, YES
, &fmt
);
212 else if(!SecretChannel(chptr
))
213 do_who_on_channel(source_p
, chptr
, server_oper
, NO
, &fmt
);
216 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
217 me
.name
, source_p
->name
, parv
[1] + operspy
);
223 if(((target_p
= find_named_person(mask
)) != NULL
) &&
224 (!server_oper
|| IsOper(target_p
)))
228 isinvis
= IsInvisible(target_p
);
229 RB_DLINK_FOREACH(lp
, target_p
->user
->channel
.head
)
232 chptr
= msptr
->chptr
;
234 member
= IsMember(source_p
, chptr
);
236 if(isinvis
&& !member
)
239 if(member
|| (!isinvis
&& PubChannel(chptr
)))
243 /* if we stopped midlist, lp->data is the membership for
247 do_who(source_p
, target_p
, lp
->data
, &fmt
);
249 do_who(source_p
, target_p
, NULL
, &fmt
);
251 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
252 me
.name
, source_p
->name
, mask
);
256 if(!IsFloodDone(source_p
))
257 flood_endgrace(source_p
);
259 /* it has to be a global who at this point, limit it */
260 if(!IsOper(source_p
))
262 if((last_used
+ ConfigFileEntry
.pace_wait
) > rb_current_time() || !ratelimit_client(source_p
, 1))
264 sendto_one(source_p
, form_str(RPL_LOAD2HI
),
265 me
.name
, source_p
->name
, "WHO");
266 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
267 me
.name
, source_p
->name
, "*");
271 last_used
= rb_current_time();
274 /* Note: operspy_dont_care_user_info does not apply to
276 if(IsOperSpy(source_p
) && ConfigFileEntry
.operspy_dont_care_user_info
)
279 /* '/who 0' for a global list. this forces clients to actually
280 * request a full list. I presume its because of too many typos
281 * with "/who" ;) --fl
283 if((*(mask
+ 1) == '\0') && (*mask
== '0'))
284 who_global(source_p
, NULL
, server_oper
, 0, &fmt
);
286 who_global(source_p
, mask
, server_oper
, operspy
, &fmt
);
288 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
289 me
.name
, source_p
->name
, mask
);
294 /* who_common_channel
295 * inputs - pointer to client requesting who
296 * - pointer to channel member chain.
297 * - char * mask to match
298 * - int if oper on a server or not
299 * - pointer to int maxmatches
302 * side effects - lists matching invisible clients on specified channel,
303 * marks matched clients.
306 who_common_channel(struct Client
*source_p
, struct Channel
*chptr
,
307 const char *mask
, int server_oper
, int *maxmatches
,
308 struct who_format
*fmt
)
310 struct membership
*msptr
;
311 struct Client
*target_p
;
314 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
317 target_p
= msptr
->client_p
;
319 if(!IsInvisible(target_p
) || IsMarked(target_p
))
322 if(server_oper
&& !IsOper(target_p
))
330 match(mask
, target_p
->name
) || match(mask
, target_p
->username
) ||
331 match(mask
, target_p
->host
) || match(mask
, target_p
->servptr
->name
) ||
332 (IsOper(source_p
) && match(mask
, target_p
->orighost
)) ||
333 match(mask
, target_p
->info
))
335 do_who(source_p
, target_p
, NULL
, fmt
);
345 * inputs - pointer to client requesting who
346 * - char * mask to match
347 * - int if oper on a server or not
348 * - int if operspy or not
351 * side effects - do a global scan of all clients looking for match
352 * this is slightly expensive on EFnet ...
353 * marks assumed cleared for all clients initially
354 * and will be left cleared on return
357 who_global(struct Client
*source_p
, const char *mask
, int server_oper
, int operspy
, struct who_format
*fmt
)
359 struct membership
*msptr
;
360 struct Client
*target_p
;
361 rb_dlink_node
*lp
, *ptr
;
362 int maxmatches
= 500;
364 /* first, list all matching INvisible clients on common channels
365 * if this is not an operspy who
369 RB_DLINK_FOREACH(lp
, source_p
->user
->channel
.head
)
372 who_common_channel(source_p
, msptr
->chptr
, mask
, server_oper
, &maxmatches
, fmt
);
375 else if (!ConfigFileEntry
.operspy_dont_care_user_info
)
376 report_operspy(source_p
, "WHO", mask
);
378 /* second, list all matching visible clients and clear all marks
379 * on invisible clients
380 * if this is an operspy who, list all matching clients, no need
383 RB_DLINK_FOREACH(ptr
, global_client_list
.head
)
385 target_p
= ptr
->data
;
386 if(!IsPerson(target_p
))
389 if(IsInvisible(target_p
) && !operspy
)
395 if(server_oper
&& !IsOper(target_p
))
401 match(mask
, target_p
->name
) || match(mask
, target_p
->username
) ||
402 match(mask
, target_p
->host
) || match(mask
, target_p
->servptr
->name
) ||
403 (IsOper(source_p
) && match(mask
, target_p
->orighost
)) ||
404 match(mask
, target_p
->info
))
406 do_who(source_p
, target_p
, NULL
, fmt
);
414 form_str(ERR_TOOMANYMATCHES
),
415 me
.name
, source_p
->name
, "WHO");
421 * inputs - pointer to client requesting who
422 * - pointer to channel to do who on
423 * - The "real name" of this channel
424 * - int if source_p is a server oper or not
425 * - int if client is member or not
428 * side effects - do a who on given channel
431 do_who_on_channel(struct Client
*source_p
, struct Channel
*chptr
,
432 int server_oper
, int member
, struct who_format
*fmt
)
434 struct Client
*target_p
;
435 struct membership
*msptr
;
438 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
441 target_p
= msptr
->client_p
;
443 if(server_oper
&& !IsOper(target_p
))
446 if(member
|| !IsInvisible(target_p
))
447 do_who(source_p
, target_p
, msptr
, fmt
);
454 * inputs - pointer to buffer
456 * - pointer to position
458 * - arguments for format
460 * side effects - position incremented, possibly beyond size of buffer
461 * this allows detecting overflow
464 append_format(char *buf
, size_t bufsize
, size_t *pos
, const char *fmt
, ...)
469 max
= *pos
>= bufsize
? 0 : bufsize
- *pos
;
471 result
= vsnprintf(buf
+ *pos
, max
, fmt
, ap
);
479 * inputs - pointer to client requesting who
480 * - pointer to client to do who on
481 * - channel membership or NULL
484 * side effects - do a who on given person
488 do_who(struct Client
*source_p
, struct Client
*target_p
, struct membership
*msptr
, struct who_format
*fmt
)
491 char str
[510 + 1]; /* linebuf.c will add \r\n */
495 sprintf(status
, "%c%s%s",
496 target_p
->user
->away
? 'G' : 'H', IsOper(target_p
) ? "*" : "", msptr
? find_channel_status(msptr
, fmt
->fields
|| IsCapable(source_p
, CLICAP_MULTI_PREFIX
)) : "");
498 if (fmt
->fields
== 0)
499 sendto_one(source_p
, form_str(RPL_WHOREPLY
), me
.name
,
500 source_p
->name
, msptr
? msptr
->chptr
->chname
: "*",
501 target_p
->username
, target_p
->host
,
502 target_p
->servptr
->name
, target_p
->name
, status
,
503 ConfigServerHide
.flatten_links
&& !IsOper(source_p
) && !IsExemptShide(source_p
) ? 0 : target_p
->hopcount
,
509 append_format(str
, sizeof str
, &pos
, ":%s %d %s",
510 me
.name
, RPL_WHOSPCRPL
, source_p
->name
);
511 if (fmt
->fields
& FIELD_QUERYTYPE
)
512 append_format(str
, sizeof str
, &pos
, " %s", fmt
->querytype
);
513 if (fmt
->fields
& FIELD_CHANNEL
)
514 append_format(str
, sizeof str
, &pos
, " %s", msptr
? msptr
->chptr
->chname
: "*");
515 if (fmt
->fields
& FIELD_USER
)
516 append_format(str
, sizeof str
, &pos
, " %s", target_p
->username
);
517 if (fmt
->fields
& FIELD_IP
)
519 if (show_ip(source_p
, target_p
) && !EmptyString(target_p
->sockhost
) && strcmp(target_p
->sockhost
, "0"))
520 append_format(str
, sizeof str
, &pos
, " %s", target_p
->sockhost
);
522 append_format(str
, sizeof str
, &pos
, " %s", "255.255.255.255");
524 if (fmt
->fields
& FIELD_HOST
)
525 append_format(str
, sizeof str
, &pos
, " %s", target_p
->host
);
526 if (fmt
->fields
& FIELD_SERVER
)
527 append_format(str
, sizeof str
, &pos
, " %s", target_p
->servptr
->name
);
528 if (fmt
->fields
& FIELD_NICK
)
529 append_format(str
, sizeof str
, &pos
, " %s", target_p
->name
);
530 if (fmt
->fields
& FIELD_FLAGS
)
531 append_format(str
, sizeof str
, &pos
, " %s", status
);
532 if (fmt
->fields
& FIELD_HOP
)
533 append_format(str
, sizeof str
, &pos
, " %d", ConfigServerHide
.flatten_links
&& !IsOper(source_p
) && !IsExemptShide(source_p
) ? 0 : target_p
->hopcount
);
534 if (fmt
->fields
& FIELD_IDLE
)
535 append_format(str
, sizeof str
, &pos
, " %d", (int)(MyClient(target_p
) ? rb_current_time() - target_p
->localClient
->last
: 0));
536 if (fmt
->fields
& FIELD_ACCOUNT
)
538 /* display as in whois */
539 q
= target_p
->user
->suser
;
545 q
= target_p
->user
->suser
;
549 append_format(str
, sizeof str
, &pos
, " %s", q
);
551 if (fmt
->fields
& FIELD_OPLEVEL
)
552 append_format(str
, sizeof str
, &pos
, " %s", is_chanop(msptr
) ? "999" : "n/a");
553 if (fmt
->fields
& FIELD_INFO
)
554 append_format(str
, sizeof str
, &pos
, " :%s", target_p
->info
);
556 if (pos
>= sizeof str
)
558 static int warned
= 0;
560 sendto_realops_snomask(SNO_DEBUG
, L_NETWIDE
,
561 "WHOX overflow while sending information about %s to %s",
562 target_p
->name
, source_p
->name
);
565 sendto_one(source_p
, "%s", str
);