]> jfr.im git - solanum.git/blob - modules/m_who.c
core/m_squit: Add AV2 description
[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 "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"
33 #include "match.h"
34 #include "s_conf.h"
35 #include "logger.h"
36 #include "msg.h"
37 #include "parse.h"
38 #include "modules.h"
39 #include "packet.h"
40 #include "s_newconf.h"
41 #include "ratelimit.h"
42 #include "supported.h"
43
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
58 struct who_format
59 {
60 int fields;
61 const char *querytype;
62 };
63
64 static int m_who(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
65
66 struct Message who_msgtab = {
67 "WHO", 0, 0, 0, 0,
68 {mg_unreg, {m_who, 2}, mg_ignore, mg_ignore, mg_ignore, {m_who, 2}}
69 };
70
71 static int
72 _modinit(void)
73 {
74 add_isupport("WHOX", isupport_string, "");
75 return 0;
76 }
77
78 static void
79 _moddeinit(void)
80 {
81 delete_isupport("WHOX");
82 }
83
84 mapi_clist_av1 who_clist[] = { &who_msgtab, NULL };
85 DECLARE_MODULE_AV2(who, _modinit, _moddeinit, who_clist, NULL, NULL, NULL, NULL, NULL);
86
87 static void do_who_on_channel(struct Client *source_p, struct Channel *chptr,
88 int server_oper, int member,
89 struct who_format *fmt);
90
91 static void who_global(struct Client *source_p, const char *mask, int server_oper, int operspy, struct who_format *fmt);
92
93 static void do_who(struct Client *source_p,
94 struct Client *target_p, struct membership *msptr,
95 struct who_format *fmt);
96
97
98 /*
99 ** m_who
100 ** parv[1] = nickname mask list
101 ** parv[2] = additional selection flag and format options
102 */
103 static int
104 m_who(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
105 {
106 static time_t last_used = 0;
107 struct Client *target_p;
108 struct membership *msptr;
109 char *mask;
110 rb_dlink_node *lp;
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;
115 struct who_format fmt;
116 const char *s;
117 char maskcopy[512];
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 }
152
153 rb_strlcpy(maskcopy, parv[1], sizeof maskcopy);
154 mask = maskcopy;
155
156 collapse(mask);
157
158 /* '/who *' */
159 if((*(mask + 1) == '\0') && (*mask == '*'))
160 {
161 if(source_p->user == NULL)
162 return 0;
163
164 if((lp = source_p->user->channel.head) != NULL)
165 {
166 msptr = lp->data;
167 do_who_on_channel(source_p, msptr->chptr, server_oper, YES, &fmt);
168 }
169
170 sendto_one(source_p, form_str(RPL_ENDOFWHO),
171 me.name, source_p->name, "*");
172 return 0;
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]);
184 return 0;
185 }
186 }
187
188 /* '/who #some_channel' */
189 if(IsChannelName(mask))
190 {
191 /* List all users on a given channel */
192 chptr = find_channel(parv[1] + operspy);
193
194 if(chptr != NULL)
195 {
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, "*");
202 return 0;
203 }
204
205 if(operspy)
206 report_operspy(source_p, "WHO", chptr->chname);
207
208 if(IsMember(source_p, chptr) || operspy)
209 do_who_on_channel(source_p, chptr, server_oper, YES, &fmt);
210 else if(!SecretChannel(chptr))
211 do_who_on_channel(source_p, chptr, server_oper, NO, &fmt);
212 }
213
214 sendto_one(source_p, form_str(RPL_ENDOFWHO),
215 me.name, source_p->name, parv[1] + operspy);
216 return 0;
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);
227 RB_DLINK_FOREACH(lp, target_p->user->channel.head)
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)
245 do_who(source_p, target_p, lp->data, &fmt);
246 else
247 do_who(source_p, target_p, NULL, &fmt);
248
249 sendto_one(source_p, form_str(RPL_ENDOFWHO),
250 me.name, source_p->name, mask);
251 return 0;
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 {
260 if((last_used + ConfigFileEntry.pace_wait) > rb_current_time() || !ratelimit_client(source_p, 1))
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, "*");
266 return 0;
267 }
268 else
269 last_used = rb_current_time();
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'))
282 who_global(source_p, NULL, server_oper, 0, &fmt);
283 else
284 who_global(source_p, mask, server_oper, operspy, &fmt);
285
286 sendto_one(source_p, form_str(RPL_ENDOFWHO),
287 me.name, source_p->name, mask);
288
289 return 0;
290 }
291
292 /* who_common_channel
293 * inputs - pointer to client requesting who
294 * - pointer to channel member chain.
295 * - char * mask to match
296 * - int if oper on a server or not
297 * - pointer to int maxmatches
298 * - format options
299 * output - NONE
300 * side effects - lists matching invisible clients on specified channel,
301 * marks matched clients.
302 */
303 static void
304 who_common_channel(struct Client *source_p, struct Channel *chptr,
305 const char *mask, int server_oper, int *maxmatches,
306 struct who_format *fmt)
307 {
308 struct membership *msptr;
309 struct Client *target_p;
310 rb_dlink_node *ptr;
311
312 RB_DLINK_FOREACH(ptr, chptr->members.head)
313 {
314 msptr = ptr->data;
315 target_p = msptr->client_p;
316
317 if(!IsInvisible(target_p) || IsMarked(target_p))
318 continue;
319
320 if(server_oper && !IsOper(target_p))
321 continue;
322
323 SetMark(target_p);
324
325 if(*maxmatches > 0)
326 {
327 if((mask == NULL) ||
328 match(mask, target_p->name) || match(mask, target_p->username) ||
329 match(mask, target_p->host) || match(mask, target_p->servptr->name) ||
330 (IsOper(source_p) && match(mask, target_p->orighost)) ||
331 match(mask, target_p->info))
332 {
333 do_who(source_p, target_p, NULL, fmt);
334 --(*maxmatches);
335 }
336 }
337 }
338 }
339
340 /*
341 * who_global
342 *
343 * inputs - pointer to client requesting who
344 * - char * mask to match
345 * - int if oper on a server or not
346 * - int if operspy or not
347 * - format options
348 * output - NONE
349 * side effects - do a global scan of all clients looking for match
350 * this is slightly expensive on EFnet ...
351 * marks assumed cleared for all clients initially
352 * and will be left cleared on return
353 */
354 static void
355 who_global(struct Client *source_p, const char *mask, int server_oper, int operspy, struct who_format *fmt)
356 {
357 struct membership *msptr;
358 struct Client *target_p;
359 rb_dlink_node *lp, *ptr;
360 int maxmatches = 500;
361
362 /* first, list all matching INvisible clients on common channels
363 * if this is not an operspy who
364 */
365 if(!operspy)
366 {
367 RB_DLINK_FOREACH(lp, source_p->user->channel.head)
368 {
369 msptr = lp->data;
370 who_common_channel(source_p, msptr->chptr, mask, server_oper, &maxmatches, fmt);
371 }
372 }
373 else if (!ConfigFileEntry.operspy_dont_care_user_info)
374 report_operspy(source_p, "WHO", mask);
375
376 /* second, list all matching visible clients and clear all marks
377 * on invisible clients
378 * if this is an operspy who, list all matching clients, no need
379 * to clear marks
380 */
381 RB_DLINK_FOREACH(ptr, global_client_list.head)
382 {
383 target_p = ptr->data;
384 if(!IsPerson(target_p))
385 continue;
386
387 if(IsInvisible(target_p) && !operspy)
388 {
389 ClearMark(target_p);
390 continue;
391 }
392
393 if(server_oper && !IsOper(target_p))
394 continue;
395
396 if(maxmatches > 0)
397 {
398 if(!mask ||
399 match(mask, target_p->name) || match(mask, target_p->username) ||
400 match(mask, target_p->host) || match(mask, target_p->servptr->name) ||
401 (IsOper(source_p) && match(mask, target_p->orighost)) ||
402 match(mask, target_p->info))
403 {
404 do_who(source_p, target_p, NULL, fmt);
405 --maxmatches;
406 }
407 }
408 }
409
410 if (maxmatches <= 0)
411 sendto_one(source_p,
412 form_str(ERR_TOOMANYMATCHES),
413 me.name, source_p->name, "WHO");
414 }
415
416 /*
417 * do_who_on_channel
418 *
419 * inputs - pointer to client requesting who
420 * - pointer to channel to do who on
421 * - The "real name" of this channel
422 * - int if source_p is a server oper or not
423 * - int if client is member or not
424 * - format options
425 * output - NONE
426 * side effects - do a who on given channel
427 */
428 static void
429 do_who_on_channel(struct Client *source_p, struct Channel *chptr,
430 int server_oper, int member, struct who_format *fmt)
431 {
432 struct Client *target_p;
433 struct membership *msptr;
434 rb_dlink_node *ptr;
435
436 RB_DLINK_FOREACH(ptr, chptr->members.head)
437 {
438 msptr = ptr->data;
439 target_p = msptr->client_p;
440
441 if(server_oper && !IsOper(target_p))
442 continue;
443
444 if(member || !IsInvisible(target_p))
445 do_who(source_p, target_p, msptr, fmt);
446 }
447 }
448
449 /*
450 * append_format
451 *
452 * inputs - pointer to buffer
453 * - size of buffer
454 * - pointer to position
455 * - format string
456 * - arguments for format
457 * output - NONE
458 * side effects - position incremented, possibly beyond size of buffer
459 * this allows detecting overflow
460 */
461 static void
462 append_format(char *buf, size_t bufsize, size_t *pos, const char *fmt, ...)
463 {
464 size_t max, result;
465 va_list ap;
466
467 max = *pos >= bufsize ? 0 : bufsize - *pos;
468 va_start(ap, fmt);
469 result = vsnprintf(buf + *pos, max, fmt, ap);
470 va_end(ap);
471 *pos += result;
472 }
473
474 /*
475 * do_who
476 *
477 * inputs - pointer to client requesting who
478 * - pointer to client to do who on
479 * - channel membership or NULL
480 * - format options
481 * output - NONE
482 * side effects - do a who on given person
483 */
484
485 static void
486 do_who(struct Client *source_p, struct Client *target_p, struct membership *msptr, struct who_format *fmt)
487 {
488 char status[16];
489 char str[510 + 1]; /* linebuf.c will add \r\n */
490 size_t pos;
491 const char *q;
492
493 sprintf(status, "%c%s%s",
494 target_p->user->away ? 'G' : 'H', IsOper(target_p) ? "*" : "", msptr ? find_channel_status(msptr, fmt->fields || IsCapable(source_p, CLICAP_MULTI_PREFIX)) : "");
495
496 if (fmt->fields == 0)
497 sendto_one(source_p, form_str(RPL_WHOREPLY), me.name,
498 source_p->name, msptr ? msptr->chptr->chname : "*",
499 target_p->username, target_p->host,
500 target_p->servptr->name, target_p->name, status,
501 ConfigServerHide.flatten_links && !IsOper(source_p) && !IsExemptShide(source_p) ? 0 : target_p->hopcount,
502 target_p->info);
503 else
504 {
505 str[0] = '\0';
506 pos = 0;
507 append_format(str, sizeof str, &pos, ":%s %d %s",
508 me.name, RPL_WHOSPCRPL, source_p->name);
509 if (fmt->fields & FIELD_QUERYTYPE)
510 append_format(str, sizeof str, &pos, " %s", fmt->querytype);
511 if (fmt->fields & FIELD_CHANNEL)
512 append_format(str, sizeof str, &pos, " %s", msptr ? msptr->chptr->chname : "*");
513 if (fmt->fields & FIELD_USER)
514 append_format(str, sizeof str, &pos, " %s", target_p->username);
515 if (fmt->fields & FIELD_IP)
516 {
517 if (show_ip(source_p, target_p) && !EmptyString(target_p->sockhost) && strcmp(target_p->sockhost, "0"))
518 append_format(str, sizeof str, &pos, " %s", target_p->sockhost);
519 else
520 append_format(str, sizeof str, &pos, " %s", "255.255.255.255");
521 }
522 if (fmt->fields & FIELD_HOST)
523 append_format(str, sizeof str, &pos, " %s", target_p->host);
524 if (fmt->fields & FIELD_SERVER)
525 append_format(str, sizeof str, &pos, " %s", target_p->servptr->name);
526 if (fmt->fields & FIELD_NICK)
527 append_format(str, sizeof str, &pos, " %s", target_p->name);
528 if (fmt->fields & FIELD_FLAGS)
529 append_format(str, sizeof str, &pos, " %s", status);
530 if (fmt->fields & FIELD_HOP)
531 append_format(str, sizeof str, &pos, " %d", ConfigServerHide.flatten_links && !IsOper(source_p) && !IsExemptShide(source_p) ? 0 : target_p->hopcount);
532 if (fmt->fields & FIELD_IDLE)
533 append_format(str, sizeof str, &pos, " %d", (int)(MyClient(target_p) ? rb_current_time() - target_p->localClient->last : 0));
534 if (fmt->fields & FIELD_ACCOUNT)
535 {
536 /* display as in whois */
537 q = target_p->user->suser;
538 if (!EmptyString(q))
539 {
540 while(IsDigit(*q))
541 q++;
542 if(*q == '\0')
543 q = target_p->user->suser;
544 }
545 else
546 q = "0";
547 append_format(str, sizeof str, &pos, " %s", q);
548 }
549 if (fmt->fields & FIELD_OPLEVEL)
550 append_format(str, sizeof str, &pos, " %s", is_chanop(msptr) ? "999" : "n/a");
551 if (fmt->fields & FIELD_INFO)
552 append_format(str, sizeof str, &pos, " :%s", target_p->info);
553
554 if (pos >= sizeof str)
555 {
556 static int warned = 0;
557 if (!warned)
558 sendto_realops_snomask(SNO_DEBUG, L_NETWIDE,
559 "WHOX overflow while sending information about %s to %s",
560 target_p->name, source_p->name);
561 warned = 1;
562 }
563 sendto_one(source_p, "%s", str);
564 }
565 }