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"
55 extern vector<Module*> modules;
56 extern vector<ircd_module*> factory;
58 /* return 0 or 1 depending if users u and u2 share one or more common channels
59 * (used by QUIT, NICK etc which arent channel specific notices) */
61 int common_channels(userrec *u, userrec *u2)
68 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
71 for (int i = 0; i != MAXCHANS; i++)
73 for (z = 0; z != MAXCHANS; 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 safedelete(userrec *p)
95 log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
96 log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
102 log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
106 void safedelete(chanrec *p)
112 log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
116 log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
121 void tidystring(char* str)
123 // strips out double spaces before a : parameter
126 bool go_again = true;
133 while ((str[0] == ' ') && (strlen(str)>0))
140 bool noparse = false;
143 while (a < strlen(str))
145 if ((a<strlen(str)-1) && (noparse==false))
147 if ((str[a] == ' ') && (str[a+1] == ' '))
149 log(DEBUG,"Tidied extra space out of string: %s",str);
157 if ((str[a] == ' ') && (str[a+1] == ':'))
163 temp[t++] = str[a++];
166 strncpy(str,temp,MAXBUF);
170 /* chop a string down to 512 characters and preserve linefeed (irc max
177 log(DEBUG,"ERROR! Null string passed to chop()!");
181 FOREACH_MOD OnServerRaw(temp,false,NULL);
182 const char* str2 = temp.c_str();
183 sprintf(str,"%s",str2);
186 if (strlen(str) >= 512)
198 log(DEBUG,"Blocking: %d",s);
199 flags = fcntl(s, F_GETFL, 0);
200 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
203 void NonBlocking(int s)
206 log(DEBUG,"NonBlocking: %d",s);
207 flags = fcntl(s, F_GETFL, 0);
208 //fcntl(s, F_SETFL, O_NONBLOCK);
209 fcntl(s, F_SETFL, flags | O_NONBLOCK);
212 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
214 struct hostent *hostPtr = NULL;
217 memset (resolvedHost, '\0',MAXBUF);
218 if(unresolvedHost == NULL)
220 if ((inet_aton(unresolvedHost,&addr)) == 0)
222 hostPtr = gethostbyaddr ((char *)&addr.s_addr,sizeof(addr.s_addr),AF_INET);
224 snprintf(resolvedHost,MAXBUF,"%s",hostPtr->h_name);
226 snprintf(resolvedHost,MAXBUF,"%s",unresolvedHost);
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 strncpy(user->fullname,gecos,MAXBUF);
254 snprintf(buffer,MAXBUF,"a %s :%s",user->nick,gecos);
255 NetSendToAll(buffer);
258 void ChangeDisplayedHost(userrec* user, const char* host)
260 strncpy(user->dhost,host,160);
262 snprintf(buffer,MAXBUF,"b %s %s",user->nick,host);
263 NetSendToAll(buffer);
266 /* verify that a user's ident and nickname is valid */
268 int isident(const char* n)
280 for (int i = 0; i != strlen(n); i++)
282 if ((n[i] < 33) || (n[i] > 125))
286 /* can't occur ANYWHERE in an Ident! */
287 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
296 int isnick(const char* n)
308 if (strlen(n) > NICKMAX-1)
312 for (int i = 0; i != strlen(n); i++)
314 if ((n[i] < 33) || (n[i] > 125))
318 /* can't occur ANYWHERE in a nickname! */
319 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
323 /* can't occur as the first char of a nickname... */
324 if ((strchr("0123456789",n[i])) && (!i))
332 /* returns the status character for a given user on a channel, e.g. @ for op,
333 * % for halfop etc. If the user has several modes set, the highest mode
334 * the user has must be returned. */
336 char* cmode(userrec *user, chanrec *chan)
338 if ((!user) || (!chan))
340 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
345 for (int i = 0; i != MAXCHANS; i++)
347 if (user->chans[i].channel)
349 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
351 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
355 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
359 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
369 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
370 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
371 * highest mode the user has must be returned. */
373 int cstatus(userrec *user, chanrec *chan)
375 if ((!chan) || (!user))
377 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
381 for (int i = 0; i != MAXCHANS; i++)
383 if (user->chans[i].channel)
385 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
387 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
391 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
395 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
399 return STATUS_NORMAL;
405 /* returns 1 if user u has channel c in their record, 0 if not */
407 int has_channel(userrec *u, chanrec *c)
411 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
414 for (int i =0; i != MAXCHANS; i++)
416 if (u->chans[i].channel)
418 if (!strcasecmp(u->chans[i].channel->name,c->name))
428 void TidyBan(char *ban)
431 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
435 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
439 char* pos_of_pling = strchr(temp,'!');
440 char* pos_of_at = strchr(temp,'@');
442 pos_of_pling[0] = '\0';
447 strncpy(NICK,temp,NICKMAX);
448 strncpy(IDENT,pos_of_pling,IDENTMAX+1);
449 strncpy(HOST,pos_of_at,160);
451 sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
456 char* chlist(userrec *user)
461 log(DEBUG,"chlist: %s",user->nick);
467 for (int i = 0; i != MAXCHANS; i++)
469 if (user->chans[i].channel != NULL)
471 if (user->chans[i].channel->name)
473 strcpy(cmp,user->chans[i].channel->name);
475 if (!strstr(lst,cmp))
477 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
479 strcat(lst,cmode(user,user->chans[i].channel));
480 strcat(lst,user->chans[i].channel->name);
489 lst[strlen(lst)-1] = '\0'; // chop trailing space
495 void send_network_quit(const char* nick, const char* reason)
498 snprintf(buffer,MAXBUF,"Q %s :%s",nick,reason);
499 NetSendToAll(buffer);