]> jfr.im git - solanum.git/blob - modules/m_who.c
Add ircu-like /who format options.
[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
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 Client *, struct Client *, int, const char **);
65
66 struct 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
71 mapi_clist_av1 who_clist[] = { &who_msgtab, NULL };
72 DECLARE_MODULE_AV1(who, NULL, NULL, who_clist, NULL, NULL, "$Revision: 3350 $");
73
74 static void do_who_on_channel(struct Client *source_p, struct Channel *chptr,
75 int server_oper, int member,
76 struct who_format *fmt);
77
78 static void who_global(struct Client *source_p, const char *mask, int server_oper, int operspy, struct who_format *fmt);
79
80 static void do_who(struct Client *source_p,
81 struct Client *target_p, const char *chname, const char *op_flags, struct who_format *fmt);
82
83
84 /*
85 ** m_who
86 ** parv[0] = sender prefix
87 ** parv[1] = nickname mask list
88 ** parv[2] = additional selection flag and format options
89 */
90 static int
91 m_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;
97 rb_dlink_node *lp;
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;
102 struct who_format fmt;
103 const char *s;
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 mask = LOCAL_COPY(parv[1]);
140
141 collapse(mask);
142
143 /* '/who *' */
144 if((*(mask + 1) == '\0') && (*mask == '*'))
145 {
146 if(source_p->user == NULL)
147 return 0;
148
149 if((lp = source_p->user->channel.head) != NULL)
150 {
151 msptr = lp->data;
152 do_who_on_channel(source_p, msptr->chptr, server_oper, YES, &fmt);
153 }
154
155 sendto_one(source_p, form_str(RPL_ENDOFWHO),
156 me.name, source_p->name, "*");
157 return 0;
158 }
159
160 if(IsOperSpy(source_p) && *mask == '!')
161 {
162 mask++;
163 operspy = 1;
164
165 if(EmptyString(mask))
166 {
167 sendto_one(source_p, form_str(RPL_ENDOFWHO),
168 me.name, source_p->name, parv[1]);
169 return 0;
170 }
171 }
172
173 /* '/who #some_channel' */
174 if(IsChannelName(mask))
175 {
176 /* List all users on a given channel */
177 chptr = find_channel(parv[1] + operspy);
178 if(chptr != NULL)
179 {
180 if(operspy)
181 report_operspy(source_p, "WHO", chptr->chname);
182
183 if(IsMember(source_p, chptr) || operspy)
184 do_who_on_channel(source_p, chptr, server_oper, YES, &fmt);
185 else if(!SecretChannel(chptr))
186 do_who_on_channel(source_p, chptr, server_oper, NO, &fmt);
187 }
188 sendto_one(source_p, form_str(RPL_ENDOFWHO),
189 me.name, source_p->name, parv[1] + operspy);
190 return 0;
191 }
192
193 /* '/who nick' */
194
195 if(((target_p = find_named_person(mask)) != NULL) &&
196 (!server_oper || IsOper(target_p)))
197 {
198 int isinvis = 0;
199
200 isinvis = IsInvisible(target_p);
201 RB_DLINK_FOREACH(lp, target_p->user->channel.head)
202 {
203 msptr = lp->data;
204 chptr = msptr->chptr;
205
206 member = IsMember(source_p, chptr);
207
208 if(isinvis && !member)
209 continue;
210
211 if(member || (!isinvis && PubChannel(chptr)))
212 break;
213 }
214
215 /* if we stopped midlist, lp->data is the membership for
216 * target_p of chptr
217 */
218 if(lp != NULL)
219 do_who(source_p, target_p, chptr->chname,
220 find_channel_status(lp->data, IsCapable(source_p, CLICAP_MULTI_PREFIX)), &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 int combine = IsCapable(source_p, CLICAP_MULTI_PREFIX);
411
412 RB_DLINK_FOREACH(ptr, chptr->members.head)
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))
421 do_who(source_p, target_p, chptr->chname,
422 find_channel_status(msptr, combine), fmt);
423 }
424 }
425
426 /*
427 * do_who
428 *
429 * inputs - pointer to client requesting who
430 * - pointer to client to do who on
431 * - The reported name
432 * - channel flags
433 * - format options
434 * output - NONE
435 * side effects - do a who on given person
436 */
437
438 static void
439 do_who(struct Client *source_p, struct Client *target_p, const char *chname, const char *op_flags, struct who_format *fmt)
440 {
441 char status[5];
442 char str[512], *p, *end;
443 const char *q;
444
445 rb_sprintf(status, "%c%s%s",
446 target_p->user->away ? 'G' : 'H', IsOper(target_p) ? "*" : "", op_flags);
447
448 if (fmt->fields == 0)
449 sendto_one(source_p, form_str(RPL_WHOREPLY), me.name,
450 source_p->name, (chname) ? (chname) : "*",
451 target_p->username, target_p->host,
452 target_p->servptr->name, target_p->name, status,
453 ConfigServerHide.flatten_links ? 0 : target_p->hopcount,
454 target_p->info);
455 else
456 {
457 str[0] = '\0';
458 p = str;
459 end = str + sizeof str;
460 if (fmt->fields & FIELD_QUERYTYPE)
461 p += rb_snprintf(p, end - p, " %s", fmt->querytype);
462 if (fmt->fields & FIELD_CHANNEL)
463 p += rb_snprintf(p, end - p, " %s", (chname) ? (chname) : "*");
464 if (fmt->fields & FIELD_USER)
465 p += rb_snprintf(p, end - p, " %s", target_p->username);
466 if (fmt->fields & FIELD_IP)
467 {
468 if (show_ip(source_p, target_p) && !EmptyString(target_p->sockhost) && strcmp(target_p->sockhost, "0"))
469 p += rb_snprintf(p, end - p, " %s", target_p->sockhost);
470 else
471 p += rb_snprintf(p, end - p, " %s", "255.255.255.255");
472 }
473 if (fmt->fields & FIELD_HOST)
474 p += rb_snprintf(p, end - p, " %s", target_p->host);
475 if (fmt->fields & FIELD_SERVER)
476 p += rb_snprintf(p, end - p, " %s", target_p->servptr->name);
477 if (fmt->fields & FIELD_NICK)
478 p += rb_snprintf(p, end - p, " %s", target_p->name);
479 if (fmt->fields & FIELD_FLAGS)
480 p += rb_snprintf(p, end - p, " %s", status);
481 if (fmt->fields & FIELD_HOP)
482 p += rb_snprintf(p, end - p, " %d", ConfigServerHide.flatten_links ? 0 : target_p->hopcount);
483 if (fmt->fields & FIELD_IDLE)
484 p += rb_snprintf(p, end - p, " %d", MyClient(target_p) ? rb_current_time() - target_p->localClient->last : 0);
485 if (fmt->fields & FIELD_ACCOUNT)
486 {
487 /* display as in whois */
488 q = target_p->user->suser;
489 if (!EmptyString(q))
490 {
491 while(IsDigit(*q))
492 q++;
493 if(*q == '\0')
494 q = target_p->user->suser;
495 }
496 else
497 q = "0";
498 p += rb_snprintf(p, end - p, " %s", q);
499 }
500 if (fmt->fields & FIELD_OPLEVEL)
501 p += rb_snprintf(p, end - p, " %s", *op_flags == '@' ? "999" : "n/a");
502 if (fmt->fields & FIELD_INFO)
503 p += rb_snprintf(p, end - p, " :%s", target_p->info);
504 sendto_one_numeric(source_p, RPL_WHOSPCRPL, "%s", str + 1);
505 }
506 }