]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands.cpp
Mass-tidyup of module global vars, theyre no longer global vars.
[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 extern std::vector<userrec*> all_opers;
63
64 void split_chlist(userrec* user, userrec* dest, const std::string &cl)
65 {
66         std::string line;
67         std::ostringstream prefix;
68         std::string::size_type start, pos, length;
69         
70         prefix << ":" << ServerInstance->Config->ServerName << " 319 " << user->nick << " " << dest->nick << " :";
71         line = prefix.str();
72         
73         for (start = 0; (pos = cl.find(' ', start)) != std::string::npos; start = pos+1)
74         {
75                 length = (pos == std::string::npos) ? cl.length() : pos;
76                 
77                 if (line.length() + length - start > 510)
78                 {
79                         user->Write(line);
80                         line = prefix.str();
81                 }
82                 
83                 if(pos == std::string::npos)
84                 {
85                         line += cl.substr(start, length - start);
86                         break;
87                 }
88                 else
89                 {
90                         line += cl.substr(start, length - start + 1);
91                 }
92         }
93         
94         if (line.length())
95         {
96                 user->Write(line);
97         }
98 }
99
100 /* XXX - these really belong in helperfuncs perhaps -- w00t */
101 bool is_uline(const char* server)
102 {
103         if (!server)
104                 return false;
105         if (!*server)
106                 return true;
107
108         return (find(ServerInstance->Config->ulines.begin(),ServerInstance->Config->ulines.end(),server) != ServerInstance->Config->ulines.end());
109 }
110
111 int operstrcmp(const char* data,const char* input)
112 {
113         int MOD_RESULT = 0;
114         FOREACH_RESULT(I_OnOperCompare,OnOperCompare(data,input))
115         log(DEBUG,"operstrcmp: %d",MOD_RESULT);
116         if (MOD_RESULT == 1)
117                 return 0;
118         if (MOD_RESULT == -1)
119                 return 1;
120         log(DEBUG,"strcmp fallback: '%s' '%s' %d",data,input,strcmp(data,input));
121         return strcmp(data,input);
122 }
123
124 long duration(const char* str)
125 {
126         char n_field[MAXBUF];
127         long total = 0;
128         n_field[0] = 0;
129
130         if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y')))
131         {
132                 std::string n = str;
133                 n += 's';
134                 return duration(n.c_str());
135         }
136         
137         for (char* i = (char*)str; *i; i++)
138         {
139                 // if we have digits, build up a string for the value in n_field,
140                 // up to 10 digits in size.
141                 if ((*i >= '0') && (*i <= '9'))
142                 {
143                         strlcat(n_field,i,10);
144                 }
145                 else
146                 {
147                         // we dont have a digit, check for numeric tokens
148                         switch (tolower(*i))
149                         {
150                                 case 's':
151                                         total += atoi(n_field);
152                                 break;
153
154                                 case 'm':
155                                         total += (atoi(n_field)*duration_m);
156                                 break;
157
158                                 case 'h':
159                                         total += (atoi(n_field)*duration_h);
160                                 break;
161
162                                 case 'd':
163                                         total += (atoi(n_field)*duration_d);
164                                 break;
165
166                                 case 'w':
167                                         total += (atoi(n_field)*duration_w);
168                                 break;
169
170                                 case 'y':
171                                         total += (atoi(n_field)*duration_y);
172                                 break;
173                         }
174                         n_field[0] = 0;
175                 }
176         }
177         // add trailing seconds
178         total += atoi(n_field);
179         
180         return total;
181 }
182
183 /* All other ircds when doing this check usually just look for a string of *@* or *. We're smarter than that, though. */
184
185 bool host_matches_everyone(const std::string &mask, userrec* user)
186 {
187         char buffer[MAXBUF];
188         char itrigger[MAXBUF];
189         long matches = 0;
190         
191         if (!ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "insane","trigger", 0, itrigger, MAXBUF))
192                 strlcpy(itrigger,"95.5",MAXBUF);
193         
194         if (ServerInstance->Config->ConfValueBool(ServerInstance->Config->config_data, "insane","hostmasks", 0))
195                 return false;
196         
197         for (user_hash::iterator u = ServerInstance->clientlist.begin(); u != ServerInstance->clientlist.end(); u++)
198         {
199                 strlcpy(buffer,u->second->ident,MAXBUF);
200                 charlcat(buffer,'@',MAXBUF);
201                 strlcat(buffer,u->second->host,MAXBUF);
202                 if (match(buffer,mask.c_str()))
203                         matches++;
204         }
205         float percent = ((float)matches / (float)ServerInstance->clientlist.size()) * 100;
206         if (percent > (float)atof(itrigger))
207         {
208                 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);
209                 return true;
210         }
211         return false;
212 }
213
214 bool ip_matches_everyone(const std::string &ip, userrec* user)
215 {
216         char itrigger[MAXBUF];
217         long matches = 0;
218         
219         if (!ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "insane","trigger",0,itrigger,MAXBUF))
220                 strlcpy(itrigger,"95.5",MAXBUF);
221         
222         if (ServerInstance->Config->ConfValueBool(ServerInstance->Config->config_data, "insane","ipmasks",0))
223                 return false;
224         
225         for (user_hash::iterator u = ServerInstance->clientlist.begin(); u != ServerInstance->clientlist.end(); u++)
226         {
227                 if (match(u->second->GetIPString(),ip.c_str(),true))
228                         matches++;
229         }
230         
231         float percent = ((float)matches / (float)ServerInstance->clientlist.size()) * 100;
232         if (percent > (float)atof(itrigger))
233         {
234                 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);
235                 return true;
236         }
237         return false;
238 }
239
240 bool nick_matches_everyone(const std::string &nick, userrec* user)
241 {
242         char itrigger[MAXBUF];
243         long matches = 0;
244         
245         if (!ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "insane","trigger",0,itrigger,MAXBUF))
246                 strlcpy(itrigger,"95.5",MAXBUF);
247         
248         if (ServerInstance->Config->ConfValueBool(ServerInstance->Config->config_data, "insane","nickmasks",0))
249                 return false;
250
251         for (user_hash::iterator u = ServerInstance->clientlist.begin(); u != ServerInstance->clientlist.end(); u++)
252         {
253                 if (match(u->second->nick,nick.c_str()))
254                         matches++;
255         }
256         
257         float percent = ((float)matches / (float)ServerInstance->clientlist.size()) * 100;
258         if (percent > (float)atof(itrigger))
259         {
260                 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);
261                 return true;
262         }
263         return false;
264 }