]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/message.cpp
483cf87d0ad688b31006e323f8ec4c13d8e3f5b4
[user/henk/code/inspircd.git] / src / message.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 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 "configreader.h"
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/errno.h>
25 #include <sys/utsname.h>
26 #include <time.h>
27 #include <string>
28 #include <ext/hash_map>
29 #include <map>
30 #include <sstream>
31 #include <vector>
32 #include <deque>
33 #include "users.h"
34 #include "ctables.h"
35 #include "globals.h"
36 #include "modules.h"
37 #include "dynamic.h"
38 #include "wildcard.h"
39 #include "commands.h"
40 #include "message.h"
41 #include "inspstring.h"
42 #include "dns.h"
43 #include "helperfuncs.h"
44
45 extern int MODCOUNT;
46 extern std::vector<Module*> modules;
47 extern std::vector<ircd_module*> factory;
48 extern time_t TIME;
49 extern ServerConfig* Config;
50
51 /* return 0 or 1 depending if users u and u2 share one or more common channels
52  * (used by QUIT, NICK etc which arent channel specific notices) */
53
54 int common_channels(userrec *u, userrec *u2)
55 {
56         if ((!u) || (!u2) || (u->registered != 7) || (u2->registered != 7))
57         {
58                 return 0;
59         }
60         for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
61         {
62                 for (std::vector<ucrec*>::const_iterator z = u2->chans.begin(); z != u2->chans.end(); z++)
63                 {
64                         if ((((ucrec*)(*i))->channel != NULL) && (((ucrec*)(*z))->channel != NULL))
65                         {
66                                 if ((((ucrec*)(*i))->channel == ((ucrec*)(*z))->channel))
67                                 {
68                                         if ((c_count(u)) && (c_count(u2)))
69                                         {
70                                                 return 1;
71                                         }
72                                 }
73                         }
74                 }
75         }
76         return 0;
77 }
78
79 void Blocking(int s)
80 {
81         int flags = fcntl(s, F_GETFL, 0);
82         fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
83 }
84
85 void NonBlocking(int s)
86 {
87         int flags = fcntl(s, F_GETFL, 0);
88         fcntl(s, F_SETFL, flags | O_NONBLOCK);
89 }
90
91 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost, bool forward)
92 {
93         bool ok;
94         std::string ipaddr;
95
96         DNS d(Config->DNSServer);
97         if (forward)
98                 ok = d.ForwardLookup(unresolvedHost, false);
99         else
100                 ok = d.ReverseLookup(unresolvedHost, false);
101         if (!ok)
102                 return 0;
103         time_t T = time(NULL)+1;
104         while ((!d.HasResult()) && (time(NULL)<T));
105         if (forward)
106                 ipaddr = d.GetResultIP();
107         else
108                 ipaddr = d.GetResult();
109         strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
110         return (ipaddr != "");
111 }
112
113 int c_count(userrec* u)
114 {
115         int z = 0;
116         for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
117                 if (((ucrec*)(*i))->channel)
118                         z++;
119         return z;
120
121 }
122
123 void ChangeName(userrec* user, const char* gecos)
124 {
125         if (user->fd > -1)
126         {
127                 int MOD_RESULT = 0;
128                 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
129                 if (MOD_RESULT)
130                         return;
131                 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
132         }
133         strlcpy(user->fullname,gecos,MAXGECOS+1);
134 }
135
136 void ChangeDisplayedHost(userrec* user, const char* host)
137 {
138         if (user->fd > -1)
139         {
140                 int MOD_RESULT = 0;
141                 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
142                 if (MOD_RESULT)
143                         return;
144                 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
145         }
146         strlcpy(user->dhost,host,63);
147         WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
148 }
149
150 /* verify that a user's ident and nickname is valid */
151
152 int isident(const char* n)
153 {
154         if (!n || !*n)
155         {
156                 return 0;
157         }
158         for (char* i = (char*)n; *i; i++)
159         {
160                 if ((*i >= 'A') && (*i <= '}'))
161                 {
162                         continue;
163                 }
164                 if (strchr(".-0123456789",*i))
165                 {
166                         continue;
167                 }
168                 return 0;
169         }
170         return 1;
171 }
172
173
174 int isnick(const char* n)
175 {
176         if (!n || !*n)
177         {
178                 return 0;
179         }
180         int p = 0;
181         for (char* i = (char*)n; *i; i++, p++)
182         {
183                 /* can occur anywhere in a nickname */
184                 if ((*i >= 'A') && (*i <= '}'))
185                 {
186                         continue;
187                 }
188                 /* can occur anywhere BUT the first char of a nickname */
189                 if ((strchr("-0123456789",*i)) && (i > n))
190                 {
191                         continue;
192                 }
193                 /* invalid character! abort */
194                 return 0;
195         }
196         return (p < NICKMAX - 1);
197 }
198
199 /* returns the status character for a given user on a channel, e.g. @ for op,
200  * % for halfop etc. If the user has several modes set, the highest mode
201  * the user has must be returned. */
202
203 const char* cmode(userrec *user, chanrec *chan)
204 {
205         if ((!user) || (!chan))
206         {
207                 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
208                 return "";
209         }
210
211         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
212         {
213                 if (((ucrec*)(*i))->channel == chan)
214                 {
215                         if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
216                         {
217                                 return "@";
218                         }
219                         if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
220                         {
221                                 return "%";
222                         }
223                         if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
224                         {
225                                 return "+";
226                         }
227                         return "";
228                 }
229         }
230         return "";
231 }
232
233 int cflags(userrec *user, chanrec *chan)
234 {
235         if ((!chan) || (!user))
236                 return 0;
237
238         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
239         {
240                 if (((ucrec*)(*i))->channel == chan)
241                 {
242                         return ((ucrec*)(*i))->uc_modes;
243                 }
244         }
245         return 0;
246 }
247
248 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
249  * op, STATUS_VOICE for voice etc. If the user has several modes set, the
250  * highest mode the user has must be returned. */
251
252 int cstatus(userrec *user, chanrec *chan)
253 {
254         if ((!chan) || (!user))
255         {
256                 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
257                 return 0;
258         }
259
260         if (is_uline(user->server))
261                 return STATUS_OP;
262
263         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
264         {
265                 if (((ucrec*)(*i))->channel == chan)
266                 {
267                         if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
268                         {
269                                 return STATUS_OP;
270                         }
271                         if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
272                         {
273                                 return STATUS_HOP;
274                         }
275                         if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
276                         {
277                                 return STATUS_VOICE;
278                         }
279                         return STATUS_NORMAL;
280                 }
281         }
282         return STATUS_NORMAL;
283 }
284
285 void TidyBan(char *ban)
286 {
287         if (!ban) {
288                 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
289                 return;
290         }
291         
292         char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
293
294         strlcpy(temp,ban,MAXBUF);
295
296         char* pos_of_pling = strchr(temp,'!');
297         char* pos_of_at = strchr(temp,'@');
298
299         pos_of_pling[0] = '\0';
300         pos_of_at[0] = '\0';
301         pos_of_pling++;
302         pos_of_at++;
303
304         strlcpy(NICK,temp,NICKMAX-1);
305         strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
306         strlcpy(HOST,pos_of_at,63);
307
308         snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
309 }
310
311 char lst[MAXBUF];
312
313 std::string chlist(userrec *user,userrec* source)
314 {
315         std::string list;
316         
317         if (!user || !source)
318                 return "";
319         
320         for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
321         {
322                 ucrec* rec = *i;
323                 
324                 if(rec->channel && rec->channel->name)
325                 {
326                         /* XXX - Why does this check need to be here at all? :< */
327                         /* Commenting this out until someone finds a case where we need it */
328                         //if (lst.find(rec->channel->name) == std::string::npos)
329                         //{
330                         
331                                 /*
332                                  * If the target is the same as the sender, let them see all their channels.
333                                  * If the channel is NOT private/secret AND the user is not invisible.
334                                  * If the user is an oper, and the <options:operspywhois> option is set.
335                                  */
336                                 if ((source == user) || (*source->oper && Config->OperSpyWhois) || (((!rec->channel->modes[CM_PRIVATE]) && (!rec->channel->modes[CM_SECRET]) && !(user->modes[UM_INVISIBLE])) || (rec->channel->HasUser(source))))
337                                 {
338                                         list.append(cmode(user, rec->channel)).append(rec->channel->name).append(" ");
339                                 }
340                         //}
341                 }
342         }
343         
344         return list;
345 }