]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/users.cpp
Get rid of Server::GetUsers(chanrec) - a throwback to before chanrec could do this...
[user/henk/code/inspircd.git] / src / users.cpp
index dd075b044a3beadcafc285ea1ee33432bcfb3533..bb3ef18e90e88768dbea368188397332f1104ce9 100644 (file)
@@ -45,11 +45,10 @@ extern userrec* fd_ref_table[MAX_DESCRIPTORS];
 extern ServerConfig *Config;
 extern user_hash clientlist;
 extern Server* MyServer;
-
-irc::whowas::whowas_users whowas;
-
 extern std::vector<userrec*> local_users;
 
+irc::whowas::whowas_users whowas;
+static unsigned long already_sent[MAX_DESCRIPTORS] = {0};
 std::vector<userrec*> all_opers;
 
 typedef std::map<irc::string,char*> opertype_t;
@@ -58,6 +57,9 @@ typedef opertype_t operclass_t;
 opertype_t opertypes;
 operclass_t operclass;
 
+/* XXX: Used for speeding up WriteCommon operations */
+unsigned long uniq_id = 0;
+
 bool InitTypes(const char* tag)
 {
        for (opertype_t::iterator n = opertypes.begin(); n != opertypes.end(); n++)
@@ -670,7 +672,7 @@ void userrec::QuitUser(userrec *user,const std::string &quitreason)
        {
                purge_empty_chans(user);
                FOREACH_MOD(I_OnUserQuit,OnUserQuit(user,reason));
-               WriteCommonExcept(user,"QUIT :%s",reason.c_str());
+               user->WriteCommonExcept("QUIT :%s",reason.c_str());
        }
 
        if (IS_LOCAL(user))
@@ -1279,19 +1281,17 @@ const char* userrec::GetIPString(char* buf)
 
 void userrec::Write(const std::string &text)
 {
-       char tb[MAXBUF];
-       int bytes;
-
        if ((this->fd < 0) || (this->fd > MAX_DESCRIPTORS))
                return;
 
-       bytes = snprintf(tb,MAXBUF,"%s\r\n",text.c_str());
+       std::string crlf = text;
+       crlf.append("\r\n");
 
        if (Config->GetIOHook(this->GetPort()))
        {
                try
                {
-                       Config->GetIOHook(this->GetPort())->OnRawSocketWrite(this->fd,tb,bytes);
+                       Config->GetIOHook(this->GetPort())->OnRawSocketWrite(this->fd, crlf.data(), crlf.length());
                }
                catch (ModuleException& modexcept)
                {
@@ -1300,9 +1300,9 @@ void userrec::Write(const std::string &text)
        }
        else
        {
-               this->AddWriteBuf(tb);
+               this->AddWriteBuf(crlf);
        }
-       ServerInstance->stats->statsSent += bytes;
+       ServerInstance->stats->statsSent += crlf.length();
 }
 
 /** Write()
@@ -1388,3 +1388,255 @@ void userrec::WriteTo(userrec *dest, const std::string &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 (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 ((FindServerName(server_one)) && (FindServerName(server_two)))
+                       {
+                               strlcpy(oper_quit,textbuffer,MAXQUIT);
+                               strlcpy(check,"*.net *.split",MAXQUIT);
+                               quit_munge = true;
+                       }
+               }
+       }
+
+       if ((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 = local_users.begin(); i != 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;
+}
+