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 if ((!u) || (!u2) || (u->registered != REG_ALL) || (u2->registered != REG_ALL))
69 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
71 /* Fetch the channel from the user */
72 ucrec* user_channel = (ucrec*)(*i);
74 if (user_channel->channel)
76 /* Eliminate the inner loop (which used to be ~equal in size to the outer loop)
77 * by replacing it with a map::find which *should* be more efficient
79 CUList* channel_user_map = user_channel->channel->GetUsers();
80 if (channel_user_map->find(u2) != channel_user_map->end())
89 int flags = fcntl(s, F_GETFL, 0);
90 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
93 void NonBlocking(int s)
95 int flags = fcntl(s, F_GETFL, 0);
96 fcntl(s, F_SETFL, flags | O_NONBLOCK);
99 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost, bool forward)
104 DNS d(Config->DNSServer);
106 ok = d.ForwardLookup(unresolvedHost, false);
108 ok = d.ReverseLookup(unresolvedHost, false);
111 time_t T = time(NULL)+1;
112 while ((!d.HasResult()) && (time(NULL)<T));
114 ipaddr = d.GetResultIP();
116 ipaddr = d.GetResult();
117 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
118 return (ipaddr != "");
121 int c_count(userrec* u)
124 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
125 if (((ucrec*)(*i))->channel)
131 void ChangeName(userrec* user, const char* gecos)
136 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
139 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
141 strlcpy(user->fullname,gecos,MAXGECOS+1);
144 void ChangeDisplayedHost(userrec* user, const char* host)
149 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
152 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
154 strlcpy(user->dhost,host,63);
155 WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
158 /* verify that a user's ident and nickname is valid */
160 int isident(const char* n)
166 for (char* i = (char*)n; *i; i++)
168 if ((*i >= 'A') && (*i <= '}'))
172 if (strchr(".-0123456789",*i))
182 int isnick(const char* n)
189 for (char* i = (char*)n; *i; i++, p++)
191 /* can occur anywhere in a nickname */
192 if ((*i >= 'A') && (*i <= '}'))
196 /* can occur anywhere BUT the first char of a nickname */
197 if ((strchr("-0123456789",*i)) && (i > n))
201 /* invalid character! abort */
204 return (p < NICKMAX - 1);
207 /* returns the status character for a given user on a channel, e.g. @ for op,
208 * % for halfop etc. If the user has several modes set, the highest mode
209 * the user has must be returned. */
211 const char* cmode(userrec *user, chanrec *chan)
213 if ((!user) || (!chan))
215 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
219 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
221 if (((ucrec*)(*i))->channel == chan)
223 if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
227 if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
231 if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
241 int cflags(userrec *user, chanrec *chan)
243 if ((!chan) || (!user))
246 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
248 if (((ucrec*)(*i))->channel == chan)
250 return ((ucrec*)(*i))->uc_modes;
256 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
257 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
258 * highest mode the user has must be returned. */
260 int cstatus(userrec *user, chanrec *chan)
262 if ((!chan) || (!user))
264 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
268 if (is_uline(user->server))
271 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
273 if (((ucrec*)(*i))->channel == chan)
275 if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
279 if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
283 if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
287 return STATUS_NORMAL;
290 return STATUS_NORMAL;
293 void TidyBan(char *ban)
296 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
300 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
302 strlcpy(temp,ban,MAXBUF);
304 char* pos_of_pling = strchr(temp,'!');
305 char* pos_of_at = strchr(temp,'@');
307 pos_of_pling[0] = '\0';
312 strlcpy(NICK,temp,NICKMAX-1);
313 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
314 strlcpy(HOST,pos_of_at,63);
316 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
321 std::string chlist(userrec *user,userrec* source)
325 if (!user || !source)
328 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
332 if(rec->channel && rec->channel->name)
334 /* XXX - Why does this check need to be here at all? :< */
335 /* Commenting this out until someone finds a case where we need it */
336 //if (lst.find(rec->channel->name) == std::string::npos)
340 * If the target is the same as the sender, let them see all their channels.
341 * If the channel is NOT private/secret AND the user is not invisible.
342 * If the user is an oper, and the <options:operspywhois> option is set.
344 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))))
346 list.append(cmode(user, rec->channel)).append(rec->channel->name).append(" ");