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 c_count(userrec* u)
101 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
108 void ChangeName(userrec* user, const char* gecos)
113 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
116 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
118 strlcpy(user->fullname,gecos,MAXGECOS+1);
121 void ChangeDisplayedHost(userrec* user, const char* host)
126 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
129 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
131 strlcpy(user->dhost,host,63);
132 WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
135 /* verify that a user's ident and nickname is valid */
137 int isident(const char* n)
143 for (char* i = (char*)n; *i; i++)
145 if ((*i >= 'A') && (*i <= '}'))
149 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
159 int isnick(const char* n)
166 for (char* i = (char*)n; *i; i++, p++)
168 /* "A"-"}" can occur anywhere in a nickname */
169 if ((*i >= 'A') && (*i <= '}'))
173 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
174 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
178 /* invalid character! abort */
181 return (p < NICKMAX - 1);
184 /* returns the status character for a given user on a channel, e.g. @ for op,
185 * % for halfop etc. If the user has several modes set, the highest mode
186 * the user has must be returned. */
188 const char* cmode(userrec *user, chanrec *chan)
190 if ((!user) || (!chan))
192 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
196 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
198 if ((*i)->channel == chan)
200 if (((*i)->uc_modes & UCMODE_OP) > 0)
204 if (((*i)->uc_modes & UCMODE_HOP) > 0)
208 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
218 int cflags(userrec *user, chanrec *chan)
220 if ((!chan) || (!user))
223 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
225 if ((*i)->channel == chan)
227 return (*i)->uc_modes;
233 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
234 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
235 * highest mode the user has must be returned. */
237 int cstatus(userrec *user, chanrec *chan)
239 if ((!chan) || (!user))
241 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
245 if (is_uline(user->server))
248 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
250 if ((*i)->channel == chan)
252 if (((*i)->uc_modes & UCMODE_OP) > 0)
256 if (((*i)->uc_modes & UCMODE_HOP) > 0)
260 if (((*i)->uc_modes & UCMODE_VOICE) > 0)
264 return STATUS_NORMAL;
267 return STATUS_NORMAL;
270 std::string chlist(userrec *user,userrec* source)
274 if (!user || !source)
277 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
281 if(rec->channel && rec->channel->name)
283 /* If the target is the same as the sender, let them see all their channels.
284 * If the channel is NOT private/secret OR the user shares a common channel
285 * If the user is an oper, and the <options:operspywhois> option is set.
287 if ((source == user) || (*source->oper && Config->OperSpyWhois) || (((!rec->channel->modes[CM_PRIVATE]) && (!rec->channel->modes[CM_SECRET])) || (rec->channel->HasUser(source))))
289 list.append(cmode(user, rec->channel)).append(rec->channel->name).append(" ");