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 ((u->chans[i].channel == u2->chans[z].channel) && (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.");
160 int flags = fcntl(s, F_GETFL, 0);
161 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
164 void NonBlocking(int s)
166 int flags = fcntl(s, F_GETFL, 0);
167 fcntl(s, F_SETFL, flags | O_NONBLOCK);
170 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
172 DNS d(Config->DNSServer);
173 int fd = d.ReverseLookup(unresolvedHost);
176 time_t T = time(NULL)+1;
177 while ((!d.HasResult()) && (time(NULL)<T));
178 std::string ipaddr = d.GetResult();
179 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
180 return (ipaddr != "");
183 int c_count(userrec* u)
186 for (unsigned int i =0; i < u->chans.size(); i++)
187 if (u->chans[i].channel != NULL)
193 bool hasumode(userrec* user, char mode)
197 return (strchr(user->modes,mode)>0);
203 void ChangeName(userrec* user, const char* gecos)
208 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
211 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
213 strlcpy(user->fullname,gecos,MAXGECOS+1);
216 void ChangeDisplayedHost(userrec* user, const char* host)
221 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
224 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
226 strlcpy(user->dhost,host,63);
227 WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
230 /* verify that a user's ident and nickname is valid */
232 int isident(const char* n)
238 for (char* i = (char*)n; *i; i++)
240 if ((*i >= 'A') && (*i <= '}'))
244 if (strchr(".-0123456789",*i))
254 int isnick(const char* n)
261 for (char* i = (char*)n; *i; i++, p++)
263 /* can occur anywhere in a nickname */
264 if ((*i >= 'A') && (*i <= '}'))
268 /* can occur anywhere BUT the first char of a nickname */
269 if ((strchr("-0123456789",*i)) && (i > n))
273 /* invalid character! abort */
276 return (p < NICKMAX - 1);
279 /* returns the status character for a given user on a channel, e.g. @ for op,
280 * % for halfop etc. If the user has several modes set, the highest mode
281 * the user has must be returned. */
283 char* cmode(userrec *user, chanrec *chan)
285 if ((!user) || (!chan))
287 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
291 for (unsigned int i = 0; i < user->chans.size(); i++)
293 if (user->chans[i].channel)
295 if ((user->chans[i].channel == chan) && (chan != NULL))
297 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
301 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
305 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
316 int cflags(userrec *user, chanrec *chan)
318 if ((!chan) || (!user))
321 for (unsigned int i = 0; i < user->chans.size(); i++)
323 if (user->chans[i].channel)
325 if ((user->chans[i].channel == chan) && (chan != NULL))
327 return user->chans[i].uc_modes;
334 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
335 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
336 * highest mode the user has must be returned. */
338 int cstatus(userrec *user, chanrec *chan)
340 if ((!chan) || (!user))
342 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
346 if (is_uline(user->server))
349 for (unsigned int i = 0; i < user->chans.size(); i++)
351 if (user->chans[i].channel)
353 if ((user->chans[i].channel == chan) && (chan != NULL))
355 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
359 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
363 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
367 return STATUS_NORMAL;
371 return STATUS_NORMAL;
374 /* returns 1 if user u has channel c in their record, 0 if not */
376 int has_channel(userrec *u, chanrec *c)
380 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
383 for (unsigned int i =0; i < u->chans.size(); i++)
385 if (u->chans[i].channel)
387 if (u->chans[i].channel == c)
397 void TidyBan(char *ban)
400 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
404 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
406 strlcpy(temp,ban,MAXBUF);
408 char* pos_of_pling = strchr(temp,'!');
409 char* pos_of_at = strchr(temp,'@');
411 pos_of_pling[0] = '\0';
416 strlcpy(NICK,temp,NICKMAX-1);
417 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
418 strlcpy(HOST,pos_of_at,63);
420 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
425 std::string chlist(userrec *user,userrec* source)
427 std::string cmp = "";
428 std::string lst = "";
429 log(DEBUG,"chlist: %s",user->nick);
434 bool userinvisible = (strchr(user->modes,'i'));
435 for (unsigned int i = 0; i < user->chans.size(); i++)
437 if (user->chans[i].channel != NULL)
439 if (user->chans[i].channel->name)
441 cmp = std::string(user->chans[i].channel->name) + " ";
442 if (!strstr(lst.c_str(),cmp.c_str()))
444 // if the channel is NOT private/secret, OR the source user is on the channel, AND the user is not invisible.
445 // if the user is the same as the source, shortcircuit the comparison.
446 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)))))
448 lst = lst + std::string(cmode(user,user->chans[i].channel)) + std::string(user->chans[i].channel->name) + " ";