+ 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++)