1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file m_who.c |
23 |
* \brief Includes required functions for processing the WHO command. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "client.h" |
30 |
#include "channel.h" |
31 |
#include "channel_mode.h" |
32 |
#include "hash.h" |
33 |
#include "ircd.h" |
34 |
#include "numeric.h" |
35 |
#include "s_serv.h" |
36 |
#include "send.h" |
37 |
#include "irc_string.h" |
38 |
#include "conf.h" |
39 |
#include "parse.h" |
40 |
#include "modules.h" |
41 |
|
42 |
|
43 |
/* do_who() |
44 |
* |
45 |
* inputs - pointer to client requesting who |
46 |
* - pointer to client to do who on |
47 |
* - The reported name |
48 |
* - channel flags |
49 |
* output - NONE |
50 |
* side effects - do a who on given person |
51 |
*/ |
52 |
static void |
53 |
do_who(struct Client *source_p, struct Client *target_p, |
54 |
const char *chname, const char *op_flags) |
55 |
{ |
56 |
char status[IRCD_BUFSIZE] = ""; |
57 |
|
58 |
if (HasUMode(source_p, UMODE_OPER)) |
59 |
snprintf(status, sizeof(status), "%c%s%s%s", target_p->away[0] ? 'G' : 'H', |
60 |
HasUMode(target_p, UMODE_REGISTERED) ? "r" : "", |
61 |
HasUMode(target_p, UMODE_OPER) ? "*" : "", op_flags); |
62 |
else |
63 |
snprintf(status, sizeof(status), "%c%s%s%s", target_p->away[0] ? 'G' : 'H', |
64 |
HasUMode(target_p, UMODE_REGISTERED) ? "r" : "", |
65 |
HasUMode(target_p, UMODE_OPER) && |
66 |
!HasUMode(target_p, UMODE_HIDDEN) ? "*" : "", op_flags); |
67 |
|
68 |
if (ConfigServerHide.hide_servers || IsHidden(target_p->servptr)) |
69 |
sendto_one_numeric(source_p, &me, RPL_WHOREPLY, |
70 |
(chname) ? (chname) : "*", |
71 |
target_p->username, target_p->host, |
72 |
HasUMode(source_p, UMODE_OPER) ? target_p->servptr->name : "*", |
73 |
target_p->name, status, |
74 |
HasUMode(source_p, UMODE_OPER) ? target_p->hopcount : 0, target_p->info); |
75 |
else |
76 |
sendto_one_numeric(source_p, &me, RPL_WHOREPLY, |
77 |
(chname) ? (chname) : "*", target_p->username, |
78 |
target_p->host, target_p->servptr->name, target_p->name, |
79 |
status, target_p->hopcount, target_p->info); |
80 |
} |
81 |
|
82 |
/* who_common_channel |
83 |
* inputs - pointer to client requesting who |
84 |
* - pointer to channel member chain. |
85 |
* - char * mask to match |
86 |
* - int if oper on a server or not |
87 |
* - pointer to int maxmatches |
88 |
* output - NONE |
89 |
* side effects - lists matching clients on specified channel, |
90 |
* marks matched clients. |
91 |
* |
92 |
*/ |
93 |
static void |
94 |
who_common_channel(struct Client *source_p, struct Channel *chptr, |
95 |
char *mask, int server_oper, int *maxmatches) |
96 |
{ |
97 |
dlink_node *ptr = NULL; |
98 |
|
99 |
DLINK_FOREACH(ptr, chptr->members.head) |
100 |
{ |
101 |
struct Client *target_p = ((struct Membership *)ptr->data)->client_p; |
102 |
|
103 |
if (!HasUMode(target_p, UMODE_INVISIBLE) || HasFlag(target_p, FLAGS_MARK)) |
104 |
continue; |
105 |
|
106 |
if (server_oper) |
107 |
if (!HasUMode(target_p, UMODE_OPER) || |
108 |
(HasUMode(target_p, UMODE_HIDDEN) && !HasUMode(source_p, UMODE_OPER))) |
109 |
continue; |
110 |
|
111 |
AddFlag(target_p, FLAGS_MARK); |
112 |
|
113 |
if ((mask == NULL) || |
114 |
!match(mask, target_p->name) || !match(mask, target_p->username) || |
115 |
!match(mask, target_p->host) || |
116 |
((!ConfigServerHide.hide_servers || HasUMode(source_p, UMODE_OPER)) && |
117 |
!match(mask, target_p->servptr->name)) || |
118 |
!match(mask, target_p->info)) |
119 |
{ |
120 |
do_who(source_p, target_p, NULL, ""); |
121 |
|
122 |
if (*maxmatches > 0) |
123 |
{ |
124 |
if (--(*maxmatches) == 0) |
125 |
return; |
126 |
} |
127 |
} |
128 |
} |
129 |
} |
130 |
|
131 |
/* who_global() |
132 |
* |
133 |
* inputs - pointer to client requesting who |
134 |
* - char * mask to match |
135 |
* - int if oper on a server or not |
136 |
* output - NONE |
137 |
* side effects - do a global scan of all clients looking for match |
138 |
* this is slightly expensive on EFnet ... |
139 |
*/ |
140 |
static void |
141 |
who_global(struct Client *source_p, char *mask, int server_oper) |
142 |
{ |
143 |
struct Channel *chptr; |
144 |
struct Client *target_p; |
145 |
dlink_node *lp = NULL, *gcptr = NULL; |
146 |
int maxmatches = 500; |
147 |
static time_t last_used = 0; |
148 |
|
149 |
if (!HasUMode(source_p, UMODE_OPER)) |
150 |
{ |
151 |
if ((last_used + ConfigFileEntry.pace_wait) > CurrentTime) |
152 |
{ |
153 |
sendto_one_numeric(source_p, &me, RPL_LOAD2HI); |
154 |
return; |
155 |
} |
156 |
|
157 |
last_used = CurrentTime; |
158 |
} |
159 |
|
160 |
/* First, list all matching invisible clients on common channels */ |
161 |
DLINK_FOREACH(lp, source_p->channel.head) |
162 |
{ |
163 |
chptr = ((struct Membership *)lp->data)->chptr; |
164 |
who_common_channel(source_p, chptr, mask, server_oper, &maxmatches); |
165 |
} |
166 |
|
167 |
/* Second, list all matching visible clients */ |
168 |
DLINK_FOREACH(gcptr, global_client_list.head) |
169 |
{ |
170 |
target_p = gcptr->data; |
171 |
|
172 |
if (!IsClient(target_p)) |
173 |
continue; |
174 |
|
175 |
if (HasUMode(target_p, UMODE_INVISIBLE)) |
176 |
{ |
177 |
DelFlag(target_p, FLAGS_MARK); |
178 |
continue; |
179 |
} |
180 |
|
181 |
if (server_oper) |
182 |
if (!HasUMode(target_p, UMODE_OPER) || |
183 |
(HasUMode(target_p, UMODE_HIDDEN) && !HasUMode(source_p, UMODE_OPER))) |
184 |
continue; |
185 |
|
186 |
if (!mask || |
187 |
!match(mask, target_p->name) || !match(mask, target_p->username) || |
188 |
!match(mask, target_p->host) || !match(mask, target_p->servptr->name) || |
189 |
!match(mask, target_p->info)) |
190 |
{ |
191 |
do_who(source_p, target_p, NULL, ""); |
192 |
|
193 |
if (maxmatches > 0) |
194 |
{ |
195 |
if (--maxmatches == 0) |
196 |
return; |
197 |
} |
198 |
} |
199 |
} |
200 |
} |
201 |
|
202 |
/* do_who_on_channel() |
203 |
* |
204 |
* inputs - pointer to client requesting who |
205 |
* - pointer to channel to do who on |
206 |
* - The "real name" of this channel |
207 |
* - int if source_p is a server oper or not |
208 |
* - int if client is member or not |
209 |
* - int server_op flag |
210 |
* output - NONE |
211 |
* side effects - do a who on given channel |
212 |
*/ |
213 |
static void |
214 |
do_who_on_channel(struct Client *source_p, struct Channel *chptr, |
215 |
const char *chname, int member, int server_oper) |
216 |
{ |
217 |
dlink_node *ptr = NULL; |
218 |
|
219 |
DLINK_FOREACH(ptr, chptr->members.head) |
220 |
{ |
221 |
struct Membership *ms = ptr->data; |
222 |
struct Client *target_p = ms->client_p; |
223 |
|
224 |
if (member || !HasUMode(target_p, UMODE_INVISIBLE)) |
225 |
{ |
226 |
if (server_oper) |
227 |
if (!HasUMode(target_p, UMODE_OPER) || |
228 |
(HasUMode(target_p, UMODE_HIDDEN) && !HasUMode(source_p, UMODE_OPER))) |
229 |
continue; |
230 |
do_who(source_p, target_p, chname, get_member_status(ms, !!HasCap(source_p, CAP_MULTI_PREFIX))); |
231 |
} |
232 |
} |
233 |
} |
234 |
|
235 |
/*! \brief WHO command handler |
236 |
* |
237 |
* \param source_p Pointer to allocated Client struct from which the message |
238 |
* originally comes from. This can be a local or remote client. |
239 |
* \param parc Integer holding the number of supplied arguments. |
240 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
241 |
* pointers. |
242 |
* \note Valid arguments for this command are: |
243 |
* - parv[0] = command |
244 |
* - parv[1] = nickname/channelname |
245 |
* - parv[2] = additional selection flag, only 'o' for now |
246 |
*/ |
247 |
static int |
248 |
m_who(struct Client *source_p, int parc, char *parv[]) |
249 |
{ |
250 |
struct Client *target_p = NULL; |
251 |
struct Channel *chptr = NULL; |
252 |
char *mask = parv[1]; |
253 |
dlink_node *ptr = NULL; |
254 |
int server_oper = parc > 2 ? (*parv[2] == 'o') : 0; /* Show OPERS only */ |
255 |
|
256 |
/* See if mask is there, collapse it or return if not there */ |
257 |
if (EmptyString(mask)) |
258 |
{ |
259 |
who_global(source_p, mask, server_oper); |
260 |
sendto_one_numeric(source_p, &me, RPL_ENDOFWHO, "*"); |
261 |
return 0; |
262 |
} |
263 |
|
264 |
/* Mask isn't NULL at this point. repeat after me... -db */ |
265 |
collapse(mask); |
266 |
|
267 |
/* '/who #some_channel' */ |
268 |
if (IsChanPrefix(*mask)) |
269 |
{ |
270 |
/* List all users on a given channel */ |
271 |
if ((chptr = hash_find_channel(mask))) |
272 |
{ |
273 |
if (IsMember(source_p, chptr) || HasUMode(source_p, UMODE_ADMIN)) |
274 |
do_who_on_channel(source_p, chptr, chptr->chname, 1, server_oper); |
275 |
else if (!SecretChannel(chptr)) |
276 |
do_who_on_channel(source_p, chptr, chptr->chname, 0, server_oper); |
277 |
} |
278 |
|
279 |
sendto_one_numeric(source_p, &me, RPL_ENDOFWHO, mask); |
280 |
return 0; |
281 |
} |
282 |
|
283 |
/* '/who nick' */ |
284 |
if (((target_p = hash_find_client(mask))) && |
285 |
IsClient(target_p) && (!server_oper || HasUMode(target_p, UMODE_OPER))) |
286 |
{ |
287 |
DLINK_FOREACH(ptr, target_p->channel.head) |
288 |
{ |
289 |
chptr = ((struct Membership *)ptr->data)->chptr; |
290 |
if (PubChannel(chptr) || IsMember(source_p, chptr)) |
291 |
break; |
292 |
} |
293 |
|
294 |
if (ptr) |
295 |
do_who(source_p, target_p, chptr->chname, |
296 |
get_member_status(ptr->data, !!HasCap(source_p, CAP_MULTI_PREFIX))); |
297 |
else |
298 |
do_who(source_p, target_p, NULL, ""); |
299 |
|
300 |
sendto_one_numeric(source_p, &me, RPL_ENDOFWHO, mask); |
301 |
return 0; |
302 |
} |
303 |
|
304 |
/* '/who *' */ |
305 |
if (!strcmp(mask, "*")) |
306 |
{ |
307 |
if ((ptr = source_p->channel.head)) |
308 |
{ |
309 |
chptr = ((struct Membership *)ptr->data)->chptr; |
310 |
do_who_on_channel(source_p, chptr, chptr->chname, 1, |
311 |
server_oper); |
312 |
} |
313 |
|
314 |
sendto_one_numeric(source_p, &me, RPL_ENDOFWHO, "*"); |
315 |
return 0; |
316 |
} |
317 |
|
318 |
/* '/who 0' */ |
319 |
if (!strcmp(mask, "0")) |
320 |
who_global(source_p, NULL, server_oper); |
321 |
else |
322 |
who_global(source_p, mask, server_oper); |
323 |
|
324 |
/* Wasn't a nick, wasn't a channel, wasn't a '*' so ... */ |
325 |
sendto_one_numeric(source_p, &me, RPL_ENDOFWHO, mask); |
326 |
return 0; |
327 |
} |
328 |
|
329 |
static struct Message who_msgtab = |
330 |
{ |
331 |
"WHO", 0, 0, 2, MAXPARA, MFLG_SLOW, 0, |
332 |
{ m_unregistered, m_who, m_ignore, m_ignore, m_who, m_ignore } |
333 |
}; |
334 |
|
335 |
static void |
336 |
module_init(void) |
337 |
{ |
338 |
mod_add_cmd(&who_msgtab); |
339 |
} |
340 |
|
341 |
static void |
342 |
module_exit(void) |
343 |
{ |
344 |
mod_del_cmd(&who_msgtab); |
345 |
} |
346 |
|
347 |
struct module module_entry = |
348 |
{ |
349 |
.node = { NULL, NULL, NULL }, |
350 |
.name = NULL, |
351 |
.version = "$Revision$", |
352 |
.handle = NULL, |
353 |
.modinit = module_init, |
354 |
.modexit = module_exit, |
355 |
.flags = 0 |
356 |
}; |