]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/snomasks.cpp
Remove InspIRCd* parameters and fields
[user/henk/code/inspircd.git] / src / snomasks.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include <stdarg.h>
18 #include "snomasks.h"
19
20 SnomaskManager::SnomaskManager()
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::EnableSnomask(char letter, const std::string &type)
44 {
45         if (SnoMasks.find(letter) == SnoMasks.end())
46         {
47                 Snomask *s = new Snomask(letter, type);
48                 SnoMasks[letter] = s;
49                 return true;
50         }
51         return false;
52 }
53
54 bool SnomaskManager::DisableSnomask(char letter)
55 {
56         SnoList::iterator n = SnoMasks.find(letter);
57         if (n != SnoMasks.end())
58         {
59                 delete n->second; // destroy the snomask
60                 SnoMasks.erase(n);
61                 return true;
62         }
63         return false;
64 }
65
66 void SnomaskManager::WriteToSnoMask(char letter, const std::string &text)
67 {
68         /* Only send to snomask chars which are enabled */
69         SnoList::iterator n = SnoMasks.find(letter);
70         if (n != SnoMasks.end())
71         {
72                 n->second->SendMessage(text);
73         }
74 }
75
76 void SnomaskManager::WriteGlobalSno(char letter, const std::string& text)
77 {
78         WriteToSnoMask(letter, text);
79         letter = toupper(letter);
80         ServerInstance->PI->SendSNONotice(std::string(1, letter), text);
81 }
82
83 void SnomaskManager::WriteToSnoMask(char letter, const char* text, ...)
84 {
85         char textbuffer[MAXBUF];
86         va_list argsPtr;
87
88         va_start(argsPtr, text);
89         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
90         va_end(argsPtr);
91
92         this->WriteToSnoMask(letter, std::string(textbuffer));
93 }
94
95 void SnomaskManager::WriteGlobalSno(char letter, const char* text, ...)
96 {
97         char textbuffer[MAXBUF];
98         va_list argsPtr;
99
100         va_start(argsPtr, text);
101         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
102         va_end(argsPtr);
103
104         this->WriteGlobalSno(letter, std::string(textbuffer));
105 }
106
107 bool SnomaskManager::IsEnabled(char letter)
108 {
109         return (SnoMasks.find(letter) != SnoMasks.end());
110 }
111
112 void SnomaskManager::SetupDefaults()
113 {
114         this->EnableSnomask('c',"CONNECT");                     /* Local connect notices */
115         this->EnableSnomask('C',"REMOTECONNECT");       /* Remote connect notices */
116         this->EnableSnomask('q',"QUIT");                        /* Local quit notices */
117         this->EnableSnomask('Q',"REMOTEQUIT");          /* Remote quit notices */
118         this->EnableSnomask('k',"KILL");                        /* Kill notices */
119         this->EnableSnomask('K',"REMOTEKILL");          /* Remote kill notices */
120         this->EnableSnomask('l',"LINK");                        /* Linking notices */
121         this->EnableSnomask('L',"REMOTELINK");                  /* Remote linking notices */
122         this->EnableSnomask('o',"OPER");                        /* Oper up/down notices */
123         this->EnableSnomask('O',"REMOTEOPER");                  /* Remote oper up/down notices */
124         this->EnableSnomask('a',"ANNOUNCEMENT");        /* formerly WriteOpers() - generic notices to all opers */
125         this->EnableSnomask('A',"REMOTEANNOUNCEMENT");  /* formerly WriteOpers() - generic notices to all opers */
126         this->EnableSnomask('d',"DEBUG");                       /* Debug notices */
127         this->EnableSnomask('x',"XLINE");                       /* Xline notice (g/z/q/k/e) */
128         this->EnableSnomask('X',"REMOTEXLINE");                 /* Remove Xline notice (g/z/q/k/e) */
129         this->EnableSnomask('t',"STATS");                       /* Local or remote stats request */
130         this->EnableSnomask('f',"FLOOD");                       /* Flooding notices */
131 }
132
133 /*************************************************************************************/
134
135 void Snomask::SendMessage(const std::string &message)
136 {
137         if (message != LastMessage)
138         {
139                 this->Flush();
140                 LastMessage = message;
141
142                 std::string desc = this->Description;
143                 ModResult MOD_RESULT;
144                 char mysnomask = MySnomask;
145                 ServerInstance->Logs->Log("snomask", DEFAULT, "%s: %s", desc.c_str(), message.c_str());
146
147                 FIRST_MOD_RESULT(OnSendSnotice, MOD_RESULT, (mysnomask, desc, message));
148
149                 LastBlocked = (MOD_RESULT == MOD_RES_DENY);
150
151                 if (!LastBlocked)
152                 {
153                         /* Only opers can receive snotices, so we iterate the oper list */
154                         std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin();
155
156                         while (i != ServerInstance->Users->all_opers.end())
157                         {
158                                 User* a = *i;
159                                 if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsNoticeMaskSet(mysnomask) && !a->quitting)
160                                 {
161                                         a->WriteServ("NOTICE %s :*** %s: %s", a->nick.c_str(), desc.c_str(), message.c_str());
162                                 }
163
164                                 i++;
165                         }
166                 }
167         }
168         Count++;
169 }
170
171 void Snomask::Flush()
172 {
173         if (Count > 1)
174         {
175                 std::string desc = this->Description;
176                 std::string mesg = "(last message repeated "+ConvToStr(Count)+" times)";
177                 char mysnomask = MySnomask;
178
179                 ServerInstance->Logs->Log("snomask", DEFAULT, "%s: %s", desc.c_str(), mesg.c_str());
180
181                 FOREACH_MOD(I_OnSendSnotice, OnSendSnotice(mysnomask, desc, mesg));
182
183                 if (!LastBlocked)
184                 {
185                         /* Only opers can receive snotices, so we iterate the oper list */
186                         std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin();
187
188                         while (i != ServerInstance->Users->all_opers.end())
189                         {
190                                 User* a = *i;
191                                 if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsNoticeMaskSet(mysnomask) && !a->quitting)
192                                 {
193                                         a->WriteServ("NOTICE %s :*** %s: %s", a->nick.c_str(), desc.c_str(), mesg.c_str());
194                                 }
195
196                                 i++;
197                         }
198                 }
199
200         }
201         LastMessage = "";
202         LastBlocked = false;
203         Count = 0;
204 }