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