]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/message.cpp
Optimize common_channels
[user/henk/code/inspircd.git] / src / message.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 "configreader.h"
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/errno.h>
25 #include <sys/utsname.h>
26 #include <time.h>
27 #include <string>
28 #include <ext/hash_map>
29 #include <map>
30 #include <sstream>
31 #include <vector>
32 #include <deque>
33 #include "users.h"
34 #include "ctables.h"
35 #include "globals.h"
36 #include "modules.h"
37 #include "dynamic.h"
38 #include "wildcard.h"
39 #include "commands.h"
40 #include "message.h"
41 #include "inspstring.h"
42 #include "dns.h"
43 #include "helperfuncs.h"
44
45 extern int MODCOUNT;
46 extern std::vector<Module*> modules;
47 extern std::vector<ircd_module*> factory;
48 extern time_t TIME;
49 extern ServerConfig* Config;
50
51 /* return 0 or 1 depending if users u and u2 share one or more common channels
52  * (used by QUIT, NICK etc which arent channel specific notices)
53  *
54  * The old algorithm in 1.0 for this was relatively inefficient, iterating over
55  * the first users channels then the second users channels within the outer loop,
56  * therefore it was a maximum of x*y iterations (upon returning 0 and checking
57  * all possible iterations). However this new function instead checks against the
58  * channel's userlist in the inner loop which is a std::map<userrec*,userrec*>
59  * and saves us time as we already know what pointer value we are after.
60  * Don't quote me on the maths as i am not a mathematician or computer scientist,
61  * but i believe this algorithm is now x+(log y) maximum iterations instead.
62  */
63 int common_channels(userrec *u, userrec *u2)
64 {
65         /* TODO: We need to get rid of this arbitary '7' and make bitmask enums for it */
66         if ((!u) || (!u2) || (u->registered != 7) || (u2->registered != 7))
67                 return 0;
68
69         /* Outer loop */
70         for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
71         {
72                 /* Fetch the channel from the user */
73                 ucrec* user_channel = (ucrec*)(*i);
74
75                 if (user_channel->channel)
76                 {
77                         /* Eliminate the inner loop (which used to be ~equal in size to the outer loop)
78                          * by replacing it with a map::find which *should* be more efficient
79                          */
80                         CUList* channel_user_map = user_channel->channel->GetUsers();
81                         if (channel_user_map->find(u2) != channel_user_map->end())
82                                 return 1;
83                 }
84         }
85         return 0;
86 }
87
88 void Blocking(int s)
89 {
90         int flags = fcntl(s, F_GETFL, 0);
91         fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
92 }
93
94 void NonBlocking(int s)
95 {
96         int flags = fcntl(s, F_GETFL, 0);
97         fcntl(s, F_SETFL, flags | O_NONBLOCK);
98 }
99
100 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost, bool forward)
101 {
102         bool ok;
103         std::string ipaddr;
104
105         DNS d(Config->DNSServer);
106         if (forward)
107                 ok = d.ForwardLookup(unresolvedHost, false);
108         else
109                 ok = d.ReverseLookup(unresolvedHost, false);
110         if (!ok)
111                 return 0;
112         time_t T = time(NULL)+1;
113         while ((!d.HasResult()) && (time(NULL)<T));
114         if (forward)
115                 ipaddr = d.GetResultIP();
116         else
117                 ipaddr = d.GetResult();
118         strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
119         return (ipaddr != "");
120 }
121
122 int c_count(userrec* u)
123 {
124         int z = 0;
125         for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
126                 if (((ucrec*)(*i))->channel)
127                         z++;
128         return z;
129
130 }
131
132 void ChangeName(userrec* user, const char* gecos)
133 {
134         if (user->fd > -1)
135         {
136                 int MOD_RESULT = 0;
137                 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
138                 if (MOD_RESULT)
139                         return;
140                 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
141         }
142         strlcpy(user->fullname,gecos,MAXGECOS+1);
143 }
144
145 void ChangeDisplayedHost(userrec* user, const char* host)
146 {
147         if (user->fd > -1)
148         {
149                 int MOD_RESULT = 0;
150                 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
151                 if (MOD_RESULT)
152                         return;
153                 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
154         }
155         strlcpy(user->dhost,host,63);
156         WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
157 }
158
159 /* verify that a user's ident and nickname is valid */
160
161 int isident(const char* n)
162 {
163         if (!n || !*n)
164         {
165                 return 0;
166         }
167         for (char* i = (char*)n; *i; i++)
168         {
169                 if ((*i >= 'A') && (*i <= '}'))
170                 {
171                         continue;
172                 }
173                 if (strchr(".-0123456789",*i))
174                 {
175                         continue;
176                 }
177                 return 0;
178         }
179         return 1;
180 }
181
182
183 int isnick(const char* n)
184 {
185         if (!n || !*n)
186         {
187                 return 0;
188         }
189         int p = 0;
190         for (char* i = (char*)n; *i; i++, p++)
191         {
192                 /* can occur anywhere in a nickname */
193                 if ((*i >= 'A') && (*i <= '}'))
194                 {
195                         continue;
196                 }
197                 /* can occur anywhere BUT the first char of a nickname */
198                 if ((strchr("-0123456789",*i)) && (i > n))
199                 {
200                         continue;
201                 }
202                 /* invalid character! abort */
203                 return 0;
204         }
205         return (p < NICKMAX - 1);
206 }
207
208 /* returns the status character for a given user on a channel, e.g. @ for op,
209  * % for halfop etc. If the user has several modes set, the highest mode
210  * the user has must be returned. */
211
212 const char* cmode(userrec *user, chanrec *chan)
213 {
214         if ((!user) || (!chan))
215         {
216                 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
217                 return "";
218         }
219
220         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
221         {
222                 if (((ucrec*)(*i))->channel == chan)
223                 {
224                         if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
225                         {
226                                 return "@";
227                         }
228                         if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
229                         {
230                                 return "%";
231                         }
232                         if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
233                         {
234                                 return "+";
235                         }
236                         return "";
237                 }
238         }
239         return "";
240 }
241
242 int cflags(userrec *user, chanrec *chan)
243 {
244         if ((!chan) || (!user))
245                 return 0;
246
247         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
248         {
249                 if (((ucrec*)(*i))->channel == chan)
250                 {
251                         return ((ucrec*)(*i))->uc_modes;
252                 }
253         }
254         return 0;
255 }
256
257 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
258  * op, STATUS_VOICE for voice etc. If the user has several modes set, the
259  * highest mode the user has must be returned. */
260
261 int cstatus(userrec *user, chanrec *chan)
262 {
263         if ((!chan) || (!user))
264         {
265                 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
266                 return 0;
267         }
268
269         if (is_uline(user->server))
270                 return STATUS_OP;
271
272         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
273         {
274                 if (((ucrec*)(*i))->channel == chan)
275                 {
276                         if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
277                         {
278                                 return STATUS_OP;
279                         }
280                         if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
281                         {
282                                 return STATUS_HOP;
283                         }
284                         if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
285                         {
286                                 return STATUS_VOICE;
287                         }
288                         return STATUS_NORMAL;
289                 }
290         }
291         return STATUS_NORMAL;
292 }
293
294 void TidyBan(char *ban)
295 {
296         if (!ban) {
297                 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
298                 return;
299         }
300         
301         char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
302
303         strlcpy(temp,ban,MAXBUF);
304
305         char* pos_of_pling = strchr(temp,'!');
306         char* pos_of_at = strchr(temp,'@');
307
308         pos_of_pling[0] = '\0';
309         pos_of_at[0] = '\0';
310         pos_of_pling++;
311         pos_of_at++;
312
313         strlcpy(NICK,temp,NICKMAX-1);
314         strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
315         strlcpy(HOST,pos_of_at,63);
316
317         snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
318 }
319
320 char lst[MAXBUF];
321
322 std::string chlist(userrec *user,userrec* source)
323 {
324         std::string list;
325         
326         if (!user || !source)
327                 return "";
328         
329         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
330         {
331                 ucrec* rec = *i;
332                 
333                 if(rec->channel && rec->channel->name)
334                 {
335                         /* XXX - Why does this check need to be here at all? :< */
336                         /* Commenting this out until someone finds a case where we need it */
337                         //if (lst.find(rec->channel->name) == std::string::npos)
338                         //{
339                         
340                                 /*
341                                  * If the target is the same as the sender, let them see all their channels.
342                                  * If the channel is NOT private/secret AND the user is not invisible.
343                                  * If the user is an oper, and the <options:operspywhois> option is set.
344                                  */
345                                 if ((source == user) || (*source->oper && Config->OperSpyWhois) || (((!rec->channel->modes[CM_PRIVATE]) && (!rec->channel->modes[CM_SECRET]) && !(user->modes[UM_INVISIBLE])) || (rec->channel->HasUser(source))))
346                                 {
347                                         list.append(cmode(user, rec->channel)).append(rec->channel->name).append(" ");
348                                 }
349                         //}
350                 }
351         }
352         
353         return list;
354 }