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 ((u->chans[i].channel == u2->chans[z].channel) && (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);
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 == chan) && (chan != NULL))
333 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
337 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
341 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
350 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
351 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
352 * highest mode the user has must be returned. */
354 int cstatus(userrec *user, chanrec *chan)
356 if ((!chan) || (!user))
358 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
362 for (int i = 0; i != MAXCHANS; i++)
364 if ((user->chans[i].channel == chan) && (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)
378 return STATUS_NORMAL;
383 /* returns 1 if user u has channel c in their record, 0 if not */
385 int has_channel(userrec *u, chanrec *c)
389 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
392 for (int i =0; i != MAXCHANS; i++)
394 if (u->chans[i].channel == c)
403 void TidyBan(char *ban)
406 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
410 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
414 char* pos_of_pling = strchr(temp,'!');
415 char* pos_of_at = strchr(temp,'@');
417 pos_of_pling[0] = '\0';
422 strncpy(NICK,temp,NICKMAX);
423 strncpy(IDENT,pos_of_pling,IDENTMAX+1);
424 strncpy(HOST,pos_of_at,160);
426 sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
431 char* chlist(userrec *user)
436 log(DEBUG,"chlist: %s",user->nick);
442 for (int i = 0; i != MAXCHANS; i++)
444 if (user->chans[i].channel != NULL)
446 if (user->chans[i].channel->name)
448 strcpy(cmp,user->chans[i].channel->name);
450 if (!strstr(lst,cmp))
452 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
454 strcat(lst,cmode(user,user->chans[i].channel));
455 strcat(lst,user->chans[i].channel->name);
464 lst[strlen(lst)-1] = '\0'; // chop trailing space
470 void send_network_quit(const char* nick, const char* reason)
473 snprintf(buffer,MAXBUF,"Q %s :%s",nick,reason);
474 NetSendToAll(buffer);