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