+const char* userrec::GetIPString(char* buf)
+{
+ static char temp[1024];
+
+ if (this->ip == NULL)
+ {
+ *buf = 0;
+ return buf;
+ }
+
+ switch (this->GetProtocolFamily())
+ {
+#ifdef SUPPORT_IP6LINKS
+ case AF_INET6:
+ {
+ sockaddr_in6* sin = (sockaddr_in6*)this->ip;
+ inet_ntop(sin->sin6_family, &sin->sin6_addr, buf, sizeof(buf));
+ /* IP addresses starting with a : on irc are a Bad Thing (tm) */
+ if (*buf == ':')
+ {
+ strlcpy(&temp[1], buf, sizeof(temp));
+ *temp = '0';
+ strlcpy(buf, temp, sizeof(temp));
+ }
+ return buf;
+ }
+ break;
+#endif
+ case AF_INET:
+ {
+ sockaddr_in* sin = (sockaddr_in*)this->ip;
+ inet_ntop(sin->sin_family, &sin->sin_addr, buf, sizeof(buf));
+ return buf;
+ }
+ break;
+
+ default:
+ ServerInstance->Log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);
+ break;
+ }
+ return "";
+}
+
+/** NOTE: We cannot pass a const reference to this method.
+ * The string is changed by the workings of the method,
+ * so that if we pass const ref, we end up copying it to
+ * something we can change anyway. Makes sense to just let
+ * the compiler do that copy for us.
+ */
+void userrec::Write(std::string text)
+{
+ if ((this->fd < 0) || (this->fd > MAX_DESCRIPTORS))
+ return;
+
+ text.append("\r\n");
+
+ if (ServerInstance->Config->GetIOHook(this->GetPort()))
+ {
+ try
+ {
+ ServerInstance->Config->GetIOHook(this->GetPort())->OnRawSocketWrite(this->fd, text.data(), text.length());
+ }
+ catch (ModuleException& modexcept)
+ {
+ ServerInstance->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
+ }
+ }
+ else
+ {
+ this->AddWriteBuf(text);
+ }
+ ServerInstance->stats->statsSent += text.length();
+}
+
+/** Write()
+ */
+void userrec::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 userrec::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 userrec::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 userrec::WriteFrom(userrec *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 userrec::WriteFrom(userrec *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 userrec::WriteTo(userrec *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 userrec::WriteTo(userrec *dest, const std::string &data)
+{
+ dest->WriteFrom(this, data);
+}
+
+
+void userrec::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 userrec::WriteCommon(const std::string &text)
+{
+ bool sent_to_at_least_one = false;
+
+ if (this->registered != REG_ALL)
+ return;
+
+ uniq_id++;
+
+ for (std::vector<ucrec*>::const_iterator v = this->chans.begin(); v != this->chans.end(); v++)
+ {
+ ucrec *n = *v;
+ if (n->channel)
+ {
+ CUList *ulist= n->channel->GetUsers();
+
+ for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
+ {
+ if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
+ {
+ already_sent[i->second->fd] = uniq_id;
+ i->second->WriteFrom(this, std::string(text));
+ 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->WriteFrom(this,std::string(text));
+ }
+}
+
+
+/* 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 userrec::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 userrec::WriteCommonExcept(const std::string &text)
+{
+ bool quit_munge = true;
+ char oper_quit[MAXBUF];
+ char textbuffer[MAXBUF];
+
+ strlcpy(textbuffer, text.c_str(), MAXBUF);
+
+ if (this->registered != REG_ALL)
+ return;
+
+ uniq_id++;
+
+ /* TODO: We need some form of WriteCommonExcept that will send two lines, one line to
+ * opers and the other line to non-opers, then all this hidebans and hidesplits gunk
+ * can go byebye.
+ */
+ if (ServerInstance->Config->HideSplits)
+ {
+ char* check = textbuffer + 6;
+
+ if (!strncasecmp(textbuffer, "QUIT :",6))
+ {
+ std::stringstream split(check);
+ std::string server_one;
+ std::string server_two;
+
+ split >> server_one;
+ split >> server_two;
+
+ if ((ServerInstance->FindServerName(server_one)) && (ServerInstance->FindServerName(server_two)))
+ {
+ strlcpy(oper_quit,textbuffer,MAXQUIT);
+ strlcpy(check,"*.net *.split",MAXQUIT);
+ quit_munge = true;
+ }
+ }
+ }
+
+ if ((ServerInstance->Config->HideBans) && (!quit_munge))
+ {
+ if ((!strncasecmp(textbuffer, "QUIT :G-Lined:",14)) || (!strncasecmp(textbuffer, "QUIT :K-Lined:",14))
+ || (!strncasecmp(textbuffer, "QUIT :Q-Lined:",14)) || (!strncasecmp(textbuffer, "QUIT :Z-Lined:",14)))
+ {
+ char* check = textbuffer + 13;
+ strlcpy(oper_quit,textbuffer,MAXQUIT);
+ *check = 0; // We don't need to strlcpy, we just chop it from the :
+ quit_munge = true;
+ }
+ }
+
+ for (std::vector<ucrec*>::const_iterator v = this->chans.begin(); v != this->chans.end(); v++)
+ {
+ ucrec* n = *v;
+ if (n->channel)
+ {
+ CUList *ulist= n->channel->GetUsers();
+
+ for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
+ {
+ if (this != i->second)
+ {
+ if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
+ {
+ already_sent[i->second->fd] = uniq_id;
+ if (quit_munge)
+ i->second->WriteFrom(this, *i->second->oper ? std::string(oper_quit) : std::string(textbuffer));
+ else
+ i->second->WriteFrom(this, std::string(textbuffer));
+ }
+ }
+ }
+ }
+ }
+
+}
+
+void userrec::WriteWallOps(const std::string &text)
+{
+ /* Does nothing if theyre not opered */
+ if ((!*this->oper) && (IS_LOCAL(this)))
+ return;
+
+ std::string wallop = "WALLOPS :";
+ wallop.append(text);
+
+ for (std::vector<userrec*>::const_iterator i = ServerInstance->local_users.begin(); i != ServerInstance->local_users.end(); i++)
+ {
+ userrec* t = *i;
+ if ((IS_LOCAL(t)) && (t->modes[UM_WALLOPS]))
+ this->WriteTo(t,wallop);
+ }
+}
+
+void userrec::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<userrec*,userrec*>
+ * 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 userrec::SharesChannelWith(userrec *other)
+{
+ if ((!other) || (this->registered != REG_ALL) || (other->registered != REG_ALL))
+ return false;
+
+ /* Outer loop */
+ for (std::vector<ucrec*>::const_iterator i = this->chans.begin(); i != this->chans.end(); i++)
+ {
+ /* Fetch the channel from the user */
+ ucrec* user_channel = *i;
+
+ if (user_channel->channel)
+ {
+ /* 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 (user_channel->channel->HasUser(other))
+ return true;
+ }
+ }
+ return false;
+}
+
+int userrec::CountChannels()
+{
+ int z = 0;
+ for (std::vector<ucrec*>::const_iterator i = this->chans.begin(); i != this->chans.end(); i++)
+ if ((*i)->channel)
+ z++;
+ return z;
+}
+
+bool userrec::ChangeName(const char* gecos)
+{
+ 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 userrec::ChangeDisplayedHost(const char* host)
+{
+ if (IS_LOCAL(this))
+ {
+ int MOD_RESULT = 0;
+ FOREACH_RESULT(I_OnChangeLocalUserHost,OnChangeLocalUserHost(this,host));
+ if (MOD_RESULT)
+ return false;
+ FOREACH_MOD(I_OnChangeHost,OnChangeHost(this,host));
+ }
+ strlcpy(this->dhost,host,63);
+
+ if (IS_LOCAL(this))
+ this->WriteServ("396 %s %s :is now your hidden host",this->nick,this->dhost);
+
+ return true;
+}
+
+void userrec::NoticeAll(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,"NOTICE $* :%s",textbuffer);
+
+ for (std::vector<userrec*>::const_iterator i = ServerInstance->local_users.begin(); i != ServerInstance->local_users.end(); i++)
+ {
+ userrec* t = *i;
+ t->WriteFrom(this, std::string(formatbuffer));
+ }
+}
+
+
+std::string userrec::ChannelList(userrec* source)
+{
+ std::string list;
+ for (std::vector<ucrec*>::const_iterator i = this->chans.begin(); i != this->chans.end(); i++)
+ {
+ ucrec* rec = *i;
+
+ if(rec->channel && rec->channel->name)
+ {
+ /* 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) || (*source->oper && ServerInstance->Config->OperSpyWhois) || (((!rec->channel->modes[CM_PRIVATE]) && (!rec->channel->modes[CM_SECRET])) || (rec->channel->HasUser(source))))
+ {
+ list.append(rec->channel->GetStatusChar(this)).append(rec->channel->name).append(" ");
+ }
+ }
+ }
+ return list;
+}
+
+void userrec::SplitChanList(userrec* dest, const std::string &cl)
+{
+ std::string line;
+ std::ostringstream prefix;
+ std::string::size_type start, pos, length;
+
+ prefix << ":" << ServerInstance->Config->ServerName << " 319 " << this->nick << " " << dest->nick << " :";
+ line = prefix.str();
+
+ for (start = 0; (pos = cl.find(' ', start)) != std::string::npos; start = pos+1)
+ {
+ length = (pos == std::string::npos) ? cl.length() : pos;
+
+ if (line.length() + length - start > 510)
+ {
+ this->Write(line);
+ 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())
+ {
+ this->Write(line);
+ }
+}
+
+
+/* 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& userrec::GetClass()
+{
+ for (ClassVector::iterator i = ServerInstance->Config->Classes.begin(); i != ServerInstance->Config->Classes.end(); i++)
+ {
+ if ((match(this->GetIPString(),i->host.c_str(),true)) || (match(this->host,i->host.c_str())))
+ return *i;
+ }
+
+ return *(ServerInstance->Config->Classes.begin());
+}
+
+void userrec::PurgeEmptyChannels()
+{
+ std::vector<chanrec*> to_delete;
+
+ // firstly decrement the count on each channel
+ for (std::vector<ucrec*>::iterator f = this->chans.begin(); f != this->chans.end(); f++)
+ {
+ ucrec* uc = *f;
+ if (uc->channel)
+ {
+ uc->channel->RemoveAllPrefixes(this);
+ if (uc->channel->DelUser(this) == 0)
+ {
+ /* No users left in here, mark it for deletion */
+ to_delete.push_back(uc->channel);
+ uc->channel = NULL;
+ }
+ }
+ }
+
+ for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
+ {
+ chanrec* 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->UnOper();
+}
+
+void userrec::ShowMOTD()
+{
+ if (!ServerInstance->Config->MOTD.size())
+ {
+ this->WriteServ("422 %s :Message of the day file is missing.",this->nick);
+ return;
+ }
+ this->WriteServ("375 %s :%s message of the day", this->nick, ServerInstance->Config->ServerName);
+
+ for (unsigned int i = 0; i < ServerInstance->Config->MOTD.size(); i++)
+ this->WriteServ("372 %s :- %s",this->nick,ServerInstance->Config->MOTD[i].c_str());
+
+ this->WriteServ("376 %s :End of message of the day.", this->nick);
+}
+
+void userrec::ShowRULES()
+{
+ if (!ServerInstance->Config->RULES.size())
+ {
+ this->WriteServ("NOTICE %s :Rules file is missing.",this->nick);
+ return;
+ }
+ this->WriteServ("NOTICE %s :%s rules",this->nick,ServerInstance->Config->ServerName);
+
+ for (unsigned int i = 0; i < ServerInstance->Config->RULES.size(); i++)
+ this->WriteServ("NOTICE %s :%s",this->nick,ServerInstance->Config->RULES[i].c_str());
+
+ this->WriteServ("NOTICE %s :End of %s rules.",this->nick,ServerInstance->Config->ServerName);
+}
+
+void userrec::HandleEvent(EventType et)
+{
+ /* WARNING: May delete this user! */
+ ServerInstance->ProcessUser(this);
+}
+