]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands.cpp
Improved strhashcomp with no allocations
[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 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include <unistd.h>
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
26 #include <cstdio>
27 #include <time.h>
28 #include <string>
29 #ifdef GCC3
30 #include <ext/hash_map>
31 #else
32 #include <hash_map>
33 #endif
34 #include <map>
35 #include <sstream>
36 #include <vector>
37 #include <deque>
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/resource.h>
41 #ifdef THREADED_DNS
42 #include <pthread.h>
43 #endif
44 #ifndef RUSAGE_SELF
45 #define   RUSAGE_SELF     0
46 #define   RUSAGE_CHILDREN     -1
47 #endif
48 #include "users.h"
49 #include "ctables.h"
50 #include "globals.h"
51 #include "modules.h"
52 #include "dynamic.h"
53 #include "wildcard.h"
54 #include "message.h"
55 #include "commands.h"
56 #include "mode.h"
57 #include "xline.h"
58 #include "inspstring.h"
59 #include "dnsqueue.h"
60 #include "helperfuncs.h"
61 #include "hashcomp.h"
62 #include "socketengine.h"
63 #include "typedefs.h"
64 #include "command_parse.h"
65
66 extern ServerConfig* Config;
67 extern InspIRCd* ServerInstance;
68
69 extern int MODCOUNT;
70 extern std::vector<Module*> modules;
71 extern std::vector<ircd_module*> factory;
72 extern time_t TIME;
73
74 const long duration_m = 60;
75 const long duration_h = duration_m * 60;
76 const long duration_d = duration_h * 24;
77 const long duration_w = duration_d * 7;
78 const long duration_y = duration_w * 52;
79
80 extern user_hash clientlist;
81 extern chan_hash chanlist;
82
83 extern std::vector<userrec*> all_opers;
84 extern std::vector<userrec*> local_users;
85
86 // This table references users by file descriptor.
87 // its an array to make it VERY fast, as all lookups are referenced
88 // by an integer, meaning there is no need for a scan/search operation.
89 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
90
91
92 void split_chlist(userrec* user, userrec* dest, std::string &cl)
93 {
94         std::stringstream channels(cl);
95         std::string line = "";
96         std::string cname = "";
97         while (!channels.eof())
98         {
99                 channels >> cname;
100                 line = line + cname + " ";
101                 if (line.length() > 400)
102                 {
103                         WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, line.c_str());
104                         line = "";
105                 }
106         }
107         if (line.length())
108         {
109                 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, line.c_str());
110         }
111 }
112
113 /* XXX - perhaps this should be in cmd_whois? -- w00t */
114 void do_whois(userrec* user, userrec* dest,unsigned long signon, unsigned long idle, char* nick)
115 {
116         // bug found by phidjit - were able to whois an incomplete connection if it had sent a NICK or USER
117         if (dest->registered == 7)
118         {
119                 WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
120                 if ((user == dest) || (*user->oper))
121                 {
122                         WriteServ(user->fd,"378 %s %s :is connecting from *@%s %s",user->nick, dest->nick, dest->host, (char*)inet_ntoa(dest->ip4));
123                 }
124                 std::string cl = chlist(dest,user);
125                 if (cl.length())
126                 {
127                         if (cl.length() > 400)
128                         {
129                                 split_chlist(user,dest,cl);
130                         }
131                         else
132                         {
133                                 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, cl.c_str());
134                         }
135                 }
136                 if (*Config->HideWhoisServer)
137                 {
138                         WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, *user->oper ? dest->server : Config->HideWhoisServer, *user->oper ? GetServerDescription(dest->server).c_str() : Config->Network);
139                 }
140                 else
141                 {
142                         WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, GetServerDescription(dest->server).c_str());
143                 }
144                 if (*dest->awaymsg)
145                 {
146                         WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
147                 }
148                 if (*dest->oper)
149                 {
150                         WriteServ(user->fd,"313 %s %s :is %s %s on %s",user->nick, dest->nick, (strchr("aeiou",*dest->oper) ? "an" : "a"),dest->oper, Config->Network);
151                 }
152                 if ((!signon) && (!idle))
153                 {
154                         FOREACH_MOD(I_OnWhois,OnWhois(user,dest));
155                 }
156                 if (!strcasecmp(user->server,dest->server))
157                 {
158                         // idle time and signon line can only be sent if youre on the same server (according to RFC)
159                         WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-TIME), dest->signon);
160                 }
161                 else
162                 {
163                         if ((idle) || (signon))
164                                 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, idle, signon);
165                 }
166                 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
167         }
168         else
169         {
170                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, nick);
171                 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, nick);
172         }
173 }
174
175
176 /* XXX - these really belong in helperfuncs perhaps -- w00t */
177 bool is_uline(const char* server)
178 {
179         if (!server)
180                 return false;
181         if (!*server)
182                 return true;
183
184         return (find(Config->ulines.begin(),Config->ulines.end(),server) != Config->ulines.end());
185 }
186
187 int operstrcmp(char* data,char* input)
188 {
189         int MOD_RESULT = 0;
190         FOREACH_RESULT(I_OnOperCompare,OnOperCompare(data,input))
191         log(DEBUG,"operstrcmp: %d",MOD_RESULT);
192         if (MOD_RESULT == 1)
193                 return 0;
194         if (MOD_RESULT == -1)
195                 return 1;
196         log(DEBUG,"strcmp fallback: '%s' '%s' %d",data,input,strcmp(data,input));
197         return strcmp(data,input);
198 }
199
200 long duration(const char* str)
201 {
202         char n_field[MAXBUF];
203         long total = 0;
204         const char* str_end = str + strlen(str);
205         n_field[0] = 0;
206
207         if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y')))
208         {
209                 std::string n = str;
210                 n = n + "s";
211                 return duration(n.c_str());
212         }
213         
214         for (char* i = (char*)str; i < str_end; i++)
215         {
216                 // if we have digits, build up a string for the value in n_field,
217                 // up to 10 digits in size.
218                 if ((*i >= '0') && (*i <= '9'))
219                 {
220                         strlcat(n_field,i,10);
221                 }
222                 else
223                 {
224                         // we dont have a digit, check for numeric tokens
225                         switch (tolower(*i))
226                         {
227                                 case 's':
228                                         total += atoi(n_field);
229                                 break;
230
231                                 case 'm':
232                                         total += (atoi(n_field)*duration_m);
233                                 break;
234
235                                 case 'h':
236                                         total += (atoi(n_field)*duration_h);
237                                 break;
238
239                                 case 'd':
240                                         total += (atoi(n_field)*duration_d);
241                                 break;
242
243                                 case 'w':
244                                         total += (atoi(n_field)*duration_w);
245                                 break;
246
247                                 case 'y':
248                                         total += (atoi(n_field)*duration_y);
249                                 break;
250                         }
251                         n_field[0] = 0;
252                 }
253         }
254         // add trailing seconds
255         total += atoi(n_field);
256         
257         return total;
258 }
259
260 /* All other ircds when doing this check usually just look for a string of *@* or *. We're smarter than that, though. */
261
262 bool host_matches_everyone(std::string mask, userrec* user)
263 {
264         char insanemasks[MAXBUF];
265         char buffer[MAXBUF];
266         char itrigger[MAXBUF];
267         Config->ConfValue("insane","hostmasks",0,insanemasks,&Config->config_f);
268         Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
269         if (*itrigger == 0)
270                 strlcpy(itrigger,"95.5",MAXBUF);
271         if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
272                 return false;
273         long matches = 0;
274         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
275         {
276                 strlcpy(buffer,u->second->ident,MAXBUF);
277                 charlcat(buffer,'@',MAXBUF);
278                 strlcat(buffer,u->second->host,MAXBUF);
279                 if (match(buffer,mask.c_str()))
280                         matches++;
281         }
282         float percent = ((float)matches / (float)clientlist.size()) * 100;
283         if (percent > (float)atof(itrigger))
284         {
285                 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);
286                 return true;
287         }
288         return false;
289 }
290
291 bool ip_matches_everyone(std::string ip, userrec* user)
292 {
293         char insanemasks[MAXBUF];
294         char itrigger[MAXBUF];
295         Config->ConfValue("insane","ipmasks",0,insanemasks,&Config->config_f);
296         Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
297         if (*itrigger == 0)
298                 strlcpy(itrigger,"95.5",MAXBUF);
299         if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
300                 return false;
301         long matches = 0;
302         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
303         {
304                 if (match((char*)inet_ntoa(u->second->ip4),ip.c_str()))
305                         matches++;
306         }
307         float percent = ((float)matches / (float)clientlist.size()) * 100;
308         if (percent > (float)atof(itrigger))
309         {
310                 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);
311                 return true;
312         }
313         return false;
314 }
315
316 bool nick_matches_everyone(std::string nick, userrec* user)
317 {
318         char insanemasks[MAXBUF];
319         char itrigger[MAXBUF];
320         Config->ConfValue("insane","nickmasks",0,insanemasks,&Config->config_f);
321         Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
322         if (*itrigger == 0)
323                 strlcpy(itrigger,"95.5",MAXBUF);
324         if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
325                 return false;
326         long matches = 0;
327         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
328         {
329                 if (match(u->second->nick,nick.c_str()))
330                         matches++;
331         }
332         float percent = ((float)matches / (float)clientlist.size()) * 100;
333         if (percent > (float)atof(itrigger))
334         {
335                 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);
336                 return true;
337         }
338         return false;
339 }