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"
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 */
61 const char *querytype
;
64 static int m_who(struct Client
*, struct Client
*, int, const char **);
66 struct Message who_msgtab
= {
67 "WHO", 0, 0, 0, MFLG_SLOW
,
68 {mg_unreg
, {m_who
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {m_who
, 2}}
71 mapi_clist_av1 who_clist
[] = { &who_msgtab
, NULL
};
72 DECLARE_MODULE_AV1(who
, NULL
, NULL
, who_clist
, NULL
, NULL
, "$Revision: 3350 $");
74 static void do_who_on_channel(struct Client
*source_p
, struct Channel
*chptr
,
75 int server_oper
, int member
,
76 struct who_format
*fmt
);
78 static void who_global(struct Client
*source_p
, const char *mask
, int server_oper
, int operspy
, struct who_format
*fmt
);
80 static void do_who(struct Client
*source_p
,
81 struct Client
*target_p
, struct membership
*msptr
,
82 struct who_format
*fmt
);
87 ** parv[1] = nickname mask list
88 ** parv[2] = additional selection flag and format options
91 m_who(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
93 static time_t last_used
= 0;
94 struct Client
*target_p
;
95 struct membership
*msptr
;
98 struct Channel
*chptr
= NULL
;
99 int server_oper
= parc
> 2 ? (*parv
[2] == 'o') : 0; /* Show OPERS only */
102 struct who_format fmt
;
107 fmt
.querytype
= NULL
;
108 if (parc
> 2 && (s
= strchr(parv
[2], '%')) != NULL
)
111 for (; *s
!= '\0'; s
++)
115 case 'c': fmt
.fields
|= FIELD_CHANNEL
; break;
116 case 'd': fmt
.fields
|= FIELD_HOP
; break;
117 case 'f': fmt
.fields
|= FIELD_FLAGS
; break;
118 case 'h': fmt
.fields
|= FIELD_HOST
; break;
119 case 'i': fmt
.fields
|= FIELD_IP
; break;
120 case 'l': fmt
.fields
|= FIELD_IDLE
; break;
121 case 'n': fmt
.fields
|= FIELD_NICK
; break;
122 case 'r': fmt
.fields
|= FIELD_INFO
; break;
123 case 's': fmt
.fields
|= FIELD_SERVER
; break;
124 case 't': fmt
.fields
|= FIELD_QUERYTYPE
; break;
125 case 'u': fmt
.fields
|= FIELD_USER
; break;
126 case 'a': fmt
.fields
|= FIELD_ACCOUNT
; break;
127 case 'o': fmt
.fields
|= FIELD_OPLEVEL
; break;
136 if (EmptyString(fmt
.querytype
) || strlen(fmt
.querytype
) > 3)
140 rb_strlcpy(maskcopy
, parv
[1], sizeof maskcopy
);
146 if((*(mask
+ 1) == '\0') && (*mask
== '*'))
148 if(source_p
->user
== NULL
)
151 if((lp
= source_p
->user
->channel
.head
) != NULL
)
154 do_who_on_channel(source_p
, msptr
->chptr
, server_oper
, YES
, &fmt
);
157 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
158 me
.name
, source_p
->name
, "*");
162 if(IsOperSpy(source_p
) && *mask
== '!')
167 if(EmptyString(mask
))
169 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
170 me
.name
, source_p
->name
, parv
[1]);
175 /* '/who #some_channel' */
176 if(IsChannelName(mask
))
178 /* List all users on a given channel */
179 chptr
= find_channel(parv
[1] + operspy
);
183 report_operspy(source_p
, "WHO", chptr
->chname
);
185 if(IsMember(source_p
, chptr
) || operspy
)
186 do_who_on_channel(source_p
, chptr
, server_oper
, YES
, &fmt
);
187 else if(!SecretChannel(chptr
))
188 do_who_on_channel(source_p
, chptr
, server_oper
, NO
, &fmt
);
190 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
191 me
.name
, source_p
->name
, parv
[1] + operspy
);
197 if(((target_p
= find_named_person(mask
)) != NULL
) &&
198 (!server_oper
|| IsOper(target_p
)))
202 isinvis
= IsInvisible(target_p
);
203 RB_DLINK_FOREACH(lp
, target_p
->user
->channel
.head
)
206 chptr
= msptr
->chptr
;
208 member
= IsMember(source_p
, chptr
);
210 if(isinvis
&& !member
)
213 if(member
|| (!isinvis
&& PubChannel(chptr
)))
217 /* if we stopped midlist, lp->data is the membership for
221 do_who(source_p
, target_p
, lp
->data
, &fmt
);
223 do_who(source_p
, target_p
, NULL
, &fmt
);
225 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
226 me
.name
, source_p
->name
, mask
);
230 if(!IsFloodDone(source_p
))
231 flood_endgrace(source_p
);
233 /* it has to be a global who at this point, limit it */
234 if(!IsOper(source_p
))
236 if((last_used
+ ConfigFileEntry
.pace_wait
) > rb_current_time())
238 sendto_one(source_p
, form_str(RPL_LOAD2HI
),
239 me
.name
, source_p
->name
, "WHO");
240 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
241 me
.name
, source_p
->name
, "*");
245 last_used
= rb_current_time();
248 /* Note: operspy_dont_care_user_info does not apply to
250 if(IsOperSpy(source_p
) && ConfigFileEntry
.operspy_dont_care_user_info
)
253 /* '/who 0' for a global list. this forces clients to actually
254 * request a full list. I presume its because of too many typos
255 * with "/who" ;) --fl
257 if((*(mask
+ 1) == '\0') && (*mask
== '0'))
258 who_global(source_p
, NULL
, server_oper
, 0, &fmt
);
260 who_global(source_p
, mask
, server_oper
, operspy
, &fmt
);
262 sendto_one(source_p
, form_str(RPL_ENDOFWHO
),
263 me
.name
, source_p
->name
, mask
);
268 /* who_common_channel
269 * inputs - pointer to client requesting who
270 * - pointer to channel member chain.
271 * - char * mask to match
272 * - int if oper on a server or not
273 * - pointer to int maxmatches
276 * side effects - lists matching invisible clients on specified channel,
277 * marks matched clients.
280 who_common_channel(struct Client
*source_p
, struct Channel
*chptr
,
281 const char *mask
, int server_oper
, int *maxmatches
,
282 struct who_format
*fmt
)
284 struct membership
*msptr
;
285 struct Client
*target_p
;
288 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
291 target_p
= msptr
->client_p
;
293 if(!IsInvisible(target_p
) || IsMarked(target_p
))
296 if(server_oper
&& !IsOper(target_p
))
304 match(mask
, target_p
->name
) || match(mask
, target_p
->username
) ||
305 match(mask
, target_p
->host
) || match(mask
, target_p
->servptr
->name
) ||
306 (IsOper(source_p
) && match(mask
, target_p
->orighost
)) ||
307 match(mask
, target_p
->info
))
309 do_who(source_p
, target_p
, NULL
, fmt
);
319 * inputs - pointer to client requesting who
320 * - char * mask to match
321 * - int if oper on a server or not
322 * - int if operspy or not
325 * side effects - do a global scan of all clients looking for match
326 * this is slightly expensive on EFnet ...
327 * marks assumed cleared for all clients initially
328 * and will be left cleared on return
331 who_global(struct Client
*source_p
, const char *mask
, int server_oper
, int operspy
, struct who_format
*fmt
)
333 struct membership
*msptr
;
334 struct Client
*target_p
;
335 rb_dlink_node
*lp
, *ptr
;
336 int maxmatches
= 500;
338 /* first, list all matching INvisible clients on common channels
339 * if this is not an operspy who
343 RB_DLINK_FOREACH(lp
, source_p
->user
->channel
.head
)
346 who_common_channel(source_p
, msptr
->chptr
, mask
, server_oper
, &maxmatches
, fmt
);
349 else if (!ConfigFileEntry
.operspy_dont_care_user_info
)
350 report_operspy(source_p
, "WHO", mask
);
352 /* second, list all matching visible clients and clear all marks
353 * on invisible clients
354 * if this is an operspy who, list all matching clients, no need
357 RB_DLINK_FOREACH(ptr
, global_client_list
.head
)
359 target_p
= ptr
->data
;
360 if(!IsPerson(target_p
))
363 if(IsInvisible(target_p
) && !operspy
)
369 if(server_oper
&& !IsOper(target_p
))
375 match(mask
, target_p
->name
) || match(mask
, target_p
->username
) ||
376 match(mask
, target_p
->host
) || match(mask
, target_p
->servptr
->name
) ||
377 (IsOper(source_p
) && match(mask
, target_p
->orighost
)) ||
378 match(mask
, target_p
->info
))
380 do_who(source_p
, target_p
, NULL
, fmt
);
388 form_str(ERR_TOOMANYMATCHES
),
389 me
.name
, source_p
->name
, "WHO");
395 * inputs - pointer to client requesting who
396 * - pointer to channel to do who on
397 * - The "real name" of this channel
398 * - int if source_p is a server oper or not
399 * - int if client is member or not
402 * side effects - do a who on given channel
405 do_who_on_channel(struct Client
*source_p
, struct Channel
*chptr
,
406 int server_oper
, int member
, struct who_format
*fmt
)
408 struct Client
*target_p
;
409 struct membership
*msptr
;
412 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
415 target_p
= msptr
->client_p
;
417 if(server_oper
&& !IsOper(target_p
))
420 if(member
|| !IsInvisible(target_p
))
421 do_who(source_p
, target_p
, msptr
, fmt
);
428 * inputs - pointer to buffer
430 * - pointer to position
432 * - arguments for format
434 * side effects - position incremented, possibly beyond size of buffer
435 * this allows detecting overflow
438 append_format(char *buf
, size_t bufsize
, size_t *pos
, const char *fmt
, ...)
443 max
= *pos
>= bufsize
? 0 : bufsize
- *pos
;
445 result
= rb_vsnprintf(buf
+ *pos
, max
, fmt
, ap
);
453 * inputs - pointer to client requesting who
454 * - pointer to client to do who on
455 * - channel membership or NULL
458 * side effects - do a who on given person
462 do_who(struct Client
*source_p
, struct Client
*target_p
, struct membership
*msptr
, struct who_format
*fmt
)
465 char str
[510 + 1]; /* linebuf.c will add \r\n */
469 rb_sprintf(status
, "%c%s%s",
470 target_p
->user
->away
? 'G' : 'H', IsOper(target_p
) ? "*" : "", msptr
? find_channel_status(msptr
, fmt
->fields
|| IsCapable(source_p
, CLICAP_MULTI_PREFIX
)) : "");
472 if (fmt
->fields
== 0)
473 sendto_one(source_p
, form_str(RPL_WHOREPLY
), me
.name
,
474 source_p
->name
, msptr
? msptr
->chptr
->chname
: "*",
475 target_p
->username
, target_p
->host
,
476 target_p
->servptr
->name
, target_p
->name
, status
,
477 ConfigServerHide
.flatten_links
&& !IsOper(source_p
) && !IsExemptShide(source_p
) ? 0 : target_p
->hopcount
,
483 append_format(str
, sizeof str
, &pos
, ":%s %d %s",
484 me
.name
, RPL_WHOSPCRPL
, source_p
->name
);
485 if (fmt
->fields
& FIELD_QUERYTYPE
)
486 append_format(str
, sizeof str
, &pos
, " %s", fmt
->querytype
);
487 if (fmt
->fields
& FIELD_CHANNEL
)
488 append_format(str
, sizeof str
, &pos
, " %s", msptr
? msptr
->chptr
->chname
: "*");
489 if (fmt
->fields
& FIELD_USER
)
490 append_format(str
, sizeof str
, &pos
, " %s", target_p
->username
);
491 if (fmt
->fields
& FIELD_IP
)
493 if (show_ip(source_p
, target_p
) && !EmptyString(target_p
->sockhost
) && strcmp(target_p
->sockhost
, "0"))
494 append_format(str
, sizeof str
, &pos
, " %s", target_p
->sockhost
);
496 append_format(str
, sizeof str
, &pos
, " %s", "255.255.255.255");
498 if (fmt
->fields
& FIELD_HOST
)
499 append_format(str
, sizeof str
, &pos
, " %s", target_p
->host
);
500 if (fmt
->fields
& FIELD_SERVER
)
501 append_format(str
, sizeof str
, &pos
, " %s", target_p
->servptr
->name
);
502 if (fmt
->fields
& FIELD_NICK
)
503 append_format(str
, sizeof str
, &pos
, " %s", target_p
->name
);
504 if (fmt
->fields
& FIELD_FLAGS
)
505 append_format(str
, sizeof str
, &pos
, " %s", status
);
506 if (fmt
->fields
& FIELD_HOP
)
507 append_format(str
, sizeof str
, &pos
, " %d", ConfigServerHide
.flatten_links
&& !IsOper(source_p
) && !IsExemptShide(source_p
) ? 0 : target_p
->hopcount
);
508 if (fmt
->fields
& FIELD_IDLE
)
509 append_format(str
, sizeof str
, &pos
, " %d", (int)(MyClient(target_p
) ? rb_current_time() - target_p
->localClient
->last
: 0));
510 if (fmt
->fields
& FIELD_ACCOUNT
)
512 /* display as in whois */
513 q
= target_p
->user
->suser
;
519 q
= target_p
->user
->suser
;
523 append_format(str
, sizeof str
, &pos
, " %s", q
);
525 if (fmt
->fields
& FIELD_OPLEVEL
)
526 append_format(str
, sizeof str
, &pos
, " %s", is_chanop(msptr
) ? "999" : "n/a");
527 if (fmt
->fields
& FIELD_INFO
)
528 append_format(str
, sizeof str
, &pos
, " :%s", target_p
->info
);
530 if (pos
>= sizeof str
)
532 static int warned
= 0;
534 sendto_realops_snomask(SNO_DEBUG
, L_NETWIDE
,
535 "WHOX overflow while sending information about %s to %s",
536 target_p
->name
, source_p
->name
);
539 sendto_one(source_p
, "%s", str
);