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