]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/snomasks.cpp
2ab473b4506594424e0ed551f650daa6acc1c69f
[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         throw "snomask not found wtf";
53 }
54
55 bool SnomaskManager::EnableSnomask(char letter, const std::string &type, bool local)
56 {
57         if (SnoMasks.find(letter) == SnoMasks.end())
58         {
59                 Snomask *s = new Snomask(ServerInstance, letter, type, local);
60                 SnoMasks[letter] = s;
61                 return true;
62         }
63         return false;
64 }
65
66 bool SnomaskManager::DisableSnomask(char letter)
67 {
68         SnoList::iterator n = SnoMasks.find(letter);
69         if (n != SnoMasks.end())
70         {
71                 delete n->second; // destroy the snomask
72                 SnoMasks.erase(n);
73                 return true;
74         }
75         return false;
76 }
77
78 void SnomaskManager::WriteToSnoMask(char letter, const std::string &text)
79 {
80         /* Only send to snomask chars which are enabled */
81         SnoList::iterator n = SnoMasks.find(letter);
82         if (n != SnoMasks.end())
83         {
84                 n->second->SendMessage(text);
85         }
86 }
87
88 void SnomaskManager::WriteToSnoMask(char letter, const char* text, ...)
89 {
90         char textbuffer[MAXBUF];
91         va_list argsPtr;
92
93         va_start(argsPtr, text);
94         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
95         va_end(argsPtr);
96
97         this->WriteToSnoMask(letter, std::string(textbuffer));
98 }
99
100 bool SnomaskManager::IsEnabled(char letter)
101 {
102         return (SnoMasks.find(letter) != SnoMasks.end());
103 }
104
105 void SnomaskManager::SetupDefaults()
106 {
107         this->EnableSnomask('c',"CONNECT", true);                       /* Local connect notices */
108         this->EnableSnomask('C',"REMOTECONNECT");               /* Remote connect notices */
109         this->EnableSnomask('q',"QUIT", true);                  /* Local quit notices */
110         this->EnableSnomask('Q',"REMOTEQUIT");                  /* Remote quit notices */
111         this->EnableSnomask('k',"KILL", true);                  /* Kill notices */
112         this->EnableSnomask('K',"REMOTEKILL");                  /* Remote kill notices */
113         this->EnableSnomask('l',"LINK");                        /* Link notices */
114         this->EnableSnomask('o',"OPER");                        /* Oper up/down notices */
115         this->EnableSnomask('A',"ANNOUNCEMENT");                /* formerly WriteOpers() - generic notices to all opers */
116         this->EnableSnomask('d',"DEBUG");                       /* Debug notices */
117         this->EnableSnomask('x',"XLINE");                       /* Xline notice (g/z/q/k/e) */
118         this->EnableSnomask('t',"STATS");                       /* Local or remote stats request */
119         this->EnableSnomask('f',"FLOOD");                       /* Flooding notices */
120 }
121
122 /*************************************************************************************/
123
124 void Snomask::SendMessage(const std::string &message)
125 {
126         if (message != LastMessage)
127         {
128                 this->Flush();
129                 LastMessage = message;
130         }
131         else
132         {
133                 Count++;
134         }
135 }
136
137 void Snomask::Flush()
138 {
139         if (this->LastMessage.empty())
140                 return;
141
142         /* Only opers can receive snotices, so we iterate the oper list */
143         for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
144         {
145                 User* a = *i;
146                 if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsModeSet('n') && a->IsNoticeMaskSet(MySnomask) && !a->quitting)
147                 {
148
149                         a->WriteServ("NOTICE %s :*** %s: %s", a->nick, this->Description.c_str(), this->LastMessage.c_str());
150                         if (Count > 1)
151                         {
152                                 a->WriteServ("NOTICE %s :*** %s: (last message repeated %u times)", a->nick, this->Description.c_str(), Count);
153                         }
154                 }
155         }
156
157         if (!LocalOnly)
158         {
159                 // XXX this is a bit ugly.
160                 std::string sno;
161                 sno[0] = MySnomask;
162
163                 ServerInstance->PI->SendSNONotice(sno, this->Description + ": " + this->LastMessage);
164                 if (Count > 1)
165                         ServerInstance->PI->SendSNONotice(sno, this->Description + ": (last message repeated " + ConvToStr(Count) + " times)");
166         }
167
168         LastMessage = "";
169         Count = 1;
170 }