]> jfr.im git - solanum.git/blob - modules/m_who.c
ircd: handle some EXIT_FAILURE cases differently on win32
[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 static const char who_desc[] =
59 "Provides the WHO command to display information for users on a channel";
60
61 struct who_format
62 {
63 int fields;
64 const char *querytype;
65 };
66
67 static void m_who(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
68
69 static void do_who_on_channel(struct Client *source_p, struct Channel *chptr,
70 int server_oper, int member,
71 struct who_format *fmt);
72 static void who_global(struct Client *source_p, const char *mask, int server_oper, int operspy, struct who_format *fmt);
73 static void do_who(struct Client *source_p,
74 struct Client *target_p, struct membership *msptr,
75 struct who_format *fmt);
76
77 struct Message who_msgtab = {
78 "WHO", 0, 0, 0, 0,
79 {mg_unreg, {m_who, 2}, mg_ignore, mg_ignore, mg_ignore, {m_who, 2}}
80 };
81
82 static int
83 _modinit(void)
84 {
85 add_isupport("WHOX", isupport_string, "");
86 return 0;
87 }
88
89 static void
90 _moddeinit(void)
91 {
92 delete_isupport("WHOX");
93 }
94
95 mapi_clist_av1 who_clist[] = { &who_msgtab, NULL };
96 DECLARE_MODULE_AV2(who, _modinit, _moddeinit, who_clist, NULL, NULL, NULL, NULL, who_desc);
97
98 /*
99 ** m_who
100 ** parv[1] = nickname mask list
101 ** parv[2] = additional selection flag and format options
102 */
103 static void
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;
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, true, &fmt);
168 }
169
170 sendto_one(source_p, form_str(RPL_ENDOFWHO),
171 me.name, source_p->name, "*");
172 return;
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;
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;
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, true, &fmt);
210 else if(!SecretChannel(chptr))
211 do_who_on_channel(source_p, chptr, server_oper, false, &fmt);
212 }
213
214 sendto_one(source_p, form_str(RPL_ENDOFWHO),
215 me.name, source_p->name, parv[1] + operspy);
216 return;
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;
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;
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
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
296 * - format options
297 * output - NONE
298 * side effects - lists matching invisible clients on specified channel,
299 * marks matched clients.
300 */
301 static void
302 who_common_channel(struct Client *source_p, struct Channel *chptr,
303 const char *mask, int server_oper, int *maxmatches,
304 struct who_format *fmt)
305 {
306 struct membership *msptr;
307 struct Client *target_p;
308 rb_dlink_node *ptr;
309
310 RB_DLINK_FOREACH(ptr, chptr->members.head)
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) ||
327 match(mask, target_p->host) || match(mask, target_p->servptr->name) ||
328 (IsOper(source_p) && match(mask, target_p->orighost)) ||
329 match(mask, target_p->info))
330 {
331 do_who(source_p, target_p, NULL, fmt);
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
344 * - int if operspy or not
345 * - format options
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 */
352 static void
353 who_global(struct Client *source_p, const char *mask, int server_oper, int operspy, struct who_format *fmt)
354 {
355 struct membership *msptr;
356 struct Client *target_p;
357 rb_dlink_node *lp, *ptr;
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 {
365 RB_DLINK_FOREACH(lp, source_p->user->channel.head)
366 {
367 msptr = lp->data;
368 who_common_channel(source_p, msptr->chptr, mask, server_oper, &maxmatches, fmt);
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 */
379 RB_DLINK_FOREACH(ptr, global_client_list.head)
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) ||
398 match(mask, target_p->host) || match(mask, target_p->servptr->name) ||
399 (IsOper(source_p) && match(mask, target_p->orighost)) ||
400 match(mask, target_p->info))
401 {
402 do_who(source_p, target_p, NULL, fmt);
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
422 * - format options
423 * output - NONE
424 * side effects - do a who on given channel
425 */
426 static void
427 do_who_on_channel(struct Client *source_p, struct Channel *chptr,
428 int server_oper, int member, struct who_format *fmt)
429 {
430 struct Client *target_p;
431 struct membership *msptr;
432 rb_dlink_node *ptr;
433
434 RB_DLINK_FOREACH(ptr, chptr->members.head)
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))
443 do_who(source_p, target_p, msptr, fmt);
444 }
445 }
446
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 */
459 static void
460 append_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);
467 result = vsnprintf(buf + *pos, max, fmt, ap);
468 va_end(ap);
469 *pos += result;
470 }
471
472 /*
473 * do_who
474 *
475 * inputs - pointer to client requesting who
476 * - pointer to client to do who on
477 * - channel membership or NULL
478 * - format options
479 * output - NONE
480 * side effects - do a who on given person
481 */
482
483 static void
484 do_who(struct Client *source_p, struct Client *target_p, struct membership *msptr, struct who_format *fmt)
485 {
486 char status[16];
487 char str[510 + 1]; /* linebuf.c will add \r\n */
488 size_t pos;
489 const char *q;
490
491 sprintf(status, "%c%s%s",
492 target_p->user->away ? 'G' : 'H', IsOper(target_p) ? "*" : "", msptr ? find_channel_status(msptr, fmt->fields || IsCapable(source_p, CLICAP_MULTI_PREFIX)) : "");
493
494 if (fmt->fields == 0)
495 sendto_one(source_p, form_str(RPL_WHOREPLY), me.name,
496 source_p->name, msptr ? msptr->chptr->chname : "*",
497 target_p->username, target_p->host,
498 target_p->servptr->name, target_p->name, status,
499 ConfigServerHide.flatten_links && !IsOper(source_p) && !IsExemptShide(source_p) ? 0 : target_p->hopcount,
500 target_p->info);
501 else
502 {
503 str[0] = '\0';
504 pos = 0;
505 append_format(str, sizeof str, &pos, ":%s %d %s",
506 me.name, RPL_WHOSPCRPL, source_p->name);
507 if (fmt->fields & FIELD_QUERYTYPE)
508 append_format(str, sizeof str, &pos, " %s", fmt->querytype);
509 if (fmt->fields & FIELD_CHANNEL)
510 append_format(str, sizeof str, &pos, " %s", msptr ? msptr->chptr->chname : "*");
511 if (fmt->fields & FIELD_USER)
512 append_format(str, sizeof str, &pos, " %s", target_p->username);
513 if (fmt->fields & FIELD_IP)
514 {
515 if (show_ip(source_p, target_p) && !EmptyString(target_p->sockhost) && strcmp(target_p->sockhost, "0"))
516 append_format(str, sizeof str, &pos, " %s", target_p->sockhost);
517 else
518 append_format(str, sizeof str, &pos, " %s", "255.255.255.255");
519 }
520 if (fmt->fields & FIELD_HOST)
521 append_format(str, sizeof str, &pos, " %s", target_p->host);
522 if (fmt->fields & FIELD_SERVER)
523 append_format(str, sizeof str, &pos, " %s", target_p->servptr->name);
524 if (fmt->fields & FIELD_NICK)
525 append_format(str, sizeof str, &pos, " %s", target_p->name);
526 if (fmt->fields & FIELD_FLAGS)
527 append_format(str, sizeof str, &pos, " %s", status);
528 if (fmt->fields & FIELD_HOP)
529 append_format(str, sizeof str, &pos, " %d", ConfigServerHide.flatten_links && !IsOper(source_p) && !IsExemptShide(source_p) ? 0 : target_p->hopcount);
530 if (fmt->fields & FIELD_IDLE)
531 append_format(str, sizeof str, &pos, " %d", (int)(MyClient(target_p) ? rb_current_time() - target_p->localClient->last : 0));
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";
545 append_format(str, sizeof str, &pos, " %s", q);
546 }
547 if (fmt->fields & FIELD_OPLEVEL)
548 append_format(str, sizeof str, &pos, " %s", is_chanop(msptr) ? "999" : "n/a");
549 if (fmt->fields & FIELD_INFO)
550 append_format(str, sizeof str, &pos, " :%s", target_p->info);
551
552 if (pos >= sizeof str)
553 {
554 static bool warned = false;
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);
559 warned = true;
560 }
561 sendto_one(source_p, "%s", str);
562 }
563 }