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