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>
29 #include <ext/hash_map>
45 #include "inspstring.h"
47 #include "helperfuncs.h"
50 extern std::vector<Module*> modules;
51 extern std::vector<ircd_module*> factory;
53 extern ServerConfig* Config;
55 /* return 0 or 1 depending if users u and u2 share one or more common channels
56 * (used by QUIT, NICK etc which arent channel specific notices) */
58 int common_channels(userrec *u, userrec *u2)
60 if ((!u) || (!u2) || (u->registered != 7) || (u2->registered != 7))
64 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
66 for (std::vector<ucrec*>::const_iterator z = u2->chans.begin(); z != u2->chans.end(); z++)
68 if ((((ucrec*)(*i))->channel != NULL) && (((ucrec*)(*z))->channel != NULL))
70 if ((((ucrec*)(*i))->channel == ((ucrec*)(*z))->channel))
72 if ((c_count(u)) && (c_count(u2)))
83 void tidystring(char* str)
85 // strips out double spaces before a : parameter
93 // pointer voodoo++ --w00t
94 while ((*str) && (*str == ' '))
102 const int lenofstr = strlen(str);
105 * by caching strlen() of str, we theoretically avoid 3 expensive calls each time this loop
106 * rolls around.. should speed things up a nanosecond or two. ;)
111 if ((a < lenofstr - 1) && (noparse == false))
113 if ((str[a] == ' ') && (str[a+1] == ' '))
115 log(DEBUG,"Tidied extra space out of string: %s",str);
121 if (a < lenofstr - 1)
123 if ((str[a] == ' ') && (str[a+1] == ':'))
129 temp[t++] = str[a++];
133 strlcpy(str,temp,MAXBUF);
137 /* chop a string down to 512 characters and preserve linefeed (irc max
144 log(DEBUG,"ERROR! Null string passed to chop()!");
147 if (strlen(str) >= 511)
152 log(DEBUG,"Excess line chopped.");
159 int flags = fcntl(s, F_GETFL, 0);
160 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
163 void NonBlocking(int s)
165 int flags = fcntl(s, F_GETFL, 0);
166 fcntl(s, F_SETFL, flags | O_NONBLOCK);
169 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost, bool forward)
174 DNS d(Config->DNSServer);
176 fd = d.ForwardLookup(unresolvedHost);
178 fd = d.ReverseLookup(unresolvedHost);
181 time_t T = time(NULL)+1;
182 while ((!d.HasResult()) && (time(NULL)<T));
184 ipaddr = d.GetResultIP();
186 ipaddr = d.GetResult();
187 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
188 return (ipaddr != "");
191 int c_count(userrec* u)
194 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
195 if (((ucrec*)(*i))->channel)
201 bool hasumode(userrec* user, char mode)
205 return (strchr(user->modes,mode)>0);
211 void ChangeName(userrec* user, const char* gecos)
216 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
219 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
221 strlcpy(user->fullname,gecos,MAXGECOS+1);
224 void ChangeDisplayedHost(userrec* user, const char* host)
229 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
232 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
234 strlcpy(user->dhost,host,63);
235 WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
238 /* verify that a user's ident and nickname is valid */
240 int isident(const char* n)
246 for (char* i = (char*)n; *i; i++)
248 if ((*i >= 'A') && (*i <= '}'))
252 if (strchr(".-0123456789",*i))
262 int isnick(const char* n)
269 for (char* i = (char*)n; *i; i++, p++)
271 /* can occur anywhere in a nickname */
272 if ((*i >= 'A') && (*i <= '}'))
276 /* can occur anywhere BUT the first char of a nickname */
277 if ((strchr("-0123456789",*i)) && (i > n))
281 /* invalid character! abort */
284 return (p < NICKMAX - 1);
287 /* returns the status character for a given user on a channel, e.g. @ for op,
288 * % for halfop etc. If the user has several modes set, the highest mode
289 * the user has must be returned. */
291 const char* cmode(userrec *user, chanrec *chan)
293 if ((!user) || (!chan))
295 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
299 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
301 if (((ucrec*)(*i))->channel == chan)
303 if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
307 if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
311 if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
321 int cflags(userrec *user, chanrec *chan)
323 if ((!chan) || (!user))
326 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
328 if (((ucrec*)(*i))->channel == chan)
330 return ((ucrec*)(*i))->uc_modes;
336 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
337 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
338 * highest mode the user has must be returned. */
340 int cstatus(userrec *user, chanrec *chan)
342 if ((!chan) || (!user))
344 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
348 if (is_uline(user->server))
351 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
353 if (((ucrec*)(*i))->channel == chan)
355 if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
359 if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
363 if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
367 return STATUS_NORMAL;
370 return STATUS_NORMAL;
373 void TidyBan(char *ban)
376 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
380 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
382 strlcpy(temp,ban,MAXBUF);
384 char* pos_of_pling = strchr(temp,'!');
385 char* pos_of_at = strchr(temp,'@');
387 pos_of_pling[0] = '\0';
392 strlcpy(NICK,temp,NICKMAX-1);
393 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
394 strlcpy(HOST,pos_of_at,63);
396 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
401 std::string chlist(userrec *user,userrec* source)
403 /* 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 */
404 std::ostringstream list;
406 if (!user || !source)
409 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
413 if(rec->channel && rec->channel->name)
415 /* XXX - Why does this check need to be here at all? :< */
416 /* Commenting this out until someone finds a case where we need it */
417 //if (lst.find(rec->channel->name) == std::string::npos)
421 * If the target is the same as the sender, let them see all their channels.
422 * If the channel is NOT private/secret AND the user is not invisible.
423 * If the user is an oper, and the <options:operspywhois> option is set.
425 if ((source == user) || (*source->oper && Config->OperSpyWhois) || (((!rec->channel->modes[CM_PRIVATE]) && (!rec->channel->modes[CM_SECRET]) && !(user->modebits & UM_INVISIBLE)) || (rec->channel->HasUser(source))))
427 list << cmode(user, rec->channel) << rec->channel->name << " ";