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