The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

snomasks.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *            the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 /* $Core */
00015 
00016 #include "inspircd.h"
00017 #include <stdarg.h>
00018 #include "snomasks.h"
00019 
00020 SnomaskManager::SnomaskManager(InspIRCd* Instance) : ServerInstance(Instance)
00021 {
00022         SnoMasks.clear();
00023         this->SetupDefaults();
00024 }
00025 
00026 SnomaskManager::~SnomaskManager()
00027 {       
00028         for (std::map<char, Snomask *>::iterator i = SnoMasks.begin(); i != SnoMasks.end(); i++)
00029         {
00030                 delete i->second;
00031         }
00032         SnoMasks.clear();
00033 }
00034 
00035 void SnomaskManager::FlushSnotices()
00036 {
00037         for (std::map<char, Snomask *>::iterator i = SnoMasks.begin(); i != SnoMasks.end(); i++)
00038         {
00039                 i->second->Flush();
00040         }
00041 }
00042 
00043 bool SnomaskManager::EnableSnomask(char letter, const std::string &type)
00044 {
00045         if (SnoMasks.find(letter) == SnoMasks.end())
00046         {
00047                 Snomask *s = new Snomask(ServerInstance, letter, type);
00048                 SnoMasks[letter] = s;
00049                 return true;
00050         }
00051         return false;
00052 }
00053 
00054 bool SnomaskManager::DisableSnomask(char letter)
00055 {
00056         SnoList::iterator n = SnoMasks.find(letter);
00057         if (n != SnoMasks.end())
00058         {
00059                 delete n->second; // destroy the snomask
00060                 SnoMasks.erase(n);
00061                 return true;
00062         }
00063         return false;
00064 }
00065 
00066 void SnomaskManager::WriteToSnoMask(char letter, const std::string &text)
00067 {
00068         /* Only send to snomask chars which are enabled */
00069         SnoList::iterator n = SnoMasks.find(letter);
00070         if (n != SnoMasks.end())
00071         {
00072                 n->second->SendMessage(text);
00073         }
00074 }
00075 
00076 void SnomaskManager::WriteToSnoMask(char letter, const char* text, ...)
00077 {
00078         char textbuffer[MAXBUF];
00079         va_list argsPtr;
00080 
00081         va_start(argsPtr, text);
00082         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
00083         va_end(argsPtr);
00084 
00085         this->WriteToSnoMask(letter, std::string(textbuffer));
00086 }
00087 
00088 bool SnomaskManager::IsEnabled(char letter)
00089 {
00090         return (SnoMasks.find(letter) != SnoMasks.end());
00091 }
00092 
00093 void SnomaskManager::SetupDefaults()
00094 {
00095         this->EnableSnomask('c',"CONNECT");                     /* Local connect notices */
00096         this->EnableSnomask('C',"REMOTECONNECT");       /* Remote connect notices */
00097         this->EnableSnomask('q',"QUIT");                        /* Local quit notices */
00098         this->EnableSnomask('Q',"REMOTEQUIT");          /* Remote quit notices */
00099         this->EnableSnomask('k',"KILL");                        /* Kill notices */
00100         this->EnableSnomask('K',"REMOTEKILL");          /* Remote kill notices */
00101         this->EnableSnomask('l',"LINK");                        /* Link notices */
00102         this->EnableSnomask('o',"OPER");                        /* Oper up/down notices */
00103         this->EnableSnomask('A',"ANNOUNCEMENT");        /* formerly WriteOpers() - generic notices to all opers */
00104         this->EnableSnomask('d',"DEBUG");                       /* Debug notices */
00105         this->EnableSnomask('x',"XLINE");                       /* Xline notice (g/z/q/k/e) */
00106         this->EnableSnomask('t',"STATS");                       /* Local or remote stats request */
00107         this->EnableSnomask('f',"FLOOD");                       /* Flooding notices */
00108 }
00109 
00110 /*************************************************************************************/
00111 
00112 void Snomask::SendMessage(const std::string &message)
00113 {
00114         if (message != LastMessage)
00115         {
00116                 this->Flush();
00117                 LastMessage = message;
00118         }
00119         else
00120         {
00121                 Count++;
00122         }
00123 }
00124 
00125 void Snomask::Flush()
00126 {
00127         if (this->LastMessage.empty())
00128                 return;
00129 
00130         ServerInstance->Logs->Log("snomask", DEFAULT, "%s: %s", this->Description.c_str(), this->LastMessage.c_str());
00131         if (Count > 1)
00132                 ServerInstance->Logs->Log("snomask", DEFAULT, "%s: (last message repeated %u times)", this->Description.c_str(), Count);
00133 
00134 
00135         int MOD_RESULT = 0;
00136         char mysnomask = MySnomask;
00137         std::string desc = this->Description;
00138 
00139         FOREACH_RESULT(I_OnSendSnotice, OnSendSnotice(mysnomask, desc, this->LastMessage));
00140 
00141         if (MOD_RESULT != 1) // 1 blocks the message
00142         {
00143                 /* Only opers can receive snotices, so we iterate the oper list */
00144                 std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin();
00145 
00146                 while (i != ServerInstance->Users->all_opers.end())
00147                 {
00148                         User* a = *i;
00149                         if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsNoticeMaskSet(mysnomask) && !a->quitting)
00150                         {
00151 
00152                                 a->WriteServ("NOTICE %s :*** %s: %s", a->nick.c_str(), desc.c_str(), this->LastMessage.c_str());
00153                                 if (Count > 1)
00154                                 {
00155                                         a->WriteServ("NOTICE %s :*** %s: (last message repeated %u times)", a->nick.c_str(), this->Description.c_str(), Count);
00156                                 }
00157                         }
00158 
00159                         i++;
00160                 }
00161         }
00162 
00163         LastMessage = "";
00164         Count = 1;
00165 }