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;
83 extern std::vector<userrec*> all_opers;
84 extern std::vector<userrec*> local_users;
86 // This table references users by file descriptor.
87 // its an array to make it VERY fast, as all lookups are referenced
88 // by an integer, meaning there is no need for a scan/search operation.
89 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
92 void split_chlist(userrec* user, userrec* dest, const std::string &cl)
95 std::ostringstream prefix;
96 std::string::size_type start, pos, length;
98 prefix << ":" << Config->ServerName << " 319 " << user->nick << " " << dest->nick << " :";
101 for (start = 0; pos = cl.find(' ', start); start = pos+1)
103 length = (pos == std::string::npos) ? cl.length() : pos;
105 if (line.length() + length - start > 510)
107 Write_NoFormat(user->fd, line.c_str());
111 if(pos == std::string::npos)
113 line += cl.substr(start, length - start);
118 line += cl.substr(start, length - start + 1);
124 Write_NoFormat(user->fd, line.c_str());
128 /* XXX - perhaps this should be in cmd_whois? -- w00t */
129 void do_whois(userrec* user, userrec* dest,unsigned long signon, unsigned long idle, char* nick)
131 // bug found by phidjit - were able to whois an incomplete connection if it had sent a NICK or USER
132 if (dest->registered == 7)
134 WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
135 if ((user == dest) || (*user->oper))
137 WriteServ(user->fd,"378 %s %s :is connecting from *@%s %s",user->nick, dest->nick, dest->host, inet_ntoa(dest->ip4));
139 std::string cl = chlist(dest,user);
142 if (cl.length() > 400)
144 split_chlist(user,dest,cl);
148 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, cl.c_str());
151 if (*Config->HideWhoisServer && !(*user->oper))
153 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, Config->HideWhoisServer, Config->Network);
157 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, GetServerDescription(dest->server).c_str());
161 WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
165 WriteServ(user->fd,"313 %s %s :is %s %s on %s",user->nick, dest->nick, (strchr("aeiou",*dest->oper) ? "an" : "a"),dest->oper, Config->Network);
167 if ((!signon) && (!idle))
169 FOREACH_MOD(I_OnWhois,OnWhois(user,dest));
171 if (!strcasecmp(user->server,dest->server))
173 // idle time and signon line can only be sent if youre on the same server (according to RFC)
174 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-TIME), dest->signon);
178 if ((idle) || (signon))
179 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, idle, signon);
181 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
185 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, nick);
186 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, nick);
191 /* XXX - these really belong in helperfuncs perhaps -- w00t */
192 bool is_uline(const char* server)
199 return (find(Config->ulines.begin(),Config->ulines.end(),server) != Config->ulines.end());
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];
221 if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y')))
225 return duration(n.c_str());
228 for (char* i = (char*)str; *i; i++)
230 // if we have digits, build up a string for the value in n_field,
231 // up to 10 digits in size.
232 if ((*i >= '0') && (*i <= '9'))
234 strlcat(n_field,i,10);
238 // we dont have a digit, check for numeric tokens
242 total += atoi(n_field);
246 total += (atoi(n_field)*duration_m);
250 total += (atoi(n_field)*duration_h);
254 total += (atoi(n_field)*duration_d);
258 total += (atoi(n_field)*duration_w);
262 total += (atoi(n_field)*duration_y);
268 // add trailing seconds
269 total += atoi(n_field);
274 /* All other ircds when doing this check usually just look for a string of *@* or *. We're smarter than that, though. */
276 bool host_matches_everyone(const std::string &mask, userrec* user)
278 char insanemasks[MAXBUF];
280 char itrigger[MAXBUF];
281 Config->ConfValue(Config->config_data, "insane","hostmasks", 0, insanemasks, MAXBUF);
282 Config->ConfValue(Config->config_data, "insane","trigger", 0, itrigger, MAXBUF);
284 strlcpy(itrigger,"95.5",MAXBUF);
285 if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
288 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
290 strlcpy(buffer,u->second->ident,MAXBUF);
291 charlcat(buffer,'@',MAXBUF);
292 strlcat(buffer,u->second->host,MAXBUF);
293 if (match(buffer,mask.c_str()))
296 float percent = ((float)matches / (float)clientlist.size()) * 100;
297 if (percent > (float)atof(itrigger))
299 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);
305 bool ip_matches_everyone(const std::string &ip, userrec* user)
307 char insanemasks[MAXBUF];
308 char itrigger[MAXBUF];
309 Config->ConfValue(Config->config_data, "insane","ipmasks",0,insanemasks,MAXBUF);
310 Config->ConfValue(Config->config_data, "insane","trigger",0,itrigger,MAXBUF);
312 strlcpy(itrigger,"95.5",MAXBUF);
313 if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
316 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
318 if (match((char*)inet_ntoa(u->second->ip4),ip.c_str()))
321 float percent = ((float)matches / (float)clientlist.size()) * 100;
322 if (percent > (float)atof(itrigger))
324 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);
330 bool nick_matches_everyone(const std::string &nick, userrec* user)
332 char insanemasks[MAXBUF];
333 char itrigger[MAXBUF];
334 Config->ConfValue(Config->config_data, "insane","nickmasks",0,insanemasks,MAXBUF);
335 Config->ConfValue(Config->config_data, "insane","trigger",0,itrigger,MAXBUF);
337 strlcpy(itrigger,"95.5",MAXBUF);
338 if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
341 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
343 if (match(u->second->nick,nick.c_str()))
346 float percent = ((float)matches / (float)clientlist.size()) * 100;
347 if (percent > (float)atof(itrigger))
349 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);