1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
30 #include <ext/hash_map>
38 #include <sys/types.h>
40 #include <sys/resource.h>
46 #define RUSAGE_CHILDREN -1
58 #include "inspstring.h"
60 #include "helperfuncs.h"
62 #include "socketengine.h"
64 #include "command_parse.h"
66 extern ServerConfig* Config;
67 extern InspIRCd* ServerInstance;
70 extern std::vector<Module*> modules;
71 extern std::vector<ircd_module*> factory;
74 const long duration_m = 60;
75 const long duration_h = duration_m * 60;
76 const long duration_d = duration_h * 24;
77 const long duration_w = duration_d * 7;
78 const long duration_y = duration_w * 52;
80 extern user_hash clientlist;
81 extern chan_hash chanlist;
82 extern whowas_hash whowas;
84 extern std::vector<userrec*> all_opers;
85 extern std::vector<userrec*> local_users;
87 // This table references users by file descriptor.
88 // its an array to make it VERY fast, as all lookups are referenced
89 // by an integer, meaning there is no need for a scan/search operation.
90 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
93 void split_chlist(userrec* user, userrec* dest, std::string &cl)
95 std::stringstream channels(cl);
96 std::string line = "";
97 std::string cname = "";
98 while (!channels.eof())
101 line = line + cname + " ";
102 if (line.length() > 400)
104 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, line.c_str());
110 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, line.c_str());
114 void do_whois(userrec* user, userrec* dest,unsigned long signon, unsigned long idle, char* nick)
116 // bug found by phidjit - were able to whois an incomplete connection if it had sent a NICK or USER
117 if (dest->registered == 7)
119 WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
120 if ((user == dest) || (strchr(user->modes,'o')))
122 WriteServ(user->fd,"378 %s %s :is connecting from *@%s %s",user->nick, dest->nick, dest->host, (char*)inet_ntoa(dest->ip4));
124 std::string cl = chlist(dest,user);
127 if (cl.length() > 400)
129 split_chlist(user,dest,cl);
133 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, cl.c_str());
136 if (*Config->HideWhoisServer)
138 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, *user->oper ? dest->server : Config->HideWhoisServer, *user->oper ? GetServerDescription(dest->server).c_str() : Config->Network);
142 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, GetServerDescription(dest->server).c_str());
146 WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
148 if (strchr(dest->modes,'o'))
152 WriteServ(user->fd,"313 %s %s :is %s %s on %s",user->nick, dest->nick, (strchr("aeiou",dest->oper[0]) ? "an" : "a"),dest->oper, Config->Network);
156 WriteServ(user->fd,"313 %s %s :is an IRC operator",user->nick, dest->nick);
159 if ((!signon) && (!idle))
161 FOREACH_MOD(I_OnWhois,OnWhois(user,dest));
163 if (!strcasecmp(user->server,dest->server))
165 // idle time and signon line can only be sent if youre on the same server (according to RFC)
166 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-TIME), dest->signon);
170 if ((idle) || (signon))
171 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, idle, signon);
173 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
177 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, nick);
178 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, nick);
182 bool is_uline(const char* server)
184 char ServName[MAXBUF];
191 for (int i = 0; i < Config->ConfValueEnum("uline",&Config->config_f); i++)
193 Config->ConfValue("uline","server",i,ServName,&Config->config_f);
194 if (!strcasecmp(server,ServName))
202 int operstrcmp(char* data,char* input)
205 FOREACH_RESULT(I_OnOperCompare,OnOperCompare(data,input))
206 log(DEBUG,"operstrcmp: %d",MOD_RESULT);
209 if (MOD_RESULT == -1)
211 log(DEBUG,"strcmp fallback: '%s' '%s' %d",data,input,strcmp(data,input));
212 return strcmp(data,input);
215 long duration(const char* str)
217 char n_field[MAXBUF];
219 const char* str_end = str + strlen(str);
222 if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y')))
226 return duration(n.c_str());
229 for (char* i = (char*)str; i < str_end; i++)
231 // if we have digits, build up a string for the value in n_field,
232 // up to 10 digits in size.
233 if ((*i >= '0') && (*i <= '9'))
235 strlcat(n_field,i,10);
239 // we dont have a digit, check for numeric tokens
243 total += atoi(n_field);
247 total += (atoi(n_field)*duration_m);
251 total += (atoi(n_field)*duration_h);
255 total += (atoi(n_field)*duration_d);
259 total += (atoi(n_field)*duration_w);
263 total += (atoi(n_field)*duration_y);
269 // add trailing seconds
270 total += atoi(n_field);
275 /* All other ircds when doing this check usually just look for a string of *@* or *. We're smarter than that, though. */
277 bool host_matches_everyone(std::string mask, userrec* user)
279 char insanemasks[MAXBUF];
281 char itrigger[MAXBUF];
282 Config->ConfValue("insane","hostmasks",0,insanemasks,&Config->config_f);
283 Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
285 strlcpy(itrigger,"95.5",MAXBUF);
286 if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
289 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
291 strlcpy(buffer,u->second->ident,MAXBUF);
292 strlcat(buffer,"@",MAXBUF);
293 strlcat(buffer,u->second->host,MAXBUF);
294 if (match(buffer,mask.c_str()))
297 float percent = ((float)matches / (float)clientlist.size()) * 100;
298 if (percent > (float)atof(itrigger))
300 WriteOpers("*** \2WARNING\2: %s tried to set a G/K/E line mask of %s, which covers %.2f%% of the network!",user->nick,mask.c_str(),percent);
306 bool ip_matches_everyone(std::string ip, userrec* user)
308 char insanemasks[MAXBUF];
309 char itrigger[MAXBUF];
310 Config->ConfValue("insane","ipmasks",0,insanemasks,&Config->config_f);
311 Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
313 strlcpy(itrigger,"95.5",MAXBUF);
314 if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
317 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
319 if (match((char*)inet_ntoa(u->second->ip4),ip.c_str()))
322 float percent = ((float)matches / (float)clientlist.size()) * 100;
323 if (percent > (float)atof(itrigger))
325 WriteOpers("*** \2WARNING\2: %s tried to set a Z line mask of %s, which covers %.2f%% of the network!",user->nick,ip.c_str(),percent);
331 bool nick_matches_everyone(std::string nick, userrec* user)
333 char insanemasks[MAXBUF];
334 char itrigger[MAXBUF];
335 Config->ConfValue("insane","nickmasks",0,insanemasks,&Config->config_f);
336 Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
338 strlcpy(itrigger,"95.5",MAXBUF);
339 if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
342 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
344 if (match(u->second->nick,nick.c_str()))
347 float percent = ((float)matches / (float)clientlist.size()) * 100;
348 if (percent > (float)atof(itrigger))
350 WriteOpers("*** \2WARNING\2: %s tried to set a Q line mask of %s, which covers %.2f%% of the network!",user->nick,nick.c_str(),percent);