]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands.cpp
And fix a bug
[user/henk/code/inspircd.git] / src / commands.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "inspircd_config.h"
18 #include "inspircd.h"
19 #include "configreader.h"
20 #include <unistd.h>
21 #include <sys/errno.h>
22 #include <sys/ioctl.h>
23 #include <sys/utsname.h>
24 #include <cstdio>
25 #include <time.h>
26 #include <string>
27 #include <sstream>
28 #include <vector>
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/resource.h>
32 #ifndef RUSAGE_SELF
33 #define   RUSAGE_SELF     0
34 #define   RUSAGE_CHILDREN     -1
35 #endif
36 #include "users.h"
37 #include "ctables.h"
38 #include "globals.h"
39 #include "modules.h"
40 #include "dynamic.h"
41 #include "wildcard.h"
42 #include "commands.h"
43 #include "mode.h"
44 #include "xline.h"
45 #include "inspstring.h"
46 #include "helperfuncs.h"
47 #include "hashcomp.h"
48 #include "socketengine.h"
49 #include "typedefs.h"
50 #include "command_parse.h"
51
52 extern InspIRCd* ServerInstance;
53
54 extern time_t TIME;
55
56 const long duration_m = 60;
57 const long duration_h = duration_m * 60;
58 const long duration_d = duration_h * 24;
59 const long duration_w = duration_d * 7;
60 const long duration_y = duration_w * 52;
61
62 /* XXX - these really belong in helperfuncs perhaps -- w00t */
63 bool is_uline(const char* server)
64 {
65         if (!server)
66                 return false;
67         if (!*server)
68                 return true;
69
70         return (find(ServerInstance->Config->ulines.begin(),ServerInstance->Config->ulines.end(),server) != ServerInstance->Config->ulines.end());
71 }
72
73 int operstrcmp(const char* data,const char* input)
74 {
75         int MOD_RESULT = 0;
76         FOREACH_RESULT(I_OnOperCompare,OnOperCompare(data,input))
77         log(DEBUG,"operstrcmp: %d",MOD_RESULT);
78         if (MOD_RESULT == 1)
79                 return 0;
80         if (MOD_RESULT == -1)
81                 return 1;
82         log(DEBUG,"strcmp fallback: '%s' '%s' %d",data,input,strcmp(data,input));
83         return strcmp(data,input);
84 }
85
86 long duration(const char* str)
87 {
88         char n_field[MAXBUF];
89         long total = 0;
90         n_field[0] = 0;
91
92         if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y')))
93         {
94                 std::string n = str;
95                 n += 's';
96                 return duration(n.c_str());
97         }
98         
99         for (char* i = (char*)str; *i; i++)
100         {
101                 // if we have digits, build up a string for the value in n_field,
102                 // up to 10 digits in size.
103                 if ((*i >= '0') && (*i <= '9'))
104                 {
105                         strlcat(n_field,i,10);
106                 }
107                 else
108                 {
109                         // we dont have a digit, check for numeric tokens
110                         switch (tolower(*i))
111                         {
112                                 case 's':
113                                         total += atoi(n_field);
114                                 break;
115
116                                 case 'm':
117                                         total += (atoi(n_field)*duration_m);
118                                 break;
119
120                                 case 'h':
121                                         total += (atoi(n_field)*duration_h);
122                                 break;
123
124                                 case 'd':
125                                         total += (atoi(n_field)*duration_d);
126                                 break;
127
128                                 case 'w':
129                                         total += (atoi(n_field)*duration_w);
130                                 break;
131
132                                 case 'y':
133                                         total += (atoi(n_field)*duration_y);
134                                 break;
135                         }
136                         n_field[0] = 0;
137                 }
138         }
139         // add trailing seconds
140         total += atoi(n_field);
141         
142         return total;
143 }
144
145 /* All other ircds when doing this check usually just look for a string of *@* or *. We're smarter than that, though. */
146
147 bool host_matches_everyone(const std::string &mask, userrec* user)
148 {
149         char buffer[MAXBUF];
150         char itrigger[MAXBUF];
151         long matches = 0;
152         
153         if (!ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "insane","trigger", 0, itrigger, MAXBUF))
154                 strlcpy(itrigger,"95.5",MAXBUF);
155         
156         if (ServerInstance->Config->ConfValueBool(ServerInstance->Config->config_data, "insane","hostmasks", 0))
157                 return false;
158         
159         for (user_hash::iterator u = ServerInstance->clientlist.begin(); u != ServerInstance->clientlist.end(); u++)
160         {
161                 strlcpy(buffer,u->second->ident,MAXBUF);
162                 charlcat(buffer,'@',MAXBUF);
163                 strlcat(buffer,u->second->host,MAXBUF);
164                 if (match(buffer,mask.c_str()))
165                         matches++;
166         }
167         float percent = ((float)matches / (float)ServerInstance->clientlist.size()) * 100;
168         if (percent > (float)atof(itrigger))
169         {
170                 ServerInstance->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);
171                 return true;
172         }
173         return false;
174 }
175
176 bool ip_matches_everyone(const std::string &ip, userrec* user)
177 {
178         char itrigger[MAXBUF];
179         long matches = 0;
180         
181         if (!ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "insane","trigger",0,itrigger,MAXBUF))
182                 strlcpy(itrigger,"95.5",MAXBUF);
183         
184         if (ServerInstance->Config->ConfValueBool(ServerInstance->Config->config_data, "insane","ipmasks",0))
185                 return false;
186         
187         for (user_hash::iterator u = ServerInstance->clientlist.begin(); u != ServerInstance->clientlist.end(); u++)
188         {
189                 if (match(u->second->GetIPString(),ip.c_str(),true))
190                         matches++;
191         }
192         
193         float percent = ((float)matches / (float)ServerInstance->clientlist.size()) * 100;
194         if (percent > (float)atof(itrigger))
195         {
196                 ServerInstance->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);
197                 return true;
198         }
199         return false;
200 }
201
202 bool nick_matches_everyone(const std::string &nick, userrec* user)
203 {
204         char itrigger[MAXBUF];
205         long matches = 0;
206         
207         if (!ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "insane","trigger",0,itrigger,MAXBUF))
208                 strlcpy(itrigger,"95.5",MAXBUF);
209         
210         if (ServerInstance->Config->ConfValueBool(ServerInstance->Config->config_data, "insane","nickmasks",0))
211                 return false;
212
213         for (user_hash::iterator u = ServerInstance->clientlist.begin(); u != ServerInstance->clientlist.end(); u++)
214         {
215                 if (match(u->second->nick,nick.c_str()))
216                         matches++;
217         }
218         
219         float percent = ((float)matches / (float)ServerInstance->clientlist.size()) * 100;
220         if (percent > (float)atof(itrigger))
221         {
222                 ServerInstance->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);
223                 return true;
224         }
225         return false;
226 }