]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/message.cpp
Removal of mesh stuff (lots of)
[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 "connection.h"
39 #include "users.h"
40 #include "servers.h"
41 #include "ctables.h"
42 #include "globals.h"
43 #include "modules.h"
44 #include "dynamic.h"
45 #include "wildcard.h"
46 #include "message.h"
47 #include "inspstring.h"
48 #include "dns.h"
49 #include "helperfuncs.h"
50
51 extern int MODCOUNT;
52 extern std::vector<Module*> modules;
53 extern std::vector<ircd_module*> factory;
54
55 extern char ServerName[MAXBUF];
56
57 extern time_t TIME;
58
59 extern FILE *log_file;
60 extern char DNSServer[MAXBUF];
61
62 /* return 0 or 1 depending if users u and u2 share one or more common channels
63  * (used by QUIT, NICK etc which arent channel specific notices) */
64
65 int common_channels(userrec *u, userrec *u2)
66 {
67         if ((!u) || (!u2))
68         {
69                 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
70                 return 0;
71         }
72         for (int i = 0; i != MAXCHANS; i++)
73         {
74                 for (int z = 0; z != MAXCHANS; z++)
75                 {
76                         if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
77                         {
78                                 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))
79                                 {
80                                         if ((c_count(u)) && (c_count(u2)))
81                                         {
82                                                 return 1;
83                                         }
84                                 }
85                         }
86                 }
87         }
88         return 0;
89 }
90
91
92 void tidystring(char* str)
93 {
94         // strips out double spaces before a : parameter
95         
96         char temp[MAXBUF];
97         bool go_again = true;
98         
99         if (!str)
100         {
101                 return;
102         }
103         
104         while ((str[0] == ' ') && (strlen(str)>0))
105         {
106                 str++;
107         }
108         
109         while (go_again)
110         {
111                 bool noparse = false;
112                 unsigned int t = 0, a = 0;
113                 go_again = false;
114                 while (a < strlen(str))
115                 {
116                         if ((a<strlen(str)-1) && (noparse==false))
117                         {
118                                 if ((str[a] == ' ') && (str[a+1] == ' '))
119                                 {
120                                         log(DEBUG,"Tidied extra space out of string: %s",str);
121                                         go_again = true;
122                                         a++;
123                                 }
124                         }
125                         
126                         if (a<strlen(str)-1)
127                         {
128                                 if ((str[a] == ' ') && (str[a+1] == ':'))
129                                 {
130                                         noparse = true;
131                                 }
132                         }
133                         
134                         temp[t++] = str[a++];
135                 }
136                 temp[t] = '\0';
137                 strlcpy(str,temp,MAXBUF);
138         }
139 }
140
141 /* chop a string down to 512 characters and preserve linefeed (irc max
142  * line length) */
143
144 void chop(char* str)
145 {
146         if (!str)
147         {
148                 log(DEBUG,"ERROR! Null string passed to chop()!");
149                 return;
150         }
151         string temp = str;
152         FOREACH_MOD OnServerRaw(temp,false,NULL);
153         const char* str2 = temp.c_str();
154         snprintf(str,MAXBUF,"%s",str2);
155         if (strlen(str) >= 511)
156         {
157                 str[510] = '\r';
158                 str[511] = '\n';
159                 str[512] = '\0';
160                 log(DEBUG,"Excess line chopped.");
161         }
162 }
163
164
165 void Blocking(int s)
166 {
167         int flags;
168         log(DEBUG,"Blocking: %d",s);
169         flags = fcntl(s, F_GETFL, 0);
170         fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
171 }
172
173 void NonBlocking(int s)
174 {
175         int flags;
176         log(DEBUG,"NonBlocking: %d",s);
177         flags = fcntl(s, F_GETFL, 0);
178         fcntl(s, F_SETFL, flags | O_NONBLOCK);
179 }
180
181 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
182 {
183         DNS d(DNSServer);
184         int fd = d.ReverseLookup(unresolvedHost);
185         if (fd < 1)
186                 return 0;
187         time_t T = time(NULL)+1;
188         while ((!d.HasResult()) && (time(NULL)<T));
189         std::string ipaddr = d.GetResult();
190         strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
191         return (ipaddr != "");
192 }
193
194 int c_count(userrec* u)
195 {
196         int z = 0;
197         for (int i =0; i != MAXCHANS; i++)
198                 if (u->chans[i].channel != NULL)
199                         z++;
200         return z;
201
202 }
203
204 bool hasumode(userrec* user, char mode)
205 {
206         if (user)
207         {
208                 return (strchr(user->modes,mode)>0);
209         }
210         else return false;
211 }
212
213
214 void ChangeName(userrec* user, const char* gecos)
215 {
216         if (!strcasecmp(user->server,ServerName))
217         {
218                 int MOD_RESULT = 0;
219                 FOREACH_RESULT(OnChangeLocalUserGECOS(user,gecos));
220                 if (MOD_RESULT)
221                         return;
222         }
223         strlcpy(user->fullname,gecos,MAXBUF);
224 }
225
226 void ChangeDisplayedHost(userrec* user, const char* host)
227 {
228         if (!strcasecmp(user->server,ServerName))
229         {
230                 int MOD_RESULT = 0;
231                 FOREACH_RESULT(OnChangeLocalUserHost(user,host));
232                 if (MOD_RESULT)
233                         return;
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         for (int i = 0; i != MAXCHANS; i++)
313         {
314                 if (user->chans[i].channel)
315                 {
316                         if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
317                         {
318                                 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
319                                 {
320                                         return "@";
321                                 }
322                                 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
323                                 {
324                                         return "%";
325                                 }
326                                 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
327                                 {
328                                         return "+";
329                                 }
330                                 return "";
331                         }
332                 }
333         }
334         return "";
335 }
336
337 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
338  * op, STATUS_VOICE for voice etc. If the user has several modes set, the
339  * highest mode the user has must be returned. */
340
341 int cstatus(userrec *user, chanrec *chan)
342 {
343         if ((!chan) || (!user))
344         {
345                 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
346                 return 0;
347         }
348
349         for (int i = 0; i != MAXCHANS; i++)
350         {
351                 if (user->chans[i].channel)
352                 {
353                         if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
354                         {
355                                 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
356                                 {
357                                         return STATUS_OP;
358                                 }
359                                 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
360                                 {
361                                         return STATUS_HOP;
362                                 }
363                                 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
364                                 {
365                                         return STATUS_VOICE;
366                                 }
367                                 return STATUS_NORMAL;
368                         }
369                 }
370         }
371         return STATUS_NORMAL;
372 }
373
374 /* returns 1 if user u has channel c in their record, 0 if not */
375
376 int has_channel(userrec *u, chanrec *c)
377 {
378         if ((!u) || (!c))
379         {
380                 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
381                 return 0;
382         }
383         for (int i =0; i != MAXCHANS; i++)
384         {
385                 if (u->chans[i].channel)
386                 {
387                         if (!strcasecmp(u->chans[i].channel->name,c->name))
388                         {
389                                 return 1;
390                         }
391                 }
392         }
393         return 0;
394 }
395
396
397 void TidyBan(char *ban)
398 {
399         if (!ban) {
400                 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
401                 return;
402         }
403         
404         char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
405
406         strlcpy(temp,ban,MAXBUF);
407
408         char* pos_of_pling = strchr(temp,'!');
409         char* pos_of_at = strchr(temp,'@');
410
411         pos_of_pling[0] = '\0';
412         pos_of_at[0] = '\0';
413         pos_of_pling++;
414         pos_of_at++;
415
416         strlcpy(NICK,temp,NICKMAX);
417         strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
418         strlcpy(HOST,pos_of_at,160);
419
420         snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
421 }
422
423 char lst[MAXBUF];
424
425 char* chlist(userrec *user,userrec* source)
426 {
427         char cmp[MAXBUF];
428         log(DEBUG,"chlist: %s",user->nick);
429         strcpy(lst,"");
430         if (!user)
431         {
432                 return lst;
433         }
434         for (int i = 0; i != MAXCHANS; i++)
435         {
436                 if (user->chans[i].channel != NULL)
437                 {
438                         if (user->chans[i].channel->name)
439                         {
440                                 strlcpy(cmp,user->chans[i].channel->name,MAXBUF);
441                                 strlcat(cmp," ",MAXBUF);
442                                 if (!strstr(lst,cmp))
443                                 {
444                                         // if the channel is NOT private/secret, OR the source user is on the channel
445                                         if (((!(user->chans[i].channel->binarymodes & CM_PRIVATE)) && (!(user->chans[i].channel->binarymodes & CM_SECRET))) || (has_channel(source,user->chans[i].channel)))
446                                         {
447                                                 strlcat(lst,cmode(user,user->chans[i].channel),MAXBUF);
448                                                 strlcat(lst,user->chans[i].channel->name,MAXBUF);
449                                                 strlcat(lst," ",MAXBUF);
450                                         }
451                                 }
452                         }
453                 }
454         }
455         if (strlen(lst))
456         {
457                 lst[strlen(lst)-1] = '\0'; // chop trailing space
458         }
459         return lst;
460 }
461
462
463