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,MAXBUF);
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);
233 /* verify that a user's ident and nickname is valid */
235 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)
268 if (strlen(n) > NICKMAX)
272 for (char* i = (char*)n; *i; i++)
274 /* can occur anywhere in a nickname */
275 if ((*i >= 'A') && (*i <= '}'))
279 /* can occur anywhere BUT the first char of a nickname */
280 if ((strchr("-0123456789",*i)) && (i > n))
284 /* invalid character! abort */
290 /* returns the status character for a given user on a channel, e.g. @ for op,
291 * % for halfop etc. If the user has several modes set, the highest mode
292 * the user has must be returned. */
294 char* cmode(userrec *user, chanrec *chan)
296 if ((!user) || (!chan))
298 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
302 for (unsigned int i = 0; i < user->chans.size(); i++)
304 if (user->chans[i].channel)
306 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
308 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
312 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
316 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
327 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
328 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
329 * highest mode the user has must be returned. */
331 int cstatus(userrec *user, chanrec *chan)
333 if ((!chan) || (!user))
335 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
339 if (is_uline(user->server))
342 for (unsigned int i = 0; i < user->chans.size(); i++)
344 if (user->chans[i].channel)
346 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
348 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
352 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
356 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
360 return STATUS_NORMAL;
364 return STATUS_NORMAL;
367 /* returns 1 if user u has channel c in their record, 0 if not */
369 int has_channel(userrec *u, chanrec *c)
373 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
376 for (unsigned int i =0; i < u->chans.size(); i++)
378 if (u->chans[i].channel)
380 if (!strcasecmp(u->chans[i].channel->name,c->name))
390 void TidyBan(char *ban)
393 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
397 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
399 strlcpy(temp,ban,MAXBUF);
401 char* pos_of_pling = strchr(temp,'!');
402 char* pos_of_at = strchr(temp,'@');
404 pos_of_pling[0] = '\0';
409 strlcpy(NICK,temp,NICKMAX);
410 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
411 strlcpy(HOST,pos_of_at,160);
413 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
418 std::string chlist(userrec *user,userrec* source)
420 std::string cmp = "";
421 std::string lst = "";
422 log(DEBUG,"chlist: %s",user->nick);
427 for (unsigned int i = 0; i < user->chans.size(); i++)
429 if (user->chans[i].channel != NULL)
431 if (user->chans[i].channel->name)
433 cmp = std::string(user->chans[i].channel->name) + " ";
434 if (!strstr(lst.c_str(),cmp.c_str()))
436 // if the channel is NOT private/secret, OR the source user is on the channel
437 if (((!(user->chans[i].channel->binarymodes & CM_PRIVATE)) && (!(user->chans[i].channel->binarymodes & CM_SECRET))) || (has_channel(source,user->chans[i].channel)))
439 lst = lst + std::string(cmode(user,user->chans[i].channel)) + std::string(user->chans[i].channel->name) + " ";