]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/message.cpp
e71e72a48b032f3ed7a9d0d25d0633ab680d736a
[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
238         // TODO: replace these with functions:
239         // NetSendToAll - to all
240         // NetSendToCommon - to all that hold users sharing a common channel with another user
241         // NetSendToOne - to one server
242         // NetSendToAllExcept - send to all but one
243         // all by servername
244
245         char buffer[MAXBUF];
246         snprintf(buffer,MAXBUF,"a %s :%s",user->nick,gecos);
247         NetSendToAll(buffer);
248 }
249
250 void ChangeDisplayedHost(userrec* user, const char* host)
251 {
252         strncpy(user->dhost,host,160);
253         char buffer[MAXBUF];
254         snprintf(buffer,MAXBUF,"b %s %s",user->nick,host);
255         NetSendToAll(buffer);
256 }
257
258 /* verify that a user's ident and nickname is valid */
259
260 int isident(const char* n)
261 {
262         char v[MAXBUF];
263         if (!n)
264
265         {
266                 return 0;
267         }
268         if (!strcmp(n,""))
269         {
270                 return 0;
271         }
272         for (int i = 0; i != strlen(n); i++)
273         {
274                 if ((n[i] < 33) || (n[i] > 125))
275                 {
276                         return 0;
277                 }
278                 /* can't occur ANYWHERE in an Ident! */
279                 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
280                 {
281                         return 0;
282                 }
283         }
284         return 1;
285 }
286
287
288 int isnick(const char* n)
289 {
290         int i = 0;
291         char v[MAXBUF];
292         if (!n)
293         {
294                 return 0;
295         }
296         if (!strcmp(n,""))
297         {
298                 return 0;
299         }
300         if (strlen(n) > NICKMAX-1)
301         {
302                 return 0;
303         }
304         for (int i = 0; i != strlen(n); i++)
305         {
306                 if ((n[i] < 33) || (n[i] > 125))
307                 {
308                         return 0;
309                 }
310                 /* can't occur ANYWHERE in a nickname! */
311                 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
312                 {
313                         return 0;
314                 }
315                 /* can't occur as the first char of a nickname... */
316                 if ((strchr("0123456789",n[i])) && (!i))
317                 {
318                         return 0;
319                 }
320         }
321         return 1;
322 }
323
324 /* returns the status character for a given user on a channel, e.g. @ for op,
325  * % for halfop etc. If the user has several modes set, the highest mode
326  * the user has must be returned. */
327
328 char* cmode(userrec *user, chanrec *chan)
329 {
330         if ((!user) || (!chan))
331         {
332                 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
333                 return "";
334         }
335
336         int i;
337         for (int i = 0; i != MAXCHANS; i++)
338         {
339                 if ((user->chans[i].channel == chan) && (chan != NULL))
340                 {
341                         if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
342                         {
343                                 return "@";
344                         }
345                         if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
346                         {
347                                 return "%";
348                         }
349                         if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
350                         {
351                                 return "+";
352                         }
353                         return "";
354                 }
355         }
356 }
357
358 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
359  * op, STATUS_VOICE for voice etc. If the user has several modes set, the
360  * highest mode the user has must be returned. */
361
362 int cstatus(userrec *user, chanrec *chan)
363 {
364         if ((!chan) || (!user))
365         {
366                 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
367                 return 0;
368         }
369
370         for (int i = 0; i != MAXCHANS; i++)
371         {
372                 if ((user->chans[i].channel == chan) && (chan != NULL))
373                 {
374                         if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
375                         {
376                                 return STATUS_OP;
377                         }
378                         if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
379                         {
380                                 return STATUS_HOP;
381                         }
382                         if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
383                         {
384                                 return STATUS_VOICE;
385                         }
386                         return STATUS_NORMAL;
387                 }
388         }
389 }
390
391 /* returns 1 if user u has channel c in their record, 0 if not */
392
393 int has_channel(userrec *u, chanrec *c)
394 {
395         if ((!u) || (!c))
396         {
397                 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
398                 return 0;
399         }
400         for (int i =0; i != MAXCHANS; i++)
401         {
402                 if (u->chans[i].channel == c)
403                 {
404                         return 1;
405                 }
406         }
407         return 0;
408 }
409
410
411 void TidyBan(char *ban)
412 {
413         if (!ban) {
414                 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
415                 return;
416         }
417         
418         char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
419
420         strcpy(temp,ban);
421
422         char* pos_of_pling = strchr(temp,'!');
423         char* pos_of_at = strchr(temp,'@');
424
425         pos_of_pling[0] = '\0';
426         pos_of_at[0] = '\0';
427         pos_of_pling++;
428         pos_of_at++;
429
430         strncpy(NICK,temp,NICKMAX);
431         strncpy(IDENT,pos_of_pling,IDENTMAX+1);
432         strncpy(HOST,pos_of_at,160);
433
434         sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
435 }
436
437 char lst[MAXBUF];
438
439 char* chlist(userrec *user)
440 {
441         int i = 0;
442         char cmp[MAXBUF];
443
444         log(DEBUG,"chlist: %s",user->nick);
445         strcpy(lst,"");
446         if (!user)
447         {
448                 return lst;
449         }
450         for (int i = 0; i != MAXCHANS; i++)
451         {
452                 if (user->chans[i].channel != NULL)
453                 {
454                         if (user->chans[i].channel->name)
455                         {
456                                 strcpy(cmp,user->chans[i].channel->name);
457                                 strcat(cmp," ");
458                                 if (!strstr(lst,cmp))
459                                 {
460                                         if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
461                                         {
462                                                 strcat(lst,cmode(user,user->chans[i].channel));
463                                                 strcat(lst,user->chans[i].channel->name);
464                                                 strcat(lst," ");
465                                         }
466                                 }
467                         }
468                 }
469         }
470         if (strlen(lst))
471         {
472                 lst[strlen(lst)-1] = '\0'; // chop trailing space
473         }
474         return lst;
475 }
476
477
478 void send_network_quit(const char* nick, const char* reason)
479 {
480         char buffer[MAXBUF];
481         snprintf(buffer,MAXBUF,"Q %s :%s",nick,reason);
482         NetSendToAll(buffer);
483 }
484
485