]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/message.cpp
Correctly use iterators and pointer to ucrec
[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 "inspircd_io.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 #ifdef GCC3
29 #include <ext/hash_map>
30 #else
31 #include <hash_map>
32 #endif
33 #include <map>
34 #include <sstream>
35 #include <vector>
36 #include <deque>
37 #include "users.h"
38 #include "ctables.h"
39 #include "globals.h"
40 #include "modules.h"
41 #include "dynamic.h"
42 #include "wildcard.h"
43 #include "commands.h"
44 #include "message.h"
45 #include "inspstring.h"
46 #include "dns.h"
47 #include "helperfuncs.h"
48
49 extern int MODCOUNT;
50 extern std::vector<Module*> modules;
51 extern std::vector<ircd_module*> factory;
52 extern time_t TIME;
53 extern ServerConfig* Config;
54
55 /* return 0 or 1 depending if users u and u2 share one or more common channels
56  * (used by QUIT, NICK etc which arent channel specific notices) */
57
58 int common_channels(userrec *u, userrec *u2)
59 {
60         if ((!u) || (!u2) || (u->registered != 7) || (u2->registered != 7))
61         {
62                 return 0;
63         }
64         for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
65         {
66                 for (std::vector<ucrec*>::const_iterator z = u2->chans.begin(); i != u2->chans.end(); z++)
67                 {
68                         if ((((ucrec*)(*i))->channel != NULL) && (((ucrec*)(*z))->channel != NULL))
69                         {
70                                 if ((((ucrec*)(*i))->channel == ((ucrec*)(*z))->channel) && (((ucrec*)(*i))->channel) && (((ucrec*)(*z))->channel))
71                                 {
72                                         if ((c_count(u)) && (c_count(u2)))
73                                         {
74                                                 return 1;
75                                         }
76                                 }
77                         }
78                 }
79         }
80         return 0;
81 }
82
83 void tidystring(char* str)
84 {
85         // strips out double spaces before a : parameter
86
87         char temp[MAXBUF];
88         bool go_again = true;
89
90         if (!str)
91                 return;
92
93         // pointer voodoo++ --w00t
94         while ((*str) && (*str == ' '))
95                 str++;
96
97         while (go_again)
98         {
99                 bool noparse = false;
100                 int t = 0, a = 0;
101                 go_again = false;
102                 const int lenofstr = strlen(str);
103
104                 /*
105                  * by caching strlen() of str, we theoretically avoid 3 expensive calls each time this loop
106                  * rolls around.. should speed things up a nanosecond or two. ;)
107                  */
108
109                 while (a < lenofstr)
110                 {
111                         if ((a < lenofstr - 1) && (noparse == false))
112                         {
113                                 if ((str[a] == ' ') && (str[a+1] == ' '))
114                                 {
115                                         log(DEBUG,"Tidied extra space out of string: %s",str);
116                                         go_again = true;
117                                         a++;
118                                 }
119                         }
120
121                         if (a < lenofstr - 1)
122                         {
123                                 if ((str[a] == ' ') && (str[a+1] == ':'))
124                                 {
125                                         noparse = true;
126                                 }
127                         }
128
129                         temp[t++] = str[a++];
130                 }
131
132                 temp[t] = '\0';
133                 strlcpy(str,temp,MAXBUF);
134         }
135 }
136
137 /* chop a string down to 512 characters and preserve linefeed (irc max
138  * line length) */
139
140 void chop(char* str)
141 {
142         if (!str)
143         {
144                 log(DEBUG,"ERROR! Null string passed to chop()!");
145                 return;
146         }
147         if (strlen(str) >= 511)
148         {
149                 str[510] = '\r';
150                 str[511] = '\n';
151                 str[512] = '\0';
152                 log(DEBUG,"Excess line chopped.");
153         }
154 }
155
156
157 void Blocking(int s)
158 {
159         int flags = fcntl(s, F_GETFL, 0);
160         fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
161 }
162
163 void NonBlocking(int s)
164 {
165         int flags = fcntl(s, F_GETFL, 0);
166         fcntl(s, F_SETFL, flags | O_NONBLOCK);
167 }
168
169 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
170 {
171         DNS d(Config->DNSServer);
172         int fd = d.ReverseLookup(unresolvedHost);
173         if (fd < 0)
174                 return 0;
175         time_t T = time(NULL)+1;
176         while ((!d.HasResult()) && (time(NULL)<T));
177         std::string ipaddr = d.GetResult();
178         strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
179         return (ipaddr != "");
180 }
181
182 int c_count(userrec* u)
183 {
184         int z = 0;
185         for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
186                 if (((ucrec*)(*i))->channel)
187                         z++;
188         return z;
189
190 }
191
192 bool hasumode(userrec* user, char mode)
193 {
194         if (user)
195         {
196                 return (strchr(user->modes,mode)>0);
197         }
198         else return false;
199 }
200
201
202 void ChangeName(userrec* user, const char* gecos)
203 {
204         if (user->fd > -1)
205         {
206                 int MOD_RESULT = 0;
207                 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
208                 if (MOD_RESULT)
209                         return;
210                 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
211         }
212         strlcpy(user->fullname,gecos,MAXGECOS+1);
213 }
214
215 void ChangeDisplayedHost(userrec* user, const char* host)
216 {
217         if (user->fd > -1)
218         {
219                 int MOD_RESULT = 0;
220                 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
221                 if (MOD_RESULT)
222                         return;
223                 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
224         }
225         strlcpy(user->dhost,host,63);
226         WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
227 }
228
229 /* verify that a user's ident and nickname is valid */
230
231 int isident(const char* n)
232 {
233         if (!n || !*n)
234         {
235                 return 0;
236         }
237         for (char* i = (char*)n; *i; i++)
238         {
239                 if ((*i >= 'A') && (*i <= '}'))
240                 {
241                         continue;
242                 }
243                 if (strchr(".-0123456789",*i))
244                 {
245                         continue;
246                 }
247                 return 0;
248         }
249         return 1;
250 }
251
252
253 int isnick(const char* n)
254 {
255         if (!n || !*n)
256         {
257                 return 0;
258         }
259         int p = 0;
260         for (char* i = (char*)n; *i; i++, p++)
261         {
262                 /* can occur anywhere in a nickname */
263                 if ((*i >= 'A') && (*i <= '}'))
264                 {
265                         continue;
266                 }
267                 /* can occur anywhere BUT the first char of a nickname */
268                 if ((strchr("-0123456789",*i)) && (i > n))
269                 {
270                         continue;
271                 }
272                 /* invalid character! abort */
273                 return 0;
274         }
275         return (p < NICKMAX - 1);
276 }
277
278 /* returns the status character for a given user on a channel, e.g. @ for op,
279  * % for halfop etc. If the user has several modes set, the highest mode
280  * the user has must be returned. */
281
282 const char* cmode(userrec *user, chanrec *chan)
283 {
284         if ((!user) || (!chan))
285         {
286                 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
287                 return "";
288         }
289
290         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
291         {
292                 if (((ucrec*)(*i))->channel == chan)
293                 {
294                         if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
295                         {
296                                 return "@";
297                         }
298                         if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
299                         {
300                                 return "%";
301                         }
302                         if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
303                         {
304                                 return "+";
305                         }
306                         return "";
307                 }
308         }
309         return "";
310 }
311
312 int cflags(userrec *user, chanrec *chan)
313 {
314         if ((!chan) || (!user))
315                 return 0;
316
317         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
318         {
319                 if (((ucrec*)(*i))->channel == chan)
320                 {
321                         return ((ucrec*)(*i))->uc_modes;
322                 }
323         }
324         return 0;
325 }
326
327 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
328  * op, STATUS_VOICE for voice etc. If the user has several modes set, the
329  * highest mode the user has must be returned. */
330
331 int cstatus(userrec *user, chanrec *chan)
332 {
333         if ((!chan) || (!user))
334         {
335                 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
336                 return 0;
337         }
338
339         if (is_uline(user->server))
340                 return STATUS_OP;
341
342         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
343         {
344                 if (((ucrec*)(*i))->channel == chan)
345                 {
346                         if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
347                         {
348                                 return STATUS_OP;
349                         }
350                         if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
351                         {
352                                 return STATUS_HOP;
353                         }
354                         if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
355                         {
356                                 return STATUS_VOICE;
357                         }
358                         return STATUS_NORMAL;
359                 }
360         }
361         return STATUS_NORMAL;
362 }
363
364 void TidyBan(char *ban)
365 {
366         if (!ban) {
367                 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
368                 return;
369         }
370         
371         char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
372
373         strlcpy(temp,ban,MAXBUF);
374
375         char* pos_of_pling = strchr(temp,'!');
376         char* pos_of_at = strchr(temp,'@');
377
378         pos_of_pling[0] = '\0';
379         pos_of_at[0] = '\0';
380         pos_of_pling++;
381         pos_of_at++;
382
383         strlcpy(NICK,temp,NICKMAX-1);
384         strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
385         strlcpy(HOST,pos_of_at,63);
386
387         snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
388 }
389
390 char lst[MAXBUF];
391
392 std::string chlist(userrec *user,userrec* source)
393 {
394         std::string cmp = "";
395         std::string lst = "";
396         if (!user || !source)
397         {
398                 return lst;
399         }
400         bool userinvisible = (strchr(user->modes,'i'));
401         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
402         {
403                 if ((((ucrec*)(*i))->channel != NULL) && (((ucrec*)(*i))->channel->name))
404                 {
405                         cmp = std::string(((ucrec*)(*i))->channel->name) + " ";
406                         if (!strstr(lst.c_str(),cmp.c_str()))
407                         {
408                                 // if the channel is NOT private/secret, OR the source user is on the channel, AND the user is not invisible.
409                                 // if the user is the same as the source, shortcircuit the comparison.
410                                 if ((source == user) || ((((!(((ucrec*)(*i))->channel->binarymodes & CM_PRIVATE)) && (!(((ucrec*)(*i))->channel->binarymodes & CM_SECRET)) && (!userinvisible)) || (((ucrec*)(*i))->channel->HasUser(source)))))
411                                 {
412                                         lst = lst + std::string(cmode(user,((ucrec*)(*i))->channel)) + std::string(((ucrec*)(*i))->channel->name) + " ";
413                                 }
414                         }
415                 }
416         }
417         return lst;
418 }
419