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