]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands.cpp
Changed fd_ref_table from 65536 to more correct MAX_DESCRIPTORS
[user/henk/code/inspircd.git] / src / commands.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 extern whowas_hash whowas;
83
84 extern std::vector<userrec*> all_opers;
85 extern std::vector<userrec*> local_users;
86
87 // This table references users by file descriptor.
88 // its an array to make it VERY fast, as all lookups are referenced
89 // by an integer, meaning there is no need for a scan/search operation.
90 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
91
92 char* CleanFilename(char* name)
93 {
94         char* p = name + strlen(name);
95         while ((p != name) && (*p != '/')) p--;
96         return (p != name ? ++p : p);
97 }
98
99 void split_chlist(userrec* user, userrec* dest, std::string &cl)
100 {
101         std::stringstream channels(cl);
102         std::string line = "";
103         std::string cname = "";
104         while (!channels.eof())
105         {
106                 channels >> cname;
107                 line = line + cname + " ";
108                 if (line.length() > 400)
109                 {
110                         WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, line.c_str());
111                         line = "";
112                 }
113         }
114         if (line.length())
115         {
116                 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, line.c_str());
117         }
118 }
119
120 void do_whois(userrec* user, userrec* dest,unsigned long signon, unsigned long idle, char* nick)
121 {
122         // bug found by phidjit - were able to whois an incomplete connection if it had sent a NICK or USER
123         if (dest->registered == 7)
124         {
125                 WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
126                 if ((user == dest) || (strchr(user->modes,'o')))
127                 {
128                         WriteServ(user->fd,"378 %s %s :is connecting from *@%s %s",user->nick, dest->nick, dest->host, dest->ip);
129                 }
130                 std::string cl = chlist(dest,user);
131                 if (cl.length())
132                 {
133                         if (cl.length() > 400)
134                         {
135                                 split_chlist(user,dest,cl);
136                         }
137                         else
138                         {
139                                 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, cl.c_str());
140                         }
141                 }
142                 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, GetServerDescription(dest->server).c_str());
143                 if (*dest->awaymsg)
144                 {
145                         WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
146                 }
147                 if (strchr(dest->modes,'o'))
148                 {
149                         if (*dest->oper)
150                         {
151                                 WriteServ(user->fd,"313 %s %s :is %s %s on %s",user->nick, dest->nick, (strchr("aeiou",dest->oper[0]) ? "an" : "a"),dest->oper, Config->Network);
152                         }
153                         else
154                         {
155                                 WriteServ(user->fd,"313 %s %s :is opered but has an unknown type",user->nick, dest->nick);
156                         }
157                 }
158                 if ((!signon) && (!idle))
159                 {
160                         FOREACH_MOD(I_OnWhois,OnWhois(user,dest));
161                 }
162                 if (!strcasecmp(user->server,dest->server))
163                 {
164                         // idle time and signon line can only be sent if youre on the same server (according to RFC)
165                         WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-TIME), dest->signon);
166                 }
167                 else
168                 {
169                         if ((idle) || (signon))
170                                 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, idle, signon);
171                 }
172                 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
173         }
174         else
175         {
176                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, nick);
177                 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, nick);
178         }
179 }
180
181 bool is_uline(const char* server)
182 {
183         char ServName[MAXBUF];
184
185         if (!server)
186                 return false;
187         if (!(*server))
188                 return true;
189
190         for (int i = 0; i < Config->ConfValueEnum("uline",&Config->config_f); i++)
191         {
192                 Config->ConfValue("uline","server",i,ServName,&Config->config_f);
193                 if (!strcasecmp(server,ServName))
194                 {
195                         return true;
196                 }
197         }
198         return false;
199 }
200
201 int operstrcmp(char* data,char* input)
202 {
203         int MOD_RESULT = 0;
204         FOREACH_RESULT(I_OnOperCompare,OnOperCompare(data,input))
205         log(DEBUG,"operstrcmp: %d",MOD_RESULT);
206         if (MOD_RESULT == 1)
207                 return 0;
208         if (MOD_RESULT == -1)
209                 return 1;
210         log(DEBUG,"strcmp fallback: '%s' '%s' %d",data,input,strcmp(data,input));
211         return strcmp(data,input);
212 }
213
214 long duration(const char* str)
215 {
216         char n_field[MAXBUF];
217         long total = 0;
218         const char* str_end = str + strlen(str);
219         n_field[0] = 0;
220
221         if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y')))
222         {
223                 std::string n = str;
224                 n = n + "s";
225                 return duration(n.c_str());
226         }
227         
228         for (char* i = (char*)str; i < str_end; i++)
229         {
230                 // if we have digits, build up a string for the value in n_field,
231                 // up to 10 digits in size.
232                 if ((*i >= '0') && (*i <= '9'))
233                 {
234                         strlcat(n_field,i,10);
235                 }
236                 else
237                 {
238                         // we dont have a digit, check for numeric tokens
239                         switch (tolower(*i))
240                         {
241                                 case 's':
242                                         total += atoi(n_field);
243                                 break;
244
245                                 case 'm':
246                                         total += (atoi(n_field)*duration_m);
247                                 break;
248
249                                 case 'h':
250                                         total += (atoi(n_field)*duration_h);
251                                 break;
252
253                                 case 'd':
254                                         total += (atoi(n_field)*duration_d);
255                                 break;
256
257                                 case 'w':
258                                         total += (atoi(n_field)*duration_w);
259                                 break;
260
261                                 case 'y':
262                                         total += (atoi(n_field)*duration_y);
263                                 break;
264                         }
265                         n_field[0] = 0;
266                 }
267         }
268         // add trailing seconds
269         total += atoi(n_field);
270         
271         return total;
272 }
273
274 /* All other ircds when doing this check usually just look for a string of *@* or *. We're smarter than that, though. */
275
276 bool host_matches_everyone(std::string mask, userrec* user)
277 {
278         char insanemasks[MAXBUF];
279         char buffer[MAXBUF];
280         char itrigger[MAXBUF];
281         Config->ConfValue("insane","hostmasks",0,insanemasks,&Config->config_f);
282         Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
283         if (*itrigger == 0)
284                 strlcpy(itrigger,"95.5",MAXBUF);
285         if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
286                 return false;
287         long matches = 0;
288         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
289         {
290                 strlcpy(buffer,u->second->ident,MAXBUF);
291                 strlcat(buffer,"@",MAXBUF);
292                 strlcat(buffer,u->second->host,MAXBUF);
293                 if (match(buffer,mask.c_str()))
294                         matches++;
295         }
296         float percent = ((float)matches / (float)clientlist.size()) * 100;
297         if (percent > (float)atof(itrigger))
298         {
299                 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);
300                 return true;
301         }
302         return false;
303 }
304
305 bool ip_matches_everyone(std::string ip, userrec* user)
306 {
307         char insanemasks[MAXBUF];
308         char itrigger[MAXBUF];
309         Config->ConfValue("insane","ipmasks",0,insanemasks,&Config->config_f);
310         Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
311         if (*itrigger == 0)
312                 strlcpy(itrigger,"95.5",MAXBUF);
313         if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
314                 return false;
315         long matches = 0;
316         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
317         {
318                 if (match(u->second->ip,ip.c_str()))
319                         matches++;
320         }
321         float percent = ((float)matches / (float)clientlist.size()) * 100;
322         if (percent > (float)atof(itrigger))
323         {
324                 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);
325                 return true;
326         }
327         return false;
328 }
329
330 bool nick_matches_everyone(std::string nick, userrec* user)
331 {
332         char insanemasks[MAXBUF];
333         char itrigger[MAXBUF];
334         Config->ConfValue("insane","nickmasks",0,insanemasks,&Config->config_f);
335         Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
336         if (*itrigger == 0)
337                 strlcpy(itrigger,"95.5",MAXBUF);
338         if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
339                 return false;
340         long matches = 0;
341         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
342         {
343                 if (match(u->second->nick,nick.c_str()))
344                         matches++;
345         }
346         float percent = ((float)matches / (float)clientlist.size()) * 100;
347         if (percent > (float)atof(itrigger))
348         {
349                 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);
350                 return true;
351         }
352         return false;
353 }