]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/snomasks.cpp
Merge pull request #548 from SaberUK/master+variadic-templates
[user/henk/code/inspircd.git] / src / snomasks.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include <stdarg.h>
25
26 void SnomaskManager::FlushSnotices()
27 {
28         for (int i=0; i < 26; i++)
29                 masks[i].Flush();
30 }
31
32 void SnomaskManager::EnableSnomask(char letter, const std::string &type)
33 {
34         if (letter >= 'a' && letter <= 'z')
35                 masks[letter - 'a'].Description = type;
36 }
37
38 void SnomaskManager::WriteToSnoMask(char letter, const std::string &text)
39 {
40         if (letter >= 'a' && letter <= 'z')
41                 masks[letter - 'a'].SendMessage(text, letter);
42         if (letter >= 'A' && letter <= 'Z')
43                 masks[letter - 'A'].SendMessage(text, letter);
44 }
45
46 void SnomaskManager::WriteGlobalSno(char letter, const std::string& text)
47 {
48         WriteToSnoMask(letter, text);
49         letter = toupper(letter);
50         ServerInstance->PI->SendSNONotice(std::string(1, letter), text);
51 }
52
53 void SnomaskManager::WriteToSnoMask(char letter, const char* text, ...)
54 {
55         std::string textbuffer;
56         VAFORMAT(textbuffer, text, text);
57         this->WriteToSnoMask(letter, textbuffer);
58 }
59
60 void SnomaskManager::WriteGlobalSno(char letter, const char* text, ...)
61 {
62         std::string textbuffer;
63         VAFORMAT(textbuffer, text, text);
64         this->WriteGlobalSno(letter, textbuffer);
65 }
66
67 SnomaskManager::SnomaskManager()
68 {
69         EnableSnomask('c',"CONNECT");                   /* Local connect notices */
70         EnableSnomask('q',"QUIT");                      /* Local quit notices */
71         EnableSnomask('k',"KILL");                      /* Kill notices */
72         EnableSnomask('l',"LINK");                      /* Linking notices */
73         EnableSnomask('o',"OPER");                      /* Oper up/down notices */
74         EnableSnomask('a',"ANNOUNCEMENT");      /* formerly WriteOpers() - generic notices to all opers */
75         EnableSnomask('d',"DEBUG");                     /* Debug notices */
76         EnableSnomask('x',"XLINE");                     /* Xline notice (g/z/q/k/e) */
77         EnableSnomask('t',"STATS");                     /* Local or remote stats request */
78         EnableSnomask('f',"FLOOD");                     /* Flooding notices */
79 }
80
81 /*************************************************************************************/
82
83 void Snomask::SendMessage(const std::string &message, char mysnomask)
84 {
85         if (ServerInstance->Config->NoSnoticeStack || message != LastMessage || mysnomask != LastLetter)
86         {
87                 this->Flush();
88                 LastMessage = message;
89                 LastLetter = mysnomask;
90
91                 std::string desc = Description;
92                 if (desc.empty())
93                         desc = std::string("SNO-") + (char)tolower(mysnomask);
94                 if (isupper(mysnomask))
95                         desc = "REMOTE" + desc;
96                 ModResult MOD_RESULT;
97                 ServerInstance->Logs->Log("snomask", LOG_DEFAULT, "%s: %s", desc.c_str(), message.c_str());
98
99                 FIRST_MOD_RESULT(OnSendSnotice, MOD_RESULT, (mysnomask, desc, message));
100
101                 LastBlocked = (MOD_RESULT == MOD_RES_DENY);
102
103                 if (!LastBlocked)
104                 {
105                         /* Only opers can receive snotices, so we iterate the oper list */
106                         std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin();
107
108                         while (i != ServerInstance->Users->all_opers.end())
109                         {
110                                 User* a = *i;
111                                 if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsNoticeMaskSet(mysnomask) && !a->quitting)
112                                 {
113                                         a->WriteNotice("*** " + desc + ": " + message);
114                                 }
115
116                                 i++;
117                         }
118                 }
119         }
120         Count++;
121 }
122
123 void Snomask::Flush()
124 {
125         if (Count > 1)
126         {
127                 std::string desc = Description;
128                 if (desc.empty())
129                         desc = std::string("SNO-") + (char)tolower(LastLetter);
130                 if (isupper(LastLetter))
131                         desc = "REMOTE" + desc;
132                 std::string mesg = "(last message repeated "+ConvToStr(Count)+" times)";
133
134                 ServerInstance->Logs->Log("snomask", LOG_DEFAULT, "%s: %s", desc.c_str(), mesg.c_str());
135
136                 FOREACH_MOD(I_OnSendSnotice, OnSendSnotice(LastLetter, desc, mesg));
137
138                 if (!LastBlocked)
139                 {
140                         /* Only opers can receive snotices, so we iterate the oper list */
141                         std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin();
142
143                         while (i != ServerInstance->Users->all_opers.end())
144                         {
145                                 User* a = *i;
146                                 if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsNoticeMaskSet(LastLetter) && !a->quitting)
147                                 {
148                                         a->WriteNotice("*** " + desc + ": " + mesg);
149                                 }
150
151                                 i++;
152                         }
153                 }
154
155         }
156         LastMessage.clear();
157         LastBlocked = false;
158         Count = 0;
159 }