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