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 int common_channels(userrec *u, userrec *u2)
56 if ((!u) || (!u2) || (u->registered != 7) || (u2->registered != 7))
60 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
62 for (std::vector<ucrec*>::const_iterator z = u2->chans.begin(); z != u2->chans.end(); z++)
64 if ((((ucrec*)(*i))->channel != NULL) && (((ucrec*)(*z))->channel != NULL))
66 if ((((ucrec*)(*i))->channel == ((ucrec*)(*z))->channel))
68 if ((c_count(u)) && (c_count(u2)))
81 int flags = fcntl(s, F_GETFL, 0);
82 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
85 void NonBlocking(int s)
87 int flags = fcntl(s, F_GETFL, 0);
88 fcntl(s, F_SETFL, flags | O_NONBLOCK);
91 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost, bool forward)
96 DNS d(Config->DNSServer);
98 ok = d.ForwardLookup(unresolvedHost, false);
100 ok = d.ReverseLookup(unresolvedHost, false);
103 time_t T = time(NULL)+1;
104 while ((!d.HasResult()) && (time(NULL)<T));
106 ipaddr = d.GetResultIP();
108 ipaddr = d.GetResult();
109 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
110 return (ipaddr != "");
113 int c_count(userrec* u)
116 for (std::vector<ucrec*>::const_iterator i = u->chans.begin(); i != u->chans.end(); i++)
117 if (((ucrec*)(*i))->channel)
123 void ChangeName(userrec* user, const char* gecos)
128 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
131 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
133 strlcpy(user->fullname,gecos,MAXGECOS+1);
136 void ChangeDisplayedHost(userrec* user, const char* host)
141 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
144 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
146 strlcpy(user->dhost,host,63);
147 WriteServ(user->fd,"396 %s %s :is now your hidden host",user->nick,user->dhost);
150 /* verify that a user's ident and nickname is valid */
152 int isident(const char* n)
158 for (char* i = (char*)n; *i; i++)
160 if ((*i >= 'A') && (*i <= '}'))
164 if (strchr(".-0123456789",*i))
174 int isnick(const char* n)
181 for (char* i = (char*)n; *i; i++, p++)
183 /* can occur anywhere in a nickname */
184 if ((*i >= 'A') && (*i <= '}'))
188 /* can occur anywhere BUT the first char of a nickname */
189 if ((strchr("-0123456789",*i)) && (i > n))
193 /* invalid character! abort */
196 return (p < NICKMAX - 1);
199 /* returns the status character for a given user on a channel, e.g. @ for op,
200 * % for halfop etc. If the user has several modes set, the highest mode
201 * the user has must be returned. */
203 const char* cmode(userrec *user, chanrec *chan)
205 if ((!user) || (!chan))
207 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
211 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
213 if (((ucrec*)(*i))->channel == chan)
215 if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
219 if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
223 if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
233 int cflags(userrec *user, chanrec *chan)
235 if ((!chan) || (!user))
238 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
240 if (((ucrec*)(*i))->channel == chan)
242 return ((ucrec*)(*i))->uc_modes;
248 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
249 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
250 * highest mode the user has must be returned. */
252 int cstatus(userrec *user, chanrec *chan)
254 if ((!chan) || (!user))
256 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
260 if (is_uline(user->server))
263 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
265 if (((ucrec*)(*i))->channel == chan)
267 if ((((ucrec*)(*i))->uc_modes & UCMODE_OP) > 0)
271 if ((((ucrec*)(*i))->uc_modes & UCMODE_HOP) > 0)
275 if ((((ucrec*)(*i))->uc_modes & UCMODE_VOICE) > 0)
279 return STATUS_NORMAL;
282 return STATUS_NORMAL;
285 void TidyBan(char *ban)
288 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
292 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
294 strlcpy(temp,ban,MAXBUF);
296 char* pos_of_pling = strchr(temp,'!');
297 char* pos_of_at = strchr(temp,'@');
299 pos_of_pling[0] = '\0';
304 strlcpy(NICK,temp,NICKMAX-1);
305 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
306 strlcpy(HOST,pos_of_at,63);
308 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
313 std::string chlist(userrec *user,userrec* source)
315 /* Should this be a stringstream? Not sure if it would be faster as streams are more oriented at appending stuff, which is all we do */
316 std::ostringstream list;
318 if (!user || !source)
321 for (std::vector<ucrec*>::const_iterator i = user->chans.begin(); i != user->chans.end(); i++)
325 if(rec->channel && rec->channel->name)
327 /* XXX - Why does this check need to be here at all? :< */
328 /* Commenting this out until someone finds a case where we need it */
329 //if (lst.find(rec->channel->name) == std::string::npos)
333 * If the target is the same as the sender, let them see all their channels.
334 * If the channel is NOT private/secret AND the user is not invisible.
335 * If the user is an oper, and the <options:operspywhois> option is set.
337 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))))
339 list << cmode(user, rec->channel) << rec->channel->name << " ";