1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 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 * ---------------------------------------------------
17 #include "inspircd_config.h"
18 #include "configreader.h"
20 #include "connection.h"
24 #include "inspstring.h"
26 #include "helperfuncs.h"
28 #include "socketengine.h"
33 #include "cull_list.h"
35 extern InspIRCd* ServerInstance;
36 extern int WHOWAS_STALE;
37 extern int WHOWAS_MAX;
38 extern std::vector<Module*> modules;
39 extern std::vector<ircd_module*> factory;
40 extern std::vector<InspSocket*> module_sockets;
42 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
44 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
45 extern ServerConfig *Config;
46 extern user_hash clientlist;
47 extern Server* MyServer;
51 extern std::vector<userrec*> local_users;
53 std::vector<userrec*> all_opers;
55 typedef std::map<irc::string,char*> opertype_t;
56 typedef opertype_t operclass_t;
59 operclass_t operclass;
61 bool InitTypes(const char* tag)
63 for (opertype_t::iterator n = opertypes.begin(); n != opertypes.end(); n++)
73 bool InitClasses(const char* tag)
75 for (operclass_t::iterator n = operclass.begin(); n != operclass.end(); n++)
85 bool DoType(const char* tag, char** entries, void** values, int* types)
87 char* TypeName = (char*)values[0];
88 char* Classes = (char*)values[1];
90 opertypes[TypeName] = strdup(Classes);
91 log(DEBUG,"Read oper TYPE '%s' with classes '%s'",TypeName,Classes);
95 bool DoClass(const char* tag, char** entries, void** values, int* types)
97 char* ClassName = (char*)values[0];
98 char* CommandList = (char*)values[1];
100 operclass[ClassName] = strdup(CommandList);
101 log(DEBUG,"Read oper CLASS '%s' with commands '%s'",ClassName,CommandList);
105 bool DoneClassesAndTypes(const char* tag)
110 bool userrec::ProcessNoticeMasks(const char *sm)
126 if ((*c >= 'A') && (*c <= 'z'))
127 this->SetNoticeMask(*c, adding);
137 void userrec::StartDNSLookup()
139 log(DEBUG,"Commencing reverse lookup");
142 res_reverse = new UserResolver(this, this->GetIPString(), false);
143 MyServer->AddResolver(res_reverse);
145 catch (ModuleException& e)
147 log(DEBUG,"Error in resolver: %s",e.GetReason());
151 UserResolver::UserResolver(userrec* user, std::string to_resolve, bool forward) : Resolver(to_resolve, forward ? DNS_QUERY_FORWARD : DNS_QUERY_REVERSE), bound_user(user)
154 this->bound_fd = user->fd;
157 void UserResolver::OnLookupComplete(const std::string &result)
159 if ((!this->fwd) && (fd_ref_table[this->bound_fd] == this->bound_user))
161 log(DEBUG,"Commencing forward lookup");
162 this->bound_user->stored_host = result;
165 bound_user->res_forward = new UserResolver(this->bound_user, result, true);
166 MyServer->AddResolver(bound_user->res_forward);
168 catch (ModuleException& e)
170 log(DEBUG,"Error in resolver: %s",e.GetReason());
173 else if ((this->fwd) && (fd_ref_table[this->bound_fd] == this->bound_user))
175 /* Both lookups completed */
176 if (this->bound_user->GetIPString() == result)
178 std::string hostname = this->bound_user->stored_host;
179 if (hostname.length() < 65)
181 /* Hostnames starting with : are not a good thing (tm) */
182 if (*(hostname.c_str()) == ':')
183 hostname = "0" + hostname;
185 WriteServ(this->bound_fd, "NOTICE Auth :*** Found your hostname (%s)", hostname.c_str());
186 this->bound_user->dns_done = true;
187 strlcpy(this->bound_user->dhost, hostname.c_str(),64);
188 strlcpy(this->bound_user->host, hostname.c_str(),64);
192 WriteServ(this->bound_fd, "NOTICE Auth :*** Your hostname is longer than the maximum of 64 characters, using your IP address (%s) instead.", this->bound_user->GetIPString());
197 WriteServ(this->bound_fd, "NOTICE Auth :*** Your hostname does not match up with your IP address. Sorry, using your IP address (%s) instead.", this->bound_user->GetIPString());
202 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
204 if (fd_ref_table[this->bound_fd] == this->bound_user)
206 /* Error message here */
207 WriteServ(this->bound_fd, "NOTICE Auth :*** Could not resolve your hostname, using your IP address (%s) instead.", this->bound_user->GetIPString());
208 this->bound_user->dns_done = true;
213 bool userrec::IsNoticeMaskSet(unsigned char sm)
215 return (snomasks[sm-65]);
218 void userrec::SetNoticeMask(unsigned char sm, bool value)
220 snomasks[sm-65] = value;
223 const char* userrec::FormatNoticeMasks()
225 static char data[MAXBUF];
228 for (int n = 0; n < 64; n++)
231 data[offset++] = n+65;
240 bool userrec::IsModeSet(unsigned char m)
242 return (modes[m-65]);
245 void userrec::SetMode(unsigned char m, bool value)
250 const char* userrec::FormatModes()
252 static char data[MAXBUF];
254 for (int n = 0; n < 64; n++)
257 data[offset++] = n+65;
265 // the PROPER way to do it, AVOID bzero at *ALL* costs
266 *password = *nick = *ident = *host = *dhost = *fullname = *awaymsg = *oper = 0;
267 server = (char*)FindServerNamePtr(Config->ServerName);
269 lines_in = fd = lastping = signon = idle_lastmsg = nping = registered = 0;
270 timeout = flood = bytes_in = bytes_out = cmds_in = cmds_out = 0;
271 haspassed = dns_done = false;
275 res_forward = res_reverse = NULL;
279 chans.resize(MAXCHANS);
280 memset(modes,0,sizeof(modes));
282 for (unsigned int n = 0; n < MAXCHANS; n++)
284 ucrec* x = new ucrec();
293 for (std::vector<ucrec*>::iterator n = chans.begin(); n != chans.end(); n++)
295 ucrec* x = (ucrec*)*n;
301 if (this->GetProtocolFamily() == AF_INET)
303 delete (sockaddr_in*)ip;
305 #ifdef SUPPORT_IP6LINKS
308 delete (sockaddr_in6*)ip;
314 /* XXX - minor point, other *Host functions return a char *, this one creates it. Might be nice to be consistant? */
315 void userrec::MakeHost(char* nhost)
317 /* This is much faster than snprintf */
319 for(char* n = ident; *n; n++)
322 for(char* n = host; *n; n++)
327 void userrec::CloseSocket()
329 shutdown(this->fd,2);
333 char* userrec::GetFullHost()
335 static char result[MAXBUF];
337 for(char* n = nick; *n; n++)
340 for(char* n = ident; *n; n++)
343 for(char* n = dhost; *n; n++)
349 char* userrec::MakeWildHost()
351 static char nresult[MAXBUF];
353 *t++ = '*'; *t++ = '!';
354 *t++ = '*'; *t++ = '@';
355 for(char* n = dhost; *n; n++)
361 int userrec::ReadData(void* buffer, size_t size)
365 return read(this->fd, buffer, size);
372 char* userrec::GetFullRealHost()
374 static char fresult[MAXBUF];
376 for(char* n = nick; *n; n++)
379 for(char* n = ident; *n; n++)
382 for(char* n = host; *n; n++)
388 bool userrec::IsInvited(irc::string &channel)
390 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
392 irc::string compare = i->channel;
394 if (compare == channel)
402 InvitedList* userrec::GetInviteList()
407 void userrec::InviteTo(irc::string &channel)
411 invites.push_back(i);
414 void userrec::RemoveInvite(irc::string &channel)
416 log(DEBUG,"Removing invites");
420 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
422 irc::string compare = i->channel;
424 if (compare == channel)
433 bool userrec::HasPermission(const std::string &command)
440 * users on remote servers can completely bypass all permissions based checks.
441 * This prevents desyncs when one server has different type/class tags to another.
442 * That having been said, this does open things up to the possibility of source changes
443 * allowing remote kills, etc - but if they have access to the src, they most likely have
444 * access to the conf - so it's an end to a means either way.
449 // are they even an oper at all?
452 opertype_t::iterator iter_opertype = opertypes.find(this->oper);
453 if (iter_opertype != opertypes.end())
455 char* Classes = strdup(iter_opertype->second);
456 char* myclass = strtok_r(Classes," ",&savept);
459 operclass_t::iterator iter_operclass = operclass.find(myclass);
460 if (iter_operclass != operclass.end())
462 char* CommandList = strdup(iter_operclass->second);
463 mycmd = strtok_r(CommandList," ",&savept2);
466 if ((!strcasecmp(mycmd,command.c_str())) || (*mycmd == '*'))
472 mycmd = strtok_r(NULL," ",&savept2);
476 myclass = strtok_r(NULL," ",&savept);
485 bool userrec::AddBuffer(const std::string &a)
489 /* NB: std::string is arsey about \r and \n and tries to translate them
490 * somehow, so we CANNOT use std::string::find() here :(
492 for (std::string::const_iterator i = a.begin(); i != a.end(); i++)
501 if (recvq.length() > (unsigned)this->recvqmax)
503 this->SetWriteError("RecvQ exceeded");
504 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
511 bool userrec::BufferIsReady()
513 return (recvq.find('\n') != std::string::npos);
516 void userrec::ClearBuffer()
521 std::string userrec::GetBuffer()
526 /* Strip any leading \r or \n off the string.
527 * Usually there are only one or two of these,
528 * so its is computationally cheap to do.
530 while ((*recvq.begin() == '\r') || (*recvq.begin() == '\n'))
531 recvq.erase(recvq.begin());
533 for (std::string::iterator x = recvq.begin(); x != recvq.end(); x++)
535 /* Find the first complete line, return it as the
536 * result, and leave the recvq as whats left
540 std::string ret = std::string(recvq.begin(), x);
541 recvq.erase(recvq.begin(), x + 1);
548 void userrec::AddWriteBuf(const std::string &data)
550 if (*this->GetWriteError())
553 if (sendq.length() + data.length() > (unsigned)this->sendqmax)
556 * Fix by brain - Set the error text BEFORE calling writeopers, because
557 * if we dont it'll recursively call here over and over again trying
558 * to repeatedly add the text to the sendq!
560 this->SetWriteError("SendQ exceeded");
561 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
565 if (data.length() > 512)
567 std::string newdata(data);
569 newdata.append("\r\n");
570 sendq.append(newdata);
578 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
579 void userrec::FlushWriteBuf()
581 if ((sendq.length()) && (this->fd != FD_MAGIC_NUMBER))
583 const char* tb = this->sendq.c_str();
584 int n_sent = write(this->fd,tb,this->sendq.length());
588 this->SetWriteError(strerror(errno));
595 // update the user's stats counters
596 this->bytes_out += n_sent;
602 void userrec::SetWriteError(const std::string &error)
604 log(DEBUG,"SetWriteError: %s",error.c_str());
605 // don't try to set the error twice, its already set take the first string.
606 if (!this->WriteError.length())
608 log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
609 this->WriteError = error;
613 const char* userrec::GetWriteError()
615 return this->WriteError.c_str();
618 void AddOper(userrec* user)
620 log(DEBUG,"Oper added to optimization list");
621 all_opers.push_back(user);
624 void DeleteOper(userrec* user)
626 for (std::vector<userrec*>::iterator a = all_opers.begin(); a < all_opers.end(); a++)
630 log(DEBUG,"Oper removed from optimization list");
637 void kill_link(userrec *user,const char* r)
639 user_hash::iterator iter = clientlist.find(user->nick);
642 * I'm pretty sure returning here is causing a desync when part of the net thinks a user is gone,
643 * and another part doesn't. We want to broadcast the quit/kill before bailing so the net stays in sync.
645 * I can't imagine this blowing up, so I'm commenting it out. We still check
646 * before playing with a bad iterator below in our if(). DISCUSS THIS BEFORE YOU DO ANYTHING. --w00t
648 * if (iter == clientlist.end())
654 strlcpy(reason,r,MAXQUIT-1);
655 log(DEBUG,"kill_link: %s %d '%s'",user->nick,user->fd,reason);
658 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
660 if (user->registered == REG_ALL)
662 purge_empty_chans(user);
663 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user,reason));
664 WriteCommonExcept(user,"QUIT :%s",reason);
668 user->FlushWriteBuf();
670 FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(user));
674 if (Config->GetIOHook(user->GetPort()))
678 Config->GetIOHook(user->GetPort())->OnRawSocketClose(user->fd);
680 catch (ModuleException& modexcept)
682 log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
686 ServerInstance->SE->DelFd(user->fd);
691 * this must come before the WriteOpers so that it doesnt try to fill their buffer with anything
692 * if they were an oper with +s.
695 * In the current implementation, we only show local quits, as we only show local connects. With
696 * the proposed implmentation of snomasks however, this will likely change in the (near?) future.
698 if (user->registered == REG_ALL)
701 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
705 if (iter != clientlist.end())
707 log(DEBUG,"deleting user hash value %lx",(unsigned long)user);
710 fd_ref_table[user->fd] = NULL;
711 if (find(local_users.begin(),local_users.end(),user) != local_users.end())
713 local_users.erase(find(local_users.begin(),local_users.end(),user));
714 log(DEBUG,"Delete local user");
717 clientlist.erase(iter);
722 WhoWasGroup::WhoWasGroup(userrec* user) : host(NULL), dhost(NULL), ident(NULL), server(NULL), gecos(NULL), signon(user->signon)
724 this->host = strdup(user->host);
725 this->dhost = strdup(user->dhost);
726 this->ident = strdup(user->ident);
727 this->server = user->server;
728 this->gecos = strdup(user->fullname);
731 WhoWasGroup::~WhoWasGroup()
743 /* adds or updates an entry in the whowas list */
744 void AddWhoWas(userrec* u)
746 whowas_users::iterator iter = whowas.find(u->nick);
748 if (iter == whowas.end())
750 whowas_set* n = new whowas_set;
751 WhoWasGroup *a = new WhoWasGroup(u);
757 whowas_set* group = (whowas_set*)iter->second;
759 if (group->size() > 10)
761 WhoWasGroup *a = (WhoWasGroup*)*(group->begin());
766 WhoWasGroup *a = new WhoWasGroup(u);
771 /* every hour, run this function which removes all entries over 3 days */
772 void MaintainWhoWas(time_t TIME)
774 for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
776 whowas_set* n = (whowas_set*)iter->second;
779 while ((n->begin() != n->end()) && ((*n->begin())->signon < TIME - 259200)) // 3 days
781 WhoWasGroup *a = *(n->begin());
783 n->erase(n->begin());
789 /* add a client connection to the sockets list */
790 void AddClient(int socket, int port, bool iscached, insp_inaddr ip)
792 std::string tempnick = ConvToStr(socket) + "-unknown";
793 user_hash::iterator iter = clientlist.find(tempnick);
794 const char *ipaddr = insp_ntoa(ip);
800 * as these nicknames are 'RFC impossible', we can be sure nobody is going to be
801 * using one as a registered connection. As they are per fd, we can also safely assume
802 * that we wont have collisions. Therefore, if the nick exists in the list, its only
803 * used by a dead socket, erase the iterator so that the new client may reclaim it.
804 * this was probably the cause of 'server ignores me when i hammer it with reconnects'
805 * issue in earlier alphas/betas
807 if (iter != clientlist.end())
809 userrec* goner = iter->second;
811 clientlist.erase(iter);
814 log(DEBUG,"AddClient: %d %d %s",socket,port,ipaddr);
816 _new = new userrec();
817 clientlist[tempnick] = _new;
819 strlcpy(_new->nick,tempnick.c_str(),NICKMAX-1);
821 _new->server = FindServerNamePtr(Config->ServerName);
822 /* We don't need range checking here, we KNOW 'unknown\0' will fit into the ident field. */
823 strcpy(_new->ident, "unknown");
825 _new->registered = REG_NONE;
826 _new->signon = TIME + Config->dns_timeout;
829 log(DEBUG,"Setting socket addresses");
830 _new->SetSockAddr(AF_FAMILY, ipaddr, port);
831 log(DEBUG,"Socket addresses set.");
833 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
834 for (const char* temp = _new->GetIPString(); *temp && j < 64; temp++, j++)
835 _new->dhost[j] = _new->host[j] = *temp;
836 _new->dhost[j] = _new->host[j] = 0;
838 // set the registration timeout for this user
839 unsigned long class_regtimeout = 90;
841 long class_threshold = 5;
842 long class_sqmax = 262144; // 256kb
843 long class_rqmax = 4096; // 4k
845 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
847 if ((i->type == CC_ALLOW) && (match(ipaddr,i->host.c_str(),true)))
849 class_regtimeout = (unsigned long)i->registration_timeout;
850 class_flood = i->flood;
851 _new->pingmax = i->pingtime;
852 class_threshold = i->threshold;
853 class_sqmax = i->sendqmax;
854 class_rqmax = i->recvqmax;
859 _new->nping = TIME + _new->pingmax + Config->dns_timeout;
860 _new->timeout = TIME+class_regtimeout;
861 _new->flood = class_flood;
862 _new->threshold = class_threshold;
863 _new->sendqmax = class_sqmax;
864 _new->recvqmax = class_rqmax;
866 fd_ref_table[socket] = _new;
867 local_users.push_back(_new);
869 if (local_users.size() > Config->SoftLimit)
871 kill_link(_new,"No more connections allowed");
875 if (local_users.size() >= MAXCLIENTS)
877 kill_link(_new,"No more connections allowed");
883 * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
884 * its a pretty big but for the moment valid assumption:
885 * file descriptors are handed out starting at 0, and are recycled as theyre freed.
886 * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
887 * irc server at once (or the irc server otherwise initiating this many connections, files etc)
888 * which for the time being is a physical impossibility (even the largest networks dont have more
889 * than about 10,000 users on ONE server!)
891 if ((unsigned)socket >= MAX_DESCRIPTORS)
893 kill_link(_new,"Server is full");
896 char* e = matches_exception(ipaddr);
899 char* r = matches_zline(ipaddr);
903 snprintf(reason,MAXBUF,"Z-Lined: %s",r);
904 kill_link(_new,reason);
911 ServerInstance->SE->AddFd(socket,true,X_ESTAB_CLIENT);
914 log(DEBUG,"Writing to client %d",_new->fd);
915 WriteServ(_new->fd,"NOTICE Auth :*** Looking up your hostname...");
918 long FindMatchingGlobal(userrec* user)
923 for (user_hash::const_iterator a = clientlist.begin(); a != clientlist.end(); a++)
925 /* We have to match ip's as strings - we don't know what protocol
926 * a remote user may be using
928 if (!strcasecmp(a->second->GetIPString(u1), user->GetIPString(u2)))
934 long FindMatchingLocal(userrec* user)
937 for (std::vector<userrec*>::const_iterator a = local_users.begin(); a != local_users.end(); a++)
941 /* I dont think theres any faster way of matching two ipv6 addresses than memcmp */
942 in6_addr* s1 = &(((sockaddr_in6*)comp->ip)->sin6_addr);
943 in6_addr* s2 = &(((sockaddr_in6*)user->ip)->sin6_addr);
944 if (!memcmp(s1->s6_addr, s2->s6_addr, sizeof(in6_addr)))
947 in_addr* s1 = &((sockaddr_in*)comp->ip)->sin_addr;
948 in_addr* s2 = &((sockaddr_in*)user->ip)->sin_addr;
949 if (s1->s_addr == s2->s_addr)
956 void FullConnectUser(userrec* user, CullList* Goners)
958 ServerInstance->stats->statsConnects++;
959 user->idle_lastmsg = TIME;
960 log(DEBUG,"ConnectUser: %s",user->nick);
962 ConnectClass a = GetClass(user);
964 if (a.type == CC_DENY)
966 Goners->AddItem(user,"Unauthorised connection");
970 if ((*(a.pass.c_str())) && (!user->haspassed))
972 Goners->AddItem(user,"Invalid password");
976 if (FindMatchingLocal(user) > a.maxlocal)
978 Goners->AddItem(user,"No more connections allowed from your host via this connect class (local)");
979 WriteOpers("*** WARNING: maximum LOCAL connections (%ld) exceeded for IP %s",a.maxlocal,user->GetIPString());
982 else if (FindMatchingGlobal(user) > a.maxglobal)
984 Goners->AddItem(user,"No more connections allowed from your host via this connect class (global)");
985 WriteOpers("*** WARNING: maximum GLOBAL connections (%ld) exceeded for IP %s",a.maxglobal,user->GetIPString());
989 char match_against[MAXBUF];
990 snprintf(match_against,MAXBUF,"%s@%s",user->ident,user->host);
991 char* e = matches_exception(match_against);
995 char* r = matches_gline(match_against);
1000 snprintf(reason,MAXBUF,"G-Lined: %s",r);
1001 Goners->AddItem(user,reason);
1005 r = matches_kline(match_against);
1009 char reason[MAXBUF];
1010 snprintf(reason,MAXBUF,"K-Lined: %s",r);
1011 Goners->AddItem(user,reason);
1017 WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Config->Network);
1018 WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Config->Network,user->nick,user->ident,user->host);
1019 WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,Config->ServerName,VERSION);
1020 WriteServ(user->fd,"003 %s :This server was created %s %s",user->nick,__TIME__,__DATE__);
1021 WriteServ(user->fd,"004 %s %s %s %s %s %s",user->nick,Config->ServerName,VERSION,ServerInstance->ModeGrok->UserModeList().c_str(),ServerInstance->ModeGrok->ChannelModeList().c_str(),+ServerInstance->ModeGrok->ParaModeList().c_str());
1023 // anfl @ #ratbox, efnet reminded me that according to the RFC this cant contain more than 13 tokens per line...
1024 // so i'd better split it :)
1025 std::stringstream out(Config->data005);
1026 std::string token = "";
1027 std::string line5 = "";
1028 int token_counter = 0;
1033 line5 = line5 + token + " ";
1036 if ((token_counter >= 13) || (out.eof() == true))
1038 WriteServ(user->fd,"005 %s %s:are supported by this server",user->nick,line5.c_str());
1047 * fix 3 by brain, move registered = 7 below these so that spurious modes and host
1048 * changes dont go out onto the network and produce 'fake direction'.
1050 FOREACH_MOD(I_OnUserConnect,OnUserConnect(user));
1051 FOREACH_MOD(I_OnGlobalConnect,OnGlobalConnect(user));
1052 user->registered = REG_ALL;
1053 WriteOpers("*** Client connecting on port %d: %s!%s@%s [%s]",user->GetPort(),user->nick,user->ident,user->host,user->GetIPString());
1057 * re-allocates a nick in the user_hash after they change nicknames,
1058 * returns a pointer to the new user as it may have moved
1060 userrec* ReHashNick(const char* Old, const char* New)
1062 //user_hash::iterator newnick;
1063 user_hash::iterator oldnick = clientlist.find(Old);
1065 log(DEBUG,"ReHashNick: %s %s",Old,New);
1067 if (!strcasecmp(Old,New))
1069 log(DEBUG,"old nick is new nick, skipping");
1070 return oldnick->second;
1073 if (oldnick == clientlist.end())
1074 return NULL; /* doesnt exist */
1076 log(DEBUG,"ReHashNick: Found hashed nick %s",Old);
1078 userrec* olduser = oldnick->second;
1079 clientlist[New] = olduser;
1080 clientlist.erase(oldnick);
1082 log(DEBUG,"ReHashNick: Nick rehashed as %s",New);
1084 return clientlist[New];
1087 void force_nickchange(userrec* user,const char* newnick)
1094 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,newnick));
1098 ServerInstance->stats->statsCollisions++;
1099 kill_link(user,"Nickname collision");
1103 if (matches_qline(newnick))
1105 ServerInstance->stats->statsCollisions++;
1106 kill_link(user,"Nickname collision");
1114 strlcpy(nick,newnick,MAXBUF-1);
1117 if (user->registered == REG_ALL)
1119 const char* pars[1];
1121 std::string cmd = "NICK";
1123 ServerInstance->Parser->CallHandler(cmd,pars,1,user);
1128 void userrec::SetSockAddr(int protocol_family, const char* ip, int port)
1130 switch (protocol_family)
1132 #ifdef SUPPORT_IP6LINKS
1135 log(DEBUG,"Set inet6 protocol address");
1136 sockaddr_in6* sin = new sockaddr_in6;
1137 sin->sin6_family = AF_INET6;
1138 sin->sin6_port = port;
1139 inet_pton(AF_INET6, ip, &sin->sin6_addr);
1140 this->ip = (sockaddr*)sin;
1146 log(DEBUG,"Set inet4 protocol address");
1147 sockaddr_in* sin = new sockaddr_in;
1148 sin->sin_family = AF_INET;
1149 sin->sin_port = port;
1150 inet_pton(AF_INET, ip, &sin->sin_addr);
1151 this->ip = (sockaddr*)sin;
1155 log(DEBUG,"Ut oh, I dont know protocol %d to be set on '%s'!", protocol_family, this->nick);
1160 int userrec::GetPort()
1162 if (this->ip == NULL)
1165 switch (this->GetProtocolFamily())
1167 #ifdef SUPPORT_IP6LINKS
1170 sockaddr_in6* sin = (sockaddr_in6*)this->ip;
1171 return sin->sin6_port;
1177 sockaddr_in* sin = (sockaddr_in*)this->ip;
1178 return sin->sin_port;
1182 log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);
1188 int userrec::GetProtocolFamily()
1190 if (this->ip == NULL)
1193 sockaddr_in* sin = (sockaddr_in*)this->ip;
1194 return sin->sin_family;
1197 const char* userrec::GetIPString()
1199 static char buf[1024];
1200 static char temp[1024];
1202 if (this->ip == NULL)
1205 switch (this->GetProtocolFamily())
1207 #ifdef SUPPORT_IP6LINKS
1210 sockaddr_in6* sin = (sockaddr_in6*)this->ip;
1211 inet_ntop(sin->sin6_family, &sin->sin6_addr, buf, sizeof(buf));
1212 /* IP addresses starting with a : on irc are a Bad Thing (tm) */
1215 strlcpy(&temp[1], buf, sizeof(temp));
1225 sockaddr_in* sin = (sockaddr_in*)this->ip;
1226 inet_ntop(sin->sin_family, &sin->sin_addr, buf, sizeof(buf));
1231 log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);
1237 const char* userrec::GetIPString(char* buf)
1239 static char temp[1024];
1241 if (this->ip == NULL)
1247 switch (this->GetProtocolFamily())
1249 #ifdef SUPPORT_IP6LINKS
1252 sockaddr_in6* sin = (sockaddr_in6*)this->ip;
1253 inet_ntop(sin->sin6_family, &sin->sin6_addr, buf, sizeof(buf));
1254 /* IP addresses starting with a : on irc are a Bad Thing (tm) */
1257 strlcpy(&temp[1], buf, sizeof(temp));
1259 strlcpy(buf, temp, sizeof(temp));
1267 sockaddr_in* sin = (sockaddr_in*)this->ip;
1268 inet_ntop(sin->sin_family, &sin->sin_addr, buf, sizeof(buf));
1274 log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);