]> jfr.im git - solanum.git/blob - modules/m_who.c
Rework oper hiding
[solanum.git] / modules / m_who.c
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 #include "stdinc.h"
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"
32 #include "match.h"
33 #include "s_conf.h"
34 #include "logger.h"
35 #include "msg.h"
36 #include "parse.h"
37 #include "modules.h"
38 #include "packet.h"
39 #include "s_newconf.h"
40 #include "ratelimit.h"
41 #include "supported.h"
42
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
57 static const char who_desc[] =
58 "Provides the WHO command to display information for users on a channel";
59
60 struct who_format
61 {
62 int fields;
63 const char *querytype;
64 };
65
66 static void m_who(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
67
68 static void do_who_on_channel(struct Client *source_p, struct Channel *chptr,
69 int server_oper, int member,
70 struct who_format *fmt);
71 static void who_global(struct Client *source_p, const char *mask, int server_oper, int operspy, struct who_format *fmt);
72 static void do_who(struct Client *source_p,
73 struct Client *target_p, struct membership *msptr,
74 struct who_format *fmt);
75
76 struct Message who_msgtab = {
77 "WHO", 0, 0, 0, 0,
78 {mg_unreg, {m_who, 2}, mg_ignore, mg_ignore, mg_ignore, {m_who, 2}}
79 };
80
81 static int
82 _modinit(void)
83 {
84 add_isupport("WHOX", isupport_string, "");
85 return 0;
86 }
87
88 static void
89 _moddeinit(void)
90 {
91 delete_isupport("WHOX");
92 }
93
94 mapi_clist_av1 who_clist[] = { &who_msgtab, NULL };
95 DECLARE_MODULE_AV2(who, _modinit, _moddeinit, who_clist, NULL, NULL, NULL, NULL, who_desc);
96
97 /*
98 ** m_who
99 ** parv[1] = nickname mask list
100 ** parv[2] = additional selection flag and format options
101 */
102 static void
103 m_who(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
104 {
105 static time_t last_used = 0;
106 struct Client *target_p;
107 struct membership *msptr;
108 char *mask;
109 rb_dlink_node *lp;
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;
114 struct who_format fmt;
115 const char *s;
116 char maskcopy[512];
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 }
151
152 rb_strlcpy(maskcopy, parv[1], sizeof maskcopy);
153 mask = maskcopy;
154
155 collapse(mask);
156
157 /* '/who *' */
158 if((*(mask + 1) == '\0') && (*mask == '*'))
159 {
160 if(source_p->user == NULL)
161 return;
162
163 if((lp = source_p->user->channel.head) != NULL)
164 {
165 msptr = lp->data;
166 do_who_on_channel(source_p, msptr->chptr, server_oper, true, &fmt);
167 }
168
169 sendto_one(source_p, form_str(RPL_ENDOFWHO),
170 me.name, source_p->name, "*");
171 return;
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]);
183 return;
184 }
185 }
186
187 /* '/who #some_channel' */
188 if(IsChannelName(mask))
189 {
190 /* List all users on a given channel */
191 chptr = find_channel(parv[1] + operspy);
192
193 if(chptr != NULL)
194 {
195 if (!IsOper(source_p) && !ratelimit_client_who(source_p, rb_dlink_list_length(&chptr->members)/50))
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, "*");
201 return;
202 }
203
204 if(operspy)
205 report_operspy(source_p, "WHO", chptr->chname);
206
207 if(IsMember(source_p, chptr) || operspy)
208 do_who_on_channel(source_p, chptr, server_oper, true, &fmt);
209 else if(!SecretChannel(chptr))
210 do_who_on_channel(source_p, chptr, server_oper, false, &fmt);
211 }
212
213 sendto_one(source_p, form_str(RPL_ENDOFWHO),
214 me.name, source_p->name, parv[1] + operspy);
215 return;
216 }
217
218 /* '/who nick' */
219
220 if(((target_p = find_named_person(mask)) != NULL) &&
221 (!server_oper || SeesOper(target_p, source_p)))
222 {
223 int isinvis = 0;
224
225 isinvis = IsInvisible(target_p);
226 RB_DLINK_FOREACH(lp, target_p->user->channel.head)
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)
244 do_who(source_p, target_p, lp->data, &fmt);
245 else
246 do_who(source_p, target_p, NULL, &fmt);
247
248 sendto_one(source_p, form_str(RPL_ENDOFWHO),
249 me.name, source_p->name, mask);
250 return;
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 */
257 if(!IsOper(source_p))
258 {
259 if((last_used + ConfigFileEntry.pace_wait) > rb_current_time() || !ratelimit_client(source_p, 1))
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, "*");
265 return;
266 }
267 else
268 last_used = rb_current_time();
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'))
281 who_global(source_p, NULL, server_oper, 0, &fmt);
282 else
283 who_global(source_p, mask, server_oper, operspy, &fmt);
284
285 sendto_one(source_p, form_str(RPL_ENDOFWHO),
286 me.name, source_p->name, mask);
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
295 * - format options
296 * output - NONE
297 * side effects - lists matching invisible clients on specified channel,
298 * marks matched clients.
299 */
300 static void
301 who_common_channel(struct Client *source_p, struct Channel *chptr,
302 const char *mask, int server_oper, int *maxmatches,
303 struct who_format *fmt)
304 {
305 struct membership *msptr;
306 struct Client *target_p;
307 rb_dlink_node *ptr;
308
309 RB_DLINK_FOREACH(ptr, chptr->members.head)
310 {
311 msptr = ptr->data;
312 target_p = msptr->client_p;
313
314 if(!IsInvisible(target_p) || IsMarked(target_p))
315 continue;
316
317 if(server_oper && !SeesOper(target_p, source_p))
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) ||
326 match(mask, target_p->host) || match(mask, target_p->servptr->name) ||
327 (IsOper(source_p) && match(mask, target_p->orighost)) ||
328 match(mask, target_p->info))
329 {
330 do_who(source_p, target_p, NULL, fmt);
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
343 * - int if operspy or not
344 * - format options
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 */
351 static void
352 who_global(struct Client *source_p, const char *mask, int server_oper, int operspy, struct who_format *fmt)
353 {
354 struct membership *msptr;
355 struct Client *target_p;
356 rb_dlink_node *lp, *ptr;
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 {
364 RB_DLINK_FOREACH(lp, source_p->user->channel.head)
365 {
366 msptr = lp->data;
367 who_common_channel(source_p, msptr->chptr, mask, server_oper, &maxmatches, fmt);
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 */
378 RB_DLINK_FOREACH(ptr, global_client_list.head)
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
390 if(server_oper && !SeesOper(target_p, source_p))
391 continue;
392
393 if(maxmatches > 0)
394 {
395 if(!mask ||
396 match(mask, target_p->name) || match(mask, target_p->username) ||
397 match(mask, target_p->host) || match(mask, target_p->servptr->name) ||
398 (IsOper(source_p) && match(mask, target_p->orighost)) ||
399 match(mask, target_p->info))
400 {
401 do_who(source_p, target_p, NULL, fmt);
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
421 * - format options
422 * output - NONE
423 * side effects - do a who on given channel
424 */
425 static void
426 do_who_on_channel(struct Client *source_p, struct Channel *chptr,
427 int server_oper, int member, struct who_format *fmt)
428 {
429 struct Client *target_p;
430 struct membership *msptr;
431 rb_dlink_node *ptr;
432
433 RB_DLINK_FOREACH(ptr, chptr->members.head)
434 {
435 msptr = ptr->data;
436 target_p = msptr->client_p;
437
438 if(server_oper && !SeesOper(target_p, source_p))
439 continue;
440
441 if(member || !IsInvisible(target_p))
442 do_who(source_p, target_p, msptr, fmt);
443 }
444 }
445
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 */
458 static void
459 append_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);
466 result = vsnprintf(buf + *pos, max, fmt, ap);
467 va_end(ap);
468 *pos += result;
469 }
470
471 /*
472 * do_who
473 *
474 * inputs - pointer to client requesting who
475 * - pointer to client to do who on
476 * - channel membership or NULL
477 * - format options
478 * output - NONE
479 * side effects - do a who on given person
480 */
481
482 static void
483 do_who(struct Client *source_p, struct Client *target_p, struct membership *msptr, struct who_format *fmt)
484 {
485 char status[16];
486 char str[510 + 1]; /* linebuf.c will add \r\n */
487 size_t pos;
488 const char *q;
489
490 sprintf(status, "%c%s%s",
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)) : "");
492
493 if (fmt->fields == 0)
494 sendto_one(source_p, form_str(RPL_WHOREPLY), me.name,
495 source_p->name, msptr ? msptr->chptr->chname : "*",
496 target_p->username, target_p->host,
497 target_p->servptr->name, target_p->name, status,
498 ConfigServerHide.flatten_links && !IsOper(source_p) && !IsExemptShide(source_p) ? 0 : target_p->hopcount,
499 target_p->info);
500 else
501 {
502 str[0] = '\0';
503 pos = 0;
504 append_format(str, sizeof str, &pos, ":%s %d %s",
505 me.name, RPL_WHOSPCRPL, source_p->name);
506 if (fmt->fields & FIELD_QUERYTYPE)
507 append_format(str, sizeof str, &pos, " %s", fmt->querytype);
508 if (fmt->fields & FIELD_CHANNEL)
509 append_format(str, sizeof str, &pos, " %s", msptr ? msptr->chptr->chname : "*");
510 if (fmt->fields & FIELD_USER)
511 append_format(str, sizeof str, &pos, " %s", target_p->username);
512 if (fmt->fields & FIELD_IP)
513 {
514 if (show_ip(source_p, target_p) && !EmptyString(target_p->sockhost) && strcmp(target_p->sockhost, "0"))
515 append_format(str, sizeof str, &pos, " %s", target_p->sockhost);
516 else
517 append_format(str, sizeof str, &pos, " %s", "255.255.255.255");
518 }
519 if (fmt->fields & FIELD_HOST)
520 append_format(str, sizeof str, &pos, " %s", target_p->host);
521 if (fmt->fields & FIELD_SERVER)
522 append_format(str, sizeof str, &pos, " %s", target_p->servptr->name);
523 if (fmt->fields & FIELD_NICK)
524 append_format(str, sizeof str, &pos, " %s", target_p->name);
525 if (fmt->fields & FIELD_FLAGS)
526 append_format(str, sizeof str, &pos, " %s", status);
527 if (fmt->fields & FIELD_HOP)
528 append_format(str, sizeof str, &pos, " %d", ConfigServerHide.flatten_links && !IsOper(source_p) && !IsExemptShide(source_p) ? 0 : target_p->hopcount);
529 if (fmt->fields & FIELD_IDLE)
530 append_format(str, sizeof str, &pos, " %d", (int)(MyClient(target_p) ? rb_current_time() - target_p->localClient->last : 0));
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";
544 append_format(str, sizeof str, &pos, " %s", q);
545 }
546 if (fmt->fields & FIELD_OPLEVEL)
547 append_format(str, sizeof str, &pos, " %s", is_chanop(msptr) ? "999" : "n/a");
548 if (fmt->fields & FIELD_INFO)
549 append_format(str, sizeof str, &pos, " :%s", target_p->info);
550
551 if (pos >= sizeof str)
552 {
553 static bool warned = false;
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);
558 warned = true;
559 }
560 sendto_one(source_p, "%s", str);
561 }
562 }