Clean.
[seven-1.x.git] / modules / m_whois.c
blob24159abbb1f454965526aabe2d53cb11693d2334
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_whois.c: Shows who a user is.
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
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.
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.
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
24 * $Id: m_whois.c 148 2006-11-13 13:08:18Z spb $
27 #include "stdinc.h"
28 #include "tools.h"
29 #include "common.h"
30 #include "client.h"
31 #include "hash.h"
32 #include "channel.h"
33 #include "hash.h"
34 #include "ircd.h"
35 #include "numeric.h"
36 #include "s_conf.h"
37 #include "s_serv.h"
38 #include "send.h"
39 #include "irc_string.h"
40 #include "sprintf_irc.h"
41 #include "s_conf.h"
42 #include "s_log.h"
43 #include "msg.h"
44 #include "parse.h"
45 #include "modules.h"
46 #include "hook.h"
47 #include "s_newconf.h"
49 static void do_whois(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
50 static void single_whois(struct Client *source_p, struct Client *target_p, int auspex);
52 static int m_whois(struct Client *, struct Client *, int, const char **);
53 static int ms_whois(struct Client *, struct Client *, int, const char **);
55 struct Message whois_msgtab = {
56 "WHOIS", 0, 0, 0, MFLG_SLOW,
57 {mg_unreg, {m_whois, 2}, {ms_whois, 2}, mg_ignore, mg_ignore, {m_whois, 2}}
60 int doing_whois_hook;
61 int doing_whois_global_hook;
63 mapi_clist_av1 whois_clist[] = { &whois_msgtab, NULL };
64 mapi_hlist_av1 whois_hlist[] = {
65 { "doing_whois", &doing_whois_hook },
66 { "doing_whois_global", &doing_whois_global_hook },
67 { NULL, NULL }
70 DECLARE_MODULE_AV1(whois, NULL, NULL, whois_clist, whois_hlist, NULL, "$Revision: 148 $");
73 * m_whois
74 * parv[0] = sender prefix
75 * parv[1] = nickname masklist
77 static int
78 m_whois(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
80 static time_t last_used = 0;
82 if(parc > 2)
84 if(EmptyString(parv[2]))
86 sendto_one(source_p, form_str(ERR_NONICKNAMEGIVEN),
87 me.name, source_p->name);
88 return 0;
91 if(!IsOper(source_p))
93 /* seeing as this is going across servers, we should limit it */
94 if((last_used + ConfigFileEntry.pace_wait_simple) > CurrentTime)
96 sendto_one(source_p, form_str(RPL_LOAD2HI),
97 me.name, source_p->name, "WHOIS");
98 sendto_one_numeric(source_p, RPL_ENDOFWHOIS,
99 form_str(RPL_ENDOFWHOIS), parv[1]);
100 return 0;
102 else
103 last_used = CurrentTime;
106 if(hunt_server(client_p, source_p, ":%s WHOIS %s :%s", 1, parc, parv) !=
107 HUNTED_ISME)
108 return 0;
110 parv[1] = parv[2];
113 do_whois(client_p, source_p, parc, parv);
115 return 0;
119 * ms_whois
120 * parv[0] = sender prefix
121 * parv[1] = server to reply
122 * parv[2] = nickname to whois
124 static int
125 ms_whois(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
127 struct Client *target_p;
129 /* note: early versions of ratbox allowed users to issue a remote
130 * whois with a blank parv[2], so we cannot treat it as a protocol
131 * violation. --anfl
133 if(parc < 3 || EmptyString(parv[2]))
135 sendto_one(source_p, form_str(ERR_NONICKNAMEGIVEN),
136 me.name, source_p->name);
137 return 0;
140 /* check if parv[1] exists */
141 if((target_p = find_client(parv[1])) == NULL)
143 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
144 form_str(ERR_NOSUCHSERVER),
145 IsDigit(parv[1][0]) ? "*" : parv[1]);
146 return 0;
149 /* if parv[1] isnt my client, or me, someone else is supposed
150 * to be handling the request.. so send it to them
152 if(!MyClient(target_p) && !IsMe(target_p))
154 sendto_one(target_p, ":%s WHOIS %s :%s",
155 get_id(source_p, target_p),
156 get_id(target_p, target_p), parv[2]);
157 return 0;
160 /* ok, the target is either us, or a client on our server, so perform the whois
161 * but first, parv[1] == server to perform the whois on, parv[2] == person
162 * to whois, so make parv[1] = parv[2] so do_whois is ok -- fl_
164 parv[1] = parv[2];
165 do_whois(client_p, source_p, parc, parv);
167 return 0;
170 /* do_whois
172 * inputs - pointer to
173 * output -
174 * side effects -
176 static void
177 do_whois(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
179 struct Client *target_p;
180 char *nick;
181 char *p = NULL;
182 int auspex = 0;
184 nick = LOCAL_COPY(parv[1]);
185 if((p = strchr(nick, ',')))
186 *p = '\0';
188 if(IsAuspex(source_p))
190 auspex = 1;
193 target_p = find_named_person(nick);
195 if(target_p != NULL)
197 single_whois(source_p, target_p, auspex);
199 else
200 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
201 form_str(ERR_NOSUCHNICK),
202 IsDigit(*nick) ? "*" : nick);
204 sendto_one_numeric(source_p, RPL_ENDOFWHOIS,
205 form_str(RPL_ENDOFWHOIS), parv[1]);
206 return;
210 * single_whois()
212 * Inputs - source_p client to report to
213 * - target_p client to report on
214 * Output - if found return 1
215 * Side Effects - do a single whois on given client
216 * writing results to source_p
218 static void
219 single_whois(struct Client *source_p, struct Client *target_p, int auspex)
221 char buf[BUFSIZE];
222 dlink_node *ptr;
223 struct Client *a2client_p;
224 struct membership *msptr;
225 struct Channel *chptr;
226 int cur_len = 0;
227 int mlen;
228 char *t;
229 int tlen;
230 hook_data_client hdata;
231 char *name;
232 char quest[] = "?";
233 int visible;
234 int extra_space = 0;
236 if(target_p->name[0] == '\0')
237 name = quest;
238 else
239 name = target_p->name;
241 if(target_p->user == NULL)
243 s_assert(0);
244 return;
247 a2client_p = target_p->servptr;
249 sendto_one_numeric(source_p, RPL_WHOISUSER, form_str(RPL_WHOISUSER),
250 target_p->name, target_p->username,
251 target_p->host, target_p->info);
253 cur_len = mlen = ircsprintf(buf, form_str(RPL_WHOISCHANNELS),
254 get_id(&me, source_p), get_id(source_p, source_p),
255 target_p->name);
257 /* Make sure it won't overflow when sending it to the client
258 * in full names; note that serverhiding may require more space
259 * for a different server name (not done here) -- jilles
261 if (!MyConnect(source_p))
263 extra_space = strlen(source_p->name) - 9;
264 if (extra_space < 0)
265 extra_space = 0;
266 extra_space += strlen(me.name) - 2; /* make sure >= 0 */
267 cur_len += extra_space;
270 t = buf + mlen;
272 DLINK_FOREACH(ptr, target_p->user->channel.head)
274 msptr = ptr->data;
275 chptr = msptr->chptr;
277 visible = IsService(target_p) ? IsMember(source_p, chptr) : ShowChannel(source_p, chptr);
279 if(visible || auspex)
281 if((cur_len + strlen(chptr->chname) + 3) > (BUFSIZE - 5))
283 sendto_one(source_p, "%s", buf);
284 cur_len = mlen + extra_space;
285 t = buf + mlen;
288 tlen = ircsprintf(t, "%s%s%s ",
289 visible ? "" : "!",
290 find_channel_status(msptr, 1),
291 chptr->chname);
292 t += tlen;
293 cur_len += tlen;
297 if(cur_len > mlen + extra_space)
298 sendto_one(source_p, "%s", buf);
300 sendto_one_numeric(source_p, RPL_WHOISSERVER, form_str(RPL_WHOISSERVER),
301 target_p->name, target_p->user->server,
302 a2client_p ? a2client_p->info : "*Not On This Net*");
304 if(target_p->user->away)
305 sendto_one_numeric(source_p, RPL_AWAY, form_str(RPL_AWAY),
306 target_p->name, target_p->user->away);
308 if(SeesOper(target_p, source_p))
310 sendto_one_numeric(source_p, RPL_WHOISOPERATOR, form_str(RPL_WHOISOPERATOR),
311 target_p->name,
312 IsService(target_p) ? ConfigFileEntry.servicestring :
313 (IsAdmin(target_p) ? GlobalSetOptions.adminstring :
314 GlobalSetOptions.operstring));
316 if (target_p->operflags &&
317 (IsOperStaffer(source_p) || source_p == target_p))
319 sendto_one_numeric(source_p, RPL_WHOISOPER_PRIVS,
320 form_str(RPL_WHOISOPER_PRIVS),
321 target_p->name, get_oper_privs(target_p->operflags));
325 if(MyClient(target_p))
327 if (IsDynSpoof(target_p) && (IsAuspex(source_p) || source_p == target_p))
329 /* trick here: show a nonoper their own IP if
330 * dynamic spoofed but not if auth{} spoofed
331 * -- jilles */
332 ClearDynSpoof(target_p);
333 sendto_one_numeric(source_p, RPL_WHOISHOST,
334 form_str(RPL_WHOISHOST),
335 target_p->name, target_p->orighost,
336 show_ip(source_p, target_p) ? target_p->sockhost : "255.255.255.255");
337 SetDynSpoof(target_p);
339 else if(ConfigFileEntry.use_whois_actually && show_ip(source_p, target_p))
340 sendto_one_numeric(source_p, RPL_WHOISACTUALLY,
341 form_str(RPL_WHOISACTUALLY),
342 target_p->name, target_p->sockhost);
344 sendto_one_numeric(source_p, RPL_WHOISIDLE, form_str(RPL_WHOISIDLE),
345 target_p->name,
346 CurrentTime - target_p->localClient->last,
347 target_p->localClient->firsttime);
349 else
351 if (IsDynSpoof(target_p) && (IsAuspex(source_p) || source_p == target_p))
353 ClearDynSpoof(target_p);
354 sendto_one_numeric(source_p, RPL_WHOISHOST,
355 form_str(RPL_WHOISHOST),
356 target_p->name, target_p->orighost,
357 show_ip(source_p, target_p) && !EmptyString(target_p->sockhost) && strcmp(target_p->sockhost, "0")? target_p->sockhost : "255.255.255.255");
358 SetDynSpoof(target_p);
360 else if(ConfigFileEntry.use_whois_actually && show_ip(source_p, target_p) &&
361 !EmptyString(target_p->sockhost) && strcmp(target_p->sockhost, "0"))
363 sendto_one_numeric(source_p, RPL_WHOISACTUALLY,
364 form_str(RPL_WHOISACTUALLY),
365 target_p->name, target_p->sockhost);
371 hdata.client = source_p;
372 hdata.target = target_p;
374 /* doing_whois_hook must only be called for local clients,
375 * doing_whois_global_hook must only be called for local targets
377 /* it is important that these are called *before* RPL_ENDOFWHOIS is
378 * sent, services compatibility code depends on it. --anfl
380 if(MyClient(source_p))
381 call_hook(doing_whois_hook, &hdata);
382 else
383 call_hook(doing_whois_global_hook, &hdata);
385 return;