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