]> jfr.im git - solanum.git/blame - modules/m_who.c
Detect stdbool.h and add conformant shims if it isn't available
[solanum.git] / modules / m_who.c
CommitLineData
212380e3
AC
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_who.c: Shows who is on a channel.
4 *
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
8 *
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.
13 *
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.
18 *
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
22 * USA
212380e3
AC
23 */
24#include "stdinc.h"
212380e3
AC
25#include "common.h"
26#include "client.h"
27#include "channel.h"
28#include "hash.h"
29#include "ircd.h"
30#include "numeric.h"
31#include "s_serv.h"
32#include "send.h"
4562c604 33#include "match.h"
212380e3 34#include "s_conf.h"
4016731b 35#include "logger.h"
212380e3
AC
36#include "msg.h"
37#include "parse.h"
38#include "modules.h"
39#include "packet.h"
40#include "s_newconf.h"
7e132ff0 41#include "ratelimit.h"
0b904d91 42#include "supported.h"
212380e3 43
48957a49
JT
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 */
57
58struct who_format
59{
60 int fields;
61 const char *querytype;
62};
63
428ca87b 64static int m_who(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
3bf449fe
AW
65static const char who_desc[] =
66 "Provides the WHO command to display information for users on a channel";
212380e3
AC
67
68struct Message who_msgtab = {
7baa37a9 69 "WHO", 0, 0, 0, 0,
212380e3
AC
70 {mg_unreg, {m_who, 2}, mg_ignore, mg_ignore, mg_ignore, {m_who, 2}}
71};
72
0b904d91
AC
73static int
74_modinit(void)
75{
76 add_isupport("WHOX", isupport_string, "");
77 return 0;
78}
79
80static void
81_moddeinit(void)
82{
83 delete_isupport("WHOX");
84}
85
212380e3 86mapi_clist_av1 who_clist[] = { &who_msgtab, NULL };
3bf449fe 87DECLARE_MODULE_AV2(who, _modinit, _moddeinit, who_clist, NULL, NULL, NULL, NULL, who_desc);
212380e3
AC
88
89static void do_who_on_channel(struct Client *source_p, struct Channel *chptr,
48957a49
JT
90 int server_oper, int member,
91 struct who_format *fmt);
212380e3 92
48957a49 93static void who_global(struct Client *source_p, const char *mask, int server_oper, int operspy, struct who_format *fmt);
212380e3
AC
94
95static void do_who(struct Client *source_p,
02eca3f1
JT
96 struct Client *target_p, struct membership *msptr,
97 struct who_format *fmt);
212380e3
AC
98
99
100/*
101** m_who
212380e3 102** parv[1] = nickname mask list
48957a49 103** parv[2] = additional selection flag and format options
212380e3
AC
104*/
105static int
428ca87b 106m_who(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
107{
108 static time_t last_used = 0;
109 struct Client *target_p;
110 struct membership *msptr;
111 char *mask;
5b96d9a6 112 rb_dlink_node *lp;
212380e3
AC
113 struct Channel *chptr = NULL;
114 int server_oper = parc > 2 ? (*parv[2] == 'o') : 0; /* Show OPERS only */
115 int member;
116 int operspy = 0;
48957a49
JT
117 struct who_format fmt;
118 const char *s;
04d77684 119 char maskcopy[512];
48957a49
JT
120
121 fmt.fields = 0;
122 fmt.querytype = NULL;
123 if (parc > 2 && (s = strchr(parv[2], '%')) != NULL)
124 {
125 s++;
126 for (; *s != '\0'; s++)
127 {
128 switch (*s)
129 {
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;
143 case ',':
144 s++;
145 fmt.querytype = s;
146 s += strlen(s);
147 s--;
148 break;
149 }
150 }
151 if (EmptyString(fmt.querytype) || strlen(fmt.querytype) > 3)
152 fmt.querytype = "0";
153 }
212380e3 154
62656efd 155 rb_strlcpy(maskcopy, parv[1], sizeof maskcopy);
04d77684 156 mask = maskcopy;
212380e3
AC
157
158 collapse(mask);
159
160 /* '/who *' */
161 if((*(mask + 1) == '\0') && (*mask == '*'))
162 {
163 if(source_p->user == NULL)
164 return 0;
165
166 if((lp = source_p->user->channel.head) != NULL)
167 {
168 msptr = lp->data;
48957a49 169 do_who_on_channel(source_p, msptr->chptr, server_oper, YES, &fmt);
212380e3
AC
170 }
171
172 sendto_one(source_p, form_str(RPL_ENDOFWHO),
173 me.name, source_p->name, "*");
174 return 0;
175 }
176
177 if(IsOperSpy(source_p) && *mask == '!')
178 {
179 mask++;
180 operspy = 1;
181
182 if(EmptyString(mask))
183 {
184 sendto_one(source_p, form_str(RPL_ENDOFWHO),
185 me.name, source_p->name, parv[1]);
186 return 0;
187 }
188 }
189
190 /* '/who #some_channel' */
191 if(IsChannelName(mask))
192 {
193 /* List all users on a given channel */
f71e18ee 194 chptr = find_channel(parv[1] + operspy);
7e132ff0 195
212380e3
AC
196 if(chptr != NULL)
197 {
7e132ff0
KB
198 if (!IsOper(source_p) && !ratelimit_client_who(source_p, rb_dlink_list_length(&chptr->members)/50))
199 {
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, "*");
204 return 0;
205 }
206
212380e3
AC
207 if(operspy)
208 report_operspy(source_p, "WHO", chptr->chname);
209
210 if(IsMember(source_p, chptr) || operspy)
48957a49 211 do_who_on_channel(source_p, chptr, server_oper, YES, &fmt);
212380e3 212 else if(!SecretChannel(chptr))
48957a49 213 do_who_on_channel(source_p, chptr, server_oper, NO, &fmt);
212380e3 214 }
7e132ff0 215
212380e3 216 sendto_one(source_p, form_str(RPL_ENDOFWHO),
f71e18ee 217 me.name, source_p->name, parv[1] + operspy);
212380e3
AC
218 return 0;
219 }
220
221 /* '/who nick' */
222
223 if(((target_p = find_named_person(mask)) != NULL) &&
224 (!server_oper || IsOper(target_p)))
225 {
226 int isinvis = 0;
227
228 isinvis = IsInvisible(target_p);
5b96d9a6 229 RB_DLINK_FOREACH(lp, target_p->user->channel.head)
212380e3
AC
230 {
231 msptr = lp->data;
232 chptr = msptr->chptr;
233
234 member = IsMember(source_p, chptr);
235
236 if(isinvis && !member)
237 continue;
238
239 if(member || (!isinvis && PubChannel(chptr)))
240 break;
241 }
242
243 /* if we stopped midlist, lp->data is the membership for
244 * target_p of chptr
245 */
246 if(lp != NULL)
02eca3f1 247 do_who(source_p, target_p, lp->data, &fmt);
212380e3 248 else
02eca3f1 249 do_who(source_p, target_p, NULL, &fmt);
212380e3 250
55abcbb2 251 sendto_one(source_p, form_str(RPL_ENDOFWHO),
212380e3
AC
252 me.name, source_p->name, mask);
253 return 0;
254 }
255
256 if(!IsFloodDone(source_p))
257 flood_endgrace(source_p);
258
259 /* it has to be a global who at this point, limit it */
260 if(!IsOper(source_p))
261 {
7e132ff0 262 if((last_used + ConfigFileEntry.pace_wait) > rb_current_time() || !ratelimit_client(source_p, 1))
212380e3
AC
263 {
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, "*");
268 return 0;
269 }
270 else
e3354945 271 last_used = rb_current_time();
212380e3
AC
272 }
273
274 /* Note: operspy_dont_care_user_info does not apply to
275 * who on channels */
276 if(IsOperSpy(source_p) && ConfigFileEntry.operspy_dont_care_user_info)
277 operspy = 1;
278
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
282 */
283 if((*(mask + 1) == '\0') && (*mask == '0'))
48957a49 284 who_global(source_p, NULL, server_oper, 0, &fmt);
212380e3 285 else
48957a49 286 who_global(source_p, mask, server_oper, operspy, &fmt);
212380e3
AC
287
288 sendto_one(source_p, form_str(RPL_ENDOFWHO),
289 me.name, source_p->name, mask);
290
291 return 0;
292}
293
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
48957a49 300 * - format options
212380e3
AC
301 * output - NONE
302 * side effects - lists matching invisible clients on specified channel,
303 * marks matched clients.
304 */
305static void
306who_common_channel(struct Client *source_p, struct Channel *chptr,
48957a49
JT
307 const char *mask, int server_oper, int *maxmatches,
308 struct who_format *fmt)
212380e3
AC
309{
310 struct membership *msptr;
311 struct Client *target_p;
5b96d9a6 312 rb_dlink_node *ptr;
212380e3 313
5b96d9a6 314 RB_DLINK_FOREACH(ptr, chptr->members.head)
212380e3
AC
315 {
316 msptr = ptr->data;
317 target_p = msptr->client_p;
318
319 if(!IsInvisible(target_p) || IsMarked(target_p))
320 continue;
321
322 if(server_oper && !IsOper(target_p))
323 continue;
324
325 SetMark(target_p);
326
327 if(*maxmatches > 0)
328 {
329 if((mask == NULL) ||
330 match(mask, target_p->name) || match(mask, target_p->username) ||
c88cdb00 331 match(mask, target_p->host) || match(mask, target_p->servptr->name) ||
212380e3
AC
332 (IsOper(source_p) && match(mask, target_p->orighost)) ||
333 match(mask, target_p->info))
334 {
02eca3f1 335 do_who(source_p, target_p, NULL, fmt);
212380e3
AC
336 --(*maxmatches);
337 }
338 }
339 }
340}
341
342/*
343 * who_global
344 *
345 * inputs - pointer to client requesting who
346 * - char * mask to match
347 * - int if oper on a server or not
48957a49
JT
348 * - int if operspy or not
349 * - format options
212380e3
AC
350 * output - NONE
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
355 */
356static void
48957a49 357who_global(struct Client *source_p, const char *mask, int server_oper, int operspy, struct who_format *fmt)
212380e3
AC
358{
359 struct membership *msptr;
360 struct Client *target_p;
5b96d9a6 361 rb_dlink_node *lp, *ptr;
212380e3
AC
362 int maxmatches = 500;
363
364 /* first, list all matching INvisible clients on common channels
365 * if this is not an operspy who
366 */
367 if(!operspy)
368 {
5b96d9a6 369 RB_DLINK_FOREACH(lp, source_p->user->channel.head)
212380e3
AC
370 {
371 msptr = lp->data;
48957a49 372 who_common_channel(source_p, msptr->chptr, mask, server_oper, &maxmatches, fmt);
212380e3
AC
373 }
374 }
375 else if (!ConfigFileEntry.operspy_dont_care_user_info)
376 report_operspy(source_p, "WHO", mask);
377
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
381 * to clear marks
382 */
5b96d9a6 383 RB_DLINK_FOREACH(ptr, global_client_list.head)
212380e3
AC
384 {
385 target_p = ptr->data;
386 if(!IsPerson(target_p))
387 continue;
388
389 if(IsInvisible(target_p) && !operspy)
390 {
391 ClearMark(target_p);
392 continue;
393 }
394
395 if(server_oper && !IsOper(target_p))
396 continue;
397
398 if(maxmatches > 0)
399 {
400 if(!mask ||
401 match(mask, target_p->name) || match(mask, target_p->username) ||
c88cdb00 402 match(mask, target_p->host) || match(mask, target_p->servptr->name) ||
212380e3
AC
403 (IsOper(source_p) && match(mask, target_p->orighost)) ||
404 match(mask, target_p->info))
405 {
02eca3f1 406 do_who(source_p, target_p, NULL, fmt);
212380e3
AC
407 --maxmatches;
408 }
409 }
410 }
411
412 if (maxmatches <= 0)
413 sendto_one(source_p,
414 form_str(ERR_TOOMANYMATCHES),
415 me.name, source_p->name, "WHO");
416}
417
418/*
419 * do_who_on_channel
420 *
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
48957a49 426 * - format options
212380e3
AC
427 * output - NONE
428 * side effects - do a who on given channel
429 */
430static void
431do_who_on_channel(struct Client *source_p, struct Channel *chptr,
48957a49 432 int server_oper, int member, struct who_format *fmt)
212380e3
AC
433{
434 struct Client *target_p;
435 struct membership *msptr;
5b96d9a6 436 rb_dlink_node *ptr;
212380e3 437
5b96d9a6 438 RB_DLINK_FOREACH(ptr, chptr->members.head)
212380e3
AC
439 {
440 msptr = ptr->data;
441 target_p = msptr->client_p;
442
443 if(server_oper && !IsOper(target_p))
444 continue;
445
446 if(member || !IsInvisible(target_p))
02eca3f1 447 do_who(source_p, target_p, msptr, fmt);
212380e3
AC
448 }
449}
450
bac250f6
JT
451/*
452 * append_format
453 *
454 * inputs - pointer to buffer
455 * - size of buffer
456 * - pointer to position
457 * - format string
458 * - arguments for format
459 * output - NONE
460 * side effects - position incremented, possibly beyond size of buffer
461 * this allows detecting overflow
462 */
463static void
464append_format(char *buf, size_t bufsize, size_t *pos, const char *fmt, ...)
465{
466 size_t max, result;
467 va_list ap;
468
469 max = *pos >= bufsize ? 0 : bufsize - *pos;
470 va_start(ap, fmt);
5203cba5 471 result = vsnprintf(buf + *pos, max, fmt, ap);
bac250f6
JT
472 va_end(ap);
473 *pos += result;
474}
475
212380e3
AC
476/*
477 * do_who
478 *
479 * inputs - pointer to client requesting who
480 * - pointer to client to do who on
02eca3f1 481 * - channel membership or NULL
48957a49 482 * - format options
212380e3
AC
483 * output - NONE
484 * side effects - do a who on given person
485 */
486
487static void
02eca3f1 488do_who(struct Client *source_p, struct Client *target_p, struct membership *msptr, struct who_format *fmt)
212380e3 489{
ea6dade2 490 char status[16];
bac250f6
JT
491 char str[510 + 1]; /* linebuf.c will add \r\n */
492 size_t pos;
48957a49 493 const char *q;
212380e3 494
5203cba5 495 sprintf(status, "%c%s%s",
c127b45b 496 target_p->user->away ? 'G' : 'H', IsOper(target_p) ? "*" : "", msptr ? find_channel_status(msptr, fmt->fields || IsCapable(source_p, CLICAP_MULTI_PREFIX)) : "");
212380e3 497
48957a49
JT
498 if (fmt->fields == 0)
499 sendto_one(source_p, form_str(RPL_WHOREPLY), me.name,
02eca3f1 500 source_p->name, msptr ? msptr->chptr->chname : "*",
48957a49
JT
501 target_p->username, target_p->host,
502 target_p->servptr->name, target_p->name, status,
55abcbb2 503 ConfigServerHide.flatten_links && !IsOper(source_p) && !IsExemptShide(source_p) ? 0 : target_p->hopcount,
48957a49
JT
504 target_p->info);
505 else
506 {
507 str[0] = '\0';
bac250f6
JT
508 pos = 0;
509 append_format(str, sizeof str, &pos, ":%s %d %s",
510 me.name, RPL_WHOSPCRPL, source_p->name);
48957a49 511 if (fmt->fields & FIELD_QUERYTYPE)
bac250f6 512 append_format(str, sizeof str, &pos, " %s", fmt->querytype);
48957a49 513 if (fmt->fields & FIELD_CHANNEL)
bac250f6 514 append_format(str, sizeof str, &pos, " %s", msptr ? msptr->chptr->chname : "*");
48957a49 515 if (fmt->fields & FIELD_USER)
bac250f6 516 append_format(str, sizeof str, &pos, " %s", target_p->username);
48957a49
JT
517 if (fmt->fields & FIELD_IP)
518 {
519 if (show_ip(source_p, target_p) && !EmptyString(target_p->sockhost) && strcmp(target_p->sockhost, "0"))
bac250f6 520 append_format(str, sizeof str, &pos, " %s", target_p->sockhost);
48957a49 521 else
bac250f6 522 append_format(str, sizeof str, &pos, " %s", "255.255.255.255");
48957a49
JT
523 }
524 if (fmt->fields & FIELD_HOST)
bac250f6 525 append_format(str, sizeof str, &pos, " %s", target_p->host);
48957a49 526 if (fmt->fields & FIELD_SERVER)
bac250f6 527 append_format(str, sizeof str, &pos, " %s", target_p->servptr->name);
48957a49 528 if (fmt->fields & FIELD_NICK)
bac250f6 529 append_format(str, sizeof str, &pos, " %s", target_p->name);
48957a49 530 if (fmt->fields & FIELD_FLAGS)
bac250f6 531 append_format(str, sizeof str, &pos, " %s", status);
48957a49 532 if (fmt->fields & FIELD_HOP)
9a7ab95b 533 append_format(str, sizeof str, &pos, " %d", ConfigServerHide.flatten_links && !IsOper(source_p) && !IsExemptShide(source_p) ? 0 : target_p->hopcount);
48957a49 534 if (fmt->fields & FIELD_IDLE)
bac250f6 535 append_format(str, sizeof str, &pos, " %d", (int)(MyClient(target_p) ? rb_current_time() - target_p->localClient->last : 0));
48957a49
JT
536 if (fmt->fields & FIELD_ACCOUNT)
537 {
538 /* display as in whois */
539 q = target_p->user->suser;
540 if (!EmptyString(q))
541 {
542 while(IsDigit(*q))
543 q++;
544 if(*q == '\0')
545 q = target_p->user->suser;
546 }
547 else
548 q = "0";
bac250f6 549 append_format(str, sizeof str, &pos, " %s", q);
48957a49
JT
550 }
551 if (fmt->fields & FIELD_OPLEVEL)
bac250f6 552 append_format(str, sizeof str, &pos, " %s", is_chanop(msptr) ? "999" : "n/a");
48957a49 553 if (fmt->fields & FIELD_INFO)
bac250f6
JT
554 append_format(str, sizeof str, &pos, " :%s", target_p->info);
555
556 if (pos >= sizeof str)
557 {
558 static int warned = 0;
559 if (!warned)
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);
563 warned = 1;
564 }
565 sendto_one(source_p, "%s", str);
48957a49 566 }
212380e3 567}