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 * The old algorithm in 1.0 for this was relatively inefficient, iterating over
55 * the first users channels then the second users channels within the outer loop,
56 * therefore it was a maximum of x*y iterations (upon returning 0 and checking
57 * all possible iterations). However this new function instead checks against the
58 * channel's userlist in the inner loop which is a std::map<userrec*,userrec*>
59 * and saves us time as we already know what pointer value we are after.
60 * Don't quote me on the maths as i am not a mathematician or computer scientist,
61 * but i believe this algorithm is now x+(log y) maximum iterations instead.
63 int common_channels(userrec *u, userrec *u2)
65 /* TODO: We need to get rid of this arbitary '7' and make bitmask enums for it */
66 if ((!u) || (!u2) || (u->registered != 7) || (u2->registered != 7))
70 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
72 /* Fetch the channel from the user */
73 ucrec* user_channel = (ucrec*)(*i);
75 if (user_channel->channel)
77 /* Eliminate the inner loop (which used to be ~equal in size to the outer loop)
78 * by replacing it with a map::find which *should* be more efficient
80 CUList* channel_user_map = user_channel->channel->GetUsers();
81 if (channel_user_map->find(u2) != channel_user_map->end())
90 int flags = fcntl(s, F_GETFL, 0);
91 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
94 void NonBlocking(int s)
96 int flags = fcntl(s, F_GETFL, 0);
97 fcntl(s, F_SETFL, flags | O_NONBLOCK);
100 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost, bool forward)
105 DNS d(Config->DNSServer);
107 ok = d.ForwardLookup(unresolvedHost, false);
109 ok = d.ReverseLookup(unresolvedHost, false);
112 time_t T = time(NULL)+1;
113 while ((!d.HasResult()) && (time(NULL)<T));
115 ipaddr = d.GetResultIP();
117 ipaddr = d.GetResult();
118 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
119 return (ipaddr != "");
122 int c_count(userrec* u)
125 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
126 if (((ucrec*)(*i))->channel)
132 void ChangeName(userrec* user, const char* gecos)
137 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
140 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
142 strlcpy(user->fullname,gecos,MAXGECOS+1);
145 void ChangeDisplayedHost(userrec* user, const char* host)
150 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
153 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
155 strlcpy(user->dhost,host,63);
156 WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
159 /* verify that a user's ident and nickname is valid */
161 int isident(const char* n)
167 for (char* i = (char*)n; *i; i++)
169 if ((*i >= 'A') && (*i <= '}'))
173 if (strchr(".-0123456789",*i))
183 int isnick(const char* n)
190 for (char* i = (char*)n; *i; i++, p++)
192 /* can occur anywhere in a nickname */
193 if ((*i >= 'A') && (*i <= '}'))
197 /* can occur anywhere BUT the first char of a nickname */
198 if ((strchr("-0123456789",*i)) && (i > n))
202 /* invalid character! abort */
205 return (p < NICKMAX - 1);
208 /* returns the status character for a given user on a channel, e.g. @ for op,
209 * % for halfop etc. If the user has several modes set, the highest mode
210 * the user has must be returned. */
212 const char* cmode(userrec *user, chanrec *chan)
214 if ((!user) || (!chan))
216 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
220 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
222 if (((ucrec*)(*i))->channel == chan)
224 if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
228 if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
232 if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
242 int cflags(userrec *user, chanrec *chan)
244 if ((!chan) || (!user))
247 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
249 if (((ucrec*)(*i))->channel == chan)
251 return ((ucrec*)(*i))->uc_modes;
257 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
258 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
259 * highest mode the user has must be returned. */
261 int cstatus(userrec *user, chanrec *chan)
263 if ((!chan) || (!user))
265 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
269 if (is_uline(user->server))
272 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
274 if (((ucrec*)(*i))->channel == chan)
276 if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
280 if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
284 if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
288 return STATUS_NORMAL;
291 return STATUS_NORMAL;
294 void TidyBan(char *ban)
297 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
301 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
303 strlcpy(temp,ban,MAXBUF);
305 char* pos_of_pling = strchr(temp,'!');
306 char* pos_of_at = strchr(temp,'@');
308 pos_of_pling[0] = '\0';
313 strlcpy(NICK,temp,NICKMAX-1);
314 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
315 strlcpy(HOST,pos_of_at,63);
317 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
322 std::string chlist(userrec *user,userrec* source)
326 if (!user || !source)
329 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
333 if(rec->channel && rec->channel->name)
335 /* XXX - Why does this check need to be here at all? :< */
336 /* Commenting this out until someone finds a case where we need it */
337 //if (lst.find(rec->channel->name) == std::string::npos)
341 * If the target is the same as the sender, let them see all their channels.
342 * If the channel is NOT private/secret AND the user is not invisible.
343 * If the user is an oper, and the <options:operspywhois> option is set.
345 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))))
347 list.append(cmode(user, rec->channel)).append(rec->channel->name).append(" ");