]> jfr.im git - solanum.git/blame_incremental - modules/m_who.c
ratelimit: Add rate-limiting to MOTD, WHO, and remote WHOIS.
[solanum.git] / modules / m_who.c
... / ...
CommitLineData
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
23 *
24 * $Id: m_who.c 3350 2007-04-02 22:03:08Z jilles $
25 */
26#include "stdinc.h"
27#include "common.h"
28#include "client.h"
29#include "channel.h"
30#include "hash.h"
31#include "ircd.h"
32#include "numeric.h"
33#include "s_serv.h"
34#include "send.h"
35#include "match.h"
36#include "s_conf.h"
37#include "logger.h"
38#include "msg.h"
39#include "parse.h"
40#include "modules.h"
41#include "packet.h"
42#include "s_newconf.h"
43#include "ratelimit.h"
44
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 */
58
59struct who_format
60{
61 int fields;
62 const char *querytype;
63};
64
65static int m_who(struct Client *, struct Client *, int, const char **);
66
67struct 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}}
70};
71
72mapi_clist_av1 who_clist[] = { &who_msgtab, NULL };
73DECLARE_MODULE_AV1(who, NULL, NULL, who_clist, NULL, NULL, "$Revision: 3350 $");
74
75static void do_who_on_channel(struct Client *source_p, struct Channel *chptr,
76 int server_oper, int member,
77 struct who_format *fmt);
78
79static void who_global(struct Client *source_p, const char *mask, int server_oper, int operspy, struct who_format *fmt);
80
81static void do_who(struct Client *source_p,
82 struct Client *target_p, struct membership *msptr,
83 struct who_format *fmt);
84
85
86/*
87** m_who
88** parv[1] = nickname mask list
89** parv[2] = additional selection flag and format options
90*/
91static int
92m_who(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
93{
94 static time_t last_used = 0;
95 struct Client *target_p;
96 struct membership *msptr;
97 char *mask;
98 rb_dlink_node *lp;
99 struct Channel *chptr = NULL;
100 int server_oper = parc > 2 ? (*parv[2] == 'o') : 0; /* Show OPERS only */
101 int member;
102 int operspy = 0;
103 struct who_format fmt;
104 const char *s;
105 char maskcopy[512];
106
107 fmt.fields = 0;
108 fmt.querytype = NULL;
109 if (parc > 2 && (s = strchr(parv[2], '%')) != NULL)
110 {
111 s++;
112 for (; *s != '\0'; s++)
113 {
114 switch (*s)
115 {
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;
129 case ',':
130 s++;
131 fmt.querytype = s;
132 s += strlen(s);
133 s--;
134 break;
135 }
136 }
137 if (EmptyString(fmt.querytype) || strlen(fmt.querytype) > 3)
138 fmt.querytype = "0";
139 }
140
141 rb_strlcpy(maskcopy, parv[1], sizeof maskcopy);
142 mask = maskcopy;
143
144 collapse(mask);
145
146 /* '/who *' */
147 if((*(mask + 1) == '\0') && (*mask == '*'))
148 {
149 if(source_p->user == NULL)
150 return 0;
151
152 if((lp = source_p->user->channel.head) != NULL)
153 {
154 msptr = lp->data;
155 do_who_on_channel(source_p, msptr->chptr, server_oper, YES, &fmt);
156 }
157
158 sendto_one(source_p, form_str(RPL_ENDOFWHO),
159 me.name, source_p->name, "*");
160 return 0;
161 }
162
163 if(IsOperSpy(source_p) && *mask == '!')
164 {
165 mask++;
166 operspy = 1;
167
168 if(EmptyString(mask))
169 {
170 sendto_one(source_p, form_str(RPL_ENDOFWHO),
171 me.name, source_p->name, parv[1]);
172 return 0;
173 }
174 }
175
176 /* '/who #some_channel' */
177 if(IsChannelName(mask))
178 {
179 /* List all users on a given channel */
180 chptr = find_channel(parv[1] + operspy);
181
182 if(chptr != NULL)
183 {
184 if (!IsOper(source_p) && !ratelimit_client_who(source_p, rb_dlink_list_length(&chptr->members)/50))
185 {
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, "*");
190 return 0;
191 }
192
193 if(operspy)
194 report_operspy(source_p, "WHO", chptr->chname);
195
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);
200 }
201
202 sendto_one(source_p, form_str(RPL_ENDOFWHO),
203 me.name, source_p->name, parv[1] + operspy);
204 return 0;
205 }
206
207 /* '/who nick' */
208
209 if(((target_p = find_named_person(mask)) != NULL) &&
210 (!server_oper || IsOper(target_p)))
211 {
212 int isinvis = 0;
213
214 isinvis = IsInvisible(target_p);
215 RB_DLINK_FOREACH(lp, target_p->user->channel.head)
216 {
217 msptr = lp->data;
218 chptr = msptr->chptr;
219
220 member = IsMember(source_p, chptr);
221
222 if(isinvis && !member)
223 continue;
224
225 if(member || (!isinvis && PubChannel(chptr)))
226 break;
227 }
228
229 /* if we stopped midlist, lp->data is the membership for
230 * target_p of chptr
231 */
232 if(lp != NULL)
233 do_who(source_p, target_p, lp->data, &fmt);
234 else
235 do_who(source_p, target_p, NULL, &fmt);
236
237 sendto_one(source_p, form_str(RPL_ENDOFWHO),
238 me.name, source_p->name, mask);
239 return 0;
240 }
241
242 if(!IsFloodDone(source_p))
243 flood_endgrace(source_p);
244
245 /* it has to be a global who at this point, limit it */
246 if(!IsOper(source_p))
247 {
248 if((last_used + ConfigFileEntry.pace_wait) > rb_current_time() || !ratelimit_client(source_p, 1))
249 {
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, "*");
254 return 0;
255 }
256 else
257 last_used = rb_current_time();
258 }
259
260 /* Note: operspy_dont_care_user_info does not apply to
261 * who on channels */
262 if(IsOperSpy(source_p) && ConfigFileEntry.operspy_dont_care_user_info)
263 operspy = 1;
264
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
268 */
269 if((*(mask + 1) == '\0') && (*mask == '0'))
270 who_global(source_p, NULL, server_oper, 0, &fmt);
271 else
272 who_global(source_p, mask, server_oper, operspy, &fmt);
273
274 sendto_one(source_p, form_str(RPL_ENDOFWHO),
275 me.name, source_p->name, mask);
276
277 return 0;
278}
279
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
286 * - format options
287 * output - NONE
288 * side effects - lists matching invisible clients on specified channel,
289 * marks matched clients.
290 */
291static void
292who_common_channel(struct Client *source_p, struct Channel *chptr,
293 const char *mask, int server_oper, int *maxmatches,
294 struct who_format *fmt)
295{
296 struct membership *msptr;
297 struct Client *target_p;
298 rb_dlink_node *ptr;
299
300 RB_DLINK_FOREACH(ptr, chptr->members.head)
301 {
302 msptr = ptr->data;
303 target_p = msptr->client_p;
304
305 if(!IsInvisible(target_p) || IsMarked(target_p))
306 continue;
307
308 if(server_oper && !IsOper(target_p))
309 continue;
310
311 SetMark(target_p);
312
313 if(*maxmatches > 0)
314 {
315 if((mask == NULL) ||
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))
320 {
321 do_who(source_p, target_p, NULL, fmt);
322 --(*maxmatches);
323 }
324 }
325 }
326}
327
328/*
329 * who_global
330 *
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
335 * - format options
336 * output - NONE
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
341 */
342static void
343who_global(struct Client *source_p, const char *mask, int server_oper, int operspy, struct who_format *fmt)
344{
345 struct membership *msptr;
346 struct Client *target_p;
347 rb_dlink_node *lp, *ptr;
348 int maxmatches = 500;
349
350 /* first, list all matching INvisible clients on common channels
351 * if this is not an operspy who
352 */
353 if(!operspy)
354 {
355 RB_DLINK_FOREACH(lp, source_p->user->channel.head)
356 {
357 msptr = lp->data;
358 who_common_channel(source_p, msptr->chptr, mask, server_oper, &maxmatches, fmt);
359 }
360 }
361 else if (!ConfigFileEntry.operspy_dont_care_user_info)
362 report_operspy(source_p, "WHO", mask);
363
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
367 * to clear marks
368 */
369 RB_DLINK_FOREACH(ptr, global_client_list.head)
370 {
371 target_p = ptr->data;
372 if(!IsPerson(target_p))
373 continue;
374
375 if(IsInvisible(target_p) && !operspy)
376 {
377 ClearMark(target_p);
378 continue;
379 }
380
381 if(server_oper && !IsOper(target_p))
382 continue;
383
384 if(maxmatches > 0)
385 {
386 if(!mask ||
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))
391 {
392 do_who(source_p, target_p, NULL, fmt);
393 --maxmatches;
394 }
395 }
396 }
397
398 if (maxmatches <= 0)
399 sendto_one(source_p,
400 form_str(ERR_TOOMANYMATCHES),
401 me.name, source_p->name, "WHO");
402}
403
404/*
405 * do_who_on_channel
406 *
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
412 * - format options
413 * output - NONE
414 * side effects - do a who on given channel
415 */
416static void
417do_who_on_channel(struct Client *source_p, struct Channel *chptr,
418 int server_oper, int member, struct who_format *fmt)
419{
420 struct Client *target_p;
421 struct membership *msptr;
422 rb_dlink_node *ptr;
423
424 RB_DLINK_FOREACH(ptr, chptr->members.head)
425 {
426 msptr = ptr->data;
427 target_p = msptr->client_p;
428
429 if(server_oper && !IsOper(target_p))
430 continue;
431
432 if(member || !IsInvisible(target_p))
433 do_who(source_p, target_p, msptr, fmt);
434 }
435}
436
437/*
438 * append_format
439 *
440 * inputs - pointer to buffer
441 * - size of buffer
442 * - pointer to position
443 * - format string
444 * - arguments for format
445 * output - NONE
446 * side effects - position incremented, possibly beyond size of buffer
447 * this allows detecting overflow
448 */
449static void
450append_format(char *buf, size_t bufsize, size_t *pos, const char *fmt, ...)
451{
452 size_t max, result;
453 va_list ap;
454
455 max = *pos >= bufsize ? 0 : bufsize - *pos;
456 va_start(ap, fmt);
457 result = rb_vsnprintf(buf + *pos, max, fmt, ap);
458 va_end(ap);
459 *pos += result;
460}
461
462/*
463 * do_who
464 *
465 * inputs - pointer to client requesting who
466 * - pointer to client to do who on
467 * - channel membership or NULL
468 * - format options
469 * output - NONE
470 * side effects - do a who on given person
471 */
472
473static void
474do_who(struct Client *source_p, struct Client *target_p, struct membership *msptr, struct who_format *fmt)
475{
476 char status[16];
477 char str[510 + 1]; /* linebuf.c will add \r\n */
478 size_t pos;
479 const char *q;
480
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)) : "");
483
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,
490 target_p->info);
491 else
492 {
493 str[0] = '\0';
494 pos = 0;
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)
504 {
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);
507 else
508 append_format(str, sizeof str, &pos, " %s", "255.255.255.255");
509 }
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)
523 {
524 /* display as in whois */
525 q = target_p->user->suser;
526 if (!EmptyString(q))
527 {
528 while(IsDigit(*q))
529 q++;
530 if(*q == '\0')
531 q = target_p->user->suser;
532 }
533 else
534 q = "0";
535 append_format(str, sizeof str, &pos, " %s", q);
536 }
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);
541
542 if (pos >= sizeof str)
543 {
544 static int warned = 0;
545 if (!warned)
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);
549 warned = 1;
550 }
551 sendto_one(source_p, "%s", str);
552 }
553}