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 "configreader.h"
22 #include "socketengine.h"
25 #include "cull_list.h"
27 static unsigned long already_sent[MAX_DESCRIPTORS] = {0};
29 /* XXX: Used for speeding up WriteCommon operations */
30 unsigned long uniq_id = 0;
32 bool InitTypes(ServerConfig* conf, const char* tag)
34 if (conf->opertypes.size())
36 conf->GetInstance()->Log(DEBUG,"Currently %d items to clear",conf->opertypes.size());
37 for (opertype_t::iterator n = conf->opertypes.begin(); n != conf->opertypes.end(); n++)
39 conf->GetInstance()->Log(DEBUG,"Clear item");
45 conf->opertypes.clear();
49 bool InitClasses(ServerConfig* conf, const char* tag)
51 if (conf->operclass.size())
53 for (operclass_t::iterator n = conf->operclass.begin(); n != conf->operclass.end(); n++)
60 conf->operclass.clear();
64 bool DoType(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
66 char* TypeName = (char*)values[0];
67 char* Classes = (char*)values[1];
69 conf->opertypes[TypeName] = strdup(Classes);
70 conf->GetInstance()->Log(DEBUG,"Read oper TYPE '%s' with classes '%s'",TypeName,Classes);
74 bool DoClass(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
76 char* ClassName = (char*)values[0];
77 char* CommandList = (char*)values[1];
79 conf->operclass[ClassName] = strdup(CommandList);
80 conf->GetInstance()->Log(DEBUG,"Read oper CLASS '%s' with commands '%s'",ClassName,CommandList);
84 bool DoneClassesAndTypes(ServerConfig* conf, const char* tag)
89 std::string userrec::ProcessNoticeMasks(const char *sm)
91 bool adding = true, oldadding = false;
95 ServerInstance->Log(DEBUG,"Process notice masks");
99 ServerInstance->Log(DEBUG,"Process notice mask %c",*c);
110 for (unsigned char d = 'A'; d <= 'z'; d++)
112 if (ServerInstance->SNO->IsEnabled(d))
114 if ((!IsNoticeMaskSet(d) && adding) || (IsNoticeMaskSet(d) && !adding))
116 if ((oldadding != adding) || (!output.length()))
117 output += (adding ? '+' : '-');
119 this->SetNoticeMask(d, adding);
128 if ((*c >= 'A') && (*c <= 'z') && (ServerInstance->SNO->IsEnabled(*c)))
130 if ((!IsNoticeMaskSet(*c) && adding) || (IsNoticeMaskSet(*c) && !adding))
132 if ((oldadding != adding) || (!output.length()))
133 output += (adding ? '+' : '-');
135 this->SetNoticeMask(*c, adding);
150 void userrec::StartDNSLookup()
152 ServerInstance->Log(DEBUG,"Commencing reverse lookup");
155 ServerInstance->Log(DEBUG,"Passing instance: %08x",this->ServerInstance);
156 res_reverse = new UserResolver(this->ServerInstance, this, this->GetIPString(), DNS_QUERY_REVERSE);
157 this->ServerInstance->AddResolver(res_reverse);
159 catch (ModuleException& e)
161 ServerInstance->Log(DEBUG,"Error in resolver: %s",e.GetReason());
165 UserResolver::UserResolver(InspIRCd* Instance, userrec* user, std::string to_resolve, QueryType qt) :
166 Resolver(Instance, to_resolve, qt), bound_user(user)
168 this->fwd = (qt == DNS_QUERY_A || qt == DNS_QUERY_AAAA);
169 this->bound_fd = user->GetFd();
172 void UserResolver::OnLookupComplete(const std::string &result)
174 if ((!this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
176 ServerInstance->Log(DEBUG,"Commencing forward lookup");
177 this->bound_user->stored_host = result;
180 /* Check we didnt time out */
181 if (this->bound_user->registered != REG_ALL)
184 const char *ip = this->bound_user->GetIPString();
185 bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, (strstr(ip,"0::ffff:") == ip ? DNS_QUERY_A : DNS_QUERY_AAAA));
187 bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_A);
189 this->ServerInstance->AddResolver(bound_user->res_forward);
192 catch (ModuleException& e)
194 ServerInstance->Log(DEBUG,"Error in resolver: %s",e.GetReason());
197 else if ((this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
199 /* Both lookups completed */
200 std::string result2 = "0::ffff:";
201 result2.append(result);
202 if (this->bound_user->GetIPString() == result || this->bound_user->GetIPString() == result2)
204 std::string hostname = this->bound_user->stored_host;
205 if (hostname.length() < 65)
207 /* Check we didnt time out */
208 if (this->bound_user->registered != REG_ALL)
210 /* Hostnames starting with : are not a good thing (tm) */
211 if (*(hostname.c_str()) == ':')
212 hostname = "0" + hostname;
214 this->bound_user->WriteServ("NOTICE Auth :*** Found your hostname (%s)", hostname.c_str());
215 this->bound_user->dns_done = true;
216 strlcpy(this->bound_user->dhost, hostname.c_str(),64);
217 strlcpy(this->bound_user->host, hostname.c_str(),64);
222 this->bound_user->WriteServ("NOTICE Auth :*** Your hostname is longer than the maximum of 64 characters, using your IP address (%s) instead.", this->bound_user->GetIPString());
227 this->bound_user->WriteServ("NOTICE Auth :*** Your hostname does not match up with your IP address. Sorry, using your IP address (%s) instead.", this->bound_user->GetIPString());
232 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
234 if (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user)
236 /* Error message here */
237 this->bound_user->WriteServ("NOTICE Auth :*** Could not resolve your hostname, using your IP address (%s) instead.", this->bound_user->GetIPString());
238 this->bound_user->dns_done = true;
243 bool userrec::IsNoticeMaskSet(unsigned char sm)
245 return (snomasks[sm-65]);
248 void userrec::SetNoticeMask(unsigned char sm, bool value)
250 snomasks[sm-65] = value;
253 const char* userrec::FormatNoticeMasks()
255 static char data[MAXBUF];
258 for (int n = 0; n < 64; n++)
261 data[offset++] = n+65;
270 bool userrec::IsModeSet(unsigned char m)
272 return (modes[m-65]);
275 void userrec::SetMode(unsigned char m, bool value)
280 const char* userrec::FormatModes()
282 static char data[MAXBUF];
284 for (int n = 0; n < 64; n++)
287 data[offset++] = n+65;
293 userrec::userrec(InspIRCd* Instance) : ServerInstance(Instance)
295 ServerInstance->Log(DEBUG,"userrec::userrec(): Instance: %08x",ServerInstance);
296 // the PROPER way to do it, AVOID bzero at *ALL* costs
297 *password = *nick = *ident = *host = *dhost = *fullname = *awaymsg = *oper = 0;
298 server = (char*)Instance->FindServerNamePtr(Instance->Config->ServerName);
299 reset_due = ServerInstance->Time();
300 age = ServerInstance->Time(true);
301 lines_in = lastping = signon = idle_lastmsg = nping = registered = 0;
302 ChannelCount = timeout = flood = bytes_in = bytes_out = cmds_in = cmds_out = 0;
303 haspassed = dns_done = false;
308 res_forward = res_reverse = NULL;
312 chans.resize(MAXCHANS);
313 memset(modes,0,sizeof(modes));
314 memset(snomasks,0,sizeof(snomasks));
316 for (unsigned int n = 0; n < MAXCHANS; n++)
318 chans[n] = new ucrec();
319 chans[n]->channel = NULL;
320 chans[n]->uc_modes = 0;
326 for (std::vector<ucrec*>::iterator n = chans.begin(); n != chans.end(); n++)
328 ucrec* x = (ucrec*)*n;
334 if (this->GetProtocolFamily() == AF_INET)
336 delete (sockaddr_in*)ip;
338 #ifdef SUPPORT_IP6LINKS
341 delete (sockaddr_in6*)ip;
347 char* userrec::MakeHost()
349 static char nhost[MAXBUF];
350 /* This is much faster than snprintf */
352 for(char* n = ident; *n; n++)
355 for(char* n = host; *n; n++)
361 char* userrec::MakeHostIP()
363 static char ihost[MAXBUF];
364 /* This is much faster than snprintf */
366 for(char* n = ident; *n; n++)
369 for(const char* n = this->GetIPString(); *n; n++)
375 void userrec::CloseSocket()
377 shutdown(this->fd,2);
381 char* userrec::GetFullHost()
383 static char result[MAXBUF];
385 for(char* n = nick; *n; n++)
388 for(char* n = ident; *n; n++)
391 for(char* n = dhost; *n; n++)
397 char* userrec::MakeWildHost()
399 static char nresult[MAXBUF];
401 *t++ = '*'; *t++ = '!';
402 *t++ = '*'; *t++ = '@';
403 for(char* n = dhost; *n; n++)
409 int userrec::ReadData(void* buffer, size_t size)
413 return read(this->fd, buffer, size);
420 char* userrec::GetFullRealHost()
422 static char fresult[MAXBUF];
424 for(char* n = nick; *n; n++)
427 for(char* n = ident; *n; n++)
430 for(char* n = host; *n; n++)
436 bool userrec::IsInvited(irc::string &channel)
438 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
440 irc::string compare = i->channel;
442 if (compare == channel)
450 InvitedList* userrec::GetInviteList()
455 void userrec::InviteTo(irc::string &channel)
459 invites.push_back(i);
462 void userrec::RemoveInvite(irc::string &channel)
464 ServerInstance->Log(DEBUG,"Removing invites");
468 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
470 irc::string compare = i->channel;
472 if (compare == channel)
481 bool userrec::HasPermission(const std::string &command)
488 * users on remote servers can completely bypass all permissions based checks.
489 * This prevents desyncs when one server has different type/class tags to another.
490 * That having been said, this does open things up to the possibility of source changes
491 * allowing remote kills, etc - but if they have access to the src, they most likely have
492 * access to the conf - so it's an end to a means either way.
497 // are they even an oper at all?
500 opertype_t::iterator iter_opertype = ServerInstance->Config->opertypes.find(this->oper);
501 if (iter_opertype != ServerInstance->Config->opertypes.end())
503 char* Classes = strdup(iter_opertype->second);
504 char* myclass = strtok_r(Classes," ",&savept);
507 operclass_t::iterator iter_operclass = ServerInstance->Config->operclass.find(myclass);
508 if (iter_operclass != ServerInstance->Config->operclass.end())
510 char* CommandList = strdup(iter_operclass->second);
511 mycmd = strtok_r(CommandList," ",&savept2);
514 if ((!strcasecmp(mycmd,command.c_str())) || (*mycmd == '*'))
520 mycmd = strtok_r(NULL," ",&savept2);
524 myclass = strtok_r(NULL," ",&savept);
532 /** NOTE: We cannot pass a const reference to this method.
533 * The string is changed by the workings of the method,
534 * so that if we pass const ref, we end up copying it to
535 * something we can change anyway. Makes sense to just let
536 * the compiler do that copy for us.
538 bool userrec::AddBuffer(std::string a)
542 std::string::size_type i = a.rfind('\r');
544 while (i != std::string::npos)
553 if (recvq.length() > (unsigned)this->recvqmax)
555 this->SetWriteError("RecvQ exceeded");
556 ServerInstance->WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
565 ServerInstance->Log(DEBUG,"Exception in userrec::AddBuffer()");
570 bool userrec::BufferIsReady()
572 return (recvq.find('\n') != std::string::npos);
575 void userrec::ClearBuffer()
580 std::string userrec::GetBuffer()
587 /* Strip any leading \r or \n off the string.
588 * Usually there are only one or two of these,
589 * so its is computationally cheap to do.
591 while ((*recvq.begin() == '\r') || (*recvq.begin() == '\n'))
592 recvq.erase(recvq.begin());
594 for (std::string::iterator x = recvq.begin(); x != recvq.end(); x++)
596 /* Find the first complete line, return it as the
597 * result, and leave the recvq as whats left
601 std::string ret = std::string(recvq.begin(), x);
602 recvq.erase(recvq.begin(), x + 1);
611 ServerInstance->Log(DEBUG,"Exception in userrec::GetBuffer()");
616 void userrec::AddWriteBuf(const std::string &data)
618 if (*this->GetWriteError())
621 if (sendq.length() + data.length() > (unsigned)this->sendqmax)
624 * Fix by brain - Set the error text BEFORE calling writeopers, because
625 * if we dont it'll recursively call here over and over again trying
626 * to repeatedly add the text to the sendq!
628 this->SetWriteError("SendQ exceeded");
629 ServerInstance->WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
635 if (data.length() > 512)
637 std::string newdata(data);
639 newdata.append("\r\n");
640 sendq.append(newdata);
649 this->SetWriteError("SendQ exceeded");
650 ServerInstance->WriteOpers("*** User %s SendQ got an exception",this->nick);
654 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
655 void userrec::FlushWriteBuf()
659 if ((this->fd == FD_MAGIC_NUMBER) || (*this->GetWriteError()))
663 if ((sendq.length()) && (this->fd != FD_MAGIC_NUMBER))
665 int old_sendq_length = sendq.length();
666 const char* tb = this->sendq.c_str();
667 int n_sent = write(this->fd,tb,this->sendq.length());
672 ServerInstance->Log(DEBUG,"EAGAIN, want write");
673 this->ServerInstance->SE->WantWrite(this);
676 this->SetWriteError(strerror(errno));
680 /*ServerInstance->Log(DEBUG,"Wrote: %d of %d: %s", n_sent, old_sendq_length, sendq.substr(0, n_sent).c_str());*/
684 // update the user's stats counters
685 this->bytes_out += n_sent;
687 if (n_sent != old_sendq_length)
689 ServerInstance->Log(DEBUG,"Not all written, want write");
690 this->ServerInstance->SE->WantWrite(this);
698 ServerInstance->Log(DEBUG,"Exception in userrec::FlushWriteBuf()");
702 void userrec::SetWriteError(const std::string &error)
706 ServerInstance->Log(DEBUG,"SetWriteError: %s",error.c_str());
707 // don't try to set the error twice, its already set take the first string.
708 if (!this->WriteError.length())
710 ServerInstance->Log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
711 this->WriteError = error;
717 ServerInstance->Log(DEBUG,"Exception in userrec::SetWriteError()");
721 const char* userrec::GetWriteError()
723 return this->WriteError.c_str();
726 void userrec::Oper(const std::string &opertype)
730 this->modes[UM_OPERATOR] = 1;
731 this->WriteServ("MODE %s :+o", this->nick);
732 FOREACH_MOD(I_OnOper, OnOper(this, opertype));
733 ServerInstance->Log(DEFAULT,"OPER: %s!%s@%s opered as type: %s", this->nick, this->ident, this->host, opertype.c_str());
734 strlcpy(this->oper, opertype.c_str(), NICKMAX - 1);
735 ServerInstance->all_opers.push_back(this);
736 FOREACH_MOD(I_OnPostOper,OnPostOper(this, opertype));
741 ServerInstance->Log(DEBUG,"Exception in userrec::Oper()");
745 void userrec::UnOper()
752 this->modes[UM_OPERATOR] = 0;
753 for (std::vector<userrec*>::iterator a = ServerInstance->all_opers.begin(); a < ServerInstance->all_opers.end(); a++)
757 ServerInstance->Log(DEBUG,"Oper removed from optimization list");
758 ServerInstance->all_opers.erase(a);
767 ServerInstance->Log(DEBUG,"Exception in userrec::UnOper()");
771 void userrec::QuitUser(InspIRCd* Instance, userrec *user, const std::string &quitreason)
773 user_hash::iterator iter = Instance->clientlist.find(user->nick);
774 std::string reason = quitreason;
776 if (reason.length() > MAXQUIT - 1)
777 reason.resize(MAXQUIT - 1);
780 user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason.c_str());
782 if (user->registered == REG_ALL)
784 user->PurgeEmptyChannels();
785 FOREACH_MOD_I(Instance,I_OnUserQuit,OnUserQuit(user,reason));
786 user->WriteCommonExcept("QUIT :%s",reason.c_str());
790 user->FlushWriteBuf();
792 FOREACH_MOD_I(Instance,I_OnUserDisconnect,OnUserDisconnect(user));
796 if (Instance->Config->GetIOHook(user->GetPort()))
800 Instance->Config->GetIOHook(user->GetPort())->OnRawSocketClose(user->fd);
802 catch (ModuleException& modexcept)
804 Instance->Log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
808 Instance->SE->DelFd(user);
813 * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
814 * if they were an oper with +sn +qQ.
816 if (user->registered == REG_ALL)
819 Instance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason.c_str());
821 Instance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",user->server,user->nick,user->ident,user->host,reason.c_str());
825 if (iter != Instance->clientlist.end())
827 Instance->Log(DEBUG,"deleting user hash value %lx",(unsigned long)user);
830 if (find(Instance->local_users.begin(),Instance->local_users.end(),user) != Instance->local_users.end())
831 Instance->local_users.erase(find(Instance->local_users.begin(),Instance->local_users.end(),user));
833 Instance->clientlist.erase(iter);
843 WhoWasGroup::WhoWasGroup(userrec* user) : host(NULL), dhost(NULL), ident(NULL), server(NULL), gecos(NULL), signon(user->signon)
845 this->host = strdup(user->host);
846 this->dhost = strdup(user->dhost);
847 this->ident = strdup(user->ident);
848 this->server = user->server;
849 this->gecos = strdup(user->fullname);
852 WhoWasGroup::~WhoWasGroup()
864 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
865 void MaintainWhoWas(InspIRCd* ServerInstance, time_t t)
867 for (whowas_users::iterator iter = ServerInstance->whowas.begin(); iter != ServerInstance->whowas.end(); iter++)
869 whowas_set* n = (whowas_set*)iter->second;
872 while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
874 WhoWasGroup *a = *(n->begin());
876 n->erase(n->begin());
881 /* on rehash, refactor maps according to new conf values */
882 void PruneWhoWas(InspIRCd* ServerInstance, time_t t)
885 int groupsize = ServerInstance->Config->WhoWasGroupSize;
886 int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
887 int maxkeep = ServerInstance->Config->WhoWasMaxKeep;
889 int groupcount = ServerInstance->whowas.size();
890 /* iterate whowas_fifo oldest first */
891 for (whowas_users_fifo::iterator iter = ServerInstance->whowas_fifo.begin(); iter != ServerInstance->whowas_fifo.end(); iter++)
893 /** prune all groups that has expired due to new maxkeep time and
894 * also any group number higher than new maxgroups. The oldest are
895 * removed first due to iteration over whowas_fifo
897 if (groupcount > maxgroups || iter->first < t - maxkeep)
899 whowas_set* n = (whowas_set*)ServerInstance->whowas.find(iter->second)->second;
902 while (n->begin() != n->end())
904 WhoWasGroup *a = *(n->begin());
906 n->erase(n->begin());
909 ServerInstance->whowas.erase(iter->second);
910 ServerInstance->whowas_fifo.erase(iter->first);
913 /* also trim individual groupsizes in case groupsize should have been lowered */
914 whowas_set* n = (whowas_set*)ServerInstance->whowas.find(iter->second)->second;
917 int nickcount = n->size();
918 while (n->begin() != n->end() && nickcount > groupsize)
920 WhoWasGroup *a = *(n->begin());
922 n->erase(n->begin());
933 /* adds or updates an entry in the whowas list */
934 void userrec::AddToWhoWas()
936 /* if whowas disabled */
937 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
942 irc::whowas::whowas_users::iterator iter = ServerInstance->whowas.find(this->nick);
944 ServerInstance->Log(DEBUG,"Add to whowas lists");
946 if (iter == ServerInstance->whowas.end())
948 ServerInstance->Log(DEBUG,"Adding new whowas set for %s",this->nick);
949 irc::whowas::whowas_set* n = new irc::whowas::whowas_set;
950 irc::whowas::WhoWasGroup *a = new irc::whowas::WhoWasGroup(this);
952 ServerInstance->whowas[this->nick] = n;
953 ServerInstance->whowas_fifo[ServerInstance->Time()] = this->nick;
954 if ((int)(ServerInstance->whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
956 ServerInstance->Log(DEBUG,"Maxgroups of %d reached deleting oldest group '%s'",ServerInstance->Config->WhoWasMaxGroups, ServerInstance->whowas_fifo.begin()->second.c_str());
957 ServerInstance->whowas.erase(ServerInstance->whowas_fifo.begin()->second);
958 ServerInstance->whowas_fifo.erase(ServerInstance->whowas_fifo.begin());
963 irc::whowas::whowas_set* group = (irc::whowas::whowas_set*)iter->second;
965 ServerInstance->Log(DEBUG,"Using existing whowas group for %s",this->nick);
967 if ((int)(group->size()) >= ServerInstance->Config->WhoWasGroupSize)
969 ServerInstance->Log(DEBUG,"Trimming existing group '%s' to %d entries",this->nick, ServerInstance->Config->WhoWasGroupSize);
970 irc::whowas::WhoWasGroup *a = (irc::whowas::WhoWasGroup*)*(group->begin());
975 irc::whowas::WhoWasGroup *a = new irc::whowas::WhoWasGroup(this);
980 /* add a client connection to the sockets list */
981 void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached, insp_inaddr ip)
983 std::string tempnick = ConvToStr(socket) + "-unknown";
984 user_hash::iterator iter = Instance->clientlist.find(tempnick);
985 const char *ipaddr = insp_ntoa(ip);
991 * as these nicknames are 'RFC impossible', we can be sure nobody is going to be
992 * using one as a registered connection. As they are per fd, we can also safely assume
993 * that we wont have collisions. Therefore, if the nick exists in the list, its only
994 * used by a dead socket, erase the iterator so that the new client may reclaim it.
995 * this was probably the cause of 'server ignores me when i hammer it with reconnects'
996 * issue in earlier alphas/betas
998 if (iter != Instance->clientlist.end())
1000 userrec* goner = iter->second;
1002 Instance->clientlist.erase(iter);
1005 Instance->Log(DEBUG,"AddClient: %d %d %s",socket,port,ipaddr);
1007 New = new userrec(Instance);
1008 Instance->clientlist[tempnick] = New;
1010 strlcpy(New->nick,tempnick.c_str(),NICKMAX-1);
1012 New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
1013 /* We don't need range checking here, we KNOW 'unknown\0' will fit into the ident field. */
1014 strcpy(New->ident, "unknown");
1016 New->registered = REG_NONE;
1017 New->signon = Instance->Time() + Instance->Config->dns_timeout;
1020 Instance->Log(DEBUG,"Setting socket addresses");
1021 New->SetSockAddr(AF_FAMILY, ipaddr, port);
1022 Instance->Log(DEBUG,"Socket addresses set.");
1024 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
1025 for (const char* temp = New->GetIPString(); *temp && j < 64; temp++, j++)
1026 New->dhost[j] = New->host[j] = *temp;
1027 New->dhost[j] = New->host[j] = 0;
1029 Instance->Log(DEBUG,"Hosts set.");
1031 // set the registration timeout for this user
1032 unsigned long class_regtimeout = 90;
1033 int class_flood = 0;
1034 long class_threshold = 5;
1035 long class_sqmax = 262144; // 256kb
1036 long class_rqmax = 4096; // 4k
1038 Instance->Log(DEBUG,"Class stuff set.");
1040 for (ClassVector::iterator i = Instance->Config->Classes.begin(); i != Instance->Config->Classes.end(); i++)
1042 if ((i->type == CC_ALLOW) && (match(ipaddr,i->host.c_str(),true)))
1044 class_regtimeout = (unsigned long)i->registration_timeout;
1045 class_flood = i->flood;
1046 New->pingmax = i->pingtime;
1047 class_threshold = i->threshold;
1048 class_sqmax = i->sendqmax;
1049 class_rqmax = i->recvqmax;
1054 Instance->Log(DEBUG,"nping etc set.");
1056 New->nping = Instance->Time() + New->pingmax + Instance->Config->dns_timeout;
1057 New->timeout = Instance->Time() + class_regtimeout;
1058 New->flood = class_flood;
1059 New->threshold = class_threshold;
1060 New->sendqmax = class_sqmax;
1061 New->recvqmax = class_rqmax;
1063 Instance->Log(DEBUG,"Push back to local users.");
1064 Instance->local_users.push_back(New);
1066 Instance->Log(DEBUG,"Check softlimit: %d %d %d",Instance->local_users.size(), Instance->Config->SoftLimit, MAXCLIENTS);
1067 if ((Instance->local_users.size() > Instance->Config->SoftLimit) || (Instance->local_users.size() >= MAXCLIENTS))
1069 Instance->Log(DEBUG,"Check softlimit failed");
1070 Instance->WriteOpers("*** Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
1071 userrec::QuitUser(Instance, New,"No more connections allowed");
1075 Instance->Log(DEBUG,"Softlimit passed.");
1079 * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
1080 * its a pretty big but for the moment valid assumption:
1081 * file descriptors are handed out starting at 0, and are recycled as theyre freed.
1082 * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
1083 * irc server at once (or the irc server otherwise initiating this many connections, files etc)
1084 * which for the time being is a physical impossibility (even the largest networks dont have more
1085 * than about 10,000 users on ONE server!)
1087 if ((unsigned int)socket >= MAX_DESCRIPTORS)
1089 userrec::QuitUser(Instance, New, "Server is full");
1093 Instance->Log(DEBUG,"socket < MAX_DESCRIPTORS passed.");
1095 ELine* e = Instance->XLines->matches_exception(New);
1098 Instance->Log(DEBUG,"Doesnt match eline.");
1099 ZLine* r = Instance->XLines->matches_zline(ipaddr);
1102 Instance->Log(DEBUG,"Matches zline.");
1103 char reason[MAXBUF];
1104 snprintf(reason,MAXBUF,"Z-Lined: %s",r->reason);
1105 userrec::QuitUser(Instance, New, reason);
1108 Instance->Log(DEBUG,"Doesnt match zline.");
1111 Instance->Log(DEBUG,"Check before AddFd.");
1115 Instance->Log(DEBUG,"Adding fd.");
1116 if (!Instance->SE->AddFd(New))
1118 Instance->Log(DEBUG,"Oops, fd already exists");
1119 userrec::QuitUser(Instance, New, "Internal error handling connection");
1124 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
1127 long userrec::GlobalCloneCount()
1131 strlcpy(u2, this->GetIPString(), 64);
1133 for (user_hash::const_iterator a = ServerInstance->clientlist.begin(); a != ServerInstance->clientlist.end(); a++)
1135 /* We have to match ip's as strings - we don't know what protocol
1136 * a remote user may be using
1138 if (strcasecmp(a->second->GetIPString(), u2) == 0)
1145 long userrec::LocalCloneCount()
1148 for (std::vector<userrec*>::const_iterator a = ServerInstance->local_users.begin(); a != ServerInstance->local_users.end(); a++)
1152 /* I dont think theres any faster way of matching two ipv6 addresses than memcmp */
1153 in6_addr* s1 = &(((sockaddr_in6*)comp->ip)->sin6_addr);
1154 in6_addr* s2 = &(((sockaddr_in6*)this->ip)->sin6_addr);
1155 if (!memcmp(s1->s6_addr, s2->s6_addr, sizeof(in6_addr)))
1158 in_addr* s1 = &((sockaddr_in*)comp->ip)->sin_addr;
1159 in_addr* s2 = &((sockaddr_in*)this->ip)->sin_addr;
1160 if (s1->s_addr == s2->s_addr)
1167 void userrec::FullConnect(CullList* Goners)
1169 ServerInstance->stats->statsConnects++;
1170 this->idle_lastmsg = ServerInstance->Time();
1172 ConnectClass a = this->GetClass();
1174 if (a.type == CC_DENY)
1176 Goners->AddItem(this,"Unauthorised connection");
1180 if ((*(a.pass.c_str())) && (!this->haspassed))
1182 Goners->AddItem(this,"Invalid password");
1186 if (this->LocalCloneCount() > a.maxlocal)
1188 Goners->AddItem(this, "No more connections allowed from your host via this connect class (local)");
1189 ServerInstance->WriteOpers("*** WARNING: maximum LOCAL connections (%ld) exceeded for IP %s", a.maxlocal, this->GetIPString());
1192 else if (this->GlobalCloneCount() > a.maxglobal)
1194 Goners->AddItem(this, "No more connections allowed from your host via this connect class (global)");
1195 ServerInstance->WriteOpers("*** WARNING: maximum GLOBAL connections (%ld) exceeded for IP %s",a.maxglobal, this->GetIPString());
1199 ELine* e = ServerInstance->XLines->matches_exception(this);
1203 GLine* r = ServerInstance->XLines->matches_gline(this);
1207 char reason[MAXBUF];
1208 snprintf(reason,MAXBUF,"G-Lined: %s",r->reason);
1209 Goners->AddItem(this, reason);
1213 KLine* n = ServerInstance->XLines->matches_kline(this);
1217 char reason[MAXBUF];
1218 snprintf(reason,MAXBUF,"K-Lined: %s",n->reason);
1219 Goners->AddItem(this, reason);
1225 this->WriteServ("NOTICE Auth :Welcome to \002%s\002!",ServerInstance->Config->Network);
1226 this->WriteServ("001 %s :Welcome to the %s IRC Network %s!%s@%s",this->nick, ServerInstance->Config->Network, this->nick, this->ident, this->host);
1227 this->WriteServ("002 %s :Your host is %s, running version %s",this->nick,ServerInstance->Config->ServerName,VERSION);
1228 this->WriteServ("003 %s :This server was created %s %s", this->nick, __TIME__, __DATE__);
1229 this->WriteServ("004 %s %s %s %s %s %s", this->nick, ServerInstance->Config->ServerName, VERSION, ServerInstance->Modes->UserModeList().c_str(), ServerInstance->Modes->ChannelModeList().c_str(), ServerInstance->Modes->ParaModeList().c_str());
1231 // anfl @ #ratbox, efnet reminded me that according to the RFC this cant contain more than 13 tokens per line...
1232 // so i'd better split it :)
1233 std::stringstream out(ServerInstance->Config->data005);
1234 std::string token = "";
1235 std::string line5 = "";
1236 int token_counter = 0;
1241 line5 = line5 + token + " ";
1244 if ((token_counter >= 13) || (out.eof() == true))
1246 this->WriteServ("005 %s %s:are supported by this server", this->nick, line5.c_str());
1255 * fix 3 by brain, move registered = 7 below these so that spurious modes and host
1256 * changes dont go out onto the network and produce 'fake direction'.
1258 FOREACH_MOD(I_OnUserConnect,OnUserConnect(this));
1260 this->registered = REG_ALL;
1262 FOREACH_MOD(I_OnPostConnect,OnPostConnect(this));
1264 ServerInstance->SNO->WriteToSnoMask('c',"Client connecting on port %d: %s!%s@%s [%s]", this->GetPort(), this->nick, this->ident, this->host, this->GetIPString());
1267 /** userrec::UpdateNick()
1268 * re-allocates a nick in the user_hash after they change nicknames,
1269 * returns a pointer to the new user as it may have moved
1271 userrec* userrec::UpdateNickHash(const char* New)
1275 //user_hash::iterator newnick;
1276 user_hash::iterator oldnick = ServerInstance->clientlist.find(this->nick);
1278 if (!strcasecmp(this->nick,New))
1279 return oldnick->second;
1281 if (oldnick == ServerInstance->clientlist.end())
1282 return NULL; /* doesnt exist */
1284 userrec* olduser = oldnick->second;
1285 ServerInstance->clientlist[New] = olduser;
1286 ServerInstance->clientlist.erase(oldnick);
1287 return ServerInstance->clientlist[New];
1292 ServerInstance->Log(DEBUG,"Exception in userrec::UpdateNickHash()");
1297 bool userrec::ForceNickChange(const char* newnick)
1303 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(this, newnick));
1307 ServerInstance->stats->statsCollisions++;
1311 if (ServerInstance->XLines->matches_qline(newnick))
1313 ServerInstance->stats->statsCollisions++;
1317 if (this->registered == REG_ALL)
1319 const char* pars[1];
1321 std::string cmd = "NICK";
1322 return (ServerInstance->Parser->CallHandler(cmd, pars, 1, this) == CMD_SUCCESS);
1329 ServerInstance->Log(DEBUG,"Exception in userrec::ForceNickChange()");
1334 void userrec::SetSockAddr(int protocol_family, const char* ip, int port)
1336 switch (protocol_family)
1338 #ifdef SUPPORT_IP6LINKS
1341 ServerInstance->Log(DEBUG,"Set inet6 protocol address");
1342 sockaddr_in6* sin = new sockaddr_in6;
1343 sin->sin6_family = AF_INET6;
1344 sin->sin6_port = port;
1345 inet_pton(AF_INET6, ip, &sin->sin6_addr);
1346 this->ip = (sockaddr*)sin;
1352 ServerInstance->Log(DEBUG,"Set inet4 protocol address");
1353 sockaddr_in* sin = new sockaddr_in;
1354 sin->sin_family = AF_INET;
1355 sin->sin_port = port;
1356 inet_pton(AF_INET, ip, &sin->sin_addr);
1357 this->ip = (sockaddr*)sin;
1361 ServerInstance->Log(DEBUG,"Ut oh, I dont know protocol %d to be set on '%s'!", protocol_family, this->nick);
1366 int userrec::GetPort()
1368 if (this->ip == NULL)
1371 switch (this->GetProtocolFamily())
1373 #ifdef SUPPORT_IP6LINKS
1376 sockaddr_in6* sin = (sockaddr_in6*)this->ip;
1377 return sin->sin6_port;
1383 sockaddr_in* sin = (sockaddr_in*)this->ip;
1384 return sin->sin_port;
1388 ServerInstance->Log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);
1394 int userrec::GetProtocolFamily()
1396 if (this->ip == NULL)
1399 sockaddr_in* sin = (sockaddr_in*)this->ip;
1400 return sin->sin_family;
1403 const char* userrec::GetIPString()
1405 static char buf[1024];
1407 if (this->ip == NULL)
1410 switch (this->GetProtocolFamily())
1412 #ifdef SUPPORT_IP6LINKS
1415 static char temp[1024];
1417 sockaddr_in6* sin = (sockaddr_in6*)this->ip;
1418 inet_ntop(sin->sin6_family, &sin->sin6_addr, buf, sizeof(buf));
1419 /* IP addresses starting with a : on irc are a Bad Thing (tm) */
1422 strlcpy(&temp[1], buf, sizeof(temp) - 1);
1432 sockaddr_in* sin = (sockaddr_in*)this->ip;
1433 inet_ntop(sin->sin_family, &sin->sin_addr, buf, sizeof(buf));
1438 ServerInstance->Log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);
1444 const char* userrec::GetIPString(char* buf)
1446 if (this->ip == NULL)
1452 switch (this->GetProtocolFamily())
1454 #ifdef SUPPORT_IP6LINKS
1457 static char temp[1024];
1459 sockaddr_in6* sin = (sockaddr_in6*)this->ip;
1460 inet_ntop(sin->sin6_family, &sin->sin6_addr, buf, sizeof(buf));
1461 /* IP addresses starting with a : on irc are a Bad Thing (tm) */
1464 strlcpy(&temp[1], buf, sizeof(temp) - 1);
1466 strlcpy(buf, temp, sizeof(temp));
1474 sockaddr_in* sin = (sockaddr_in*)this->ip;
1475 inet_ntop(sin->sin_family, &sin->sin_addr, buf, sizeof(buf));
1481 ServerInstance->Log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);
1487 /** NOTE: We cannot pass a const reference to this method.
1488 * The string is changed by the workings of the method,
1489 * so that if we pass const ref, we end up copying it to
1490 * something we can change anyway. Makes sense to just let
1491 * the compiler do that copy for us.
1493 void userrec::Write(std::string text)
1495 if ((this->fd < 0) || (this->fd > MAX_DESCRIPTORS))
1500 text.append("\r\n");
1504 ServerInstance->Log(DEBUG,"Exception in userrec::Write() std::string::append");
1508 if (ServerInstance->Config->GetIOHook(this->GetPort()))
1512 ServerInstance->Config->GetIOHook(this->GetPort())->OnRawSocketWrite(this->fd, text.data(), text.length());
1514 catch (ModuleException& modexcept)
1516 ServerInstance->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1521 this->AddWriteBuf(text);
1523 ServerInstance->stats->statsSent += text.length();
1524 this->ServerInstance->SE->WantWrite(this);
1529 void userrec::Write(const char *text, ...)
1532 char textbuffer[MAXBUF];
1534 va_start(argsPtr, text);
1535 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1538 this->Write(std::string(textbuffer));
1541 void userrec::WriteServ(const std::string& text)
1543 char textbuffer[MAXBUF];
1545 snprintf(textbuffer,MAXBUF,":%s %s",ServerInstance->Config->ServerName,text.c_str());
1546 this->Write(std::string(textbuffer));
1550 * Same as Write(), except `text' is prefixed with `:server.name '.
1552 void userrec::WriteServ(const char* text, ...)
1555 char textbuffer[MAXBUF];
1557 va_start(argsPtr, text);
1558 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1561 this->WriteServ(std::string(textbuffer));
1565 void userrec::WriteFrom(userrec *user, const std::string &text)
1569 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
1571 this->Write(std::string(tb));
1575 /* write text from an originating user to originating user */
1577 void userrec::WriteFrom(userrec *user, const char* text, ...)
1580 char textbuffer[MAXBUF];
1582 va_start(argsPtr, text);
1583 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1586 this->WriteFrom(user, std::string(textbuffer));
1590 /* write text to an destination user from a source user (e.g. user privmsg) */
1592 void userrec::WriteTo(userrec *dest, const char *data, ...)
1594 char textbuffer[MAXBUF];
1597 va_start(argsPtr, data);
1598 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
1601 this->WriteTo(dest, std::string(textbuffer));
1604 void userrec::WriteTo(userrec *dest, const std::string &data)
1606 dest->WriteFrom(this, data);
1610 void userrec::WriteCommon(const char* text, ...)
1612 char textbuffer[MAXBUF];
1615 if (this->registered != REG_ALL)
1618 va_start(argsPtr, text);
1619 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1622 this->WriteCommon(std::string(textbuffer));
1625 void userrec::WriteCommon(const std::string &text)
1629 bool sent_to_at_least_one = false;
1631 if (this->registered != REG_ALL)
1636 for (std::vector<ucrec*>::const_iterator v = this->chans.begin(); v != this->chans.end(); v++)
1641 CUList *ulist= n->channel->GetUsers();
1643 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1645 if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
1647 already_sent[i->second->fd] = uniq_id;
1648 i->second->WriteFrom(this, std::string(text));
1649 sent_to_at_least_one = true;
1656 * if the user was not in any channels, no users will receive the text. Make sure the user
1657 * receives their OWN message for WriteCommon
1659 if (!sent_to_at_least_one)
1661 this->WriteFrom(this,std::string(text));
1667 ServerInstance->Log(DEBUG,"Exception in userrec::WriteCommon()");
1672 /* write a formatted string to all users who share at least one common
1673 * channel, NOT including the source user e.g. for use in QUIT
1676 void userrec::WriteCommonExcept(const char* text, ...)
1678 char textbuffer[MAXBUF];
1681 va_start(argsPtr, text);
1682 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1685 this->WriteCommonExcept(std::string(textbuffer));
1688 void userrec::WriteCommonExcept(const std::string &text)
1690 bool quit_munge = false;
1691 char oper_quit[MAXBUF];
1692 char textbuffer[MAXBUF];
1694 strlcpy(textbuffer, text.c_str(), MAXBUF);
1696 if (this->registered != REG_ALL)
1701 /* TODO: We need some form of WriteCommonExcept that will send two lines, one line to
1702 * opers and the other line to non-opers, then all this hidebans and hidesplits gunk
1705 if (ServerInstance->Config->HideSplits)
1707 char* check = textbuffer + 6;
1709 if (!strncasecmp(textbuffer, "QUIT :",6))
1711 std::stringstream split(check);
1712 std::string server_one;
1713 std::string server_two;
1715 split >> server_one;
1716 split >> server_two;
1718 if ((ServerInstance->FindServerName(server_one)) && (ServerInstance->FindServerName(server_two)))
1720 strlcpy(oper_quit,textbuffer,MAXQUIT);
1721 strlcpy(check,"*.net *.split",MAXQUIT);
1727 if ((ServerInstance->Config->HideBans) && (!quit_munge))
1729 if ((!strncasecmp(textbuffer, "QUIT :G-Lined:",14)) || (!strncasecmp(textbuffer, "QUIT :K-Lined:",14))
1730 || (!strncasecmp(textbuffer, "QUIT :Q-Lined:",14)) || (!strncasecmp(textbuffer, "QUIT :Z-Lined:",14)))
1732 char* check = textbuffer + 13;
1733 strlcpy(oper_quit,textbuffer,MAXQUIT);
1734 *check = 0; // We don't need to strlcpy, we just chop it from the :
1739 for (std::vector<ucrec*>::const_iterator v = this->chans.begin(); v != this->chans.end(); v++)
1744 CUList *ulist= n->channel->GetUsers();
1746 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1748 if (this != i->second)
1750 if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
1752 already_sent[i->second->fd] = uniq_id;
1754 i->second->WriteFrom(this, *i->second->oper ? std::string(oper_quit) : std::string(textbuffer));
1756 i->second->WriteFrom(this, std::string(textbuffer));
1765 void userrec::WriteWallOps(const std::string &text)
1767 /* Does nothing if theyre not opered */
1768 if ((!*this->oper) && (IS_LOCAL(this)))
1771 std::string wallop = "WALLOPS :";
1775 wallop.append(text);
1779 ServerInstance->Log(DEBUG,"Exception in userrec::Write() std::string::append");
1783 for (std::vector<userrec*>::const_iterator i = ServerInstance->local_users.begin(); i != ServerInstance->local_users.end(); i++)
1786 if ((IS_LOCAL(t)) && (t->modes[UM_WALLOPS]))
1787 this->WriteTo(t,wallop);
1791 void userrec::WriteWallOps(const char* text, ...)
1793 char textbuffer[MAXBUF];
1796 va_start(argsPtr, text);
1797 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1800 this->WriteWallOps(std::string(textbuffer));
1803 /* return 0 or 1 depending if users u and u2 share one or more common channels
1804 * (used by QUIT, NICK etc which arent channel specific notices)
1806 * The old algorithm in 1.0 for this was relatively inefficient, iterating over
1807 * the first users channels then the second users channels within the outer loop,
1808 * therefore it was a maximum of x*y iterations (upon returning 0 and checking
1809 * all possible iterations). However this new function instead checks against the
1810 * channel's userlist in the inner loop which is a std::map<userrec*,userrec*>
1811 * and saves us time as we already know what pointer value we are after.
1812 * Don't quote me on the maths as i am not a mathematician or computer scientist,
1813 * but i believe this algorithm is now x+(log y) maximum iterations instead.
1815 bool userrec::SharesChannelWith(userrec *other)
1817 if ((!other) || (this->registered != REG_ALL) || (other->registered != REG_ALL))
1821 for (std::vector<ucrec*>::const_iterator i = this->chans.begin(); i != this->chans.end(); i++)
1823 /* Fetch the channel from the user */
1824 ucrec* user_channel = *i;
1826 if (user_channel->channel)
1828 /* Eliminate the inner loop (which used to be ~equal in size to the outer loop)
1829 * by replacing it with a map::find which *should* be more efficient
1831 if (user_channel->channel->HasUser(other))
1838 int userrec::CountChannels()
1840 return ChannelCount;
1843 void userrec::ModChannelCount(int n)
1848 bool userrec::ChangeName(const char* gecos)
1850 if (!strcmp(gecos, this->fullname))
1856 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(this,gecos));
1859 FOREACH_MOD(I_OnChangeName,OnChangeName(this,gecos));
1861 strlcpy(this->fullname,gecos,MAXGECOS+1);
1865 bool userrec::ChangeDisplayedHost(const char* host)
1867 if (!strcmp(host, this->dhost))
1873 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(this,host));
1876 FOREACH_MOD(I_OnChangeHost,OnChangeHost(this,host));
1878 if (this->ServerInstance->Config->CycleHosts)
1879 this->WriteCommonExcept("%s","QUIT :Changing hosts");
1881 /* Fix by Om: userrec::dhost is 65 long, this was truncating some long hosts */
1882 strlcpy(this->dhost,host,64);
1884 if (this->ServerInstance->Config->CycleHosts)
1886 for (std::vector<ucrec*>::const_iterator i = this->chans.begin(); i != this->chans.end(); i++)
1890 (*i)->channel->WriteAllExceptSender(this, false, 0, "JOIN %s", (*i)->channel->name);
1891 std::string n = this->ServerInstance->Modes->ModeString(this, (*i)->channel);
1893 (*i)->channel->WriteAllExceptSender(this, true, 0, "MODE %s +%s", (*i)->channel->name, n.c_str());
1899 this->WriteServ("396 %s %s :is now your hidden host",this->nick,this->dhost);
1904 bool userrec::ChangeIdent(const char* newident)
1906 if (!strcmp(newident, this->ident))
1909 if (this->ServerInstance->Config->CycleHosts)
1910 this->WriteCommonExcept("%s","QUIT :Changing ident");
1912 strlcpy(this->ident, newident, IDENTMAX+2);
1914 if (this->ServerInstance->Config->CycleHosts)
1916 for (std::vector<ucrec*>::const_iterator i = this->chans.begin(); i != this->chans.end(); i++)
1920 (*i)->channel->WriteAllExceptSender(this, false, 0, "JOIN %s", (*i)->channel->name);
1921 std::string n = this->ServerInstance->Modes->ModeString(this, (*i)->channel);
1923 (*i)->channel->WriteAllExceptSender(this, true, 0, "MODE %s +%s", (*i)->channel->name, n.c_str());
1931 void userrec::NoticeAll(char* text, ...)
1933 char textbuffer[MAXBUF];
1934 char formatbuffer[MAXBUF];
1937 va_start(argsPtr, text);
1938 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1941 snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
1943 for (std::vector<userrec*>::const_iterator i = ServerInstance->local_users.begin(); i != ServerInstance->local_users.end(); i++)
1946 t->WriteFrom(this, std::string(formatbuffer));
1951 std::string userrec::ChannelList(userrec* source)
1956 for (std::vector<ucrec*>::const_iterator i = this->chans.begin(); i != this->chans.end(); i++)
1960 if(rec->channel && rec->channel->name)
1962 /* If the target is the same as the sender, let them see all their channels.
1963 * If the channel is NOT private/secret OR the user shares a common channel
1964 * If the user is an oper, and the <options:operspywhois> option is set.
1966 if ((source == this) || (*source->oper && ServerInstance->Config->OperSpyWhois) || (((!rec->channel->modes[CM_PRIVATE]) && (!rec->channel->modes[CM_SECRET])) || (rec->channel->HasUser(source))))
1968 list.append(rec->channel->GetPrefixChar(this)).append(rec->channel->name).append(" ");
1976 ServerInstance->Log(DEBUG,"Exception in userrec::ChannelList()");
1981 void userrec::SplitChanList(userrec* dest, const std::string &cl)
1984 std::ostringstream prefix;
1985 std::string::size_type start, pos, length;
1989 prefix << this->nick << " " << dest->nick << " :";
1990 line = prefix.str();
1991 int namelen = strlen(ServerInstance->Config->ServerName) + 6;
1993 for (start = 0; (pos = cl.find(' ', start)) != std::string::npos; start = pos+1)
1995 length = (pos == std::string::npos) ? cl.length() : pos;
1997 if (line.length() + namelen + length - start > 510)
2000 line = prefix.str();
2003 if(pos == std::string::npos)
2005 line.append(cl.substr(start, length - start));
2010 line.append(cl.substr(start, length - start + 1));
2016 ServerInstance->SendWhoisLine(this, dest, 319, "%s", line.c_str());
2022 ServerInstance->Log(DEBUG,"Exception in userrec::SplitChanList()");
2027 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags)
2028 * NOTE: If the <ALLOW> or <DENY> tag specifies an ip, and this user resolves,
2029 * then their ip will be taken as 'priority' anyway, so for example,
2030 * <connect allow="127.0.0.1"> will match joe!bloggs@localhost
2032 ConnectClass& userrec::GetClass()
2034 for (ClassVector::iterator i = ServerInstance->Config->Classes.begin(); i != ServerInstance->Config->Classes.end(); i++)
2036 if ((match(this->GetIPString(),i->host.c_str(),true)) || (match(this->host,i->host.c_str())))
2040 return *(ServerInstance->Config->Classes.begin());
2043 void userrec::PurgeEmptyChannels()
2045 std::vector<chanrec*> to_delete;
2047 // firstly decrement the count on each channel
2048 for (std::vector<ucrec*>::iterator f = this->chans.begin(); f != this->chans.end(); f++)
2053 uc->channel->RemoveAllPrefixes(this);
2054 if (uc->channel->DelUser(this) == 0)
2056 /* No users left in here, mark it for deletion */
2059 to_delete.push_back(uc->channel);
2063 ServerInstance->Log(DEBUG,"Exception in userrec::PurgeEmptyChannels to_delete.push_back()");
2070 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
2072 chanrec* thischan = *n;
2073 chan_hash::iterator i2 = ServerInstance->chanlist.find(thischan->name);
2074 if (i2 != ServerInstance->chanlist.end())
2076 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
2078 ServerInstance->chanlist.erase(i2);
2085 void userrec::ShowMOTD()
2087 if (!ServerInstance->Config->MOTD.size())
2089 this->WriteServ("422 %s :Message of the day file is missing.",this->nick);
2092 this->WriteServ("375 %s :%s message of the day", this->nick, ServerInstance->Config->ServerName);
2094 for (unsigned int i = 0; i < ServerInstance->Config->MOTD.size(); i++)
2095 this->WriteServ("372 %s :- %s",this->nick,ServerInstance->Config->MOTD[i].c_str());
2097 this->WriteServ("376 %s :End of message of the day.", this->nick);
2100 void userrec::ShowRULES()
2102 if (!ServerInstance->Config->RULES.size())
2104 this->WriteServ("NOTICE %s :Rules file is missing.",this->nick);
2107 this->WriteServ("NOTICE %s :%s rules",this->nick,ServerInstance->Config->ServerName);
2109 for (unsigned int i = 0; i < ServerInstance->Config->RULES.size(); i++)
2110 this->WriteServ("NOTICE %s :%s",this->nick,ServerInstance->Config->RULES[i].c_str());
2112 this->WriteServ("NOTICE %s :End of %s rules.",this->nick,ServerInstance->Config->ServerName);
2115 void userrec::HandleEvent(EventType et, int errornum)
2117 /* WARNING: May delete this user! */
2123 ServerInstance->ProcessUser(this);
2126 this->FlushWriteBuf();
2129 this->SetWriteError(errornum ? strerror(errornum) : "EOF from client");
2135 ServerInstance->Log(DEBUG,"Exception in userrec::HandleEvent intercepted");