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"
37 extern vector<Module*> modules;
38 extern vector<ircd_module*> factory;
40 /* return 0 or 1 depending if users u and u2 share one or more common channels
41 * (used by QUIT, NICK etc which arent channel specific notices) */
43 int common_channels(userrec *u, userrec *u2)
50 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
53 for (int i = 0; i != MAXCHANS; i++)
55 for (z = 0; z != MAXCHANS; z++)
57 if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
59 if ((u->chans[i].channel == u2->chans[z].channel) && (u->chans[i].channel) && (u2->chans[z].channel) && (u->registered == 7) && (u2->registered == 7))
61 if ((c_count(u)) && (c_count(u2)))
73 void safedelete(userrec *p)
77 log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
78 log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
84 log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
88 void safedelete(chanrec *p)
94 log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
98 log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
103 void tidystring(char* str)
105 // strips out double spaces before a : parameter
108 bool go_again = true;
115 while ((str[0] == ' ') && (strlen(str)>0))
122 bool noparse = false;
125 while (a < strlen(str))
127 if ((a<strlen(str)-1) && (noparse==false))
129 if ((str[a] == ' ') && (str[a+1] == ' '))
131 log(DEBUG,"Tidied extra space out of string: %s",str);
139 if ((str[a] == ' ') && (str[a+1] == ':'))
145 temp[t++] = str[a++];
148 strncpy(str,temp,MAXBUF);
152 /* chop a string down to 512 characters and preserve linefeed (irc max
159 log(DEBUG,"ERROR! Null string passed to chop()!");
163 FOREACH_MOD OnServerRaw(temp,false);
164 const char* str2 = temp.c_str();
165 sprintf(str,"%s",str2);
168 if (strlen(str) >= 512)
180 log(DEBUG,"Blocking: %d",s);
181 flags = fcntl(s, F_GETFL, 0);
182 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
185 void NonBlocking(int s)
188 log(DEBUG,"NonBlocking: %d",s);
189 flags = fcntl(s, F_GETFL, 0);
190 //fcntl(s, F_SETFL, O_NONBLOCK);
191 fcntl(s, F_SETFL, flags | O_NONBLOCK);
194 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
196 struct hostent *hostPtr = NULL;
199 memset (resolvedHost, '\0',MAXBUF);
200 if(unresolvedHost == NULL)
202 if ((inet_aton(unresolvedHost,&addr)) == 0)
204 hostPtr = gethostbyaddr ((char *)&addr.s_addr,sizeof(addr.s_addr),AF_INET);
206 snprintf(resolvedHost,MAXBUF,"%s",hostPtr->h_name);
208 snprintf(resolvedHost,MAXBUF,"%s",unresolvedHost);
212 int c_count(userrec* u)
215 for (int i =0; i != MAXCHANS; i++)
216 if (u->chans[i].channel != NULL)
222 bool hasumode(userrec* user, char mode)
226 return (strchr(user->modes,mode)>0);
232 void ChangeName(userrec* user, const char* gecos)
234 strncpy(user->fullname,gecos,MAXBUF);
236 // TODO: replace these with functions:
237 // NetSendToAll - to all
238 // NetSendToCommon - to all that hold users sharing a common channel with another user
239 // NetSendToOne - to one server
240 // NetSendToAllExcept - send to all but one
244 snprintf(buffer,MAXBUF,"a %s :%s",user->nick,gecos);
245 NetSendToAll(buffer);
248 void ChangeDisplayedHost(userrec* user, const char* host)
250 strncpy(user->dhost,host,160);
252 snprintf(buffer,MAXBUF,"b %s %s",user->nick,host);
253 NetSendToAll(buffer);
256 /* verify that a user's ident and nickname is valid */
258 int isident(const char* n)
270 for (int i = 0; i != strlen(n); i++)
272 if ((n[i] < 33) || (n[i] > 125))
276 /* can't occur ANYWHERE in an Ident! */
277 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
286 int isnick(const char* n)
298 if (strlen(n) > NICKMAX-1)
302 for (int i = 0; i != strlen(n); i++)
304 if ((n[i] < 33) || (n[i] > 125))
308 /* can't occur ANYWHERE in a nickname! */
309 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
313 /* can't occur as the first char of a nickname... */
314 if ((strchr("0123456789",n[i])) && (!i))
322 /* returns the status character for a given user on a channel, e.g. @ for op,
323 * % for halfop etc. If the user has several modes set, the highest mode
324 * the user has must be returned. */
326 char* cmode(userrec *user, chanrec *chan)
328 if ((!user) || (!chan))
330 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
335 for (int i = 0; i != MAXCHANS; i++)
337 if ((user->chans[i].channel == chan) && (chan != NULL))
339 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
343 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
347 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
356 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
357 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
358 * highest mode the user has must be returned. */
360 int cstatus(userrec *user, chanrec *chan)
362 if ((!chan) || (!user))
364 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
368 for (int i = 0; i != MAXCHANS; i++)
370 if ((user->chans[i].channel == chan) && (chan != NULL))
372 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
376 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
380 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
384 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 == c)
409 void TidyBan(char *ban)
412 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
416 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
420 char* pos_of_pling = strchr(temp,'!');
421 char* pos_of_at = strchr(temp,'@');
423 pos_of_pling[0] = '\0';
428 strncpy(NICK,temp,NICKMAX);
429 strncpy(IDENT,pos_of_pling,IDENTMAX+1);
430 strncpy(HOST,pos_of_at,160);
432 sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
437 char* chlist(userrec *user)
442 log(DEBUG,"chlist: %s",user->nick);
448 for (int i = 0; i != MAXCHANS; i++)
450 if (user->chans[i].channel != NULL)
452 if (user->chans[i].channel->name)
454 strcpy(cmp,user->chans[i].channel->name);
456 if (!strstr(lst,cmp))
458 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
460 strcat(lst,cmode(user,user->chans[i].channel));
461 strcat(lst,user->chans[i].channel->name);
470 lst[strlen(lst)-1] = '\0'; // chop trailing space
476 void send_network_quit(const char* nick, const char* reason)
479 snprintf(buffer,MAXBUF,"Q %s :%s",nick,reason);
480 NetSendToAll(buffer);