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 = *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 if (user_channel->channel->HasUser(u2))
88 int flags = fcntl(s, F_GETFL, 0);
89 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
92 void NonBlocking(int s)
94 int flags = fcntl(s, F_GETFL, 0);
95 fcntl(s, F_SETFL, flags | O_NONBLOCK);
98 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost, bool forward, unsigned long timeout)
103 DNS d(Config->DNSServer);
105 ok = d.ForwardLookup(unresolvedHost, false);
107 ok = d.ReverseLookup(unresolvedHost, false);
110 time_t T = time(NULL)+timeout;
111 while ((!d.HasResult()) && (time(NULL)<T));
113 ipaddr = d.GetResultIP();
115 ipaddr = d.GetResult();
116 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
117 return (ipaddr != "");
120 int c_count(userrec* u)
123 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
124 if (((ucrec*)(*i))->channel)
130 void ChangeName(userrec* user, const char* gecos)
135 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
138 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
140 strlcpy(user->fullname,gecos,MAXGECOS+1);
143 void ChangeDisplayedHost(userrec* user, const char* host)
148 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
151 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
153 strlcpy(user->dhost,host,63);
154 WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
157 /* verify that a user's ident and nickname is valid */
159 int isident(const char* n)
165 for (char* i = (char*)n; *i; i++)
167 if ((*i >= 'A') && (*i <= '}'))
171 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
181 int isnick(const char* n)
188 for (char* i = (char*)n; *i; i++, p++)
190 /* "A"-"}" can occur anywhere in a nickname */
191 if ((*i >= 'A') && (*i <= '}'))
195 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
196 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
200 /* invalid character! abort */
203 return (p < NICKMAX - 1);
206 /* returns the status character for a given user on a channel, e.g. @ for op,
207 * % for halfop etc. If the user has several modes set, the highest mode
208 * the user has must be returned. */
210 const char* cmode(userrec *user, chanrec *chan)
212 if ((!user) || (!chan))
214 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
218 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
220 if (((ucrec*)(*i))->channel == chan)
222 if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
226 if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
230 if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
240 int cflags(userrec *user, chanrec *chan)
242 if ((!chan) || (!user))
245 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
247 if (((ucrec*)(*i))->channel == chan)
249 return ((ucrec*)(*i))->uc_modes;
255 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
256 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
257 * highest mode the user has must be returned. */
259 int cstatus(userrec *user, chanrec *chan)
261 if ((!chan) || (!user))
263 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
267 if (is_uline(user->server))
270 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
272 if (((ucrec*)(*i))->channel == chan)
274 if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
278 if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
282 if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
286 return STATUS_NORMAL;
289 return STATUS_NORMAL;
292 std::string chlist(userrec *user,userrec* source)
296 if (!user || !source)
299 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
303 if(rec->channel && rec->channel->name)
305 /* If the target is the same as the sender, let them see all their channels.
306 * If the channel is NOT private/secret OR the user shares a common channel
307 * If the user is an oper, and the <options:operspywhois> option is set.
309 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))))
311 list.append(cmode(user, rec->channel)).append(rec->channel->name).append(" ");