2 #include "inspircd_io.h"
3 #include "inspircd_util.h"
4 #include "inspircd_config.h"
9 #include <sys/utsname.h>
14 #include <ext/hash_map>
26 #include "connection.h"
39 extern vector<Module*> modules;
40 extern vector<ircd_module*> factory;
42 /* return 0 or 1 depending if users u and u2 share one or more common channels
43 * (used by QUIT, NICK etc which arent channel specific notices) */
45 int common_channels(userrec *u, userrec *u2)
52 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
55 for (int i = 0; i != MAXCHANS; i++)
57 for (z = 0; z != MAXCHANS; z++)
59 if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
61 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))
63 if ((c_count(u)) && (c_count(u2)))
75 void safedelete(userrec *p)
79 log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
80 log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
86 log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
90 void safedelete(chanrec *p)
96 log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
100 log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
105 void tidystring(char* str)
107 // strips out double spaces before a : parameter
110 bool go_again = true;
117 while ((str[0] == ' ') && (strlen(str)>0))
124 bool noparse = false;
127 while (a < strlen(str))
129 if ((a<strlen(str)-1) && (noparse==false))
131 if ((str[a] == ' ') && (str[a+1] == ' '))
133 log(DEBUG,"Tidied extra space out of string: %s",str);
141 if ((str[a] == ' ') && (str[a+1] == ':'))
147 temp[t++] = str[a++];
150 strncpy(str,temp,MAXBUF);
154 /* chop a string down to 512 characters and preserve linefeed (irc max
161 log(DEBUG,"ERROR! Null string passed to chop()!");
165 FOREACH_MOD OnServerRaw(temp,false,NULL);
166 const char* str2 = temp.c_str();
167 sprintf(str,"%s",str2);
170 if (strlen(str) >= 512)
182 log(DEBUG,"Blocking: %d",s);
183 flags = fcntl(s, F_GETFL, 0);
184 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
187 void NonBlocking(int s)
190 log(DEBUG,"NonBlocking: %d",s);
191 flags = fcntl(s, F_GETFL, 0);
192 //fcntl(s, F_SETFL, O_NONBLOCK);
193 fcntl(s, F_SETFL, flags | O_NONBLOCK);
196 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
198 struct hostent *hostPtr = NULL;
201 memset (resolvedHost, '\0',MAXBUF);
202 if(unresolvedHost == NULL)
204 if ((inet_aton(unresolvedHost,&addr)) == 0)
206 hostPtr = gethostbyaddr ((char *)&addr.s_addr,sizeof(addr.s_addr),AF_INET);
208 snprintf(resolvedHost,MAXBUF,"%s",hostPtr->h_name);
210 snprintf(resolvedHost,MAXBUF,"%s",unresolvedHost);
214 int c_count(userrec* u)
217 for (int i =0; i != MAXCHANS; i++)
218 if (u->chans[i].channel != NULL)
224 bool hasumode(userrec* user, char mode)
228 return (strchr(user->modes,mode)>0);
234 void ChangeName(userrec* user, const char* gecos)
236 strncpy(user->fullname,gecos,MAXBUF);
238 snprintf(buffer,MAXBUF,"a %s :%s",user->nick,gecos);
239 NetSendToAll(buffer);
242 void ChangeDisplayedHost(userrec* user, const char* host)
244 strncpy(user->dhost,host,160);
246 snprintf(buffer,MAXBUF,"b %s %s",user->nick,host);
247 NetSendToAll(buffer);
250 /* verify that a user's ident and nickname is valid */
252 int isident(const char* n)
264 for (int i = 0; i != strlen(n); i++)
266 if ((n[i] < 33) || (n[i] > 125))
270 /* can't occur ANYWHERE in an Ident! */
271 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
280 int isnick(const char* n)
292 if (strlen(n) > NICKMAX-1)
296 for (int i = 0; i != strlen(n); i++)
298 if ((n[i] < 33) || (n[i] > 125))
302 /* can't occur ANYWHERE in a nickname! */
303 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
307 /* can't occur as the first char of a nickname... */
308 if ((strchr("0123456789",n[i])) && (!i))
316 /* returns the status character for a given user on a channel, e.g. @ for op,
317 * % for halfop etc. If the user has several modes set, the highest mode
318 * the user has must be returned. */
320 char* cmode(userrec *user, chanrec *chan)
322 if ((!user) || (!chan))
324 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
329 for (int i = 0; i != MAXCHANS; i++)
331 if (user->chans[i].channel)
333 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
335 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
339 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
343 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
353 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
354 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
355 * highest mode the user has must be returned. */
357 int cstatus(userrec *user, chanrec *chan)
359 if ((!chan) || (!user))
361 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
365 for (int i = 0; i != MAXCHANS; i++)
367 if (user->chans[i].channel)
369 if ((!strcasecmp(user->chans[i].channel->name,chan->name)) && (chan != NULL))
371 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
375 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
379 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
383 return STATUS_NORMAL;
389 /* returns 1 if user u has channel c in their record, 0 if not */
391 int has_channel(userrec *u, chanrec *c)
395 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
398 for (int i =0; i != MAXCHANS; i++)
400 if (u->chans[i].channel)
402 if (!strcasecmp(u->chans[i].channel->name,c->name))
412 void TidyBan(char *ban)
415 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
419 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
423 char* pos_of_pling = strchr(temp,'!');
424 char* pos_of_at = strchr(temp,'@');
426 pos_of_pling[0] = '\0';
431 strncpy(NICK,temp,NICKMAX);
432 strncpy(IDENT,pos_of_pling,IDENTMAX+1);
433 strncpy(HOST,pos_of_at,160);
435 sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
440 char* chlist(userrec *user)
445 log(DEBUG,"chlist: %s",user->nick);
451 for (int i = 0; i != MAXCHANS; i++)
453 if (user->chans[i].channel != NULL)
455 if (user->chans[i].channel->name)
457 strcpy(cmp,user->chans[i].channel->name);
459 if (!strstr(lst,cmp))
461 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
463 strcat(lst,cmode(user,user->chans[i].channel));
464 strcat(lst,user->chans[i].channel->name);
473 lst[strlen(lst)-1] = '\0'; // chop trailing space
479 void send_network_quit(const char* nick, const char* reason)
482 snprintf(buffer,MAXBUF,"Q %s :%s",nick,reason);
483 NetSendToAll(buffer);