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 int c_count(userrec* u)
106 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
113 void ChangeName(userrec* user, const char* gecos)
118 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
121 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
123 strlcpy(user->fullname,gecos,MAXGECOS+1);
126 void ChangeDisplayedHost(userrec* user, const char* host)
131 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
134 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
136 strlcpy(user->dhost,host,63);
137 WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
140 /* verify that a user's ident and nickname is valid */
142 int isident(const char* n)
148 for (char* i = (char*)n; *i; i++)
150 if ((*i >= 'A') && (*i <= '}'))
154 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
164 int isnick(const char* n)
171 for (char* i = (char*)n; *i; i++, p++)
173 /* "A"-"}" can occur anywhere in a nickname */
174 if ((*i >= 'A') && (*i <= '}'))
178 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
179 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
183 /* invalid character! abort */
186 return (p < NICKMAX - 1);
189 /* returns the status character for a given user on a channel, e.g. @ for op,
190 * % for halfop etc. If the user has several modes set, the highest mode
191 * the user has must be returned. */
193 const char* cmode(userrec *user, chanrec *chan)
195 if ((!user) || (!chan))
197 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
201 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
203 if ((*i)->channel == chan)
205 if (((*i)->uc_modes & UCMODE_OP) > 0)
209 if (((*i)->uc_modes & UCMODE_HOP) > 0)
213 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
223 int cflags(userrec *user, chanrec *chan)
225 if ((!chan) || (!user))
228 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
230 if ((*i)->channel == chan)
232 return (*i)->uc_modes;
238 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
239 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
240 * highest mode the user has must be returned. */
242 int cstatus(userrec *user, chanrec *chan)
244 if ((!chan) || (!user))
246 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
250 if (is_uline(user->server))
253 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
255 if ((*i)->channel == chan)
257 if (((*i)->uc_modes & UCMODE_OP) > 0)
261 if (((*i)->uc_modes & UCMODE_HOP) > 0)
265 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
269 return STATUS_NORMAL;
272 return STATUS_NORMAL;
275 std::string chlist(userrec *user,userrec* source)
279 if (!user || !source)
282 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
286 if(rec->channel && rec->channel->name)
288 /* If the target is the same as the sender, let them see all their channels.
289 * If the channel is NOT private/secret OR the user shares a common channel
290 * If the user is an oper, and the <options:operspywhois> option is set.
292 if ((source == user) || (*source->oper && Config->OperSpyWhois) || (((!rec->channel->modes[CM_PRIVATE]) && (!rec->channel->modes[CM_SECRET])) || (rec->channel->HasUser(source))))
294 list.append(cmode(user, rec->channel)).append(rec->channel->name).append(" ");