+ else
+ {
+ this->AddWriteBuf(text);
+ }
+ ServerInstance->stats->statsSent += text.length();
+ this->ServerInstance->SE->WantWrite(this);
+}
+
+/** Write()
+ */
+void User::Write(const char *text, ...)
+{
+ va_list argsPtr;
+ char textbuffer[MAXBUF];
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+
+ this->Write(std::string(textbuffer));
+}
+
+void User::WriteServ(const std::string& text)
+{
+ char textbuffer[MAXBUF];
+
+ snprintf(textbuffer,MAXBUF,":%s %s",ServerInstance->Config->ServerName,text.c_str());
+ this->Write(std::string(textbuffer));
+}
+
+/** WriteServ()
+ * Same as Write(), except `text' is prefixed with `:server.name '.
+ */
+void User::WriteServ(const char* text, ...)
+{
+ va_list argsPtr;
+ char textbuffer[MAXBUF];
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+
+ this->WriteServ(std::string(textbuffer));
+}
+
+
+void User::WriteNumeric(unsigned int numeric, const char* text, ...)
+{
+ va_list argsPtr;
+ char textbuffer[MAXBUF];
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+
+ this->WriteNumeric(numeric, std::string(textbuffer));
+}
+
+void User::WriteNumeric(unsigned int numeric, const std::string &text)
+{
+ char textbuffer[MAXBUF];
+ int MOD_RESULT = 0;
+
+ FOREACH_RESULT(I_OnNumeric, OnNumeric(this, numeric, text));
+
+ if (MOD_RESULT)
+ return;
+
+ snprintf(textbuffer,MAXBUF,":%s %03u %s",ServerInstance->Config->ServerName, numeric, text.c_str());
+ this->Write(std::string(textbuffer));
+}
+
+void User::WriteFrom(User *user, const std::string &text)
+{
+ char tb[MAXBUF];
+
+ snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
+
+ this->Write(std::string(tb));
+}
+
+
+/* write text from an originating user to originating user */
+
+void User::WriteFrom(User *user, const char* text, ...)
+{
+ va_list argsPtr;
+ char textbuffer[MAXBUF];
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+
+ this->WriteFrom(user, std::string(textbuffer));
+}
+
+
+/* write text to an destination user from a source user (e.g. user privmsg) */
+
+void User::WriteTo(User *dest, const char *data, ...)
+{
+ char textbuffer[MAXBUF];
+ va_list argsPtr;
+
+ va_start(argsPtr, data);
+ vsnprintf(textbuffer, MAXBUF, data, argsPtr);
+ va_end(argsPtr);
+
+ this->WriteTo(dest, std::string(textbuffer));
+}
+
+void User::WriteTo(User *dest, const std::string &data)
+{
+ dest->WriteFrom(this, data);
+}
+
+
+void User::WriteCommon(const char* text, ...)
+{
+ char textbuffer[MAXBUF];
+ va_list argsPtr;
+
+ if (this->registered != REG_ALL)
+ return;
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+
+ this->WriteCommon(std::string(textbuffer));
+}
+
+void User::WriteCommon(const std::string &text)
+{
+ bool sent_to_at_least_one = false;
+ char tb[MAXBUF];
+
+ if (this->registered != REG_ALL)
+ return;
+
+ uniq_id++;
+
+ /* We dont want to be doing this n times, just once */
+ snprintf(tb,MAXBUF,":%s %s",this->GetFullHost(),text.c_str());
+ std::string out = tb;
+
+ for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
+ {
+ CUList* ulist = v->first->GetUsers();
+ for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
+ {
+ if ((IS_LOCAL(i->first)) && (already_sent[i->first->fd] != uniq_id))
+ {
+ already_sent[i->first->fd] = uniq_id;
+ i->first->Write(out);
+ sent_to_at_least_one = true;
+ }
+ }
+ }
+
+ /*
+ * if the user was not in any channels, no users will receive the text. Make sure the user
+ * receives their OWN message for WriteCommon
+ */
+ if (!sent_to_at_least_one)
+ {
+ this->Write(std::string(tb));
+ }
+}
+
+
+/* write a formatted string to all users who share at least one common
+ * channel, NOT including the source user e.g. for use in QUIT
+ */
+
+void User::WriteCommonExcept(const char* text, ...)
+{
+ char textbuffer[MAXBUF];
+ va_list argsPtr;
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+
+ this->WriteCommonExcept(std::string(textbuffer));
+}
+
+void User::WriteCommonQuit(const std::string &normal_text, const std::string &oper_text)
+{
+ char tb1[MAXBUF];
+ char tb2[MAXBUF];
+
+ if (this->registered != REG_ALL)
+ return;
+
+ uniq_id++;
+ snprintf(tb1,MAXBUF,":%s QUIT :%s",this->GetFullHost(),normal_text.c_str());
+ snprintf(tb2,MAXBUF,":%s QUIT :%s",this->GetFullHost(),oper_text.c_str());
+ std::string out1 = tb1;
+ std::string out2 = tb2;
+
+ for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
+ {
+ CUList *ulist = v->first->GetUsers();
+ for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
+ {
+ if (this != i->first)
+ {
+ if ((IS_LOCAL(i->first)) && (already_sent[i->first->fd] != uniq_id))
+ {
+ already_sent[i->first->fd] = uniq_id;
+ i->first->Write(IS_OPER(i->first) ? out2 : out1);
+ }
+ }
+ }
+ }
+}
+
+void User::WriteCommonExcept(const std::string &text)
+{
+ char tb1[MAXBUF];
+ std::string out1;
+
+ if (this->registered != REG_ALL)
+ return;
+
+ uniq_id++;
+ snprintf(tb1,MAXBUF,":%s %s",this->GetFullHost(),text.c_str());
+ out1 = tb1;
+
+ for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
+ {
+ CUList *ulist = v->first->GetUsers();
+ for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
+ {
+ if (this != i->first)
+ {
+ if ((IS_LOCAL(i->first)) && (already_sent[i->first->fd] != uniq_id))
+ {
+ already_sent[i->first->fd] = uniq_id;
+ i->first->Write(out1);
+ }
+ }
+ }
+ }
+
+}
+
+void User::WriteWallOps(const std::string &text)
+{
+ if (!IS_OPER(this) && IS_LOCAL(this))
+ return;
+
+ std::string wallop("WALLOPS :");
+ wallop.append(text);
+
+ for (std::vector<User*>::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); i++)
+ {
+ User* t = *i;
+ if (t->IsModeSet('w'))
+ this->WriteTo(t,wallop);
+ }
+}
+
+void User::WriteWallOps(const char* text, ...)
+{
+ char textbuffer[MAXBUF];
+ va_list argsPtr;
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+
+ this->WriteWallOps(std::string(textbuffer));
+}
+
+/* return 0 or 1 depending if users u and u2 share one or more common channels
+ * (used by QUIT, NICK etc which arent channel specific notices)
+ *
+ * The old algorithm in 1.0 for this was relatively inefficient, iterating over
+ * the first users channels then the second users channels within the outer loop,
+ * therefore it was a maximum of x*y iterations (upon returning 0 and checking
+ * all possible iterations). However this new function instead checks against the
+ * channel's userlist in the inner loop which is a std::map<User*,User*>
+ * and saves us time as we already know what pointer value we are after.
+ * Don't quote me on the maths as i am not a mathematician or computer scientist,
+ * but i believe this algorithm is now x+(log y) maximum iterations instead.
+ */
+bool User::SharesChannelWith(User *other)
+{
+ if ((!other) || (this->registered != REG_ALL) || (other->registered != REG_ALL))
+ return false;
+
+ /* Outer loop */
+ for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
+ {
+ /* Eliminate the inner loop (which used to be ~equal in size to the outer loop)
+ * by replacing it with a map::find which *should* be more efficient
+ */
+ if (i->first->HasUser(other))
+ return true;
+ }
+ return false;
+}
+
+bool User::ChangeName(const char* gecos)
+{
+ if (!strcmp(gecos, this->fullname))
+ return true;
+
+ if (IS_LOCAL(this))
+ {
+ int MOD_RESULT = 0;
+ FOREACH_RESULT(I_OnChangeLocalUserGECOS,OnChangeLocalUserGECOS(this,gecos));
+ if (MOD_RESULT)
+ return false;
+ FOREACH_MOD(I_OnChangeName,OnChangeName(this,gecos));
+ }
+ strlcpy(this->fullname,gecos,MAXGECOS+1);
+
+ return true;
+}
+
+bool User::ChangeDisplayedHost(const char* shost)
+{
+ if (!strcmp(shost, this->dhost))
+ return true;
+
+ if (IS_LOCAL(this))
+ {
+ int MOD_RESULT = 0;
+ FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(this,shost));
+ if (MOD_RESULT)
+ return false;
+ FOREACH_MOD(I_OnChangeHost,OnChangeHost(this,shost));
+ }
+
+ if (this->ServerInstance->Config->CycleHosts)
+ this->WriteCommonExcept("QUIT :Changing hosts");
+
+ /* Fix by Om: User::dhost is 65 long, this was truncating some long hosts */
+ strlcpy(this->dhost,shost,64);
+
+ this->InvalidateCache();
+
+ if (this->ServerInstance->Config->CycleHosts)
+ {
+ for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
+ {
+ i->first->WriteAllExceptSender(this, false, 0, "JOIN %s", i->first->name);
+ std::string n = this->ServerInstance->Modes->ModeString(this, i->first);
+ if (n.length() > 0)
+ i->first->WriteAllExceptSender(this, true, 0, "MODE %s +%s", i->first->name, n.c_str());
+ }
+ }
+
+ if (IS_LOCAL(this))
+ this->WriteNumeric(396, "%s %s :is now your displayed host",this->nick,this->dhost);
+
+ return true;
+}
+
+bool User::ChangeIdent(const char* newident)
+{
+ if (!strcmp(newident, this->ident))
+ return true;
+
+ if (this->ServerInstance->Config->CycleHosts)
+ this->WriteCommonExcept("%s","QUIT :Changing ident");
+
+ strlcpy(this->ident, newident, IDENTMAX+1);
+
+ this->InvalidateCache();
+
+ if (this->ServerInstance->Config->CycleHosts)
+ {
+ for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
+ {
+ i->first->WriteAllExceptSender(this, false, 0, "JOIN %s", i->first->name);
+ std::string n = this->ServerInstance->Modes->ModeString(this, i->first);
+ if (n.length() > 0)
+ i->first->WriteAllExceptSender(this, true, 0, "MODE %s +%s", i->first->name, n.c_str());
+ }
+ }
+
+ return true;
+}
+
+void User::SendAll(const char* command, const char* text, ...)
+{
+ char textbuffer[MAXBUF];
+ char formatbuffer[MAXBUF];
+ va_list argsPtr;
+
+ va_start(argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+
+ snprintf(formatbuffer,MAXBUF,":%s %s $* :%s", this->GetFullHost(), command, textbuffer);
+ std::string fmt = formatbuffer;
+
+ for (std::vector<User*>::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); i++)
+ {
+ (*i)->Write(fmt);
+ }
+}
+
+
+std::string User::ChannelList(User* source)
+{
+ std::string list;
+
+ for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
+ {
+ /* If the target is the same as the sender, let them see all their channels.
+ * If the channel is NOT private/secret OR the user shares a common channel
+ * If the user is an oper, and the <options:operspywhois> option is set.
+ */
+ if ((source == this) || (IS_OPER(source) && ServerInstance->Config->OperSpyWhois) || (((!i->first->IsModeSet('p')) && (!i->first->IsModeSet('s'))) || (i->first->HasUser(source))))
+ {
+ list.append(i->first->GetPrefixChar(this)).append(i->first->name).append(" ");
+ }
+ }
+
+ return list;
+}
+
+void User::SplitChanList(User* dest, const std::string &cl)
+{
+ std::string line;
+ std::ostringstream prefix;
+ std::string::size_type start, pos, length;
+
+ prefix << this->nick << " " << dest->nick << " :";
+ line = prefix.str();
+ int namelen = strlen(ServerInstance->Config->ServerName) + 6;
+
+ for (start = 0; (pos = cl.find(' ', start)) != std::string::npos; start = pos+1)
+ {
+ length = (pos == std::string::npos) ? cl.length() : pos;
+
+ if (line.length() + namelen + length - start > 510)
+ {
+ ServerInstance->SendWhoisLine(this, dest, 319, "%s", line.c_str());
+ line = prefix.str();
+ }
+
+ if(pos == std::string::npos)
+ {
+ line.append(cl.substr(start, length - start));
+ break;
+ }
+ else
+ {
+ line.append(cl.substr(start, length - start + 1));
+ }
+ }
+
+ if (line.length())
+ {
+ ServerInstance->SendWhoisLine(this, dest, 319, "%s", line.c_str());
+ }
+}
+
+unsigned int User::GetMaxChans()
+{
+ return this->MaxChans;
+}
+
+
+/*
+ * Sets a user's connection class.
+ * If the class name is provided, it will be used. Otherwise, the class will be guessed using host/ip/ident/etc.
+ * NOTE: If the <ALLOW> or <DENY> tag specifies an ip, and this user resolves,
+ * then their ip will be taken as 'priority' anyway, so for example,
+ * <connect allow="127.0.0.1"> will match joe!bloggs@localhost
+ */
+ConnectClass* User::SetClass(const std::string &explicit_name)
+{
+ ConnectClass *found = NULL;
+
+ if (!IS_LOCAL(this))
+ return NULL;
+
+ if (!explicit_name.empty())
+ {
+ for (ClassVector::iterator i = ServerInstance->Config->Classes.begin(); i != ServerInstance->Config->Classes.end(); i++)
+ {
+ ConnectClass* c = *i;
+
+ if (explicit_name == c->GetName() && !c->GetDisabled())
+ {
+ found = c;
+ }
+ }
+ }
+ else
+ {
+ for (ClassVector::iterator i = ServerInstance->Config->Classes.begin(); i != ServerInstance->Config->Classes.end(); i++)
+ {
+ ConnectClass* c = *i;
+
+ if (((match(this->GetIPString(),c->GetHost().c_str(),true)) || (match(this->host,c->GetHost().c_str()))))
+ {
+ if (c->GetPort())
+ {
+ if (this->GetPort() == c->GetPort() && !c->GetDisabled())
+ {
+ found = c;
+ }
+ else
+ continue;
+ }
+ else
+ {
+ if (!c->GetDisabled())
+ found = c;
+ }
+ }
+ }
+ }
+
+ /* ensure we don't fuck things up refcount wise, only remove them from a class if we find a new one :P */
+ if (found)
+ {
+ /* deny change if change will take class over the limit */
+ if (found->limit && (found->RefCount + 1 >= found->limit))
+ {
+ ServerInstance->Logs->Log("USERS", DEBUG, "OOPS: Connect class limit (%lu) hit, denying", found->limit);
+ return this->MyClass;
+ }
+
+ /* should always be valid, but just in case .. */
+ if (this->MyClass)
+ {
+ if (found == this->MyClass) // no point changing this shit :P
+ return this->MyClass;
+ this->MyClass->RefCount--;
+ ServerInstance->Logs->Log("USERS", DEBUG, "Untying user from connect class -- refcount: %lu", this->MyClass->RefCount);
+ }
+
+ this->MyClass = found;
+ this->MyClass->RefCount++;
+ ServerInstance->Logs->Log("USERS", DEBUG, "User tied to new class -- connect refcount now: %lu", this->MyClass->RefCount);
+ }
+
+ return this->MyClass;
+}
+
+/* looks up a users password for their connection class (<ALLOW>/<DENY> tags)
+ * NOTE: If the <ALLOW> or <DENY> tag specifies an ip, and this user resolves,
+ * then their ip will be taken as 'priority' anyway, so for example,
+ * <connect allow="127.0.0.1"> will match joe!bloggs@localhost
+ */
+ConnectClass* User::GetClass()
+{
+ return this->MyClass;
+}
+
+void User::PurgeEmptyChannels()
+{
+ std::vector<Channel*> to_delete;
+
+ // firstly decrement the count on each channel
+ for (UCListIter f = this->chans.begin(); f != this->chans.end(); f++)
+ {
+ f->first->RemoveAllPrefixes(this);
+ if (f->first->DelUser(this) == 0)
+ {
+ /* No users left in here, mark it for deletion */
+ try
+ {
+ to_delete.push_back(f->first);
+ }
+ catch (...)
+ {
+ ServerInstance->Logs->Log("USERS", DEBUG,"Exception in User::PurgeEmptyChannels to_delete.push_back()");
+ }
+ }
+ }
+
+ for (std::vector<Channel*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
+ {
+ Channel* thischan = *n;
+ chan_hash::iterator i2 = ServerInstance->chanlist->find(thischan->name);
+ if (i2 != ServerInstance->chanlist->end())
+ {
+ FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
+ delete i2->second;
+ ServerInstance->chanlist->erase(i2);
+ this->chans.erase(*n);
+ }
+ }
+
+ this->UnOper();
+}
+
+void User::ShowMOTD()
+{
+ if (!ServerInstance->Config->MOTD.size())
+ {
+ this->WriteNumeric(422, "%s :Message of the day file is missing.",this->nick);
+ return;
+ }
+ this->WriteNumeric(375, "%s :%s message of the day", this->nick, ServerInstance->Config->ServerName);
+
+ for (file_cache::iterator i = ServerInstance->Config->MOTD.begin(); i != ServerInstance->Config->MOTD.end(); i++)
+ this->WriteNumeric(372, "%s :- %s",this->nick,i->c_str());
+
+ this->WriteNumeric(376, "%s :End of message of the day.", this->nick);
+}
+
+void User::ShowRULES()
+{
+ if (!ServerInstance->Config->RULES.size())
+ {
+ this->WriteNumeric(434, "%s :RULES File is missing",this->nick);
+ return;
+ }
+
+ this->WriteNumeric(308, "%s :- %s Server Rules -",this->nick,ServerInstance->Config->ServerName);
+
+ for (file_cache::iterator i = ServerInstance->Config->RULES.begin(); i != ServerInstance->Config->RULES.end(); i++)
+ this->WriteNumeric(232, "%s :- %s",this->nick,i->c_str());
+
+ this->WriteNumeric(309, "%s :End of RULES command.",this->nick);
+}
+
+void User::HandleEvent(EventType et, int errornum)
+{
+ if (this->quitting) // drop everything, user is due to be quit
+ return;
+
+ /* WARNING: May delete this user! */
+ int thisfd = this->GetFd();
+
+ try
+ {
+ switch (et)
+ {
+ case EVENT_READ:
+ ServerInstance->ProcessUser(this);
+ break;
+ case EVENT_WRITE:
+ this->FlushWriteBuf();
+ break;
+ case EVENT_ERROR:
+ /** This should be safe, but dont DARE do anything after it -- Brain */
+ this->SetWriteError(errornum ? strerror(errornum) : "EOF from client");
+ break;
+ }
+ }
+ catch (...)
+ {
+ ServerInstance->Logs->Log("USERS", DEBUG,"Exception in User::HandleEvent intercepted");
+ }
+
+ /* If the user has raised an error whilst being processed, quit them now we're safe to */
+ if ((ServerInstance->SE->GetRef(thisfd) == this))
+ {
+ if (!WriteError.empty())
+ {
+ User::QuitUser(ServerInstance, this, GetWriteError());
+ }
+ }
+}
+
+void User::SetOperQuit(const std::string &oquit)
+{
+ operquitmsg = oquit;
+}
+
+const char* User::GetOperQuit()
+{
+ return operquitmsg.c_str();
+}
+
+void User::IncreasePenalty(int increase)
+{
+ this->Penalty += increase;
+}
+
+void User::DecreasePenalty(int decrease)
+{
+ this->Penalty -= decrease;
+}
+
+VisData::VisData()
+{
+}
+
+VisData::~VisData()
+{
+}
+
+bool VisData::VisibleTo(User* user)
+{
+ return true;