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"
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()!");
149 FOREACH_MOD OnServerRaw(temp,false,NULL);
150 const char* str2 = temp.c_str();
151 snprintf(str,MAXBUF,"%s",str2);
152 if (strlen(str) >= 511)
157 log(DEBUG,"Excess line chopped.");
165 log(DEBUG,"Blocking: %d",s);
166 flags = fcntl(s, F_GETFL, 0);
167 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
170 void NonBlocking(int s)
173 log(DEBUG,"NonBlocking: %d",s);
174 flags = fcntl(s, F_GETFL, 0);
175 fcntl(s, F_SETFL, flags | O_NONBLOCK);
178 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
180 DNS d(Config->DNSServer);
181 int fd = d.ReverseLookup(unresolvedHost);
184 time_t T = time(NULL)+1;
185 while ((!d.HasResult()) && (time(NULL)<T));
186 std::string ipaddr = d.GetResult();
187 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
188 return (ipaddr != "");
191 int c_count(userrec* u)
194 for (unsigned int i =0; i < u->chans.size(); i++)
195 if (u->chans[i].channel != NULL)
201 bool hasumode(userrec* user, char mode)
205 return (strchr(user->modes,mode)>0);
211 void ChangeName(userrec* user, const char* gecos)
216 FOREACH_RESULT(OnChangeLocalUserGECOS(user,gecos));
219 FOREACH_MOD OnChangeName(user,gecos);
221 strlcpy(user->fullname,gecos,MAXBUF);
224 void ChangeDisplayedHost(userrec* user, const char* host)
229 FOREACH_RESULT(OnChangeLocalUserHost(user,host));
232 FOREACH_MOD OnChangeHost(user,host);
234 strlcpy(user->dhost,host,160);
237 /* verify that a user's ident and nickname is valid */
239 int isident(const char* n)
250 for (unsigned int i = 0; i < strlen(n); i++)
252 if ((n[i] < 33) || (n[i] > 125))
256 /* can't occur ANYWHERE in an Ident! */
257 if (strchr("<>,/?:;@'~#=+()*&%$£ \"!",n[i]))
266 int isnick(const char* n)
276 if (strlen(n) > NICKMAX)
280 for (unsigned int i = 0; i != strlen(n); i++)
282 if ((n[i] < 33) || (n[i] > 125))
286 /* can't occur ANYWHERE in a nickname! */
287 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
291 /* can't occur as the first char of a nickname... */
292 if ((strchr("0123456789",n[i])) && (!i))
300 /* returns the status character for a given user on a channel, e.g. @ for op,
301 * % for halfop etc. If the user has several modes set, the highest mode
302 * the user has must be returned. */
304 char* cmode(userrec *user, chanrec *chan)
306 if ((!user) || (!chan))
308 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
312 for (unsigned int i = 0; i < user->chans.size(); i++)
314 if (user->chans[i].channel)
316 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
318 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
322 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
326 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
337 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
338 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
339 * highest mode the user has must be returned. */
341 int cstatus(userrec *user, chanrec *chan)
343 if ((!chan) || (!user))
345 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
349 if (is_uline(user->server))
352 for (unsigned int i = 0; i < user->chans.size(); i++)
354 if (user->chans[i].channel)
356 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
358 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
362 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
366 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
370 return STATUS_NORMAL;
374 return STATUS_NORMAL;
377 /* returns 1 if user u has channel c in their record, 0 if not */
379 int has_channel(userrec *u, chanrec *c)
383 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
386 for (unsigned int i =0; i < u->chans.size(); i++)
388 if (u->chans[i].channel)
390 if (!strcasecmp(u->chans[i].channel->name,c->name))
400 void TidyBan(char *ban)
403 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
407 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
409 strlcpy(temp,ban,MAXBUF);
411 char* pos_of_pling = strchr(temp,'!');
412 char* pos_of_at = strchr(temp,'@');
414 pos_of_pling[0] = '\0';
419 strlcpy(NICK,temp,NICKMAX);
420 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
421 strlcpy(HOST,pos_of_at,160);
423 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
428 std::string chlist(userrec *user,userrec* source)
430 std::string cmp = "";
431 std::string lst = "";
432 log(DEBUG,"chlist: %s",user->nick);
437 for (unsigned int i = 0; i < user->chans.size(); i++)
439 if (user->chans[i].channel != NULL)
441 if (user->chans[i].channel->name)
443 cmp = std::string(user->chans[i].channel->name) + " ";
444 if (!strstr(lst.c_str(),cmp.c_str()))
446 // if the channel is NOT private/secret, OR the source user is on the channel
447 if (((!(user->chans[i].channel->binarymodes & CM_PRIVATE)) && (!(user->chans[i].channel->binarymodes & CM_SECRET))) || (has_channel(source,user->chans[i].channel)))
449 lst = lst + std::string(cmode(user,user->chans[i].channel)) + std::string(user->chans[i].channel->name) + " ";