]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/message.cpp
Removed references to deprecated files servers.* and connection.*
[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 "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
53 extern char ServerName[MAXBUF];
54
55 extern time_t TIME;
56
57 extern FILE *log_file;
58 extern char DNSServer[MAXBUF];
59
60 /* return 0 or 1 depending if users u and u2 share one or more common channels
61  * (used by QUIT, NICK etc which arent channel specific notices) */
62
63 int common_channels(userrec *u, userrec *u2)
64 {
65         if ((!u) || (!u2))
66         {
67                 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
68                 return 0;
69         }
70         for (int i = 0; i != MAXCHANS; i++)
71         {
72                 for (int z = 0; z != MAXCHANS; z++)
73                 {
74                         if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
75                         {
76                                 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))
77                                 {
78                                         if ((c_count(u)) && (c_count(u2)))
79                                         {
80                                                 return 1;
81                                         }
82                                 }
83                         }
84                 }
85         }
86         return 0;
87 }
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         {
99                 return;
100         }
101         
102         while ((str[0] == ' ') && (strlen(str)>0))
103         {
104                 str++;
105         }
106         
107         while (go_again)
108         {
109                 bool noparse = false;
110                 unsigned int t = 0, a = 0;
111                 go_again = false;
112                 while (a < strlen(str))
113                 {
114                         if ((a<strlen(str)-1) && (noparse==false))
115                         {
116                                 if ((str[a] == ' ') && (str[a+1] == ' '))
117                                 {
118                                         log(DEBUG,"Tidied extra space out of string: %s",str);
119                                         go_again = true;
120                                         a++;
121                                 }
122                         }
123                         
124                         if (a<strlen(str)-1)
125                         {
126                                 if ((str[a] == ' ') && (str[a+1] == ':'))
127                                 {
128                                         noparse = true;
129                                 }
130                         }
131                         
132                         temp[t++] = str[a++];
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(DNSServer);
182         int fd = d.ReverseLookup(unresolvedHost);
183         if (fd < 1)
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 (int i =0; i != MAXCHANS; 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 (!strcasecmp(user->server,ServerName))
215         {
216                 int MOD_RESULT = 0;
217                 FOREACH_RESULT(OnChangeLocalUserGECOS(user,gecos));
218                 if (MOD_RESULT)
219                         return;
220         }
221         strlcpy(user->fullname,gecos,MAXBUF);
222 }
223
224 void ChangeDisplayedHost(userrec* user, const char* host)
225 {
226         if (!strcasecmp(user->server,ServerName))
227         {
228                 int MOD_RESULT = 0;
229                 FOREACH_RESULT(OnChangeLocalUserHost(user,host));
230                 if (MOD_RESULT)
231                         return;
232         }
233         strlcpy(user->dhost,host,160);
234 }
235
236 /* verify that a user's ident and nickname is valid */
237
238 int isident(const char* n)
239 {
240         if (!n)
241
242         {
243                 return 0;
244         }
245         if (!strcmp(n,""))
246         {
247                 return 0;
248         }
249         for (unsigned int i = 0; i < strlen(n); i++)
250         {
251                 if ((n[i] < 33) || (n[i] > 125))
252                 {
253                         return 0;
254                 }
255                 /* can't occur ANYWHERE in an Ident! */
256                 if (strchr("<>,/?:;@'~#=+()*&%$£ \"!",n[i]))
257                 {
258                         return 0;
259                 }
260         }
261         return 1;
262 }
263
264
265 int isnick(const char* n)
266 {
267         if (!n)
268         {
269                 return 0;
270         }
271         if (!strcmp(n,""))
272         {
273                 return 0;
274         }
275         if (strlen(n) > NICKMAX)
276         {
277                 return 0;
278         }
279         for (unsigned int i = 0; i != strlen(n); i++)
280         {
281                 if ((n[i] < 33) || (n[i] > 125))
282                 {
283                         return 0;
284                 }
285                 /* can't occur ANYWHERE in a nickname! */
286                 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
287                 {
288                         return 0;
289                 }
290                 /* can't occur as the first char of a nickname... */
291                 if ((strchr("0123456789",n[i])) && (!i))
292                 {
293                         return 0;
294                 }
295         }
296         return 1;
297 }
298
299 /* returns the status character for a given user on a channel, e.g. @ for op,
300  * % for halfop etc. If the user has several modes set, the highest mode
301  * the user has must be returned. */
302
303 char* cmode(userrec *user, chanrec *chan)
304 {
305         if ((!user) || (!chan))
306         {
307                 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
308                 return "";
309         }
310         for (int i = 0; i != MAXCHANS; i++)
311         {
312                 if (user->chans[i].channel)
313                 {
314                         if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
315                         {
316                                 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
317                                 {
318                                         return "@";
319                                 }
320                                 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
321                                 {
322                                         return "%";
323                                 }
324                                 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
325                                 {
326                                         return "+";
327                                 }
328                                 return "";
329                         }
330                 }
331         }
332         return "";
333 }
334
335 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
336  * op, STATUS_VOICE for voice etc. If the user has several modes set, the
337  * highest mode the user has must be returned. */
338
339 int cstatus(userrec *user, chanrec *chan)
340 {
341         if ((!chan) || (!user))
342         {
343                 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
344                 return 0;
345         }
346
347         for (int i = 0; i != MAXCHANS; i++)
348         {
349                 if (user->chans[i].channel)
350                 {
351                         if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
352                         {
353                                 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
354                                 {
355                                         return STATUS_OP;
356                                 }
357                                 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
358                                 {
359                                         return STATUS_HOP;
360                                 }
361                                 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
362                                 {
363                                         return STATUS_VOICE;
364                                 }
365                                 return STATUS_NORMAL;
366                         }
367                 }
368         }
369         return STATUS_NORMAL;
370 }
371
372 /* returns 1 if user u has channel c in their record, 0 if not */
373
374 int has_channel(userrec *u, chanrec *c)
375 {
376         if ((!u) || (!c))
377         {
378                 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
379                 return 0;
380         }
381         for (int i =0; i != MAXCHANS; i++)
382         {
383                 if (u->chans[i].channel)
384                 {
385                         if (!strcasecmp(u->chans[i].channel->name,c->name))
386                         {
387                                 return 1;
388                         }
389                 }
390         }
391         return 0;
392 }
393
394
395 void TidyBan(char *ban)
396 {
397         if (!ban) {
398                 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
399                 return;
400         }
401         
402         char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
403
404         strlcpy(temp,ban,MAXBUF);
405
406         char* pos_of_pling = strchr(temp,'!');
407         char* pos_of_at = strchr(temp,'@');
408
409         pos_of_pling[0] = '\0';
410         pos_of_at[0] = '\0';
411         pos_of_pling++;
412         pos_of_at++;
413
414         strlcpy(NICK,temp,NICKMAX);
415         strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
416         strlcpy(HOST,pos_of_at,160);
417
418         snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
419 }
420
421 char lst[MAXBUF];
422
423 char* chlist(userrec *user,userrec* source)
424 {
425         char cmp[MAXBUF];
426         log(DEBUG,"chlist: %s",user->nick);
427         strcpy(lst,"");
428         if (!user)
429         {
430                 return lst;
431         }
432         for (int i = 0; i != MAXCHANS; i++)
433         {
434                 if (user->chans[i].channel != NULL)
435                 {
436                         if (user->chans[i].channel->name)
437                         {
438                                 strlcpy(cmp,user->chans[i].channel->name,MAXBUF);
439                                 strlcat(cmp," ",MAXBUF);
440                                 if (!strstr(lst,cmp))
441                                 {
442                                         // if the channel is NOT private/secret, OR the source user is on the channel
443                                         if (((!(user->chans[i].channel->binarymodes & CM_PRIVATE)) && (!(user->chans[i].channel->binarymodes & CM_SECRET))) || (has_channel(source,user->chans[i].channel)))
444                                         {
445                                                 strlcat(lst,cmode(user,user->chans[i].channel),MAXBUF);
446                                                 strlcat(lst,user->chans[i].channel->name,MAXBUF);
447                                                 strlcat(lst," ",MAXBUF);
448                                         }
449                                 }
450                         }
451                 }
452         }
453         if (strlen(lst))
454         {
455                 lst[strlen(lst)-1] = '\0'; // chop trailing space
456         }
457         return lst;
458 }
459
460
461