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"
45 #define FIELD_CHANNEL 0x0001
46 #define FIELD_HOP 0x0002
47 #define FIELD_FLAGS 0x0004
48 #define FIELD_HOST 0x0008
49 #define FIELD_IP 0x0010
50 #define FIELD_IDLE 0x0020
51 #define FIELD_NICK 0x0040
52 #define FIELD_INFO 0x0080
53 #define FIELD_SERVER 0x0100
54 #define FIELD_QUERYTYPE 0x0200 /* cookie for client */
55 #define FIELD_USER 0x0400
56 #define FIELD_ACCOUNT 0x0800
57 #define FIELD_OPLEVEL 0x1000 /* meaningless and stupid, but whatever */
62 const char *querytype
;
65 static int m_who(struct Client
*, struct Client
*, int, const char **);
67 struct Message who_msgtab
= {
68 "WHO", 0, 0, 0, MFLG_SLOW
,
69 {mg_unreg
, {m_who
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {m_who
, 2}}
72 mapi_clist_av1 who_clist
[] = { &who_msgtab
, NULL
};
73 DECLARE_MODULE_AV1(who
, NULL
, NULL
, who_clist
, NULL
, NULL
, "$Revision: 3350 $");
75 static void do_who_on_channel(struct Client
*source_p
, struct Channel
*chptr
,
76 int server_oper
, int member
,
77 struct who_format
*fmt
);
79 static void who_global(struct Client
*source_p
, const char *mask
, int server_oper
, int operspy
, struct who_format
*fmt
);
81 static void do_who(struct Client
*source_p
,
82 struct Client
*target_p
, struct membership
*msptr
,
83 struct who_format
*fmt
);
88 ** parv[1] = nickname mask list
89 ** parv[2] = additional selection flag and format options
92 m_who(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
94 static time_t last_used
= 0;
95 struct Client
*target_p
;
96 struct membership
*msptr
;
99 struct Channel
*chptr
= NULL
;
100 int server_oper
= parc
> 2 ? (*parv
[2] == 'o') : 0; /* Show OPERS only */
103 struct who_format fmt
;
108 fmt
.querytype
= NULL
;
109 if (parc
> 2 && (s
= strchr(parv
[2], '%')) != NULL
)
112 for (; *s
!= '\0'; s
++)
116 case 'c': fmt
.fields
|= FIELD_CHANNEL
; break;
117 case 'd': fmt
.fields
|= FIELD_HOP
; break;
118 case 'f': fmt
.fields
|= FIELD_FLAGS
; break;
119 case 'h': fmt
.fields
|= FIELD_HOST
; break;
120 case 'i': fmt
.fields
|= FIELD_IP
; break;
121 case 'l': fmt
.fields
|= FIELD_IDLE
; break;
122 case 'n': fmt
.fields
|= FIELD_NICK
; break;
123 case 'r': fmt
.fields
|= FIELD_INFO
; break;
124 case 's': fmt
.fields
|= FIELD_SERVER
; break;
125 case 't': fmt
.fields
|= FIELD_QUERYTYPE
; break;
126 case 'u': fmt
.fields
|= FIELD_USER
; break;
127 case 'a': fmt
.fields
|= FIELD_ACCOUNT
; break;
128 case 'o': fmt
.fields
|= FIELD_OPLEVEL
; break;
137 if (EmptyString(fmt
.querytype
) || strlen(fmt
.querytype
) > 3)
141 rb_strlcpy(maskcopy
, parv
[1], sizeof maskcopy
);
147 if((*(mask
+ 1) == '\0') && (*mask
== '*'))
149 if(source_p
->user
== NULL
)
152 if((lp
= source_p
->user
->channel
.head
) != NULL
)
155 do_who_on_channel(source_p
, msptr
->chptr
, server_oper
, YES
, &fmt
);
158 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
159 me
.name
, source_p
->name
, "*");
163 if(IsOperSpy(source_p
) && *mask
== '!')
168 if(EmptyString(mask
))
170 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
171 me
.name
, source_p
->name
, parv
[1]);
176 /* '/who #some_channel' */
177 if(IsChannelName(mask
))
179 /* List all users on a given channel */
180 chptr
= find_channel(parv
[1] + operspy
);
184 if (!IsOper(source_p
) && !ratelimit_client_who(source_p
, rb_dlink_list_length(&chptr
->members
)/50))
186 sendto_one(source_p
, form_str(RPL_LOAD2HI
),
187 me
.name
, source_p
->name
, "WHO");
188 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
189 me
.name
, source_p
->name
, "*");
194 report_operspy(source_p
, "WHO", chptr
->chname
);
196 if(IsMember(source_p
, chptr
) || operspy
)
197 do_who_on_channel(source_p
, chptr
, server_oper
, YES
, &fmt
);
198 else if(!SecretChannel(chptr
))
199 do_who_on_channel(source_p
, chptr
, server_oper
, NO
, &fmt
);
202 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
203 me
.name
, source_p
->name
, parv
[1] + operspy
);
209 if(((target_p
= find_named_person(mask
)) != NULL
) &&
210 (!server_oper
|| IsOper(target_p
)))
214 isinvis
= IsInvisible(target_p
);
215 RB_DLINK_FOREACH(lp
, target_p
->user
->channel
.head
)
218 chptr
= msptr
->chptr
;
220 member
= IsMember(source_p
, chptr
);
222 if(isinvis
&& !member
)
225 if(member
|| (!isinvis
&& PubChannel(chptr
)))
229 /* if we stopped midlist, lp->data is the membership for
233 do_who(source_p
, target_p
, lp
->data
, &fmt
);
235 do_who(source_p
, target_p
, NULL
, &fmt
);
237 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
238 me
.name
, source_p
->name
, mask
);
242 if(!IsFloodDone(source_p
))
243 flood_endgrace(source_p
);
245 /* it has to be a global who at this point, limit it */
246 if(!IsOper(source_p
))
248 if((last_used
+ ConfigFileEntry
.pace_wait
) > rb_current_time() || !ratelimit_client(source_p
, 1))
250 sendto_one(source_p
, form_str(RPL_LOAD2HI
),
251 me
.name
, source_p
->name
, "WHO");
252 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
253 me
.name
, source_p
->name
, "*");
257 last_used
= rb_current_time();
260 /* Note: operspy_dont_care_user_info does not apply to
262 if(IsOperSpy(source_p
) && ConfigFileEntry
.operspy_dont_care_user_info
)
265 /* '/who 0' for a global list. this forces clients to actually
266 * request a full list. I presume its because of too many typos
267 * with "/who" ;) --fl
269 if((*(mask
+ 1) == '\0') && (*mask
== '0'))
270 who_global(source_p
, NULL
, server_oper
, 0, &fmt
);
272 who_global(source_p
, mask
, server_oper
, operspy
, &fmt
);
274 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
275 me
.name
, source_p
->name
, mask
);
280 /* who_common_channel
281 * inputs - pointer to client requesting who
282 * - pointer to channel member chain.
283 * - char * mask to match
284 * - int if oper on a server or not
285 * - pointer to int maxmatches
288 * side effects - lists matching invisible clients on specified channel,
289 * marks matched clients.
292 who_common_channel(struct Client
*source_p
, struct Channel
*chptr
,
293 const char *mask
, int server_oper
, int *maxmatches
,
294 struct who_format
*fmt
)
296 struct membership
*msptr
;
297 struct Client
*target_p
;
300 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
303 target_p
= msptr
->client_p
;
305 if(!IsInvisible(target_p
) || IsMarked(target_p
))
308 if(server_oper
&& !IsOper(target_p
))
316 match(mask
, target_p
->name
) || match(mask
, target_p
->username
) ||
317 match(mask
, target_p
->host
) || match(mask
, target_p
->servptr
->name
) ||
318 (IsOper(source_p
) && match(mask
, target_p
->orighost
)) ||
319 match(mask
, target_p
->info
))
321 do_who(source_p
, target_p
, NULL
, fmt
);
331 * inputs - pointer to client requesting who
332 * - char * mask to match
333 * - int if oper on a server or not
334 * - int if operspy or not
337 * side effects - do a global scan of all clients looking for match
338 * this is slightly expensive on EFnet ...
339 * marks assumed cleared for all clients initially
340 * and will be left cleared on return
343 who_global(struct Client
*source_p
, const char *mask
, int server_oper
, int operspy
, struct who_format
*fmt
)
345 struct membership
*msptr
;
346 struct Client
*target_p
;
347 rb_dlink_node
*lp
, *ptr
;
348 int maxmatches
= 500;
350 /* first, list all matching INvisible clients on common channels
351 * if this is not an operspy who
355 RB_DLINK_FOREACH(lp
, source_p
->user
->channel
.head
)
358 who_common_channel(source_p
, msptr
->chptr
, mask
, server_oper
, &maxmatches
, fmt
);
361 else if (!ConfigFileEntry
.operspy_dont_care_user_info
)
362 report_operspy(source_p
, "WHO", mask
);
364 /* second, list all matching visible clients and clear all marks
365 * on invisible clients
366 * if this is an operspy who, list all matching clients, no need
369 RB_DLINK_FOREACH(ptr
, global_client_list
.head
)
371 target_p
= ptr
->data
;
372 if(!IsPerson(target_p
))
375 if(IsInvisible(target_p
) && !operspy
)
381 if(server_oper
&& !IsOper(target_p
))
387 match(mask
, target_p
->name
) || match(mask
, target_p
->username
) ||
388 match(mask
, target_p
->host
) || match(mask
, target_p
->servptr
->name
) ||
389 (IsOper(source_p
) && match(mask
, target_p
->orighost
)) ||
390 match(mask
, target_p
->info
))
392 do_who(source_p
, target_p
, NULL
, fmt
);
400 form_str(ERR_TOOMANYMATCHES
),
401 me
.name
, source_p
->name
, "WHO");
407 * inputs - pointer to client requesting who
408 * - pointer to channel to do who on
409 * - The "real name" of this channel
410 * - int if source_p is a server oper or not
411 * - int if client is member or not
414 * side effects - do a who on given channel
417 do_who_on_channel(struct Client
*source_p
, struct Channel
*chptr
,
418 int server_oper
, int member
, struct who_format
*fmt
)
420 struct Client
*target_p
;
421 struct membership
*msptr
;
424 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
427 target_p
= msptr
->client_p
;
429 if(server_oper
&& !IsOper(target_p
))
432 if(member
|| !IsInvisible(target_p
))
433 do_who(source_p
, target_p
, msptr
, fmt
);
440 * inputs - pointer to buffer
442 * - pointer to position
444 * - arguments for format
446 * side effects - position incremented, possibly beyond size of buffer
447 * this allows detecting overflow
450 append_format(char *buf
, size_t bufsize
, size_t *pos
, const char *fmt
, ...)
455 max
= *pos
>= bufsize
? 0 : bufsize
- *pos
;
457 result
= rb_vsnprintf(buf
+ *pos
, max
, fmt
, ap
);
465 * inputs - pointer to client requesting who
466 * - pointer to client to do who on
467 * - channel membership or NULL
470 * side effects - do a who on given person
474 do_who(struct Client
*source_p
, struct Client
*target_p
, struct membership
*msptr
, struct who_format
*fmt
)
477 char str
[510 + 1]; /* linebuf.c will add \r\n */
481 rb_sprintf(status
, "%c%s%s",
482 target_p
->user
->away
? 'G' : 'H', IsOper(target_p
) ? "*" : "", msptr
? find_channel_status(msptr
, fmt
->fields
|| IsCapable(source_p
, CLICAP_MULTI_PREFIX
)) : "");
484 if (fmt
->fields
== 0)
485 sendto_one(source_p
, form_str(RPL_WHOREPLY
), me
.name
,
486 source_p
->name
, msptr
? msptr
->chptr
->chname
: "*",
487 target_p
->username
, target_p
->host
,
488 target_p
->servptr
->name
, target_p
->name
, status
,
489 ConfigServerHide
.flatten_links
&& !IsOper(source_p
) && !IsExemptShide(source_p
) ? 0 : target_p
->hopcount
,
495 append_format(str
, sizeof str
, &pos
, ":%s %d %s",
496 me
.name
, RPL_WHOSPCRPL
, source_p
->name
);
497 if (fmt
->fields
& FIELD_QUERYTYPE
)
498 append_format(str
, sizeof str
, &pos
, " %s", fmt
->querytype
);
499 if (fmt
->fields
& FIELD_CHANNEL
)
500 append_format(str
, sizeof str
, &pos
, " %s", msptr
? msptr
->chptr
->chname
: "*");
501 if (fmt
->fields
& FIELD_USER
)
502 append_format(str
, sizeof str
, &pos
, " %s", target_p
->username
);
503 if (fmt
->fields
& FIELD_IP
)
505 if (show_ip(source_p
, target_p
) && !EmptyString(target_p
->sockhost
) && strcmp(target_p
->sockhost
, "0"))
506 append_format(str
, sizeof str
, &pos
, " %s", target_p
->sockhost
);
508 append_format(str
, sizeof str
, &pos
, " %s", "255.255.255.255");
510 if (fmt
->fields
& FIELD_HOST
)
511 append_format(str
, sizeof str
, &pos
, " %s", target_p
->host
);
512 if (fmt
->fields
& FIELD_SERVER
)
513 append_format(str
, sizeof str
, &pos
, " %s", target_p
->servptr
->name
);
514 if (fmt
->fields
& FIELD_NICK
)
515 append_format(str
, sizeof str
, &pos
, " %s", target_p
->name
);
516 if (fmt
->fields
& FIELD_FLAGS
)
517 append_format(str
, sizeof str
, &pos
, " %s", status
);
518 if (fmt
->fields
& FIELD_HOP
)
519 append_format(str
, sizeof str
, &pos
, " %d", ConfigServerHide
.flatten_links
&& !IsOper(source_p
) && !IsExemptShide(source_p
) ? 0 : target_p
->hopcount
);
520 if (fmt
->fields
& FIELD_IDLE
)
521 append_format(str
, sizeof str
, &pos
, " %d", (int)(MyClient(target_p
) ? rb_current_time() - target_p
->localClient
->last
: 0));
522 if (fmt
->fields
& FIELD_ACCOUNT
)
524 /* display as in whois */
525 q
= target_p
->user
->suser
;
531 q
= target_p
->user
->suser
;
535 append_format(str
, sizeof str
, &pos
, " %s", q
);
537 if (fmt
->fields
& FIELD_OPLEVEL
)
538 append_format(str
, sizeof str
, &pos
, " %s", is_chanop(msptr
) ? "999" : "n/a");
539 if (fmt
->fields
& FIELD_INFO
)
540 append_format(str
, sizeof str
, &pos
, " :%s", target_p
->info
);
542 if (pos
>= sizeof str
)
544 static int warned
= 0;
546 sendto_realops_snomask(SNO_DEBUG
, L_NETWIDE
,
547 "WHOX overflow while sending information about %s to %s",
548 target_p
->name
, source_p
->name
);
551 sendto_one(source_p
, "%s", str
);