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