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 * ---------------------------------------------------
18 #include "inspircd_io.h"
19 #include "inspircd_util.h"
20 #include "inspircd_config.h"
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
30 #include <ext/hash_map>
42 #include "connection.h"
51 #include "inspstring.h"
53 #include "helperfuncs.h"
58 extern std::vector<Module*> modules;
59 extern std::vector<ircd_module*> factory;
61 extern char ServerName[MAXBUF];
65 extern FILE *log_file;
66 extern char DNSServer[MAXBUF];
68 /* return 0 or 1 depending if users u and u2 share one or more common channels
69 * (used by QUIT, NICK etc which arent channel specific notices) */
71 int common_channels(userrec *u, userrec *u2)
75 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
78 for (int i = 0; i != MAXCHANS; i++)
80 for (int z = 0; z != MAXCHANS; z++)
82 if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
84 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))
86 if ((c_count(u)) && (c_count(u2)))
98 void safedelete(userrec *p)
102 log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
103 log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
109 log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
113 void safedelete(chanrec *p)
119 log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
123 log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
128 void tidystring(char* str)
130 // strips out double spaces before a : parameter
133 bool go_again = true;
140 while ((str[0] == ' ') && (strlen(str)>0))
147 bool noparse = false;
150 while (a < strlen(str))
152 if ((a<strlen(str)-1) && (noparse==false))
154 if ((str[a] == ' ') && (str[a+1] == ' '))
156 log(DEBUG,"Tidied extra space out of string: %s",str);
164 if ((str[a] == ' ') && (str[a+1] == ':'))
170 temp[t++] = str[a++];
173 strlcpy(str,temp,MAXBUF);
177 /* chop a string down to 512 characters and preserve linefeed (irc max
184 log(DEBUG,"ERROR! Null string passed to chop()!");
188 FOREACH_MOD OnServerRaw(temp,false,NULL);
189 const char* str2 = temp.c_str();
190 snprintf(str,MAXBUF,"%s",str2);
191 if (strlen(str) >= 511)
196 log(DEBUG,"Excess line chopped.");
204 log(DEBUG,"Blocking: %d",s);
205 flags = fcntl(s, F_GETFL, 0);
206 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
209 void NonBlocking(int s)
212 log(DEBUG,"NonBlocking: %d",s);
213 flags = fcntl(s, F_GETFL, 0);
214 fcntl(s, F_SETFL, flags | O_NONBLOCK);
217 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
220 int fd = d.ReverseLookup(unresolvedHost);
223 time_t T = time(NULL)+1;
224 while ((!d.HasResult()) && (time(NULL)<T));
225 std::string ipaddr = d.GetResult();
226 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
227 return (ipaddr != "");
230 int c_count(userrec* u)
233 for (int i =0; i != MAXCHANS; i++)
234 if (u->chans[i].channel != NULL)
240 bool hasumode(userrec* user, char mode)
244 return (strchr(user->modes,mode)>0);
250 void ChangeName(userrec* user, const char* gecos)
252 if (!strcasecmp(user->server,ServerName))
255 FOREACH_RESULT(OnChangeLocalUserGECOS(user,gecos));
259 strlcpy(user->fullname,gecos,MAXBUF);
261 snprintf(buffer,MAXBUF,"a %s :%s",user->nick,gecos);
262 NetSendToAll(buffer);
265 void ChangeDisplayedHost(userrec* user, const char* host)
267 if (!strcasecmp(user->server,ServerName))
270 FOREACH_RESULT(OnChangeLocalUserHost(user,host));
274 strlcpy(user->dhost,host,160);
276 snprintf(buffer,MAXBUF,"b %s %s",user->nick,host);
277 NetSendToAll(buffer);
280 /* verify that a user's ident and nickname is valid */
282 int isident(const char* n)
293 for (int i = 0; i != strlen(n); i++)
295 if ((n[i] < 33) || (n[i] > 125))
299 /* can't occur ANYWHERE in an Ident! */
300 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
309 int isnick(const char* n)
319 if (strlen(n) > NICKMAX-1)
323 for (int i = 0; i != strlen(n); i++)
325 if ((n[i] < 33) || (n[i] > 125))
329 /* can't occur ANYWHERE in a nickname! */
330 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
334 /* can't occur as the first char of a nickname... */
335 if ((strchr("0123456789",n[i])) && (!i))
343 /* returns the status character for a given user on a channel, e.g. @ for op,
344 * % for halfop etc. If the user has several modes set, the highest mode
345 * the user has must be returned. */
347 char* cmode(userrec *user, chanrec *chan)
349 if ((!user) || (!chan))
351 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
354 for (int i = 0; i != MAXCHANS; 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)
379 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
380 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
381 * highest mode the user has must be returned. */
383 int cstatus(userrec *user, chanrec *chan)
385 if ((!chan) || (!user))
387 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
391 for (int i = 0; i != MAXCHANS; i++)
393 if (user->chans[i].channel)
395 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
397 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
401 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
405 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
409 return STATUS_NORMAL;
413 return STATUS_NORMAL;
416 /* returns 1 if user u has channel c in their record, 0 if not */
418 int has_channel(userrec *u, chanrec *c)
422 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
425 for (int i =0; i != MAXCHANS; i++)
427 if (u->chans[i].channel)
429 if (!strcasecmp(u->chans[i].channel->name,c->name))
439 void TidyBan(char *ban)
442 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
446 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
448 strlcpy(temp,ban,MAXBUF);
450 char* pos_of_pling = strchr(temp,'!');
451 char* pos_of_at = strchr(temp,'@');
453 pos_of_pling[0] = '\0';
458 strlcpy(NICK,temp,NICKMAX);
459 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
460 strlcpy(HOST,pos_of_at,160);
462 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
467 char* chlist(userrec *user,userrec* source)
470 log(DEBUG,"chlist: %s",user->nick);
476 for (int i = 0; i != MAXCHANS; i++)
478 if (user->chans[i].channel != NULL)
480 if (user->chans[i].channel->name)
482 strlcpy(cmp,user->chans[i].channel->name,MAXBUF);
483 strlcat(cmp," ",MAXBUF);
484 if (!strstr(lst,cmp))
486 // if the channel is NOT private/secret, OR the source user is on the channel
487 if (((!(user->chans[i].channel->binarymodes & CM_PRIVATE)) && (!(user->chans[i].channel->binarymodes & CM_SECRET))) || (has_channel(source,user->chans[i].channel)))
489 strlcat(lst,cmode(user,user->chans[i].channel),MAXBUF);
490 strlcat(lst,user->chans[i].channel->name,MAXBUF);
491 strlcat(lst," ",MAXBUF);
499 lst[strlen(lst)-1] = '\0'; // chop trailing space
505 void send_network_quit(const char* nick, const char* reason)
508 snprintf(buffer,MAXBUF,"Q %s :%s",nick,reason);
509 NetSendToAll(buffer);