1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "configreader.h"
24 #include <sys/errno.h>
25 #include <sys/utsname.h>
28 #include <ext/hash_map>
41 #include "inspstring.h"
43 #include "helperfuncs.h"
46 extern std::vector<Module*> modules;
47 extern std::vector<ircd_module*> factory;
49 extern ServerConfig* Config;
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) */
54 int common_channels(userrec *u, userrec *u2)
56 if ((!u) || (!u2) || (u->registered != 7) || (u2->registered != 7))
60 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
62 for (std::vector<ucrec*>::const_iterator z = u2->chans.begin(); z != u2->chans.end(); z++)
64 if ((((ucrec*)(*i))->channel != NULL) && (((ucrec*)(*z))->channel != NULL))
66 if ((((ucrec*)(*i))->channel == ((ucrec*)(*z))->channel))
68 if ((c_count(u)) && (c_count(u2)))
79 void tidystring(char* str)
81 // strips out double spaces before a : parameter
89 // pointer voodoo++ --w00t
90 while ((*str) && (*str == ' '))
98 const int lenofstr = strlen(str);
101 * by caching strlen() of str, we theoretically avoid 3 expensive calls each time this loop
102 * rolls around.. should speed things up a nanosecond or two. ;)
107 if ((a < lenofstr - 1) && (noparse == false))
109 if ((str[a] == ' ') && (str[a+1] == ' '))
111 log(DEBUG,"Tidied extra space out of string: %s",str);
117 if (a < lenofstr - 1)
119 if ((str[a] == ' ') && (str[a+1] == ':'))
125 temp[t++] = str[a++];
129 strlcpy(str,temp,MAXBUF);
133 /* chop a string down to 512 characters and preserve linefeed (irc max
140 log(DEBUG,"ERROR! Null string passed to chop()!");
143 if (strlen(str) >= 511)
148 log(DEBUG,"Excess line chopped.");
155 int flags = fcntl(s, F_GETFL, 0);
156 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
159 void NonBlocking(int s)
161 int flags = fcntl(s, F_GETFL, 0);
162 fcntl(s, F_SETFL, flags | O_NONBLOCK);
165 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost, bool forward)
170 DNS d(Config->DNSServer);
172 ok = d.ForwardLookup(unresolvedHost, false);
174 ok = d.ReverseLookup(unresolvedHost, false);
177 time_t T = time(NULL)+1;
178 while ((!d.HasResult()) && (time(NULL)<T));
180 ipaddr = d.GetResultIP();
182 ipaddr = d.GetResult();
183 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
184 return (ipaddr != "");
187 int c_count(userrec* u)
190 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
191 if (((ucrec*)(*i))->channel)
197 void ChangeName(userrec* user, const char* gecos)
202 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
205 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
207 strlcpy(user->fullname,gecos,MAXGECOS+1);
210 void ChangeDisplayedHost(userrec* user, const char* host)
215 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
218 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
220 strlcpy(user->dhost,host,63);
221 WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
224 /* verify that a user's ident and nickname is valid */
226 int isident(const char* n)
232 for (char* i = (char*)n; *i; i++)
234 if ((*i >= 'A') && (*i <= '}'))
238 if (strchr(".-0123456789",*i))
248 int isnick(const char* n)
255 for (char* i = (char*)n; *i; i++, p++)
257 /* can occur anywhere in a nickname */
258 if ((*i >= 'A') && (*i <= '}'))
262 /* can occur anywhere BUT the first char of a nickname */
263 if ((strchr("-0123456789",*i)) && (i > n))
267 /* invalid character! abort */
270 return (p < NICKMAX - 1);
273 /* returns the status character for a given user on a channel, e.g. @ for op,
274 * % for halfop etc. If the user has several modes set, the highest mode
275 * the user has must be returned. */
277 const char* cmode(userrec *user, chanrec *chan)
279 if ((!user) || (!chan))
281 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
285 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
287 if (((ucrec*)(*i))->channel == chan)
289 if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
293 if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
297 if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
307 int cflags(userrec *user, chanrec *chan)
309 if ((!chan) || (!user))
312 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
314 if (((ucrec*)(*i))->channel == chan)
316 return ((ucrec*)(*i))->uc_modes;
322 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
323 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
324 * highest mode the user has must be returned. */
326 int cstatus(userrec *user, chanrec *chan)
328 if ((!chan) || (!user))
330 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
334 if (is_uline(user->server))
337 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
339 if (((ucrec*)(*i))->channel == chan)
341 if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
345 if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
349 if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
353 return STATUS_NORMAL;
356 return STATUS_NORMAL;
359 void TidyBan(char *ban)
362 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
366 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
368 strlcpy(temp,ban,MAXBUF);
370 char* pos_of_pling = strchr(temp,'!');
371 char* pos_of_at = strchr(temp,'@');
373 pos_of_pling[0] = '\0';
378 strlcpy(NICK,temp,NICKMAX-1);
379 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
380 strlcpy(HOST,pos_of_at,63);
382 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
387 std::string chlist(userrec *user,userrec* source)
389 /* Should this be a stringstream? Not sure if it would be faster as streams are more oriented at appending stuff, which is all we do */
390 std::ostringstream list;
392 if (!user || !source)
395 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
399 if(rec->channel && rec->channel->name)
401 /* XXX - Why does this check need to be here at all? :< */
402 /* Commenting this out until someone finds a case where we need it */
403 //if (lst.find(rec->channel->name) == std::string::npos)
407 * If the target is the same as the sender, let them see all their channels.
408 * If the channel is NOT private/secret AND the user is not invisible.
409 * If the user is an oper, and the <options:operspywhois> option is set.
411 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))))
413 list << cmode(user, rec->channel) << rec->channel->name << " ";