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 // TODO: replace these with functions:
239 // NetSendToAll - to all
240 // NetSendToCommon - to all that hold users sharing a common channel with another user
241 // NetSendToOne - to one server
242 // NetSendToAllExcept - send to all but one
246 snprintf(buffer,MAXBUF,"a %s :%s",user->nick,gecos);
247 NetSendToAll(buffer);
250 void ChangeDisplayedHost(userrec* user, const char* host)
252 strncpy(user->dhost,host,160);
254 snprintf(buffer,MAXBUF,"b %s %s",user->nick,host);
255 NetSendToAll(buffer);
258 /* verify that a user's ident and nickname is valid */
260 int isident(const char* n)
272 for (int i = 0; i != strlen(n); i++)
274 if ((n[i] < 33) || (n[i] > 125))
278 /* can't occur ANYWHERE in an Ident! */
279 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
288 int isnick(const char* n)
300 if (strlen(n) > NICKMAX-1)
304 for (int i = 0; i != strlen(n); i++)
306 if ((n[i] < 33) || (n[i] > 125))
310 /* can't occur ANYWHERE in a nickname! */
311 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
315 /* can't occur as the first char of a nickname... */
316 if ((strchr("0123456789",n[i])) && (!i))
324 /* returns the status character for a given user on a channel, e.g. @ for op,
325 * % for halfop etc. If the user has several modes set, the highest mode
326 * the user has must be returned. */
328 char* cmode(userrec *user, chanrec *chan)
330 if ((!user) || (!chan))
332 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
337 for (int i = 0; i != MAXCHANS; i++)
339 if ((user->chans[i].channel == chan) && (chan != NULL))
341 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
345 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
349 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
358 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
359 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
360 * highest mode the user has must be returned. */
362 int cstatus(userrec *user, chanrec *chan)
364 if ((!chan) || (!user))
366 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
370 for (int i = 0; i != MAXCHANS; i++)
372 if ((user->chans[i].channel == chan) && (chan != NULL))
374 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
378 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
382 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
386 return STATUS_NORMAL;
391 /* returns 1 if user u has channel c in their record, 0 if not */
393 int has_channel(userrec *u, chanrec *c)
397 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
400 for (int i =0; i != MAXCHANS; i++)
402 if (u->chans[i].channel == c)
411 void TidyBan(char *ban)
414 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
418 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
422 char* pos_of_pling = strchr(temp,'!');
423 char* pos_of_at = strchr(temp,'@');
425 pos_of_pling[0] = '\0';
430 strncpy(NICK,temp,NICKMAX);
431 strncpy(IDENT,pos_of_pling,IDENTMAX+1);
432 strncpy(HOST,pos_of_at,160);
434 sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
439 char* chlist(userrec *user)
444 log(DEBUG,"chlist: %s",user->nick);
450 for (int i = 0; i != MAXCHANS; i++)
452 if (user->chans[i].channel != NULL)
454 if (user->chans[i].channel->name)
456 strcpy(cmp,user->chans[i].channel->name);
458 if (!strstr(lst,cmp))
460 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
462 strcat(lst,cmode(user,user->chans[i].channel));
463 strcat(lst,user->chans[i].channel->name);
472 lst[strlen(lst)-1] = '\0'; // chop trailing space
478 void send_network_quit(const char* nick, const char* reason)
481 snprintf(buffer,MAXBUF,"Q %s :%s",nick,reason);
482 NetSendToAll(buffer);