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