]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands.cpp
Test-fix for crazy's bug (/who 0 * as oper while on no channels)
[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 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
93 void split_chlist(userrec* user, userrec* dest, std::string &cl)
94 {
95         std::stringstream channels(cl);
96         std::string line = "";
97         std::string cname = "";
98         while (!channels.eof())
99         {
100                 channels >> cname;
101                 line = line + cname + " ";
102                 if (line.length() > 400)
103                 {
104                         WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, line.c_str());
105                         line = "";
106                 }
107         }
108         if (line.length())
109         {
110                 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, line.c_str());
111         }
112 }
113
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) || (strchr(user->modes,'o')))
121                 {
122                         WriteServ(user->fd,"378 %s %s :is connecting from *@%s %s",user->nick, dest->nick, dest->host, dest->ip);
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                 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, GetServerDescription(dest->server).c_str());
137                 if (*dest->awaymsg)
138                 {
139                         WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
140                 }
141                 if (strchr(dest->modes,'o'))
142                 {
143                         if (*dest->oper)
144                         {
145                                 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);
146                         }
147                         else
148                         {
149                                 WriteServ(user->fd,"313 %s %s :is opered but has an unknown type",user->nick, dest->nick);
150                         }
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 bool is_uline(const char* server)
176 {
177         char ServName[MAXBUF];
178
179         if (!server)
180                 return false;
181         if (!(*server))
182                 return true;
183
184         for (int i = 0; i < Config->ConfValueEnum("uline",&Config->config_f); i++)
185         {
186                 Config->ConfValue("uline","server",i,ServName,&Config->config_f);
187                 if (!strcasecmp(server,ServName))
188                 {
189                         return true;
190                 }
191         }
192         return false;
193 }
194
195 int operstrcmp(char* data,char* input)
196 {
197         int MOD_RESULT = 0;
198         FOREACH_RESULT(I_OnOperCompare,OnOperCompare(data,input))
199         log(DEBUG,"operstrcmp: %d",MOD_RESULT);
200         if (MOD_RESULT == 1)
201                 return 0;
202         if (MOD_RESULT == -1)
203                 return 1;
204         log(DEBUG,"strcmp fallback: '%s' '%s' %d",data,input,strcmp(data,input));
205         return strcmp(data,input);
206 }
207
208 long duration(const char* str)
209 {
210         char n_field[MAXBUF];
211         long total = 0;
212         const char* str_end = str + strlen(str);
213         n_field[0] = 0;
214
215         if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y')))
216         {
217                 std::string n = str;
218                 n = n + "s";
219                 return duration(n.c_str());
220         }
221         
222         for (char* i = (char*)str; i < str_end; i++)
223         {
224                 // if we have digits, build up a string for the value in n_field,
225                 // up to 10 digits in size.
226                 if ((*i >= '0') && (*i <= '9'))
227                 {
228                         strlcat(n_field,i,10);
229                 }
230                 else
231                 {
232                         // we dont have a digit, check for numeric tokens
233                         switch (tolower(*i))
234                         {
235                                 case 's':
236                                         total += atoi(n_field);
237                                 break;
238
239                                 case 'm':
240                                         total += (atoi(n_field)*duration_m);
241                                 break;
242
243                                 case 'h':
244                                         total += (atoi(n_field)*duration_h);
245                                 break;
246
247                                 case 'd':
248                                         total += (atoi(n_field)*duration_d);
249                                 break;
250
251                                 case 'w':
252                                         total += (atoi(n_field)*duration_w);
253                                 break;
254
255                                 case 'y':
256                                         total += (atoi(n_field)*duration_y);
257                                 break;
258                         }
259                         n_field[0] = 0;
260                 }
261         }
262         // add trailing seconds
263         total += atoi(n_field);
264         
265         return total;
266 }
267
268 /* All other ircds when doing this check usually just look for a string of *@* or *. We're smarter than that, though. */
269
270 bool host_matches_everyone(std::string mask, userrec* user)
271 {
272         char insanemasks[MAXBUF];
273         char buffer[MAXBUF];
274         char itrigger[MAXBUF];
275         Config->ConfValue("insane","hostmasks",0,insanemasks,&Config->config_f);
276         Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
277         if (*itrigger == 0)
278                 strlcpy(itrigger,"95.5",MAXBUF);
279         if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
280                 return false;
281         long matches = 0;
282         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
283         {
284                 strlcpy(buffer,u->second->ident,MAXBUF);
285                 strlcat(buffer,"@",MAXBUF);
286                 strlcat(buffer,u->second->host,MAXBUF);
287                 if (match(buffer,mask.c_str()))
288                         matches++;
289         }
290         float percent = ((float)matches / (float)clientlist.size()) * 100;
291         if (percent > (float)atof(itrigger))
292         {
293                 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);
294                 return true;
295         }
296         return false;
297 }
298
299 bool ip_matches_everyone(std::string ip, userrec* user)
300 {
301         char insanemasks[MAXBUF];
302         char itrigger[MAXBUF];
303         Config->ConfValue("insane","ipmasks",0,insanemasks,&Config->config_f);
304         Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
305         if (*itrigger == 0)
306                 strlcpy(itrigger,"95.5",MAXBUF);
307         if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
308                 return false;
309         long matches = 0;
310         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
311         {
312                 if (match(u->second->ip,ip.c_str()))
313                         matches++;
314         }
315         float percent = ((float)matches / (float)clientlist.size()) * 100;
316         if (percent > (float)atof(itrigger))
317         {
318                 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);
319                 return true;
320         }
321         return false;
322 }
323
324 bool nick_matches_everyone(std::string nick, userrec* user)
325 {
326         char insanemasks[MAXBUF];
327         char itrigger[MAXBUF];
328         Config->ConfValue("insane","nickmasks",0,insanemasks,&Config->config_f);
329         Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
330         if (*itrigger == 0)
331                 strlcpy(itrigger,"95.5",MAXBUF);
332         if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
333                 return false;
334         long matches = 0;
335         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
336         {
337                 if (match(u->second->nick,nick.c_str()))
338                         matches++;
339         }
340         float percent = ((float)matches / (float)clientlist.size()) * 100;
341         if (percent > (float)atof(itrigger))
342         {
343                 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);
344                 return true;
345         }
346         return false;
347 }