]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/message.cpp
63fdae1358181f1955dbe0ca5c8c57f58c0924bd
[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
37 /* return 0 or 1 depending if users u and u2 share one or more common channels
38  * (used by QUIT, NICK etc which arent channel specific notices) */
39
40 int common_channels(userrec *u, userrec *u2)
41 {
42         int i = 0;
43         int z = 0;
44
45         if ((!u) || (!u2))
46         {
47                 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
48                 return 0;
49         }
50         for (int i = 0; i != MAXCHANS; i++)
51         {
52                 for (z = 0; z != MAXCHANS; z++)
53                 {
54                         if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
55                         {
56                                 if ((u->chans[i].channel == u2->chans[z].channel) && (u->chans[i].channel) && (u2->chans[z].channel) && (u->registered == 7) && (u2->registered == 7))
57                                 {
58                                         if ((c_count(u)) && (c_count(u2)))
59                                         {
60                                                 return 1;
61                                         }
62                                 }
63                         }
64                 }
65         }
66         return 0;
67 }
68
69
70 void safedelete(userrec *p)
71 {
72         if (p)
73         {
74                 log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
75                 log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
76                 delete p;
77                 p = NULL;
78         }
79         else
80         {
81                 log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
82         }
83 }
84
85 void safedelete(chanrec *p)
86 {
87         if (p)
88         {
89                 delete p;
90                 p = NULL;
91                 log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
92         }
93         else
94         {
95                 log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
96         }
97 }
98
99
100 void tidystring(char* str)
101 {
102         // strips out double spaces before a : parameter
103         
104         char temp[MAXBUF];
105         bool go_again = true;
106         
107         if (!str)
108         {
109                 return;
110         }
111         
112         while ((str[0] == ' ') && (strlen(str)>0))
113         {
114                 str++;
115         }
116         
117         while (go_again)
118         {
119                 bool noparse = false;
120                 int t = 0, a = 0;
121                 go_again = false;
122                 while (a < strlen(str))
123                 {
124                         if ((a<strlen(str)-1) && (noparse==false))
125                         {
126                                 if ((str[a] == ' ') && (str[a+1] == ' '))
127                                 {
128                                         log(DEBUG,"Tidied extra space out of string: %s",str);
129                                         go_again = true;
130                                         a++;
131                                 }
132                         }
133                         
134                         if (a<strlen(str)-1)
135                         {
136                                 if ((str[a] == ' ') && (str[a+1] == ':'))
137                                 {
138                                         noparse = true;
139                                 }
140                         }
141                         
142                         temp[t++] = str[a++];
143                 }
144                 temp[t] = '\0';
145                 strncpy(str,temp,MAXBUF);
146         }
147 }
148
149 /* chop a string down to 512 characters and preserve linefeed (irc max
150  * line length) */
151
152 void chop(char* str)
153 {
154   if (!str)
155   {
156         log(DEBUG,"ERROR! Null string passed to chop()!");
157         return;
158   }
159   string temp = str;
160   FOREACH_MOD OnServerRaw(temp,false);
161   const char* str2 = temp.c_str();
162   sprintf(str,"%s",str2);
163   
164
165   if (strlen(str) >= 512)
166   {
167         str[509] = '\r';
168         str[510] = '\n';
169         str[511] = '\0';
170   }
171 }
172
173
174 void Blocking(int s)
175 {
176   int flags;
177   log(DEBUG,"Blocking: %d",s);
178   flags = fcntl(s, F_GETFL, 0);
179   fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
180 }
181
182 void NonBlocking(int s)
183 {
184   int flags;
185   log(DEBUG,"NonBlocking: %d",s);
186   flags = fcntl(s, F_GETFL, 0);
187   //fcntl(s, F_SETFL, O_NONBLOCK);
188   fcntl(s, F_SETFL, flags | O_NONBLOCK);
189 }
190
191 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
192 {
193   struct hostent *hostPtr = NULL;
194   struct in_addr addr;
195
196   memset (resolvedHost, '\0',MAXBUF);
197   if(unresolvedHost == NULL)
198         return(ERROR);
199   if ((inet_aton(unresolvedHost,&addr)) == 0)
200         return(ERROR);
201   hostPtr = gethostbyaddr ((char *)&addr.s_addr,sizeof(addr.s_addr),AF_INET);
202   if (hostPtr != NULL)
203         snprintf(resolvedHost,MAXBUF,"%s",hostPtr->h_name);
204   else
205         snprintf(resolvedHost,MAXBUF,"%s",unresolvedHost);
206   return (TRUE);
207 }
208
209 int c_count(userrec* u)
210 {
211         int z = 0;
212         for (int i =0; i != MAXCHANS; i++)
213                 if (u->chans[i].channel != NULL)
214                         z++;
215         return z;
216
217 }
218
219 bool hasumode(userrec* user, char mode)
220 {
221         if (user)
222         {
223                 return (strchr(user->modes,mode)>0);
224         }
225         else return false;
226 }
227
228
229 void ChangeName(userrec* user, const char* gecos)
230 {
231         strncpy(user->fullname,gecos,MAXBUF);
232
233         // TODO: replace these with functions:
234         // NetSendToAll - to all
235         // NetSendToCommon - to all that hold users sharing a common channel with another user
236         // NetSendToOne - to one server
237         // NetSendToAllExcept - send to all but one
238         // all by servername
239
240         char buffer[MAXBUF];
241         snprintf(buffer,MAXBUF,"a %s :%s",user->nick,gecos);
242         NetSendToAll(buffer);
243 }
244
245 void ChangeDisplayedHost(userrec* user, const char* host)
246 {
247         strncpy(user->dhost,host,160);
248         char buffer[MAXBUF];
249         snprintf(buffer,MAXBUF,"b %s %s",user->nick,host);
250         NetSendToAll(buffer);
251 }
252
253 /* verify that a user's ident and nickname is valid */
254
255 int isident(const char* n)
256 {
257         char v[MAXBUF];
258         if (!n)
259
260         {
261                 return 0;
262         }
263         if (!strcmp(n,""))
264         {
265                 return 0;
266         }
267         for (int i = 0; i != strlen(n); i++)
268         {
269                 if ((n[i] < 33) || (n[i] > 125))
270                 {
271                         return 0;
272                 }
273                 /* can't occur ANYWHERE in an Ident! */
274                 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
275                 {
276                         return 0;
277                 }
278         }
279         return 1;
280 }
281
282
283 int isnick(const char* n)
284 {
285         int i = 0;
286         char v[MAXBUF];
287         if (!n)
288         {
289                 return 0;
290         }
291         if (!strcmp(n,""))
292         {
293                 return 0;
294         }
295         if (strlen(n) > NICKMAX-1)
296         {
297                 return 0;
298         }
299         for (int i = 0; i != strlen(n); i++)
300         {
301                 if ((n[i] < 33) || (n[i] > 125))
302                 {
303                         return 0;
304                 }
305                 /* can't occur ANYWHERE in a nickname! */
306                 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
307                 {
308                         return 0;
309                 }
310                 /* can't occur as the first char of a nickname... */
311                 if ((strchr("0123456789",n[i])) && (!i))
312                 {
313                         return 0;
314                 }
315         }
316         return 1;
317 }
318
319 /* returns the status character for a given user on a channel, e.g. @ for op,
320  * % for halfop etc. If the user has several modes set, the highest mode
321  * the user has must be returned. */
322
323 char* cmode(userrec *user, chanrec *chan)
324 {
325         if ((!user) || (!chan))
326         {
327                 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
328                 return "";
329         }
330
331         int i;
332         for (int i = 0; i != MAXCHANS; i++)
333         {
334                 if ((user->chans[i].channel == chan) && (chan != NULL))
335                 {
336                         if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
337                         {
338                                 return "@";
339                         }
340                         if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
341                         {
342                                 return "%";
343                         }
344                         if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
345                         {
346                                 return "+";
347                         }
348                         return "";
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 == chan) && (chan != NULL))
368                 {
369                         if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
370                         {
371                                 return STATUS_OP;
372                         }
373                         if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
374                         {
375                                 return STATUS_HOP;
376                         }
377                         if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
378                         {
379                                 return STATUS_VOICE;
380                         }
381                         return STATUS_NORMAL;
382                 }
383         }
384 }
385
386 /* returns 1 if user u has channel c in their record, 0 if not */
387
388 int has_channel(userrec *u, chanrec *c)
389 {
390         if ((!u) || (!c))
391         {
392                 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
393                 return 0;
394         }
395         for (int i =0; i != MAXCHANS; i++)
396         {
397                 if (u->chans[i].channel == c)
398                 {
399                         return 1;
400                 }
401         }
402         return 0;
403 }
404
405
406