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