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