1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #include "configreader.h"
19 #include "socketengine.h"
22 #include "cull_list.h"
24 static unsigned long already_sent[MAX_DESCRIPTORS] = {0};
26 /* XXX: Used for speeding up WriteCommon operations */
27 unsigned long uniq_id = 0;
29 bool InitTypes(ServerConfig* conf, const char* tag)
31 if (conf->opertypes.size())
33 conf->GetInstance()->Log(DEBUG,"Currently %d items to clear",conf->opertypes.size());
34 for (opertype_t::iterator n = conf->opertypes.begin(); n != conf->opertypes.end(); n++)
36 conf->GetInstance()->Log(DEBUG,"Clear item");
42 conf->opertypes.clear();
46 bool InitClasses(ServerConfig* conf, const char* tag)
48 if (conf->operclass.size())
50 for (operclass_t::iterator n = conf->operclass.begin(); n != conf->operclass.end(); n++)
57 conf->operclass.clear();
61 bool DoType(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
63 const char* TypeName = values[0].GetString();
64 const char* Classes = values[1].GetString();
66 conf->opertypes[TypeName] = strdup(Classes);
67 conf->GetInstance()->Log(DEBUG,"Read oper TYPE '%s' with classes '%s'",TypeName,Classes);
71 bool DoClass(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
73 const char* ClassName = values[0].GetString();
74 const char* CommandList = values[1].GetString();
76 conf->operclass[ClassName] = strdup(CommandList);
77 conf->GetInstance()->Log(DEBUG,"Read oper CLASS '%s' with commands '%s'",ClassName,CommandList);
81 bool DoneClassesAndTypes(ServerConfig* conf, const char* tag)
86 std::string userrec::ProcessNoticeMasks(const char *sm)
88 bool adding = true, oldadding = false;
92 ServerInstance->Log(DEBUG,"Process notice masks");
96 ServerInstance->Log(DEBUG,"Process notice mask %c",*c);
107 for (unsigned char d = 'A'; d <= 'z'; d++)
109 if (ServerInstance->SNO->IsEnabled(d))
111 if ((!IsNoticeMaskSet(d) && adding) || (IsNoticeMaskSet(d) && !adding))
113 if ((oldadding != adding) || (!output.length()))
114 output += (adding ? '+' : '-');
116 this->SetNoticeMask(d, adding);
125 if ((*c >= 'A') && (*c <= 'z') && (ServerInstance->SNO->IsEnabled(*c)))
127 if ((!IsNoticeMaskSet(*c) && adding) || (IsNoticeMaskSet(*c) && !adding))
129 if ((oldadding != adding) || (!output.length()))
130 output += (adding ? '+' : '-');
132 this->SetNoticeMask(*c, adding);
147 void userrec::StartDNSLookup()
149 ServerInstance->Log(DEBUG,"Commencing reverse lookup");
152 ServerInstance->Log(DEBUG,"Passing instance: %08x",this->ServerInstance);
153 res_reverse = new UserResolver(this->ServerInstance, this, this->GetIPString(), DNS_QUERY_REVERSE);
154 this->ServerInstance->AddResolver(res_reverse);
156 catch (CoreException& e)
158 ServerInstance->Log(DEBUG,"Error in resolver: %s",e.GetReason());
162 UserResolver::UserResolver(InspIRCd* Instance, userrec* user, std::string to_resolve, QueryType qt) :
163 Resolver(Instance, to_resolve, qt), bound_user(user)
165 this->fwd = (qt == DNS_QUERY_A || qt == DNS_QUERY_AAAA);
166 this->bound_fd = user->GetFd();
169 void UserResolver::OnLookupComplete(const std::string &result, unsigned int ttl)
171 if ((!this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
173 ServerInstance->Log(DEBUG,"Commencing forward lookup");
174 this->bound_user->stored_host = result;
177 /* Check we didnt time out */
178 if (this->bound_user->registered != REG_ALL)
181 const char *ip = this->bound_user->GetIPString();
182 bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, (strstr(ip,"0::ffff:") == ip ? DNS_QUERY_A : DNS_QUERY_AAAA));
184 bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_A);
186 this->ServerInstance->AddResolver(bound_user->res_forward);
189 catch (CoreException& e)
191 ServerInstance->Log(DEBUG,"Error in resolver: %s",e.GetReason());
194 else if ((this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
196 /* Both lookups completed */
197 std::string result2 = "0::ffff:";
198 result2.append(result);
199 if (this->bound_user->GetIPString() == result || this->bound_user->GetIPString() == result2)
201 std::string hostname = this->bound_user->stored_host;
202 if (hostname.length() < 65)
204 /* Check we didnt time out */
205 if (this->bound_user->registered != REG_ALL)
207 /* Hostnames starting with : are not a good thing (tm) */
208 if (*(hostname.c_str()) == ':')
209 hostname = "0" + hostname;
211 this->bound_user->WriteServ("NOTICE Auth :*** Found your hostname (%s)", hostname.c_str());
212 this->bound_user->dns_done = true;
213 strlcpy(this->bound_user->dhost, hostname.c_str(),64);
214 strlcpy(this->bound_user->host, hostname.c_str(),64);
215 /* Invalidate cache */
216 this->bound_user->InvalidateCache();
221 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());
226 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());
231 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
233 if (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user)
235 /* Error message here */
236 this->bound_user->WriteServ("NOTICE Auth :*** Could not resolve your hostname: %s; using your IP address (%s) instead.", errormessage.c_str(), this->bound_user->GetIPString());
237 this->bound_user->dns_done = true;
242 bool userrec::IsNoticeMaskSet(unsigned char sm)
244 return (snomasks[sm-65]);
247 void userrec::SetNoticeMask(unsigned char sm, bool value)
249 snomasks[sm-65] = value;
252 const char* userrec::FormatNoticeMasks()
254 static char data[MAXBUF];
257 for (int n = 0; n < 64; n++)
260 data[offset++] = n+65;
269 bool userrec::IsModeSet(unsigned char m)
271 return (modes[m-65]);
274 void userrec::SetMode(unsigned char m, bool value)
279 const char* userrec::FormatModes()
281 static char data[MAXBUF];
283 for (int n = 0; n < 64; n++)
286 data[offset++] = n+65;
292 void userrec::DecrementModes()
294 for (int n = 0; n < 64; n++)
298 ModeHandler* mh = ServerInstance->Modes->FindMode(n+65, MODETYPE_USER);
305 userrec::userrec(InspIRCd* Instance) : ServerInstance(Instance)
307 ServerInstance->Log(DEBUG,"userrec::userrec(): Instance: %08x",ServerInstance);
308 // the PROPER way to do it, AVOID bzero at *ALL* costs
309 *password = *nick = *ident = *host = *dhost = *fullname = *awaymsg = *oper = 0;
310 server = (char*)Instance->FindServerNamePtr(Instance->Config->ServerName);
311 reset_due = ServerInstance->Time();
312 age = ServerInstance->Time(true);
313 lines_in = lastping = signon = idle_lastmsg = nping = registered = 0;
314 ChannelCount = timeout = flood = bytes_in = bytes_out = cmds_in = cmds_out = 0;
315 exempt = haspassed = dns_done = false;
320 res_forward = res_reverse = NULL;
324 memset(modes,0,sizeof(modes));
325 memset(snomasks,0,sizeof(snomasks));
326 /* Invalidate cache */
327 cached_fullhost = cached_hostip = cached_makehost = cached_fullrealhost = NULL;
332 this->InvalidateCache();
333 this->DecrementModes();
336 clonemap::iterator x = ServerInstance->local_clones.find(this->GetIPString());
337 if (x != ServerInstance->local_clones.end())
342 ServerInstance->local_clones.erase(x);
346 clonemap::iterator y = ServerInstance->global_clones.find(this->GetIPString());
347 if (y != ServerInstance->global_clones.end())
352 ServerInstance->global_clones.erase(y);
356 if (this->GetProtocolFamily() == AF_INET)
358 delete (sockaddr_in*)ip;
360 #ifdef SUPPORT_IP6LINKS
363 delete (sockaddr_in6*)ip;
369 char* userrec::MakeHost()
371 if (this->cached_makehost)
372 return this->cached_makehost;
375 /* This is much faster than snprintf */
377 for(char* n = ident; *n; n++)
380 for(char* n = host; *n; n++)
384 this->cached_makehost = strdup(nhost);
386 return this->cached_makehost;
389 char* userrec::MakeHostIP()
391 if (this->cached_hostip)
392 return this->cached_hostip;
395 /* This is much faster than snprintf */
397 for(char* n = ident; *n; n++)
400 for(const char* n = this->GetIPString(); *n; n++)
404 this->cached_hostip = strdup(ihost);
406 return this->cached_hostip;
409 void userrec::CloseSocket()
411 shutdown(this->fd,2);
415 char* userrec::GetFullHost()
417 if (this->cached_fullhost)
418 return this->cached_fullhost;
422 for(char* n = nick; *n; n++)
425 for(char* n = ident; *n; n++)
428 for(char* n = dhost; *n; n++)
432 this->cached_fullhost = strdup(result);
434 return this->cached_fullhost;
437 char* userrec::MakeWildHost()
439 static char nresult[MAXBUF];
441 *t++ = '*'; *t++ = '!';
442 *t++ = '*'; *t++ = '@';
443 for(char* n = dhost; *n; n++)
449 int userrec::ReadData(void* buffer, size_t size)
453 return read(this->fd, buffer, size);
460 char* userrec::GetFullRealHost()
462 if (this->cached_fullrealhost)
463 return this->cached_fullrealhost;
465 char fresult[MAXBUF];
467 for(char* n = nick; *n; n++)
470 for(char* n = ident; *n; n++)
473 for(char* n = host; *n; n++)
477 this->cached_fullrealhost = strdup(fresult);
479 return this->cached_fullrealhost;
482 bool userrec::IsInvited(const irc::string &channel)
484 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
494 InvitedList* userrec::GetInviteList()
499 void userrec::InviteTo(const irc::string &channel)
501 invites.push_back(channel);
504 void userrec::RemoveInvite(const irc::string &channel)
506 ServerInstance->Log(DEBUG,"Removing invites");
507 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
517 bool userrec::HasPermission(const std::string &command)
524 * users on remote servers can completely bypass all permissions based checks.
525 * This prevents desyncs when one server has different type/class tags to another.
526 * That having been said, this does open things up to the possibility of source changes
527 * allowing remote kills, etc - but if they have access to the src, they most likely have
528 * access to the conf - so it's an end to a means either way.
533 // are they even an oper at all?
536 opertype_t::iterator iter_opertype = ServerInstance->Config->opertypes.find(this->oper);
537 if (iter_opertype != ServerInstance->Config->opertypes.end())
539 char* Classes = strdup(iter_opertype->second);
540 char* myclass = strtok_r(Classes," ",&savept);
543 operclass_t::iterator iter_operclass = ServerInstance->Config->operclass.find(myclass);
544 if (iter_operclass != ServerInstance->Config->operclass.end())
546 char* CommandList = strdup(iter_operclass->second);
547 mycmd = strtok_r(CommandList," ",&savept2);
550 if ((!strcasecmp(mycmd,command.c_str())) || (*mycmd == '*'))
556 mycmd = strtok_r(NULL," ",&savept2);
560 myclass = strtok_r(NULL," ",&savept);
568 /** NOTE: We cannot pass a const reference to this method.
569 * The string is changed by the workings of the method,
570 * so that if we pass const ref, we end up copying it to
571 * something we can change anyway. Makes sense to just let
572 * the compiler do that copy for us.
574 bool userrec::AddBuffer(std::string a)
578 std::string::size_type i = a.rfind('\r');
580 while (i != std::string::npos)
589 if (recvq.length() > (unsigned)this->recvqmax)
591 this->SetWriteError("RecvQ exceeded");
592 ServerInstance->WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
601 ServerInstance->Log(DEBUG,"Exception in userrec::AddBuffer()");
606 bool userrec::BufferIsReady()
608 return (recvq.find('\n') != std::string::npos);
611 void userrec::ClearBuffer()
616 std::string userrec::GetBuffer()
623 /* Strip any leading \r or \n off the string.
624 * Usually there are only one or two of these,
625 * so its is computationally cheap to do.
627 while ((*recvq.begin() == '\r') || (*recvq.begin() == '\n'))
628 recvq.erase(recvq.begin());
630 for (std::string::iterator x = recvq.begin(); x != recvq.end(); x++)
632 /* Find the first complete line, return it as the
633 * result, and leave the recvq as whats left
637 std::string ret = std::string(recvq.begin(), x);
638 recvq.erase(recvq.begin(), x + 1);
647 ServerInstance->Log(DEBUG,"Exception in userrec::GetBuffer()");
652 void userrec::AddWriteBuf(const std::string &data)
654 if (*this->GetWriteError())
657 if (sendq.length() + data.length() > (unsigned)this->sendqmax)
660 * Fix by brain - Set the error text BEFORE calling writeopers, because
661 * if we dont it'll recursively call here over and over again trying
662 * to repeatedly add the text to the sendq!
664 this->SetWriteError("SendQ exceeded");
665 ServerInstance->WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
671 if (data.length() > 512)
672 sendq.append(data.substr(0,510)).append("\r\n");
678 this->SetWriteError("SendQ exceeded");
679 ServerInstance->WriteOpers("*** User %s SendQ got an exception",this->nick);
683 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
684 void userrec::FlushWriteBuf()
688 if ((this->fd == FD_MAGIC_NUMBER) || (*this->GetWriteError()))
692 if ((sendq.length()) && (this->fd != FD_MAGIC_NUMBER))
694 int old_sendq_length = sendq.length();
695 int n_sent = write(this->fd, this->sendq.data(), this->sendq.length());
700 /* The socket buffer is full. This isnt fatal,
703 ServerInstance->Log(DEBUG,"EAGAIN, want write");
704 this->ServerInstance->SE->WantWrite(this);
708 /* Fatal error, set write error and bail
710 this->SetWriteError(strerror(errno));
716 /* advance the queue */
718 this->sendq = this->sendq.substr(n_sent);
719 /* update the user's stats counters */
720 this->bytes_out += n_sent;
722 if (n_sent != old_sendq_length)
724 ServerInstance->Log(DEBUG,"Not all written, want write");
725 this->ServerInstance->SE->WantWrite(this);
733 ServerInstance->Log(DEBUG,"Exception in userrec::FlushWriteBuf()");
737 void userrec::SetWriteError(const std::string &error)
741 // don't try to set the error twice, its already set take the first string.
742 if (this->WriteError.empty())
744 ServerInstance->Log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
745 this->WriteError = error;
751 ServerInstance->Log(DEBUG,"Exception in userrec::SetWriteError()");
755 const char* userrec::GetWriteError()
757 return this->WriteError.c_str();
760 void userrec::Oper(const std::string &opertype)
764 this->modes[UM_OPERATOR] = 1;
765 this->WriteServ("MODE %s :+o", this->nick);
766 FOREACH_MOD(I_OnOper, OnOper(this, opertype));
767 ServerInstance->Log(DEFAULT,"OPER: %s!%s@%s opered as type: %s", this->nick, this->ident, this->host, opertype.c_str());
768 strlcpy(this->oper, opertype.c_str(), NICKMAX - 1);
769 ServerInstance->all_opers.push_back(this);
770 FOREACH_MOD(I_OnPostOper,OnPostOper(this, opertype));
775 ServerInstance->Log(DEBUG,"Exception in userrec::Oper()");
779 void userrec::UnOper()
786 this->modes[UM_OPERATOR] = 0;
787 for (std::vector<userrec*>::iterator a = ServerInstance->all_opers.begin(); a < ServerInstance->all_opers.end(); a++)
791 ServerInstance->Log(DEBUG,"Oper removed from optimization list");
792 ServerInstance->all_opers.erase(a);
801 ServerInstance->Log(DEBUG,"Exception in userrec::UnOper()");
805 void userrec::QuitUser(InspIRCd* Instance, userrec *user, const std::string &quitreason)
807 user_hash::iterator iter = Instance->clientlist->find(user->nick);
808 std::string reason = quitreason;
810 if (reason.length() > MAXQUIT - 1)
811 reason.resize(MAXQUIT - 1);
813 if (user->registered != REG_ALL)
814 if (Instance->unregistered_count)
815 Instance->unregistered_count--;
819 user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason.c_str());
820 if ((!user->sendq.empty()) && (!(*user->GetWriteError())))
821 user->FlushWriteBuf();
824 if (user->registered == REG_ALL)
826 user->PurgeEmptyChannels();
827 user->WriteCommonExcept("QUIT :%s",reason.c_str());
828 FOREACH_MOD_I(Instance,I_OnUserQuit,OnUserQuit(user,reason));
831 FOREACH_MOD_I(Instance,I_OnUserDisconnect,OnUserDisconnect(user));
835 if (Instance->Config->GetIOHook(user->GetPort()))
839 Instance->Config->GetIOHook(user->GetPort())->OnRawSocketClose(user->fd);
841 catch (CoreException& modexcept)
843 Instance->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
847 Instance->SE->DelFd(user);
852 * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
853 * if they were an oper with +sn +qQ.
855 if (user->registered == REG_ALL)
858 Instance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason.c_str());
860 Instance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",user->server,user->nick,user->ident,user->host,reason.c_str());
864 if (iter != Instance->clientlist->end())
866 Instance->Log(DEBUG,"deleting user hash value %lx",(unsigned long)user);
869 std::vector<userrec*>::iterator x = find(Instance->local_users.begin(),Instance->local_users.end(),user);
870 if (x != Instance->local_users.end())
871 Instance->local_users.erase(x);
873 Instance->clientlist->erase(iter);
883 WhoWasGroup::WhoWasGroup(userrec* user) : host(NULL), dhost(NULL), ident(NULL), server(NULL), gecos(NULL), signon(user->signon)
885 this->host = strdup(user->host);
886 this->dhost = strdup(user->dhost);
887 this->ident = strdup(user->ident);
888 this->server = user->server;
889 this->gecos = strdup(user->fullname);
892 WhoWasGroup::~WhoWasGroup()
904 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
905 void MaintainWhoWas(InspIRCd* ServerInstance, time_t t)
907 for (whowas_users::iterator iter = ServerInstance->whowas.begin(); iter != ServerInstance->whowas.end(); iter++)
909 whowas_set* n = (whowas_set*)iter->second;
912 while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
914 WhoWasGroup *a = *(n->begin());
916 n->erase(n->begin());
921 /* on rehash, refactor maps according to new conf values */
922 void PruneWhoWas(InspIRCd* ServerInstance, time_t t)
925 int groupsize = ServerInstance->Config->WhoWasGroupSize;
926 int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
927 int maxkeep = ServerInstance->Config->WhoWasMaxKeep;
929 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
930 whowas_users::iterator iter;
932 while ((fifosize = (int)ServerInstance->whowas_fifo.size()) > 0)
934 if (fifosize > maxgroups || ServerInstance->whowas_fifo[0].first < t - maxkeep)
936 iter = ServerInstance->whowas.find(ServerInstance->whowas_fifo[0].second);
937 /* hopefully redundant integrity check, but added while debugging r6216 */
938 if (iter == ServerInstance->whowas.end())
940 /* this should never happen, if it does maps are corrupt */
941 ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (1)");
944 whowas_set* n = (whowas_set*)iter->second;
947 while (n->begin() != n->end())
949 WhoWasGroup *a = *(n->begin());
954 ServerInstance->whowas.erase(iter);
955 ServerInstance->whowas_fifo.pop_front();
961 /* Then cut the whowas sets to new size (groupsize) */
962 for (int i = 0; i < fifosize; i++)
964 iter = ServerInstance->whowas.find(ServerInstance->whowas_fifo[0].second);
965 /* hopefully redundant integrity check, but added while debugging r6216 */
966 if (iter == ServerInstance->whowas.end())
968 /* this should never happen, if it does maps are corrupt */
969 ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (2)");
972 whowas_set* n = (whowas_set*)iter->second;
975 int nickcount = n->size();
976 while (n->begin() != n->end() && nickcount > groupsize)
978 WhoWasGroup *a = *(n->begin());
989 /* adds or updates an entry in the whowas list */
990 void userrec::AddToWhoWas()
992 /* if whowas disabled */
993 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
998 irc::whowas::whowas_users::iterator iter = ServerInstance->whowas.find(this->nick);
1000 ServerInstance->Log(DEBUG,"Add to whowas lists");
1002 if (iter == ServerInstance->whowas.end())
1004 ServerInstance->Log(DEBUG,"Adding new whowas set for %s",this->nick);
1006 irc::whowas::whowas_set* n = new irc::whowas::whowas_set;
1007 irc::whowas::WhoWasGroup *a = new irc::whowas::WhoWasGroup(this);
1009 ServerInstance->whowas[this->nick] = n;
1010 ServerInstance->whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),this->nick));
1012 if ((int)(ServerInstance->whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
1014 ServerInstance->Log(DEBUG,"Maxgroups of %d reached deleting oldest group '%s'",ServerInstance->Config->WhoWasMaxGroups, ServerInstance->whowas_fifo[0].second.c_str());
1016 irc::whowas::whowas_users::iterator iter = ServerInstance->whowas.find(ServerInstance->whowas_fifo[0].second);
1017 if (iter != ServerInstance->whowas.end())
1019 irc::whowas::whowas_set* n = (irc::whowas::whowas_set*)iter->second;
1022 while (n->begin() != n->end())
1024 irc::whowas::WhoWasGroup *a = *(n->begin());
1029 ServerInstance->whowas.erase(iter);
1031 ServerInstance->whowas_fifo.pop_front();
1036 irc::whowas::whowas_set* group = (irc::whowas::whowas_set*)iter->second;
1038 ServerInstance->Log(DEBUG,"Using existing whowas group for %s",this->nick);
1040 irc::whowas::WhoWasGroup *a = new irc::whowas::WhoWasGroup(this);
1041 group->push_back(a);
1043 if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
1045 ServerInstance->Log(DEBUG,"Trimming existing group '%s' to %d entries",this->nick, ServerInstance->Config->WhoWasGroupSize);
1046 irc::whowas::WhoWasGroup *a = (irc::whowas::WhoWasGroup*)*(group->begin());
1053 /* add a client connection to the sockets list */
1054 void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached, insp_inaddr ip)
1056 std::string tempnick = ConvToStr(socket) + "-unknown";
1057 user_hash::iterator iter = Instance->clientlist->find(tempnick);
1058 const char *ipaddr = insp_ntoa(ip);
1062 Instance->unregistered_count++;
1066 * as these nicknames are 'RFC impossible', we can be sure nobody is going to be
1067 * using one as a registered connection. As they are per fd, we can also safely assume
1068 * that we wont have collisions. Therefore, if the nick exists in the list, its only
1069 * used by a dead socket, erase the iterator so that the new client may reclaim it.
1070 * this was probably the cause of 'server ignores me when i hammer it with reconnects'
1071 * issue in earlier alphas/betas
1073 if (iter != Instance->clientlist->end())
1075 userrec* goner = iter->second;
1077 Instance->clientlist->erase(iter);
1080 Instance->Log(DEBUG,"AddClient: %d %d %s",socket,port,ipaddr);
1082 New = new userrec(Instance);
1083 (*(Instance->clientlist))[tempnick] = New;
1085 strlcpy(New->nick,tempnick.c_str(),NICKMAX-1);
1087 New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
1088 /* We don't need range checking here, we KNOW 'unknown\0' will fit into the ident field. */
1089 strcpy(New->ident, "unknown");
1091 New->registered = REG_NONE;
1092 New->signon = Instance->Time() + Instance->Config->dns_timeout;
1095 New->SetSockAddr(AF_FAMILY, ipaddr, port);
1097 /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
1098 for (const char* temp = New->GetIPString(); *temp && j < 64; temp++, j++)
1099 New->dhost[j] = New->host[j] = *temp;
1100 New->dhost[j] = New->host[j] = 0;
1102 Instance->AddLocalClone(New);
1103 Instance->AddGlobalClone(New);
1105 ConnectClass* i = New->GetClass();
1107 if ((!i) || (i->GetType() == CC_DENY))
1109 userrec::QuitUser(Instance, New,"Unauthorised connection");
1113 New->pingmax = i->GetPingTime();
1114 New->nping = Instance->Time() + i->GetPingTime() + Instance->Config->dns_timeout;
1115 New->timeout = Instance->Time() + i->GetRegTimeout();
1116 New->flood = i->GetFlood();
1117 New->threshold = i->GetThreshold();
1118 New->sendqmax = i->GetSendqMax();
1119 New->recvqmax = i->GetRecvqMax();
1121 Instance->local_users.push_back(New);
1123 if ((Instance->local_users.size() > Instance->Config->SoftLimit) || (Instance->local_users.size() >= MAXCLIENTS))
1125 Instance->WriteOpers("*** Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
1126 userrec::QuitUser(Instance, New,"No more connections allowed");
1132 * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
1133 * its a pretty big but for the moment valid assumption:
1134 * file descriptors are handed out starting at 0, and are recycled as theyre freed.
1135 * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
1136 * irc server at once (or the irc server otherwise initiating this many connections, files etc)
1137 * which for the time being is a physical impossibility (even the largest networks dont have more
1138 * than about 10,000 users on ONE server!)
1140 if ((unsigned int)socket >= MAX_DESCRIPTORS)
1142 userrec::QuitUser(Instance, New, "Server is full");
1146 New->exempt = (Instance->XLines->matches_exception(New) != NULL);
1149 ZLine* r = Instance->XLines->matches_zline(ipaddr);
1152 char reason[MAXBUF];
1153 snprintf(reason,MAXBUF,"Z-Lined: %s",r->reason);
1154 userrec::QuitUser(Instance, New, reason);
1161 if (!Instance->SE->AddFd(New))
1163 userrec::QuitUser(Instance, New, "Internal error handling connection");
1168 /* NOTE: even if dns lookups are *off*, we still need to display this.
1169 * BOPM and other stuff requires it.
1171 New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
1174 unsigned long userrec::GlobalCloneCount()
1176 clonemap::iterator x = ServerInstance->global_clones.find(this->GetIPString());
1177 if (x != ServerInstance->global_clones.end())
1183 unsigned long userrec::LocalCloneCount()
1185 clonemap::iterator x = ServerInstance->local_clones.find(this->GetIPString());
1186 if (x != ServerInstance->local_clones.end())
1192 void userrec::FullConnect(CullList* Goners)
1194 ServerInstance->stats->statsConnects++;
1195 this->idle_lastmsg = ServerInstance->Time();
1197 ConnectClass* a = this->GetClass();
1199 if ((!a) || (a->GetType() == CC_DENY))
1201 Goners->AddItem(this,"Unauthorised connection");
1205 if ((!a->GetPass().empty()) && (!this->haspassed))
1207 Goners->AddItem(this,"Invalid password");
1211 if (this->LocalCloneCount() > a->GetMaxLocal())
1213 Goners->AddItem(this, "No more connections allowed from your host via this connect class (local)");
1214 ServerInstance->WriteOpers("*** WARNING: maximum LOCAL connections (%ld) exceeded for IP %s", a->GetMaxLocal(), this->GetIPString());
1217 else if (this->GlobalCloneCount() > a->GetMaxGlobal())
1219 Goners->AddItem(this, "No more connections allowed from your host via this connect class (global)");
1220 ServerInstance->WriteOpers("*** WARNING: maximum GLOBAL connections (%ld) exceeded for IP %s",a->GetMaxGlobal(), this->GetIPString());
1226 GLine* r = ServerInstance->XLines->matches_gline(this);
1230 char reason[MAXBUF];
1231 snprintf(reason,MAXBUF,"G-Lined: %s",r->reason);
1232 Goners->AddItem(this, reason);
1236 KLine* n = ServerInstance->XLines->matches_kline(this);
1240 char reason[MAXBUF];
1241 snprintf(reason,MAXBUF,"K-Lined: %s",n->reason);
1242 Goners->AddItem(this, reason);
1248 this->WriteServ("NOTICE Auth :Welcome to \002%s\002!",ServerInstance->Config->Network);
1249 this->WriteServ("001 %s :Welcome to the %s IRC Network %s!%s@%s",this->nick, ServerInstance->Config->Network, this->nick, this->ident, this->host);
1250 this->WriteServ("002 %s :Your host is %s, running version %s",this->nick,ServerInstance->Config->ServerName,VERSION);
1251 this->WriteServ("003 %s :This server was created %s %s", this->nick, __TIME__, __DATE__);
1252 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());
1254 ServerInstance->Config->Send005(this);
1258 /* Now registered */
1259 if (ServerInstance->unregistered_count)
1260 ServerInstance->unregistered_count--;
1263 * fix 3 by brain, move registered = 7 below these so that spurious modes and host
1264 * changes dont go out onto the network and produce 'fake direction'.
1266 FOREACH_MOD(I_OnUserConnect,OnUserConnect(this));
1268 this->registered = REG_ALL;
1270 FOREACH_MOD(I_OnPostConnect,OnPostConnect(this));
1272 ServerInstance->SNO->WriteToSnoMask('c',"Client connecting on port %d: %s!%s@%s [%s]", this->GetPort(), this->nick, this->ident, this->host, this->GetIPString());
1275 /** userrec::UpdateNick()
1276 * re-allocates a nick in the user_hash after they change nicknames,
1277 * returns a pointer to the new user as it may have moved
1279 userrec* userrec::UpdateNickHash(const char* New)
1283 //user_hash::iterator newnick;
1284 user_hash::iterator oldnick = ServerInstance->clientlist->find(this->nick);
1286 if (!strcasecmp(this->nick,New))
1287 return oldnick->second;
1289 if (oldnick == ServerInstance->clientlist->end())
1290 return NULL; /* doesnt exist */
1292 userrec* olduser = oldnick->second;
1293 (*(ServerInstance->clientlist))[New] = olduser;
1294 ServerInstance->clientlist->erase(oldnick);
1300 ServerInstance->Log(DEBUG,"Exception in userrec::UpdateNickHash()");
1305 void userrec::InvalidateCache()
1307 /* Invalidate cache */
1308 if (cached_fullhost)
1309 free(cached_fullhost);
1311 free(cached_hostip);
1312 if (cached_makehost)
1313 free(cached_makehost);
1314 if (cached_fullrealhost)
1315 free(cached_fullrealhost);
1316 cached_fullhost = cached_hostip = cached_makehost = cached_fullrealhost = NULL;
1319 bool userrec::ForceNickChange(const char* newnick)
1325 this->InvalidateCache();
1327 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(this, newnick));
1331 ServerInstance->stats->statsCollisions++;
1335 if (ServerInstance->XLines->matches_qline(newnick))
1337 ServerInstance->stats->statsCollisions++;
1341 if (this->registered == REG_ALL)
1343 const char* pars[1];
1345 std::string cmd = "NICK";
1346 return (ServerInstance->Parser->CallHandler(cmd, pars, 1, this) == CMD_SUCCESS);
1353 ServerInstance->Log(DEBUG,"Exception in userrec::ForceNickChange()");
1358 void userrec::SetSockAddr(int protocol_family, const char* ip, int port)
1360 switch (protocol_family)
1362 #ifdef SUPPORT_IP6LINKS
1365 ServerInstance->Log(DEBUG,"Set inet6 protocol address");
1366 sockaddr_in6* sin = new sockaddr_in6;
1367 sin->sin6_family = AF_INET6;
1368 sin->sin6_port = port;
1369 inet_pton(AF_INET6, ip, &sin->sin6_addr);
1370 this->ip = (sockaddr*)sin;
1376 ServerInstance->Log(DEBUG,"Set inet4 protocol address");
1377 sockaddr_in* sin = new sockaddr_in;
1378 sin->sin_family = AF_INET;
1379 sin->sin_port = port;
1380 inet_pton(AF_INET, ip, &sin->sin_addr);
1381 this->ip = (sockaddr*)sin;
1385 ServerInstance->Log(DEBUG,"Ut oh, I dont know protocol %d to be set on '%s'!", protocol_family, this->nick);
1390 int userrec::GetPort()
1392 if (this->ip == NULL)
1395 switch (this->GetProtocolFamily())
1397 #ifdef SUPPORT_IP6LINKS
1400 sockaddr_in6* sin = (sockaddr_in6*)this->ip;
1401 return sin->sin6_port;
1407 sockaddr_in* sin = (sockaddr_in*)this->ip;
1408 return sin->sin_port;
1417 int userrec::GetProtocolFamily()
1419 if (this->ip == NULL)
1422 sockaddr_in* sin = (sockaddr_in*)this->ip;
1423 return sin->sin_family;
1426 const char* userrec::GetIPString()
1428 static char buf[1024];
1430 if (this->ip == NULL)
1433 switch (this->GetProtocolFamily())
1435 #ifdef SUPPORT_IP6LINKS
1438 static char temp[1024];
1440 sockaddr_in6* sin = (sockaddr_in6*)this->ip;
1441 inet_ntop(sin->sin6_family, &sin->sin6_addr, buf, sizeof(buf));
1442 /* IP addresses starting with a : on irc are a Bad Thing (tm) */
1445 strlcpy(&temp[1], buf, sizeof(temp) - 1);
1455 sockaddr_in* sin = (sockaddr_in*)this->ip;
1456 inet_ntop(sin->sin_family, &sin->sin_addr, buf, sizeof(buf));
1466 const char* userrec::GetIPString(char* buf)
1468 if (this->ip == NULL)
1474 switch (this->GetProtocolFamily())
1476 #ifdef SUPPORT_IP6LINKS
1479 static char temp[1024];
1481 sockaddr_in6* sin = (sockaddr_in6*)this->ip;
1482 inet_ntop(sin->sin6_family, &sin->sin6_addr, buf, sizeof(buf));
1483 /* IP addresses starting with a : on irc are a Bad Thing (tm) */
1486 strlcpy(&temp[1], buf, sizeof(temp) - 1);
1488 strlcpy(buf, temp, sizeof(temp));
1496 sockaddr_in* sin = (sockaddr_in*)this->ip;
1497 inet_ntop(sin->sin_family, &sin->sin_addr, buf, sizeof(buf));
1508 /** NOTE: We cannot pass a const reference to this method.
1509 * The string is changed by the workings of the method,
1510 * so that if we pass const ref, we end up copying it to
1511 * something we can change anyway. Makes sense to just let
1512 * the compiler do that copy for us.
1514 void userrec::Write(std::string text)
1516 if ((this->fd < 0) || (this->fd > MAX_DESCRIPTORS))
1521 text.append("\r\n");
1525 ServerInstance->Log(DEBUG,"Exception in userrec::Write() std::string::append");
1529 if (ServerInstance->Config->GetIOHook(this->GetPort()))
1533 ServerInstance->Config->GetIOHook(this->GetPort())->OnRawSocketWrite(this->fd, text.data(), text.length());
1535 catch (CoreException& modexcept)
1537 ServerInstance->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
1542 this->AddWriteBuf(text);
1544 ServerInstance->stats->statsSent += text.length();
1545 this->ServerInstance->SE->WantWrite(this);
1550 void userrec::Write(const char *text, ...)
1553 char textbuffer[MAXBUF];
1555 va_start(argsPtr, text);
1556 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1559 this->Write(std::string(textbuffer));
1562 void userrec::WriteServ(const std::string& text)
1564 char textbuffer[MAXBUF];
1566 snprintf(textbuffer,MAXBUF,":%s %s",ServerInstance->Config->ServerName,text.c_str());
1567 this->Write(std::string(textbuffer));
1571 * Same as Write(), except `text' is prefixed with `:server.name '.
1573 void userrec::WriteServ(const char* text, ...)
1576 char textbuffer[MAXBUF];
1578 va_start(argsPtr, text);
1579 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1582 this->WriteServ(std::string(textbuffer));
1586 void userrec::WriteFrom(userrec *user, const std::string &text)
1590 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
1592 this->Write(std::string(tb));
1596 /* write text from an originating user to originating user */
1598 void userrec::WriteFrom(userrec *user, const char* text, ...)
1601 char textbuffer[MAXBUF];
1603 va_start(argsPtr, text);
1604 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1607 this->WriteFrom(user, std::string(textbuffer));
1611 /* write text to an destination user from a source user (e.g. user privmsg) */
1613 void userrec::WriteTo(userrec *dest, const char *data, ...)
1615 char textbuffer[MAXBUF];
1618 va_start(argsPtr, data);
1619 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
1622 this->WriteTo(dest, std::string(textbuffer));
1625 void userrec::WriteTo(userrec *dest, const std::string &data)
1627 dest->WriteFrom(this, data);
1631 void userrec::WriteCommon(const char* text, ...)
1633 char textbuffer[MAXBUF];
1636 if (this->registered != REG_ALL)
1639 va_start(argsPtr, text);
1640 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1643 this->WriteCommon(std::string(textbuffer));
1646 void userrec::WriteCommon(const std::string &text)
1650 bool sent_to_at_least_one = false;
1653 if (this->registered != REG_ALL)
1658 /* We dont want to be doing this n times, just once */
1659 snprintf(tb,MAXBUF,":%s %s",this->GetFullHost(),text.c_str());
1660 std::string out = tb;
1662 for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
1664 CUList* ulist = v->first->GetUsers();
1665 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1667 if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
1669 already_sent[i->second->fd] = uniq_id;
1670 i->second->Write(out);
1671 sent_to_at_least_one = true;
1677 * if the user was not in any channels, no users will receive the text. Make sure the user
1678 * receives their OWN message for WriteCommon
1680 if (!sent_to_at_least_one)
1682 this->Write(std::string(tb));
1688 ServerInstance->Log(DEBUG,"Exception in userrec::WriteCommon()");
1693 /* write a formatted string to all users who share at least one common
1694 * channel, NOT including the source user e.g. for use in QUIT
1697 void userrec::WriteCommonExcept(const char* text, ...)
1699 char textbuffer[MAXBUF];
1702 va_start(argsPtr, text);
1703 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1706 this->WriteCommonExcept(std::string(textbuffer));
1709 void userrec::WriteCommonExcept(const std::string &text)
1711 bool quit_munge = false;
1712 char oper_quit[MAXBUF];
1713 char textbuffer[MAXBUF];
1719 strlcpy(textbuffer, text.c_str(), MAXBUF);
1721 if (this->registered != REG_ALL)
1726 snprintf(tb1,MAXBUF,":%s %s",this->GetFullHost(),textbuffer);
1728 /* TODO: We need some form of WriteCommonExcept that will send two lines, one line to
1729 * opers and the other line to non-opers, then all this hidebans and hidesplits gunk
1732 if (ServerInstance->Config->HideSplits)
1734 char* check = textbuffer + 6;
1736 if (!strncasecmp(textbuffer, "QUIT :",6))
1738 std::stringstream split(check);
1739 std::string server_one;
1740 std::string server_two;
1742 split >> server_one;
1743 split >> server_two;
1745 if ((ServerInstance->FindServerName(server_one)) && (ServerInstance->FindServerName(server_two)))
1747 strlcpy(oper_quit,textbuffer,MAXQUIT);
1748 strlcpy(check,"*.net *.split",MAXQUIT);
1750 snprintf(tb2,MAXBUF,":%s %s",this->GetFullHost(),oper_quit);
1756 if ((ServerInstance->Config->HideBans) && (!quit_munge))
1758 if ((!strncasecmp(textbuffer, "QUIT :G-Lined:",14)) || (!strncasecmp(textbuffer, "QUIT :K-Lined:",14))
1759 || (!strncasecmp(textbuffer, "QUIT :Q-Lined:",14)) || (!strncasecmp(textbuffer, "QUIT :Z-Lined:",14)))
1761 char* check = textbuffer + 13;
1762 strlcpy(oper_quit,textbuffer,MAXQUIT);
1763 *check = 0; // We don't need to strlcpy, we just chop it from the :
1765 snprintf(tb2,MAXBUF,":%s %s",this->GetFullHost(),oper_quit);
1772 for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
1774 CUList *ulist = v->first->GetUsers();
1775 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1777 if (this != i->second)
1779 if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
1781 already_sent[i->second->fd] = uniq_id;
1783 i->second->Write(*i->second->oper ? out2 : out1);
1785 i->second->Write(out1);
1793 void userrec::WriteWallOps(const std::string &text)
1795 /* Does nothing if theyre not opered */
1796 if ((!*this->oper) && (IS_LOCAL(this)))
1799 std::string wallop = "WALLOPS :";
1803 wallop.append(text);
1807 ServerInstance->Log(DEBUG,"Exception in userrec::Write() std::string::append");
1811 for (std::vector<userrec*>::const_iterator i = ServerInstance->local_users.begin(); i != ServerInstance->local_users.end(); i++)
1814 if ((IS_LOCAL(t)) && (t->modes[UM_WALLOPS]))
1815 this->WriteTo(t,wallop);
1819 void userrec::WriteWallOps(const char* text, ...)
1821 char textbuffer[MAXBUF];
1824 va_start(argsPtr, text);
1825 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1828 this->WriteWallOps(std::string(textbuffer));
1831 /* return 0 or 1 depending if users u and u2 share one or more common channels
1832 * (used by QUIT, NICK etc which arent channel specific notices)
1834 * The old algorithm in 1.0 for this was relatively inefficient, iterating over
1835 * the first users channels then the second users channels within the outer loop,
1836 * therefore it was a maximum of x*y iterations (upon returning 0 and checking
1837 * all possible iterations). However this new function instead checks against the
1838 * channel's userlist in the inner loop which is a std::map<userrec*,userrec*>
1839 * and saves us time as we already know what pointer value we are after.
1840 * Don't quote me on the maths as i am not a mathematician or computer scientist,
1841 * but i believe this algorithm is now x+(log y) maximum iterations instead.
1843 bool userrec::SharesChannelWith(userrec *other)
1845 if ((!other) || (this->registered != REG_ALL) || (other->registered != REG_ALL))
1849 for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
1851 /* Eliminate the inner loop (which used to be ~equal in size to the outer loop)
1852 * by replacing it with a map::find which *should* be more efficient
1854 if (i->first->HasUser(other))
1860 bool userrec::ChangeName(const char* gecos)
1862 if (!strcmp(gecos, this->fullname))
1868 FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(this,gecos));
1871 FOREACH_MOD(I_OnChangeName,OnChangeName(this,gecos));
1873 strlcpy(this->fullname,gecos,MAXGECOS+1);
1878 bool userrec::ChangeDisplayedHost(const char* host)
1880 if (!strcmp(host, this->dhost))
1886 FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(this,host));
1889 FOREACH_MOD(I_OnChangeHost,OnChangeHost(this,host));
1891 if (this->ServerInstance->Config->CycleHosts)
1892 this->WriteCommonExcept("QUIT :Changing hosts");
1894 /* Fix by Om: userrec::dhost is 65 long, this was truncating some long hosts */
1895 strlcpy(this->dhost,host,64);
1897 this->InvalidateCache();
1899 if (this->ServerInstance->Config->CycleHosts)
1901 for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
1903 i->first->WriteAllExceptSender(this, false, 0, "JOIN %s", i->first->name);
1904 std::string n = this->ServerInstance->Modes->ModeString(this, i->first);
1906 i->first->WriteAllExceptSender(this, true, 0, "MODE %s +%s", i->first->name, n.c_str());
1911 this->WriteServ("396 %s %s :is now your hidden host",this->nick,this->dhost);
1916 bool userrec::ChangeIdent(const char* newident)
1918 if (!strcmp(newident, this->ident))
1921 if (this->ServerInstance->Config->CycleHosts)
1922 this->WriteCommonExcept("%s","QUIT :Changing ident");
1924 strlcpy(this->ident, newident, IDENTMAX+2);
1926 this->InvalidateCache();
1928 if (this->ServerInstance->Config->CycleHosts)
1930 for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
1932 i->first->WriteAllExceptSender(this, false, 0, "JOIN %s", i->first->name);
1933 std::string n = this->ServerInstance->Modes->ModeString(this, i->first);
1935 i->first->WriteAllExceptSender(this, true, 0, "MODE %s +%s", i->first->name, n.c_str());
1942 void userrec::NoticeAll(char* text, ...)
1944 char textbuffer[MAXBUF];
1945 char formatbuffer[MAXBUF];
1948 va_start(argsPtr, text);
1949 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1952 snprintf(formatbuffer,MAXBUF,":%s NOTICE $* :%s", this->GetFullHost(), textbuffer);
1953 std::string fmt = formatbuffer;
1955 for (std::vector<userrec*>::const_iterator i = ServerInstance->local_users.begin(); i != ServerInstance->local_users.end(); i++)
1962 std::string userrec::ChannelList(userrec* source)
1967 for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
1969 /* If the target is the same as the sender, let them see all their channels.
1970 * If the channel is NOT private/secret OR the user shares a common channel
1971 * If the user is an oper, and the <options:operspywhois> option is set.
1973 if ((source == this) || (*source->oper && ServerInstance->Config->OperSpyWhois) || (((!i->first->modes[CM_PRIVATE]) && (!i->first->modes[CM_SECRET])) || (i->first->HasUser(source))))
1975 list.append(i->first->GetPrefixChar(this)).append(i->first->name).append(" ");
1982 ServerInstance->Log(DEBUG,"Exception in userrec::ChannelList()");
1987 void userrec::SplitChanList(userrec* dest, const std::string &cl)
1990 std::ostringstream prefix;
1991 std::string::size_type start, pos, length;
1995 prefix << this->nick << " " << dest->nick << " :";
1996 line = prefix.str();
1997 int namelen = strlen(ServerInstance->Config->ServerName) + 6;
1999 for (start = 0; (pos = cl.find(' ', start)) != std::string::npos; start = pos+1)
2001 length = (pos == std::string::npos) ? cl.length() : pos;
2003 if (line.length() + namelen + length - start > 510)
2005 ServerInstance->SendWhoisLine(this, dest, 319, "%s", line.c_str());
2006 line = prefix.str();
2009 if(pos == std::string::npos)
2011 line.append(cl.substr(start, length - start));
2016 line.append(cl.substr(start, length - start + 1));
2022 ServerInstance->SendWhoisLine(this, dest, 319, "%s", line.c_str());
2028 ServerInstance->Log(DEBUG,"Exception in userrec::SplitChanList()");
2033 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags)
2034 * NOTE: If the <ALLOW> or <DENY> tag specifies an ip, and this user resolves,
2035 * then their ip will be taken as 'priority' anyway, so for example,
2036 * <connect allow="127.0.0.1"> will match joe!bloggs@localhost
2038 ConnectClass* userrec::GetClass()
2040 for (ClassVector::iterator i = ServerInstance->Config->Classes.begin(); i != ServerInstance->Config->Classes.end(); i++)
2042 if ((match(this->GetIPString(),i->GetHost().c_str(),true)) || (match(this->host,i->GetHost().c_str())))
2048 void userrec::PurgeEmptyChannels()
2050 std::vector<chanrec*> to_delete;
2052 // firstly decrement the count on each channel
2053 for (UCListIter f = this->chans.begin(); f != this->chans.end(); f++)
2055 f->first->RemoveAllPrefixes(this);
2056 if (f->first->DelUser(this) == 0)
2058 /* No users left in here, mark it for deletion */
2061 to_delete.push_back(f->first);
2065 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);
2079 this->chans.erase(*n);
2086 void userrec::ShowMOTD()
2088 if (!ServerInstance->Config->MOTD.size())
2090 this->WriteServ("422 %s :Message of the day file is missing.",this->nick);
2093 this->WriteServ("375 %s :%s message of the day", this->nick, ServerInstance->Config->ServerName);
2095 for (file_cache::iterator i = ServerInstance->Config->MOTD.begin(); i != ServerInstance->Config->MOTD.end(); i++)
2096 this->WriteServ("372 %s :- %s",this->nick,i->c_str());
2098 this->WriteServ("376 %s :End of message of the day.", this->nick);
2101 void userrec::ShowRULES()
2103 if (!ServerInstance->Config->RULES.size())
2105 this->WriteServ("NOTICE %s :Rules file is missing.",this->nick);
2108 this->WriteServ("NOTICE %s :%s rules",this->nick,ServerInstance->Config->ServerName);
2110 for (file_cache::iterator i = ServerInstance->Config->RULES.begin(); i != ServerInstance->Config->RULES.end(); i++)
2111 this->WriteServ("NOTICE %s :%s",this->nick,i->c_str());
2113 this->WriteServ("NOTICE %s :End of %s rules.",this->nick,ServerInstance->Config->ServerName);
2116 void userrec::HandleEvent(EventType et, int errornum)
2118 /* WARNING: May delete this user! */
2119 int thisfd = this->GetFd();
2126 ServerInstance->ProcessUser(this);
2129 this->FlushWriteBuf();
2132 /** This should be safe, but dont DARE do anything after it -- Brain */
2133 this->SetWriteError(errornum ? strerror(errornum) : "EOF from client");
2139 ServerInstance->Log(DEBUG,"Exception in userrec::HandleEvent intercepted");
2142 /* If the user has raised an error whilst being processed, quit them now we're safe to */
2143 if ((ServerInstance->SE->GetRef(thisfd) == this))
2145 if (!WriteError.empty())
2147 userrec::QuitUser(ServerInstance, this, GetWriteError());