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