1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 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"
22 #include "inspircd_util.h"
25 #include <sys/errno.h>
26 #include <sys/utsname.h>
30 #include <ext/hash_map>
46 #include "inspstring.h"
48 #include "helperfuncs.h"
51 extern std::vector<Module*> modules;
52 extern std::vector<ircd_module*> factory;
54 extern ServerConfig* Config;
56 /* return 0 or 1 depending if users u and u2 share one or more common channels
57 * (used by QUIT, NICK etc which arent channel specific notices) */
59 int common_channels(userrec *u, userrec *u2)
63 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
66 for (unsigned int i = 0; i < u->chans.size(); i++)
68 for (unsigned int z = 0; z != u2->chans.size(); z++)
70 if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
72 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))
74 if ((c_count(u)) && (c_count(u2)))
85 void tidystring(char* str)
87 // strips out double spaces before a : parameter
95 // pointer voodoo++ --w00t
96 while ((*str) && (*str == ' '))
101 bool noparse = false;
104 const int lenofstr = strlen(str);
107 * by caching strlen() of str, we theoretically avoid 3 expensive calls each time this loop
108 * rolls around.. should speed things up a nanosecond or two. ;)
113 if ((a < lenofstr - 1) && (noparse == false))
115 if ((str[a] == ' ') && (str[a+1] == ' '))
117 log(DEBUG,"Tidied extra space out of string: %s",str);
123 if (a < lenofstr - 1)
125 if ((str[a] == ' ') && (str[a+1] == ':'))
131 temp[t++] = str[a++];
135 strlcpy(str,temp,MAXBUF);
139 /* chop a string down to 512 characters and preserve linefeed (irc max
146 log(DEBUG,"ERROR! Null string passed to chop()!");
150 FOREACH_MOD OnServerRaw(temp,false,NULL);
151 const char* str2 = temp.c_str();
152 snprintf(str,MAXBUF,"%s",str2);
153 if (strlen(str) >= 511)
158 log(DEBUG,"Excess line chopped.");
166 log(DEBUG,"Blocking: %d",s);
167 flags = fcntl(s, F_GETFL, 0);
168 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
171 void NonBlocking(int s)
174 log(DEBUG,"NonBlocking: %d",s);
175 flags = fcntl(s, F_GETFL, 0);
176 fcntl(s, F_SETFL, flags | O_NONBLOCK);
179 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
181 DNS d(Config->DNSServer);
182 int fd = d.ReverseLookup(unresolvedHost);
185 time_t T = time(NULL)+1;
186 while ((!d.HasResult()) && (time(NULL)<T));
187 std::string ipaddr = d.GetResult();
188 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
189 return (ipaddr != "");
192 int c_count(userrec* u)
195 for (unsigned int i =0; i < u->chans.size(); i++)
196 if (u->chans[i].channel != NULL)
202 bool hasumode(userrec* user, char mode)
206 return (strchr(user->modes,mode)>0);
212 void ChangeName(userrec* user, const char* gecos)
217 FOREACH_RESULT(OnChangeLocalUserGECOS(user,gecos));
220 FOREACH_MOD OnChangeName(user,gecos);
222 strlcpy(user->fullname,gecos,MAXBUF);
225 void ChangeDisplayedHost(userrec* user, const char* host)
230 FOREACH_RESULT(OnChangeLocalUserHost(user,host));
233 FOREACH_MOD OnChangeHost(user,host);
235 strlcpy(user->dhost,host,160);
238 /* verify that a user's ident and nickname is valid */
240 int isident(const char* n)
251 for (unsigned int i = 0; i < strlen(n); i++)
253 if ((n[i] < 33) || (n[i] > 125))
257 /* can't occur ANYWHERE in an Ident! */
258 if (strchr("<>,/?:;@'~#=+()*&%$£ \"!",n[i]))
267 int isnick(const char* n)
277 if (strlen(n) > NICKMAX)
281 for (unsigned int i = 0; i != strlen(n); i++)
283 if ((n[i] < 33) || (n[i] > 125))
287 /* can't occur ANYWHERE in a nickname! */
288 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
292 /* can't occur as the first char of a nickname... */
293 if ((strchr("0123456789",n[i])) && (!i))
301 /* returns the status character for a given user on a channel, e.g. @ for op,
302 * % for halfop etc. If the user has several modes set, the highest mode
303 * the user has must be returned. */
305 char* cmode(userrec *user, chanrec *chan)
307 if ((!user) || (!chan))
309 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
313 for (unsigned int i = 0; i < user->chans.size(); i++)
315 if (user->chans[i].channel)
317 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
319 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
323 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
327 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
338 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
339 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
340 * highest mode the user has must be returned. */
342 int cstatus(userrec *user, chanrec *chan)
344 if ((!chan) || (!user))
346 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
350 if (is_uline(user->server))
353 for (unsigned int i = 0; i < user->chans.size(); i++)
355 if (user->chans[i].channel)
357 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
359 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
363 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
367 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
371 return STATUS_NORMAL;
375 return STATUS_NORMAL;
378 /* returns 1 if user u has channel c in their record, 0 if not */
380 int has_channel(userrec *u, chanrec *c)
384 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
387 for (unsigned int i =0; i < u->chans.size(); i++)
389 if (u->chans[i].channel)
391 if (!strcasecmp(u->chans[i].channel->name,c->name))
401 void TidyBan(char *ban)
404 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
408 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
410 strlcpy(temp,ban,MAXBUF);
412 char* pos_of_pling = strchr(temp,'!');
413 char* pos_of_at = strchr(temp,'@');
415 pos_of_pling[0] = '\0';
420 strlcpy(NICK,temp,NICKMAX);
421 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
422 strlcpy(HOST,pos_of_at,160);
424 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
429 std::string chlist(userrec *user,userrec* source)
431 std::string cmp = "";
432 std::string lst = "";
433 log(DEBUG,"chlist: %s",user->nick);
438 for (unsigned int i = 0; i < user->chans.size(); i++)
440 if (user->chans[i].channel != NULL)
442 if (user->chans[i].channel->name)
444 cmp = std::string(user->chans[i].channel->name) + " ";
445 if (!strstr(lst.c_str(),cmp.c_str()))
447 // if the channel is NOT private/secret, OR the source user is on the channel
448 if (((!(user->chans[i].channel->binarymodes & CM_PRIVATE)) && (!(user->chans[i].channel->binarymodes & CM_SECRET))) || (has_channel(source,user->chans[i].channel)))
450 lst = lst + std::string(cmode(user,user->chans[i].channel)) + std::string(user->chans[i].channel->name) + " ";