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 (int i = 0; i < u->chans.size(); i++)
73 for (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)))
91 void tidystring(char* str)
93 // strips out double spaces before a : parameter
103 while ((str[0] == ' ') && (strlen(str)>0))
110 bool noparse = false;
111 unsigned int t = 0, a = 0;
113 while (a < strlen(str))
115 if ((a<strlen(str)-1) && (noparse==false))
117 if ((str[a] == ' ') && (str[a+1] == ' '))
119 log(DEBUG,"Tidied extra space out of string: %s",str);
127 if ((str[a] == ' ') && (str[a+1] == ':'))
133 temp[t++] = str[a++];
136 strlcpy(str,temp,MAXBUF);
140 /* chop a string down to 512 characters and preserve linefeed (irc max
147 log(DEBUG,"ERROR! Null string passed to chop()!");
151 FOREACH_MOD OnServerRaw(temp,false,NULL);
152 const char* str2 = temp.c_str();
153 snprintf(str,MAXBUF,"%s",str2);
154 if (strlen(str) >= 511)
159 log(DEBUG,"Excess line chopped.");
167 log(DEBUG,"Blocking: %d",s);
168 flags = fcntl(s, F_GETFL, 0);
169 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
172 void NonBlocking(int s)
175 log(DEBUG,"NonBlocking: %d",s);
176 flags = fcntl(s, F_GETFL, 0);
177 fcntl(s, F_SETFL, flags | O_NONBLOCK);
180 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
183 int fd = d.ReverseLookup(unresolvedHost);
186 time_t T = time(NULL)+1;
187 while ((!d.HasResult()) && (time(NULL)<T));
188 std::string ipaddr = d.GetResult();
189 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
190 return (ipaddr != "");
193 int c_count(userrec* u)
196 for (int i =0; i < u->chans.size(); i++)
197 if (u->chans[i].channel != NULL)
203 bool hasumode(userrec* user, char mode)
207 return (strchr(user->modes,mode)>0);
213 void ChangeName(userrec* user, const char* gecos)
215 if (!strcasecmp(user->server,ServerName))
218 FOREACH_RESULT(OnChangeLocalUserGECOS(user,gecos));
221 FOREACH_MOD OnChangeName(user,gecos);
223 strlcpy(user->fullname,gecos,MAXBUF);
226 void ChangeDisplayedHost(userrec* user, const char* host)
228 if (!strcasecmp(user->server,ServerName))
231 FOREACH_RESULT(OnChangeLocalUserHost(user,host));
234 FOREACH_MOD OnChangeHost(user,host);
236 strlcpy(user->dhost,host,160);
239 /* verify that a user's ident and nickname is valid */
241 int isident(const char* n)
252 for (unsigned int i = 0; i < strlen(n); i++)
254 if ((n[i] < 33) || (n[i] > 125))
258 /* can't occur ANYWHERE in an Ident! */
259 if (strchr("<>,/?:;@'~#=+()*&%$£ \"!",n[i]))
268 int isnick(const char* n)
278 if (strlen(n) > NICKMAX)
282 for (unsigned int i = 0; i != strlen(n); i++)
284 if ((n[i] < 33) || (n[i] > 125))
288 /* can't occur ANYWHERE in a nickname! */
289 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
293 /* can't occur as the first char of a nickname... */
294 if ((strchr("0123456789",n[i])) && (!i))
302 /* returns the status character for a given user on a channel, e.g. @ for op,
303 * % for halfop etc. If the user has several modes set, the highest mode
304 * the user has must be returned. */
306 char* cmode(userrec *user, chanrec *chan)
308 if ((!user) || (!chan))
310 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
314 for (int i = 0; i < user->chans.size(); i++)
316 if (user->chans[i].channel)
318 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
320 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
324 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
328 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
339 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
340 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
341 * highest mode the user has must be returned. */
343 int cstatus(userrec *user, chanrec *chan)
345 if ((!chan) || (!user))
347 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
351 if (is_uline(user->server))
354 for (int i = 0; i < user->chans.size(); i++)
356 if (user->chans[i].channel)
358 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
360 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
364 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
368 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
372 return STATUS_NORMAL;
376 return STATUS_NORMAL;
379 /* returns 1 if user u has channel c in their record, 0 if not */
381 int has_channel(userrec *u, chanrec *c)
385 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
388 for (int i =0; i < u->chans.size(); i++)
390 if (u->chans[i].channel)
392 if (!strcasecmp(u->chans[i].channel->name,c->name))
402 void TidyBan(char *ban)
405 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
409 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
411 strlcpy(temp,ban,MAXBUF);
413 char* pos_of_pling = strchr(temp,'!');
414 char* pos_of_at = strchr(temp,'@');
416 pos_of_pling[0] = '\0';
421 strlcpy(NICK,temp,NICKMAX);
422 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
423 strlcpy(HOST,pos_of_at,160);
425 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
430 char* chlist(userrec *user,userrec* source)
433 log(DEBUG,"chlist: %s",user->nick);
439 for (int i = 0; i < user->chans.size(); i++)
441 if (user->chans[i].channel != NULL)
443 if (user->chans[i].channel->name)
445 strlcpy(cmp,user->chans[i].channel->name,MAXBUF);
446 strlcat(cmp," ",MAXBUF);
447 if (!strstr(lst,cmp))
449 // if the channel is NOT private/secret, OR the source user is on the channel
450 if (((!(user->chans[i].channel->binarymodes & CM_PRIVATE)) && (!(user->chans[i].channel->binarymodes & CM_SECRET))) || (has_channel(source,user->chans[i].channel)))
452 strlcat(lst,cmode(user,user->chans[i].channel),MAXBUF);
453 strlcat(lst,user->chans[i].channel->name,MAXBUF);
454 strlcat(lst," ",MAXBUF);
462 lst[strlen(lst)-1] = '\0'; // chop trailing space