]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/message.cpp
Added m_alias module which provides command aliases.
[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 ((!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))
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,NULL);
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)
332                 {
333                         if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
334                         {
335                                 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
336                                 {
337                                         return "@";
338                                 }
339                                 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
340                                 {
341                                         return "%";
342                                 }
343                                 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
344                                 {
345                                         return "+";
346                                 }
347                                 return "";
348                         }
349                 }
350         }
351 }
352
353 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
354  * op, STATUS_VOICE for voice etc. If the user has several modes set, the
355  * highest mode the user has must be returned. */
356
357 int cstatus(userrec *user, chanrec *chan)
358 {
359         if ((!chan) || (!user))
360         {
361                 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
362                 return 0;
363         }
364
365         for (int i = 0; i != MAXCHANS; i++)
366         {
367                 if (user->chans[i].channel)
368                 {
369                         if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
370                         {
371                                 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
372                                 {
373                                         return STATUS_OP;
374                                 }
375                                 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
376                                 {
377                                         return STATUS_HOP;
378                                 }
379                                 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
380                                 {
381                                         return STATUS_VOICE;
382                                 }
383                                 return STATUS_NORMAL;
384                         }
385                 }
386         }
387 }
388
389 /* returns 1 if user u has channel c in their record, 0 if not */
390
391 int has_channel(userrec *u, chanrec *c)
392 {
393         if ((!u) || (!c))
394         {
395                 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
396                 return 0;
397         }
398         for (int i =0; i != MAXCHANS; i++)
399         {
400                 if (u->chans[i].channel)
401                 {
402                         if (!strcasecmp(u->chans[i].channel->name,c->name))
403                         {
404                                 return 1;
405                         }
406                 }
407         }
408         return 0;
409 }
410
411
412 void TidyBan(char *ban)
413 {
414         if (!ban) {
415                 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
416                 return;
417         }
418         
419         char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
420
421         strcpy(temp,ban);
422
423         char* pos_of_pling = strchr(temp,'!');
424         char* pos_of_at = strchr(temp,'@');
425
426         pos_of_pling[0] = '\0';
427         pos_of_at[0] = '\0';
428         pos_of_pling++;
429         pos_of_at++;
430
431         strncpy(NICK,temp,NICKMAX);
432         strncpy(IDENT,pos_of_pling,IDENTMAX+1);
433         strncpy(HOST,pos_of_at,160);
434
435         sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
436 }
437
438 char lst[MAXBUF];
439
440 char* chlist(userrec *user)
441 {
442         int i = 0;
443         char cmp[MAXBUF];
444
445         log(DEBUG,"chlist: %s",user->nick);
446         strcpy(lst,"");
447         if (!user)
448         {
449                 return lst;
450         }
451         for (int i = 0; i != MAXCHANS; i++)
452         {
453                 if (user->chans[i].channel != NULL)
454                 {
455                         if (user->chans[i].channel->name)
456                         {
457                                 strcpy(cmp,user->chans[i].channel->name);
458                                 strcat(cmp," ");
459                                 if (!strstr(lst,cmp))
460                                 {
461                                         if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
462                                         {
463                                                 strcat(lst,cmode(user,user->chans[i].channel));
464                                                 strcat(lst,user->chans[i].channel->name);
465                                                 strcat(lst," ");
466                                         }
467                                 }
468                         }
469                 }
470         }
471         if (strlen(lst))
472         {
473                 lst[strlen(lst)-1] = '\0'; // chop trailing space
474         }
475         return lst;
476 }
477
478
479 void send_network_quit(const char* nick, const char* reason)
480 {
481         char buffer[MAXBUF];
482         snprintf(buffer,MAXBUF,"Q %s :%s",nick,reason);
483         NetSendToAll(buffer);
484 }
485
486