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 "inspircd_io.h"
24 #include <sys/errno.h>
25 #include <sys/utsname.h>
29 #include <ext/hash_map>
45 #include "inspstring.h"
47 #include "helperfuncs.h"
50 extern std::vector<Module*> modules;
51 extern std::vector<ircd_module*> factory;
53 extern ServerConfig* Config;
55 /* return 0 or 1 depending if users u and u2 share one or more common channels
56 * (used by QUIT, NICK etc which arent channel specific notices) */
58 int common_channels(userrec *u, userrec *u2)
62 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
65 for (unsigned int i = 0; i < u->chans.size(); i++)
67 for (unsigned int z = 0; z != u2->chans.size(); z++)
69 if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
71 if ((!strcasecmp(u->chans[i].channel->name,u2->chans[z].channel->name)) && (u->chans[i].channel) && (u2->chans[z].channel) && (u->registered == 7) && (u2->registered == 7))
73 if ((c_count(u)) && (c_count(u2)))
84 void tidystring(char* str)
86 // strips out double spaces before a : parameter
94 // pointer voodoo++ --w00t
95 while ((*str) && (*str == ' '))
100 bool noparse = false;
103 const int lenofstr = strlen(str);
106 * by caching strlen() of str, we theoretically avoid 3 expensive calls each time this loop
107 * rolls around.. should speed things up a nanosecond or two. ;)
112 if ((a < lenofstr - 1) && (noparse == false))
114 if ((str[a] == ' ') && (str[a+1] == ' '))
116 log(DEBUG,"Tidied extra space out of string: %s",str);
122 if (a < lenofstr - 1)
124 if ((str[a] == ' ') && (str[a+1] == ':'))
130 temp[t++] = str[a++];
134 strlcpy(str,temp,MAXBUF);
138 /* chop a string down to 512 characters and preserve linefeed (irc max
145 log(DEBUG,"ERROR! Null string passed to chop()!");
148 if (strlen(str) >= 511)
153 log(DEBUG,"Excess line chopped.");
161 log(DEBUG,"Blocking: %d",s);
162 flags = fcntl(s, F_GETFL, 0);
163 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
166 void NonBlocking(int s)
169 log(DEBUG,"NonBlocking: %d",s);
170 flags = fcntl(s, F_GETFL, 0);
171 fcntl(s, F_SETFL, flags | O_NONBLOCK);
174 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
176 DNS d(Config->DNSServer);
177 int fd = d.ReverseLookup(unresolvedHost);
180 time_t T = time(NULL)+1;
181 while ((!d.HasResult()) && (time(NULL)<T));
182 std::string ipaddr = d.GetResult();
183 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
184 return (ipaddr != "");
187 int c_count(userrec* u)
190 for (unsigned int i =0; i < u->chans.size(); i++)
191 if (u->chans[i].channel != NULL)
197 bool hasumode(userrec* user, char mode)
201 return (strchr(user->modes,mode)>0);
207 void ChangeName(userrec* user, const char* gecos)
212 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(user,gecos));
215 FOREACH_MOD(I_OnChangeName,OnChangeName(user,gecos));
217 strlcpy(user->fullname,gecos,MAXBUF);
220 void ChangeDisplayedHost(userrec* user, const char* host)
225 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(user,host));
228 FOREACH_MOD(I_OnChangeHost,OnChangeHost(user,host));
230 strlcpy(user->dhost,host,160);
233 /* verify that a user's ident and nickname is valid */
235 int isident(const char* n)
246 for (unsigned int i = 0; i < strlen(n); i++)
248 if ((n[i] < 33) || (n[i] > 125))
252 /* can't occur ANYWHERE in an Ident! */
253 if (strchr("<>,/?:;@'~#=+()*&%$£ \"!",n[i]))
262 int isnick(const char* n)
272 if (strlen(n) > NICKMAX)
276 for (unsigned int i = 0; i != strlen(n); i++)
278 if ((n[i] < 33) || (n[i] > 125))
282 /* can't occur ANYWHERE in a nickname! */
283 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
287 /* can't occur as the first char of a nickname... */
288 if ((strchr("0123456789",n[i])) && (!i))
296 /* returns the status character for a given user on a channel, e.g. @ for op,
297 * % for halfop etc. If the user has several modes set, the highest mode
298 * the user has must be returned. */
300 char* cmode(userrec *user, chanrec *chan)
302 if ((!user) || (!chan))
304 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
308 for (unsigned int i = 0; i < user->chans.size(); i++)
310 if (user->chans[i].channel)
312 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
314 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
318 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
322 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
333 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
334 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
335 * highest mode the user has must be returned. */
337 int cstatus(userrec *user, chanrec *chan)
339 if ((!chan) || (!user))
341 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
345 if (is_uline(user->server))
348 for (unsigned int i = 0; i < user->chans.size(); i++)
350 if (user->chans[i].channel)
352 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
354 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
358 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
362 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
366 return STATUS_NORMAL;
370 return STATUS_NORMAL;
373 /* returns 1 if user u has channel c in their record, 0 if not */
375 int has_channel(userrec *u, chanrec *c)
379 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
382 for (unsigned int i =0; i < u->chans.size(); i++)
384 if (u->chans[i].channel)
386 if (!strcasecmp(u->chans[i].channel->name,c->name))
396 void TidyBan(char *ban)
399 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
403 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
405 strlcpy(temp,ban,MAXBUF);
407 char* pos_of_pling = strchr(temp,'!');
408 char* pos_of_at = strchr(temp,'@');
410 pos_of_pling[0] = '\0';
415 strlcpy(NICK,temp,NICKMAX);
416 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
417 strlcpy(HOST,pos_of_at,160);
419 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
424 std::string chlist(userrec *user,userrec* source)
426 std::string cmp = "";
427 std::string lst = "";
428 log(DEBUG,"chlist: %s",user->nick);
433 for (unsigned int i = 0; i < user->chans.size(); i++)
435 if (user->chans[i].channel != NULL)
437 if (user->chans[i].channel->name)
439 cmp = std::string(user->chans[i].channel->name) + " ";
440 if (!strstr(lst.c_str(),cmp.c_str()))
442 // if the channel is NOT private/secret, OR the source user is on the channel
443 if (((!(user->chans[i].channel->binarymodes & CM_PRIVATE)) && (!(user->chans[i].channel->binarymodes & CM_SECRET))) || (has_channel(source,user->chans[i].channel)))
445 lst = lst + std::string(cmode(user,user->chans[i].channel)) + std::string(user->chans[i].channel->name) + " ";