]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands.cpp
Move remaining functions:
[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 /* XXX - these really belong in helperfuncs perhaps -- w00t */
53 bool InspIRCd::is_uline(const char* server)
54 {
55         if (!server)
56                 return false;
57         if (!*server)
58                 return true;
59
60         return (find(Config->ulines.begin(),Config->ulines.end(),server) != Config->ulines.end());
61 }
62
63 int InspIRCd::operstrcmp(const char* data,const char* input)
64 {
65         int MOD_RESULT = 0;
66         FOREACH_RESULT_I(this,I_OnOperCompare,OnOperCompare(data,input))
67         Log(DEBUG,"operstrcmp: %d",MOD_RESULT);
68         if (MOD_RESULT == 1)
69                 return 0;
70         if (MOD_RESULT == -1)
71                 return 1;
72         Log(DEBUG,"strcmp fallback: '%s' '%s' %d",data,input,strcmp(data,input));
73         return strcmp(data,input);
74 }
75
76 long InspIRCd::duration(const char* str)
77 {
78         char n_field[MAXBUF];
79         long total = 0;
80         n_field[0] = 0;
81
82         if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y')))
83         {
84                 std::string n = str;
85                 n += 's';
86                 return duration(n.c_str());
87         }
88         
89         for (char* i = (char*)str; *i; i++)
90         {
91                 // if we have digits, build up a string for the value in n_field,
92                 // up to 10 digits in size.
93                 if ((*i >= '0') && (*i <= '9'))
94                 {
95                         strlcat(n_field,i,10);
96                 }
97                 else
98                 {
99                         // we dont have a digit, check for numeric tokens
100                         switch (tolower(*i))
101                         {
102                                 case 's':
103                                         total += atoi(n_field);
104                                 break;
105
106                                 case 'm':
107                                         total += (atoi(n_field)*duration_m);
108                                 break;
109
110                                 case 'h':
111                                         total += (atoi(n_field)*duration_h);
112                                 break;
113
114                                 case 'd':
115                                         total += (atoi(n_field)*duration_d);
116                                 break;
117
118                                 case 'w':
119                                         total += (atoi(n_field)*duration_w);
120                                 break;
121
122                                 case 'y':
123                                         total += (atoi(n_field)*duration_y);
124                                 break;
125                         }
126                         n_field[0] = 0;
127                 }
128         }
129         // add trailing seconds
130         total += atoi(n_field);
131         
132         return total;
133 }
134
135 /* All other ircds when doing this check usually just look for a string of *@* or *. We're smarter than that, though. */
136
137 bool InspIRCd::host_matches_everyone(const std::string &mask, userrec* user)
138 {
139         char buffer[MAXBUF];
140         char itrigger[MAXBUF];
141         long matches = 0;
142         
143         if (!Config->ConfValue(Config->config_data, "insane","trigger", 0, itrigger, MAXBUF))
144                 strlcpy(itrigger,"95.5",MAXBUF);
145         
146         if (Config->ConfValueBool(Config->config_data, "insane","hostmasks", 0))
147                 return false;
148         
149         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
150         {
151                 strlcpy(buffer,u->second->ident,MAXBUF);
152                 charlcat(buffer,'@',MAXBUF);
153                 strlcat(buffer,u->second->host,MAXBUF);
154                 if (match(buffer,mask.c_str()))
155                         matches++;
156         }
157         float percent = ((float)matches / (float)clientlist.size()) * 100;
158         if (percent > (float)atof(itrigger))
159         {
160                 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);
161                 return true;
162         }
163         return false;
164 }
165
166 bool InspIRCd::ip_matches_everyone(const std::string &ip, userrec* user)
167 {
168         char itrigger[MAXBUF];
169         long matches = 0;
170         
171         if (!Config->ConfValue(Config->config_data, "insane","trigger",0,itrigger,MAXBUF))
172                 strlcpy(itrigger,"95.5",MAXBUF);
173         
174         if (Config->ConfValueBool(Config->config_data, "insane","ipmasks",0))
175                 return false;
176         
177         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
178         {
179                 if (match(u->second->GetIPString(),ip.c_str(),true))
180                         matches++;
181         }
182         
183         float percent = ((float)matches / (float)clientlist.size()) * 100;
184         if (percent > (float)atof(itrigger))
185         {
186                 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);
187                 return true;
188         }
189         return false;
190 }
191
192 bool InspIRCd::nick_matches_everyone(const std::string &nick, userrec* user)
193 {
194         char itrigger[MAXBUF];
195         long matches = 0;
196         
197         if (!Config->ConfValue(Config->config_data, "insane","trigger",0,itrigger,MAXBUF))
198                 strlcpy(itrigger,"95.5",MAXBUF);
199         
200         if (Config->ConfValueBool(Config->config_data, "insane","nickmasks",0))
201                 return false;
202
203         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
204         {
205                 if (match(u->second->nick,nick.c_str()))
206                         matches++;
207         }
208         
209         float percent = ((float)matches / (float)clientlist.size()) * 100;
210         if (percent > (float)atof(itrigger))
211         {
212                 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);
213                 return true;
214         }
215         return false;
216 }