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"
57 extern std::vector<Module*> modules;
58 extern std::vector<ircd_module*> factory;
60 extern char ServerName[MAXBUF];
64 extern FILE *log_file;
65 extern char DNSServer[MAXBUF];
67 /* return 0 or 1 depending if users u and u2 share one or more common channels
68 * (used by QUIT, NICK etc which arent channel specific notices) */
70 int common_channels(userrec *u, userrec *u2)
77 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
80 for (int i = 0; i != MAXCHANS; i++)
82 for (z = 0; z != MAXCHANS; z++)
84 if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
86 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))
88 if ((c_count(u)) && (c_count(u2)))
100 void safedelete(userrec *p)
104 log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
105 log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
111 log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
115 void safedelete(chanrec *p)
121 log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
125 log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
130 void tidystring(char* str)
132 // strips out double spaces before a : parameter
135 bool go_again = true;
142 while ((str[0] == ' ') && (strlen(str)>0))
149 bool noparse = false;
152 while (a < strlen(str))
154 if ((a<strlen(str)-1) && (noparse==false))
156 if ((str[a] == ' ') && (str[a+1] == ' '))
158 log(DEBUG,"Tidied extra space out of string: %s",str);
166 if ((str[a] == ' ') && (str[a+1] == ':'))
172 temp[t++] = str[a++];
175 strlcpy(str,temp,MAXBUF);
179 /* chop a string down to 512 characters and preserve linefeed (irc max
186 log(DEBUG,"ERROR! Null string passed to chop()!");
190 FOREACH_MOD OnServerRaw(temp,false,NULL);
191 const char* str2 = temp.c_str();
192 snprintf(str,MAXBUF,"%s",str2);
193 if (strlen(str) >= 512)
205 log(DEBUG,"Blocking: %d",s);
206 flags = fcntl(s, F_GETFL, 0);
207 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
210 void NonBlocking(int s)
213 log(DEBUG,"NonBlocking: %d",s);
214 flags = fcntl(s, F_GETFL, 0);
215 fcntl(s, F_SETFL, flags | O_NONBLOCK);
218 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
221 int fd = d.ReverseLookup(unresolvedHost);
224 time_t T = time(NULL)+1;
225 while ((!d.HasResult()) && (time(NULL)<T));
226 std::string ipaddr = d.GetResult();
227 strlcpy(resolvedHost,ipaddr.c_str(),MAXBUF);
228 return (ipaddr != "");
231 int c_count(userrec* u)
234 for (int i =0; i != MAXCHANS; i++)
235 if (u->chans[i].channel != NULL)
241 bool hasumode(userrec* user, char mode)
245 return (strchr(user->modes,mode)>0);
251 void ChangeName(userrec* user, const char* gecos)
253 if (!strcasecmp(user->server,ServerName))
256 FOREACH_RESULT(OnChangeLocalUserGECOS(user,gecos));
260 strlcpy(user->fullname,gecos,MAXBUF);
262 snprintf(buffer,MAXBUF,"a %s :%s",user->nick,gecos);
263 NetSendToAll(buffer);
266 void ChangeDisplayedHost(userrec* user, const char* host)
268 if (!strcasecmp(user->server,ServerName))
271 FOREACH_RESULT(OnChangeLocalUserHost(user,host));
275 strlcpy(user->dhost,host,160);
277 snprintf(buffer,MAXBUF,"b %s %s",user->nick,host);
278 NetSendToAll(buffer);
281 /* verify that a user's ident and nickname is valid */
283 int isident(const char* n)
295 for (int i = 0; i != strlen(n); i++)
297 if ((n[i] < 33) || (n[i] > 125))
301 /* can't occur ANYWHERE in an Ident! */
302 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
311 int isnick(const char* n)
323 if (strlen(n) > NICKMAX-1)
327 for (int i = 0; i != strlen(n); i++)
329 if ((n[i] < 33) || (n[i] > 125))
333 /* can't occur ANYWHERE in a nickname! */
334 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
338 /* can't occur as the first char of a nickname... */
339 if ((strchr("0123456789",n[i])) && (!i))
347 /* returns the status character for a given user on a channel, e.g. @ for op,
348 * % for halfop etc. If the user has several modes set, the highest mode
349 * the user has must be returned. */
351 char* cmode(userrec *user, chanrec *chan)
353 if ((!user) || (!chan))
355 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
360 for (int i = 0; i != MAXCHANS; i++)
362 if (user->chans[i].channel)
364 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
366 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
370 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
374 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
385 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
386 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
387 * highest mode the user has must be returned. */
389 int cstatus(userrec *user, chanrec *chan)
391 if ((!chan) || (!user))
393 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
397 for (int i = 0; i != MAXCHANS; i++)
399 if (user->chans[i].channel)
401 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
403 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
407 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
411 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
415 return STATUS_NORMAL;
419 return STATUS_NORMAL;
422 /* returns 1 if user u has channel c in their record, 0 if not */
424 int has_channel(userrec *u, chanrec *c)
428 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
431 for (int i =0; i != MAXCHANS; i++)
433 if (u->chans[i].channel)
435 if (!strcasecmp(u->chans[i].channel->name,c->name))
445 void TidyBan(char *ban)
448 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
452 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
454 strlcpy(temp,ban,MAXBUF);
456 char* pos_of_pling = strchr(temp,'!');
457 char* pos_of_at = strchr(temp,'@');
459 pos_of_pling[0] = '\0';
464 strlcpy(NICK,temp,NICKMAX);
465 strlcpy(IDENT,pos_of_pling,IDENTMAX+1);
466 strlcpy(HOST,pos_of_at,160);
468 snprintf(ban,MAXBUF,"%s!%s@%s",NICK,IDENT,HOST);
473 char* chlist(userrec *user)
478 log(DEBUG,"chlist: %s",user->nick);
484 for (int i = 0; i != MAXCHANS; i++)
486 if (user->chans[i].channel != NULL)
488 if (user->chans[i].channel->name)
490 strlcpy(cmp,user->chans[i].channel->name,MAXBUF);
491 strlcat(cmp," ",MAXBUF);
492 if (!strstr(lst,cmp))
494 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
496 strlcat(lst,cmode(user,user->chans[i].channel),MAXBUF);
497 strlcat(lst,user->chans[i].channel->name,MAXBUF);
498 strlcat(lst," ",MAXBUF);
506 lst[strlen(lst)-1] = '\0'; // chop trailing space
512 void send_network_quit(const char* nick, const char* reason)
515 snprintf(buffer,MAXBUF,"Q %s :%s",nick,reason);
516 NetSendToAll(buffer);