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 char ServerName[MAXBUF];
58 extern FILE *log_file;
59 extern char DNSServer[MAXBUF];
61 /* return 0 or 1 depending if users u and u2 share one or more common channels
62 * (used by QUIT, NICK etc which arent channel specific notices) */
64 int common_channels(userrec *u, userrec *u2)
68 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
71 for (unsigned int i = 0; i < u->chans.size(); i++)
73 for (unsigned int z = 0; z != u2->chans.size(); z++)
75 if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
77 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))
79 if ((c_count(u)) && (c_count(u2)))
90 void tidystring(char* str)
92 // strips out double spaces before a : parameter
100 // pointer voodoo++ --w00t
101 while ((*str) && (*str == ' '))
106 bool noparse = false;
109 const int lenofstr = strlen(str);
112 * by caching strlen() of str, we theoretically avoid 3 expensive calls each time this loop
113 * rolls around.. should speed things up a nanosecond or two. ;)
118 if ((a < lenofstr - 1) && (noparse == false))
120 if ((str[a] == ' ') && (str[a+1] == ' '))
122 log(DEBUG,"Tidied extra space out of string: %s",str);
128 if (a < lenofstr - 1)
130 if ((str[a] == ' ') && (str[a+1] == ':'))
136 temp[t++] = str[a++];
140 strlcpy(str,temp,MAXBUF);
144 /* chop a string down to 512 characters and preserve linefeed (irc max
151 log(DEBUG,"ERROR! Null string passed to chop()!");
155 FOREACH_MOD OnServerRaw(temp,false,NULL);
156 const char* str2 = temp.c_str();
157 snprintf(str,MAXBUF,"%s",str2);
158 if (strlen(str) >= 511)
163 log(DEBUG,"Excess line chopped.");
171 log(DEBUG,"Blocking: %d",s);
172 flags = fcntl(s, F_GETFL, 0);
173 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
176 void NonBlocking(int s)
179 log(DEBUG,"NonBlocking: %d",s);
180 flags = fcntl(s, F_GETFL, 0);
181 fcntl(s, F_SETFL, flags | O_NONBLOCK);
184 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
187 int fd = d.ReverseLookup(unresolvedHost);
190 time_t T = time(NULL)+1;
191 while ((!d.HasResult()) && (time(NULL)<T));
192 std::string ipaddr = d.GetResult();
193 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
194 return (ipaddr != "");
197 int c_count(userrec* u)
200 for (unsigned int i =0; i < u->chans.size(); i++)
201 if (u->chans[i].channel != NULL)
207 bool hasumode(userrec* user, char mode)
211 return (strchr(user->modes,mode)>0);
217 void ChangeName(userrec* user, const char* gecos)
222 FOREACH_RESULT(OnChangeLocalUserGECOS(user,gecos));
225 FOREACH_MOD OnChangeName(user,gecos);
227 strlcpy(user->fullname,gecos,MAXBUF);
230 void ChangeDisplayedHost(userrec* user, const char* host)
235 FOREACH_RESULT(OnChangeLocalUserHost(user,host));
238 FOREACH_MOD OnChangeHost(user,host);
240 strlcpy(user->dhost,host,160);
243 /* verify that a user's ident and nickname is valid */
245 int isident(const char* n)
256 for (unsigned int i = 0; i < strlen(n); i++)
258 if ((n[i] < 33) || (n[i] > 125))
262 /* can't occur ANYWHERE in an Ident! */
263 if (strchr("<>,/?:;@'~#=+()*&%$£ \"!",n[i]))
272 int isnick(const char* n)
282 if (strlen(n) > NICKMAX)
286 for (unsigned int i = 0; i != strlen(n); i++)
288 if ((n[i] < 33) || (n[i] > 125))
292 /* can't occur ANYWHERE in a nickname! */
293 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
297 /* can't occur as the first char of a nickname... */
298 if ((strchr("0123456789",n[i])) && (!i))
306 /* returns the status character for a given user on a channel, e.g. @ for op,
307 * % for halfop etc. If the user has several modes set, the highest mode
308 * the user has must be returned. */
310 char* cmode(userrec *user, chanrec *chan)
312 if ((!user) || (!chan))
314 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
318 for (unsigned int i = 0; i < user->chans.size(); i++)
320 if (user->chans[i].channel)
322 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
324 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
328 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
332 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
343 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
344 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
345 * highest mode the user has must be returned. */
347 int cstatus(userrec *user, chanrec *chan)
349 if ((!chan) || (!user))
351 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
355 if (is_uline(user->server))
358 for (unsigned int i = 0; i < user->chans.size(); i++)
360 if (user->chans[i].channel)
362 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
364 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
368 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
372 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
376 return STATUS_NORMAL;
380 return STATUS_NORMAL;
383 /* returns 1 if user u has channel c in their record, 0 if not */
385 int has_channel(userrec *u, chanrec *c)
389 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
392 for (unsigned int i =0; i < u->chans.size(); i++)
394 if (u->chans[i].channel)
396 if (!strcasecmp(u->chans[i].channel->name,c->name))
406 void TidyBan(char *ban)
409 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
413 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
415 strlcpy(temp,ban,MAXBUF);
417 char* pos_of_pling = strchr(temp,'!');
418 char* pos_of_at = strchr(temp,'@');
420 pos_of_pling[0] = '\0';
425 strlcpy(NICK,temp,NICKMAX);
426 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
427 strlcpy(HOST,pos_of_at,160);
429 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
434 std::string chlist(userrec *user,userrec* source)
436 std::string cmp = "";
437 std::string lst = "";
438 log(DEBUG,"chlist: %s",user->nick);
443 for (unsigned int i = 0; i < user->chans.size(); i++)
445 if (user->chans[i].channel != NULL)
447 if (user->chans[i].channel->name)
449 cmp = std::string(user->chans[i].channel->name) + " ";
450 if (!strstr(lst.c_str(),cmp.c_str()))
452 // if the channel is NOT private/secret, OR the source user is on the channel
453 if (((!(user->chans[i].channel->binarymodes & CM_PRIVATE)) && (!(user->chans[i].channel->binarymodes & CM_SECRET))) || (has_channel(source,user->chans[i].channel)))
455 lst = lst + std::string(cmode(user,user->chans[i].channel)) + std::string(user->chans[i].channel->name) + " ";