]> jfr.im git - irc/quakenet/snircd.git/blame - ircd/m_who.c
merge with snircd head
[irc/quakenet/snircd.git] / ircd / m_who.c
CommitLineData
189935b1 1/*
2 * IRC - Internet Relay Chat, ircd/m_who.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
5 *
6 * See file AUTHORS in IRC package for additional names of
7 * the programmers.
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 1, or (at your option)
12 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
081bd606 23 * $Id: m_who.c,v 1.22.2.3 2007/08/14 03:54:48 entrope Exp $
189935b1 24 */
25
26/*
27 * m_functions execute protocol messages on this server:
28 *
29 * cptr is always NON-NULL, pointing to a *LOCAL* client
30 * structure (with an open socket connected!). This
31 * identifies the physical socket where the message
32 * originated (or which caused the m_function to be
33 * executed--some m_functions may call others...).
34 *
35 * sptr is the source of the message, defined by the
36 * prefix part of the message if present. If not
37 * or prefix not found, then sptr==cptr.
38 *
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
41 *
42 * (IsServer(cptr))
43 * (sptr == cptr) => the message didn't
44 * have the prefix.
45 *
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
48 *
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
51 * user (not local).
52 *
53 * combining
54 *
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
58 *
59 * *Always* true (if 'parse' and others are working correct):
60 *
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
62 *
63 * 2) MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64 * *cannot* be a local connection, unless it's
65 * actually cptr!). [MyConnect(x) should probably
66 * be defined as (x == x->from) --msa ]
67 *
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
70 *
71 * parv a NULL terminated list of parameter pointers,
72 *
73 * parv[0], sender (prefix string), if not present
74 * this points to an empty string.
75 * parv[1]...parv[parc-1]
76 * pointers to additional parameters
77 * parv[parc] == NULL, *always*
78 *
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
80 * non-NULL pointers.
81 */
82#include "config.h"
83
84#include "channel.h"
85#include "client.h"
86#include "hash.h"
87#include "ircd.h"
88#include "ircd_chattr.h"
89#include "ircd_features.h"
90#include "ircd_log.h"
91#include "ircd_reply.h"
92#include "ircd_string.h"
93#include "match.h"
94#include "numeric.h"
95#include "numnicks.h"
96#include "send.h"
97#include "whocmds.h"
98
99/* #include <assert.h> -- Now using assert in ircd_log.h */
100#include <string.h>
101
102
103/*
104 * A little spin-marking utility to tell us which clients we have already
105 * processed and which not
106 */
107static int who_marker = 0;
108static void move_marker(void)
109{
110 if (!++who_marker)
111 {
112 struct Client *cptr = GlobalClientList;
113 while (cptr)
114 {
115 cli_marker(cptr) = 0;
116 cptr = cli_next(cptr);
117 }
118 who_marker++;
119 }
120}
121
122#define CheckMark(x, y) ((x == y) ? 0 : (x = y))
123#define Process(cptr) CheckMark(cli_marker(cptr), who_marker)
124
125/*
126 * m_who - generic message handler
127 *
128 * parv[0] = sender prefix
129 * parv[1] = nickname mask list
130 * parv[2] = additional selection flag, only 'o' for now.
131 * and %flags to specify what fields to output
132 * plus a ,querytype if the t flag is specified
133 * so the final thing will be like o%tnchu,777
134 * parv[3] = _optional_ parameter that overrides parv[1]
135 * This can be used as "/quote who foo % :The Black Hacker
136 * to find me, parv[3] _can_ contain spaces !.
137 */
138int m_who(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
139{
140 char *mask; /* The mask we are looking for */
141 char ch; /* Scratch char register */
142 struct Channel *chptr; /* Channel to show */
143 struct Client *acptr; /* Client to show */
144
145 int bitsel; /* Mask of selectors to apply */
146 int matchsel; /* Which fields the match should apply on */
147 int counter; /* Query size counter,
148 initially used to count fields */
149 int commas; /* Does our mask contain any comma ?
150 If so is a list.. */
151 int fields; /* Mask of fields to show */
152 int isthere = 0; /* When this set the user is member of chptr */
153 char *nick; /* Single element extracted from
154 the mask list */
155 char *p; /* Scratch char pointer */
156 char *qrt; /* Pointer to the query type */
157 static char mymask[512]; /* To save the mask before corrupting it */
158
159 /* Let's find where is our mask, and if actually contains something */
160 mask = ((parc > 1) ? parv[1] : 0);
161 if (parc > 3 && parv[3])
162 mask = parv[3];
163 if (mask && ((mask[0] == '\0') ||
164 (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
165 mask = 0;
166
167 /* Evaluate the flags now, we consider the second parameter
168 as "matchFlags%fieldsToInclude,querytype" */
169 bitsel = fields = counter = matchsel = 0;
170 qrt = 0;
171 if (parc > 2 && parv[2] && *parv[2])
172 {
173 p = parv[2];
174 while (((ch = *(p++))) && (ch != '%') && (ch != ','))
175 switch (ch)
176 {
177 case 'd':
178 case 'D':
179 bitsel |= WHOSELECT_DELAY;
180 continue;
181 case 'o':
182 case 'O':
183 bitsel |= WHOSELECT_OPER;
184 continue;
185 case 'x':
186 case 'X':
187 bitsel |= WHOSELECT_EXTRA;
188 if (HasPriv(sptr, PRIV_WHOX))
189 log_write(LS_WHO, L_INFO, LOG_NOSNOTICE, "%#C WHO %s %s", sptr,
190 (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
191 continue;
192 case 'n':
193 case 'N':
194 matchsel |= WHO_FIELD_NIC;
195 continue;
196 case 'u':
197 case 'U':
198 matchsel |= WHO_FIELD_UID;
199 continue;
200 case 'h':
201 case 'H':
202 matchsel |= WHO_FIELD_HOS;
203 continue;
204 case 'i':
205 case 'I':
206 matchsel |= WHO_FIELD_NIP;
207 continue;
208 case 's':
209 case 'S':
210 matchsel |= WHO_FIELD_SER;
211 continue;
212 case 'r':
213 case 'R':
214 matchsel |= WHO_FIELD_REN;
215 continue;
216 case 'a':
217 case 'A':
218 matchsel |= WHO_FIELD_ACC;
219 continue;
220 }
221 if (ch == '%')
222 while ((ch = *p++) && (ch != ','))
223 {
224 counter++;
225 switch (ch)
226 {
227 case 'c':
228 case 'C':
229 fields |= WHO_FIELD_CHA;
230 break;
231 case 'd':
232 case 'D':
233 fields |= WHO_FIELD_DIS;
234 break;
235 case 'f':
236 case 'F':
237 fields |= WHO_FIELD_FLA;
238 break;
239 case 'h':
240 case 'H':
241 fields |= WHO_FIELD_HOS;
242 break;
243 case 'i':
244 case 'I':
245 fields |= WHO_FIELD_NIP;
246 break;
247 case 'l':
248 case 'L':
249 fields |= WHO_FIELD_IDL;
250 case 'n':
251 case 'N':
252 fields |= WHO_FIELD_NIC;
253 break;
254 case 'r':
255 case 'R':
256 fields |= WHO_FIELD_REN;
257 break;
258 case 's':
259 case 'S':
260 fields |= WHO_FIELD_SER;
261 break;
262 case 't':
263 case 'T':
264 fields |= WHO_FIELD_QTY;
265 break;
266 case 'u':
267 case 'U':
268 fields |= WHO_FIELD_UID;
269 break;
270 case 'a':
271 case 'A':
272 fields |= WHO_FIELD_ACC;
273 break;
0c466275 274 case 'o':
275 case 'O':
276 fields |= WHO_FIELD_OPL;
277 break;
189935b1 278 default:
279 break;
280 }
281 };
282 if (ch)
283 qrt = p;
284 }
285
286 if (!matchsel)
287 matchsel = WHO_FIELD_DEF;
288 if (!fields)
289 counter = 7;
290
291 if (feature_bool(FEAT_HIS_WHO_SERVERNAME) && !IsAnOper(sptr))
292 matchsel &= ~WHO_FIELD_SER;
293
d8e74551 294 if (feature_bool(FEAT_HIS_WHO_FILTERIP) && !IsAnOper(sptr))
295 matchsel &= ~WHO_FIELD_NIP;
296
189935b1 297 if (qrt && (fields & WHO_FIELD_QTY))
298 {
299 p = qrt;
300 if (!((*p > '9') || (*p < '0')))
301 p++;
302 if (!((*p > '9') || (*p < '0')))
303 p++;
304 if (!((*p > '9') || (*p < '0')))
305 p++;
306 *p = '\0';
307 }
308 else
309 qrt = 0;
310
311 /* I'd love to add also a check on the number of matches fields per time */
312 counter = (2048 / (counter + 4));
313 if (mask && (strlen(mask) > 510))
314 mask[510] = '\0';
315 move_marker();
316 commas = (mask && strchr(mask, ','));
317
318 /* First treat mask as a list of plain nicks/channels */
319 if (mask)
320 {
321 strcpy(mymask, mask);
322 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
323 nick = ircd_strtok(&p, 0, ","))
324 {
325 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
326 {
327 isthere = (find_channel_member(sptr, chptr) != 0);
328 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
329 {
330 struct Membership* member;
331 for (member = chptr->members; member; member = member->next_member)
332 {
333 acptr = member->user;
334 if ((bitsel & WHOSELECT_OPER) && !SeeOper(sptr,acptr))
335 continue;
336 if ((acptr != sptr)
337 && ((member->status & CHFL_ZOMBIE)
338 || ((member->status & CHFL_DELAYED)
339 && !(bitsel & WHOSELECT_DELAY))))
340 continue;
341 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
342 continue;
343 if (!Process(acptr)) /* This can't be moved before other checks */
344 continue;
345 if (!(isthere || (SHOW_MORE(sptr, counter))))
346 break;
347 do_who(sptr, acptr, chptr, fields, qrt);
348 }
349 }
350 }
351 else
352 {
353 if ((acptr = FindUser(nick)) &&
354 ((!(bitsel & WHOSELECT_OPER)) || SeeOper(sptr,acptr)) &&
355 Process(acptr) && SHOW_MORE(sptr, counter))
356 {
357 do_who(sptr, acptr, 0, fields, qrt);
358 }
359 }
360 }
361 }
362
363 /* If we didn't have any comma in the mask treat it as a
364 real mask and try to match all relevant fields */
365 if (!(commas || (counter < 1)))
366 {
367 struct irc_in_addr imask;
368 int minlen, cset;
369 unsigned char ibits;
370
371 if (mask)
372 {
373 matchcomp(mymask, &minlen, &cset, mask);
374 if (!ipmask_parse(mask, &imask, &ibits))
375 matchsel &= ~WHO_FIELD_NIP;
376 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
377 matchsel &= ~WHO_FIELD_NIC;
378 if ((matchsel & WHO_FIELD_SER) &&
379 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
380 || (!markMatchexServer(mymask, minlen))))
381 matchsel &= ~WHO_FIELD_SER;
382 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
383 matchsel &= ~WHO_FIELD_UID;
384 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
385 matchsel &= ~WHO_FIELD_HOS;
386 if ((minlen > ACCOUNTLEN))
387 matchsel &= ~WHO_FIELD_ACC;
388 }
389
390 /* First of all loop through the clients in common channels */
391 if ((!(counter < 1)) && matchsel) {
392 struct Membership* member;
393 struct Membership* chan;
394 for (chan = cli_user(sptr)->channel; chan; chan = chan->next_channel) {
395 chptr = chan->channel;
396 for (member = chptr->members; member; member = member->next_member)
397 {
398 acptr = member->user;
399 if (!(IsUser(acptr) && Process(acptr)))
400 continue; /* Now Process() is at the beginning, if we fail
401 we'll never have to show this acptr in this query */
402 if ((bitsel & WHOSELECT_OPER) && !SeeOper(sptr,acptr))
403 continue;
404 if ((mask) &&
405 ((!(matchsel & WHO_FIELD_NIC))
406 || matchexec(cli_name(acptr), mymask, minlen))
407 && ((!(matchsel & WHO_FIELD_UID))
408 || matchexec(cli_user(acptr)->username, mymask, minlen))
409 && ((!(matchsel & WHO_FIELD_SER))
410 || (!(HasFlag(cli_user(acptr)->server, FLAG_MAP))))
411 && ((!(matchsel & WHO_FIELD_HOS))
412 || matchexec(cli_user(acptr)->host, mymask, minlen))
413 && ((!(matchsel & WHO_FIELD_HOS))
d8e74551 414 || !HasSetHost(acptr)
189935b1 415 || !HasHiddenHost(acptr)
416 || !IsAnOper(sptr)
417 || matchexec(cli_user(acptr)->realhost, mymask, minlen))
418 && ((!(matchsel & WHO_FIELD_REN))
419 || matchexec(cli_info(acptr), mymask, minlen))
420 && ((!(matchsel & WHO_FIELD_NIP))
d8e74551 421 || ((HasHiddenHost(acptr) || HasSetHost(acptr)) && !IsAnOper(sptr))
189935b1 422 || !ipmask_check(&cli_ip(acptr), &imask, ibits))
423 && ((!(matchsel & WHO_FIELD_ACC))
424 || matchexec(cli_user(acptr)->account, mymask, minlen)))
425 continue;
426 if (!SHOW_MORE(sptr, counter))
427 break;
428 do_who(sptr, acptr, chptr, fields, qrt);
429 }
430 }
431 }
432 /* Loop through all clients :-\, if we still have something to match to
433 and we can show more clients */
434 if ((!(counter < 1)) && matchsel)
435 for (acptr = cli_prev(&me); acptr; acptr = cli_prev(acptr))
436 {
437 if (!(IsUser(acptr) && Process(acptr)))
438 continue;
439 if ((bitsel & WHOSELECT_OPER) && !SeeOper(sptr,acptr))
440 continue;
441 if (!(SEE_USER(sptr, acptr, bitsel)))
442 continue;
443 if ((mask) &&
444 ((!(matchsel & WHO_FIELD_NIC))
445 || matchexec(cli_name(acptr), mymask, minlen))
446 && ((!(matchsel & WHO_FIELD_UID))
447 || matchexec(cli_user(acptr)->username, mymask, minlen))
448 && ((!(matchsel & WHO_FIELD_SER))
449 || (!(HasFlag(cli_user(acptr)->server, FLAG_MAP))))
450 && ((!(matchsel & WHO_FIELD_HOS))
451 || matchexec(cli_user(acptr)->host, mymask, minlen))
452 && ((!(matchsel & WHO_FIELD_HOS))
d8e74551 453 || !HasSetHost(acptr)
189935b1 454 || !HasHiddenHost(acptr)
455 || !IsAnOper(sptr)
456 || matchexec(cli_user(acptr)->realhost, mymask, minlen))
457 && ((!(matchsel & WHO_FIELD_REN))
458 || matchexec(cli_info(acptr), mymask, minlen))
459 && ((!(matchsel & WHO_FIELD_NIP))
d8e74551 460 || ((HasHiddenHost(acptr) || HasSetHost(acptr)) && !IsAnOper(sptr))
189935b1 461 || !ipmask_check(&cli_ip(acptr), &imask, ibits))
462 && ((!(matchsel & WHO_FIELD_ACC))
463 || matchexec(cli_user(acptr)->account, mymask, minlen)))
464 continue;
465 if (!SHOW_MORE(sptr, counter))
466 break;
467 do_who(sptr, acptr, 0, fields, qrt);
468 }
469 }
470
471 /* Make a clean mask suitable to be sent in the "end of" */
472 if (mask && (p = strchr(mask, ' ')))
473 *p = '\0';
189935b1 474 /* Notify the user if we decided that his query was too long */
475 if (counter < 0)
081bd606 476 send_reply(sptr, ERR_QUERYTOOLONG, BadPtr(mask) ? "*" : mask);
477 send_reply(sptr, RPL_ENDOFWHO, BadPtr(mask) ? "*" : mask);
189935b1 478
479 return 0;
480}