]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/snomasks.cpp
Fix
[user/henk/code/inspircd.git] / src / snomasks.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDsnomasks */
15
16 #include "inspircd.h"
17 #include <stdarg.h>
18 #include "snomasks.h"
19
20 SnomaskManager::SnomaskManager(InspIRCd* Instance) : ServerInstance(Instance)
21 {
22         SnoMasks.clear();
23         this->SetupDefaults();
24 }
25
26 SnomaskManager::~SnomaskManager()
27 {       
28         for (std::map<char, Snomask *>::iterator i = SnoMasks.begin(); i != SnoMasks.end(); i++)
29         {
30                 delete i->second;
31         }
32         SnoMasks.clear();
33 }
34
35 void SnomaskManager::FlushSnotices()
36 {
37         for (std::map<char, Snomask *>::iterator i = SnoMasks.begin(); i != SnoMasks.end(); i++)
38         {
39                 i->second->Flush();
40         }
41 }
42
43 bool SnomaskManager::SetLocalOnly(char letter, bool local)
44 {
45         SnoList::iterator n = SnoMasks.find(letter);
46         if (n != SnoMasks.end())
47         {
48                 n->second->LocalOnly = local;
49                 return n->second->LocalOnly;
50         }
51
52         ServerInstance->Logs->Log("SNOMASK", DEFAULT, "Snomask %d not found, unmatched modules?", letter);
53         return true;
54 }
55
56 bool SnomaskManager::EnableSnomask(char letter, const std::string &type, bool local)
57 {
58         if (SnoMasks.find(letter) == SnoMasks.end())
59         {
60                 Snomask *s = new Snomask(ServerInstance, letter, type, local);
61                 SnoMasks[letter] = s;
62                 return true;
63         }
64         return false;
65 }
66
67 bool SnomaskManager::DisableSnomask(char letter)
68 {
69         SnoList::iterator n = SnoMasks.find(letter);
70         if (n != SnoMasks.end())
71         {
72                 delete n->second; // destroy the snomask
73                 SnoMasks.erase(n);
74                 return true;
75         }
76         return false;
77 }
78
79 void SnomaskManager::WriteToSnoMask(char letter, const std::string &text)
80 {
81         /* Only send to snomask chars which are enabled */
82         SnoList::iterator n = SnoMasks.find(letter);
83         if (n != SnoMasks.end())
84         {
85                 n->second->SendMessage(text);
86         }
87 }
88
89 void SnomaskManager::WriteToSnoMask(char letter, const char* text, ...)
90 {
91         char textbuffer[MAXBUF];
92         va_list argsPtr;
93
94         va_start(argsPtr, text);
95         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
96         va_end(argsPtr);
97
98         this->WriteToSnoMask(letter, std::string(textbuffer));
99 }
100
101 bool SnomaskManager::IsEnabled(char letter)
102 {
103         return (SnoMasks.find(letter) != SnoMasks.end());
104 }
105
106 void SnomaskManager::SetupDefaults()
107 {
108         this->EnableSnomask('c',"CONNECT", true);                       /* Local connect notices */
109         this->EnableSnomask('C',"REMOTECONNECT");               /* Remote connect notices */
110         this->EnableSnomask('q',"QUIT", true);                  /* Local quit notices */
111         this->EnableSnomask('Q',"REMOTEQUIT");                  /* Remote quit notices */
112         this->EnableSnomask('k',"KILL", true);                  /* Kill notices */
113         this->EnableSnomask('K',"REMOTEKILL");                  /* Remote kill notices */
114         this->EnableSnomask('l',"LINK");                        /* Link notices */
115         this->EnableSnomask('o',"OPER");                        /* Oper up/down notices */
116         this->EnableSnomask('A',"ANNOUNCEMENT");                /* formerly WriteOpers() - generic notices to all opers */
117         this->EnableSnomask('d',"DEBUG");                       /* Debug notices */
118         this->EnableSnomask('x',"XLINE");                       /* Xline notice (g/z/q/k/e) */
119         this->EnableSnomask('t',"STATS");                       /* Local or remote stats request */
120         this->EnableSnomask('f',"FLOOD");                       /* Flooding notices */
121 }
122
123 /*************************************************************************************/
124
125 void Snomask::SendMessage(const std::string &message)
126 {
127         if (message != LastMessage)
128         {
129                 this->Flush();
130                 LastMessage = message;
131         }
132         else
133         {
134                 Count++;
135         }
136 }
137
138 void Snomask::Flush()
139 {
140         if (this->LastMessage.empty())
141                 return;
142
143         /* Only opers can receive snotices, so we iterate the oper list */
144         for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
145         {
146                 User* a = *i;
147                 if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsModeSet('n') && a->IsNoticeMaskSet(MySnomask) && !a->quitting)
148                 {
149
150                         a->WriteServ("NOTICE %s :*** %s: %s", a->nick, this->Description.c_str(), this->LastMessage.c_str());
151                         if (Count > 1)
152                         {
153                                 a->WriteServ("NOTICE %s :*** %s: (last message repeated %u times)", a->nick, this->Description.c_str(), Count);
154                         }
155                 }
156         }
157
158         if (!LocalOnly)
159         {
160                 // XXX this is a bit ugly.
161                 std::string sno;
162                 sno += MySnomask;
163
164                 ServerInstance->PI->SendSNONotice(sno, this->Description + ": " + this->LastMessage);
165                 if (Count > 1)
166                         ServerInstance->PI->SendSNONotice(sno, this->Description + ": (last message repeated " + ConvToStr(Count) + " times)");
167         }
168
169         LastMessage = "";
170         Count = 1;
171 }