]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/snomasks.cpp
13eb326bf2773596a0b926dfeafbe509f8a80763
[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://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 */
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::EnableSnomask(char letter, const std::string &type)
44 {
45         if (SnoMasks.find(letter) == SnoMasks.end())
46         {
47                 Snomask *s = new Snomask(ServerInstance, 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                 // XXX: Always try flush here. This removes snomask stacking effectively, as it's too annoying in it's present form. This may be reworked for RC3, or delayed until post-release.
74                 n->second->Flush();
75         }
76 }
77
78 void SnomaskManager::WriteToSnoMask(char letter, const char* text, ...)
79 {
80         char textbuffer[MAXBUF];
81         va_list argsPtr;
82
83         va_start(argsPtr, text);
84         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
85         va_end(argsPtr);
86
87         this->WriteToSnoMask(letter, std::string(textbuffer));
88 }
89
90 bool SnomaskManager::IsEnabled(char letter)
91 {
92         return (SnoMasks.find(letter) != SnoMasks.end());
93 }
94
95 void SnomaskManager::SetupDefaults()
96 {
97         this->EnableSnomask('c',"CONNECT");                     /* Local connect notices */
98         this->EnableSnomask('C',"REMOTECONNECT");       /* Remote connect notices */
99         this->EnableSnomask('q',"QUIT");                        /* Local quit notices */
100         this->EnableSnomask('Q',"REMOTEQUIT");          /* Remote quit notices */
101         this->EnableSnomask('k',"KILL");                        /* Kill notices */
102         this->EnableSnomask('K',"REMOTEKILL");          /* Remote kill notices */
103         this->EnableSnomask('l',"LINK");                        /* Linking notices */
104         this->EnableSnomask('L',"REMOTELINK");                  /* Remote linking notices */
105         this->EnableSnomask('o',"OPER");                        /* Oper up/down notices */
106         this->EnableSnomask('O',"REMOTEOPER");                  /* Remote oper up/down notices */
107         this->EnableSnomask('A',"ANNOUNCEMENT");        /* formerly WriteOpers() - generic notices to all opers */
108         this->EnableSnomask('d',"DEBUG");                       /* Debug notices */
109         this->EnableSnomask('x',"XLINE");                       /* Xline notice (g/z/q/k/e) */
110         this->EnableSnomask('t',"STATS");                       /* Local or remote stats request */
111         this->EnableSnomask('f',"FLOOD");                       /* Flooding notices */
112 }
113
114 /*************************************************************************************/
115
116 void Snomask::SendMessage(const std::string &message)
117 {
118         if (message != LastMessage)
119         {
120                 this->Flush();
121                 LastMessage = message;
122         }
123         else
124         {
125                 Count++;
126         }
127 }
128
129 void Snomask::Flush()
130 {
131         if (this->LastMessage.empty())
132                 return;
133
134         ServerInstance->Logs->Log("snomask", DEFAULT, "%s: %s", this->Description.c_str(), this->LastMessage.c_str());
135         if (Count > 1)
136                 ServerInstance->Logs->Log("snomask", DEFAULT, "%s: (last message repeated %u times)", this->Description.c_str(), Count);
137
138
139         int MOD_RESULT = 0;
140         char mysnomask = MySnomask;
141         std::string desc = this->Description;
142
143         FOREACH_RESULT(I_OnSendSnotice, OnSendSnotice(mysnomask, desc, this->LastMessage));
144
145         if (MOD_RESULT != 1) // 1 blocks the message
146         {
147                 /* Only opers can receive snotices, so we iterate the oper list */
148                 std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin();
149
150                 while (i != ServerInstance->Users->all_opers.end())
151                 {
152                         User* a = *i;
153                         if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsNoticeMaskSet(mysnomask) && !a->quitting)
154                         {
155
156                                 a->WriteServ("NOTICE %s :*** %s: %s", a->nick.c_str(), desc.c_str(), this->LastMessage.c_str());
157                                 if (Count > 1)
158                                 {
159                                         a->WriteServ("NOTICE %s :*** %s: (last message repeated %u times)", a->nick.c_str(), this->Description.c_str(), Count);
160                                 }
161                         }
162
163                         i++;
164                 }
165         }
166
167         LastMessage = "";
168         Count = 1;
169 }