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 "inspircd_io.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)
62 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
65 for (unsigned int i = 0; i < u->chans.size(); i++)
67 for (unsigned int z = 0; z != u2->chans.size(); z++)
69 if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
71 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))
73 if ((c_count(u)) && (c_count(u2)))
84 void tidystring(char* str)
86 // strips out double spaces before a : parameter
94 // pointer voodoo++ --w00t
95 while ((*str) && (*str == ' '))
100 bool noparse = false;
103 const int lenofstr = strlen(str);
106 * by caching strlen() of str, we theoretically avoid 3 expensive calls each time this loop
107 * rolls around.. should speed things up a nanosecond or two. ;)
112 if ((a < lenofstr - 1) && (noparse == false))
114 if ((str[a] == ' ') && (str[a+1] == ' '))
116 log(DEBUG,"Tidied extra space out of string: %s",str);
122 if (a < lenofstr - 1)
124 if ((str[a] == ' ') && (str[a+1] == ':'))
130 temp[t++] = str[a++];
134 strlcpy(str,temp,MAXBUF);
138 /* chop a string down to 512 characters and preserve linefeed (irc max
145 log(DEBUG,"ERROR! Null string passed to chop()!");
148 if (strlen(str) >= 511)
153 log(DEBUG,"Excess line chopped.");
161 log(DEBUG,"Blocking: %d",s);
162 flags = fcntl(s, F_GETFL, 0);
163 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
166 void NonBlocking(int s)
169 log(DEBUG,"NonBlocking: %d",s);
170 flags = fcntl(s, F_GETFL, 0);
171 fcntl(s, F_SETFL, flags | O_NONBLOCK);
174 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
176 DNS d(Config->DNSServer);
177 int fd = d.ReverseLookup(unresolvedHost);
180 time_t T = time(NULL)+1;
181 while ((!d.HasResult()) && (time(NULL)<T));
182 std::string ipaddr = d.GetResult();
183 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
184 return (ipaddr != "");
187 int c_count(userrec* u)
190 for (unsigned int i =0; i < u->chans.size(); i++)
191 if (u->chans[i].channel != NULL)
197 bool hasumode(userrec* user, char mode)
201 return (strchr(user->modes,mode)>0);
207 void ChangeName(userrec* user, const char* gecos)
212 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
215 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
217 strlcpy(user->fullname,gecos,MAXGECOS+1);
220 void ChangeDisplayedHost(userrec* user, const char* host)
225 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
228 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
230 strlcpy(user->dhost,host,160);
231 WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
234 /* verify that a user's ident and nickname is valid */
236 int isident(const char* n)
247 for (char* i = (char*)n; *i; i++)
249 if ((*i >= 'A') && (*i <= '}'))
253 if (strchr(".-0123456789",*i))
263 int isnick(const char* n)
269 if (strlen(n) > NICKMAX)
273 for (char* i = (char*)n; *i; i++)
275 /* can occur anywhere in a nickname */
276 if ((*i >= 'A') && (*i <= '}'))
280 /* can occur anywhere BUT the first char of a nickname */
281 if ((strchr("-0123456789",*i)) && (i > n))
285 /* invalid character! abort */
291 /* returns the status character for a given user on a channel, e.g. @ for op,
292 * % for halfop etc. If the user has several modes set, the highest mode
293 * the user has must be returned. */
295 char* cmode(userrec *user, chanrec *chan)
297 if ((!user) || (!chan))
299 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
303 for (unsigned int i = 0; i < user->chans.size(); i++)
305 if (user->chans[i].channel)
307 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
309 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
313 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
317 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
328 int cflags(userrec *user, chanrec *chan)
330 if ((!chan) || (!user))
333 for (unsigned int i = 0; i < user->chans.size(); i++)
335 if (user->chans[i].channel)
337 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
339 return user->chans[i].uc_modes;
346 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
347 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
348 * highest mode the user has must be returned. */
350 int cstatus(userrec *user, chanrec *chan)
352 if ((!chan) || (!user))
354 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
358 if (is_uline(user->server))
361 for (unsigned int i = 0; i < user->chans.size(); i++)
363 if (user->chans[i].channel)
365 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
367 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
371 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
375 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
379 return STATUS_NORMAL;
383 return STATUS_NORMAL;
386 /* returns 1 if user u has channel c in their record, 0 if not */
388 int has_channel(userrec *u, chanrec *c)
392 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
395 for (unsigned int i =0; i < u->chans.size(); i++)
397 if (u->chans[i].channel)
399 if (!strcasecmp(u->chans[i].channel->name,c->name))
409 void TidyBan(char *ban)
412 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
416 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
418 strlcpy(temp,ban,MAXBUF);
420 char* pos_of_pling = strchr(temp,'!');
421 char* pos_of_at = strchr(temp,'@');
423 pos_of_pling[0] = '\0';
428 strlcpy(NICK,temp,NICKMAX-1);
429 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
430 strlcpy(HOST,pos_of_at,160);
432 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
437 std::string chlist(userrec *user,userrec* source)
439 std::string cmp = "";
440 std::string lst = "";
441 log(DEBUG,"chlist: %s",user->nick);
446 bool userinvisible = (strchr(user->modes,'i'));
447 for (unsigned int i = 0; i < user->chans.size(); i++)
449 if (user->chans[i].channel != NULL)
451 if (user->chans[i].channel->name)
453 cmp = std::string(user->chans[i].channel->name) + " ";
454 if (!strstr(lst.c_str(),cmp.c_str()))
456 // if the channel is NOT private/secret, OR the source user is on the channel, AND the user is not invisible.
457 // if the user is the same as the source, shortcircuit the comparison.
458 if ((source == user) || ((((!(user->chans[i].channel->binarymodes & CM_PRIVATE)) && (!(user->chans[i].channel->binarymodes & CM_SECRET)) && (!userinvisible)) || (has_channel(source,user->chans[i].channel)))))
460 lst = lst + std::string(cmode(user,user->chans[i].channel)) + std::string(user->chans[i].channel->name) + " ";