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