]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/snomasks.cpp
Change type of snomask parameter to char in ProtocolInterface::SendSNONotice()
[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(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('o',"OPER");                      /* Oper up/down notices */
73         EnableSnomask('a',"ANNOUNCEMENT");      /* formerly WriteOpers() - generic notices to all opers */
74         EnableSnomask('d',"DEBUG");                     /* Debug notices */
75         EnableSnomask('x',"XLINE");                     /* Xline notice (g/z/q/k/e) */
76         EnableSnomask('t',"STATS");                     /* Local or remote stats request */
77 }
78
79 bool SnomaskManager::IsSnomaskUsable(char ch) const
80 {
81         return ((isalpha(ch)) && (!masks[tolower(ch) - 'a'].Description.empty()));
82 }
83
84 Snomask::Snomask()
85         : Count(0)
86 {
87 }
88
89 void Snomask::SendMessage(const std::string& message, char letter)
90 {
91         if ((!ServerInstance->Config->NoSnoticeStack) && (message == LastMessage) && (letter == LastLetter))
92         {
93                 Count++;
94                 return;
95         }
96
97         this->Flush();
98
99         std::string desc = GetDescription(letter);
100         ModResult MOD_RESULT;
101         FIRST_MOD_RESULT(OnSendSnotice, MOD_RESULT, (letter, desc, message));
102         if (MOD_RESULT == MOD_RES_DENY)
103                 return;
104
105         Snomask::Send(letter, desc, message);
106         LastMessage = message;
107         LastLetter = letter;
108         Count++;
109 }
110
111 void Snomask::Flush()
112 {
113         if (Count > 1)
114         {
115                 std::string desc = GetDescription(LastLetter);
116                 std::string msg = "(last message repeated " + ConvToStr(Count) + " times)";
117
118                 FOREACH_MOD(OnSendSnotice, (LastLetter, desc, msg));
119                 Snomask::Send(LastLetter, desc, msg);
120         }
121
122         LastMessage.clear();
123         Count = 0;
124 }
125
126 void Snomask::Send(char letter, const std::string& desc, const std::string& msg)
127 {
128         std::string log = desc;
129         log.append(": ").append(msg);
130         ServerInstance->Logs->Log("snomask", LOG_DEFAULT, log);
131
132         std::string finalmsg = "*** ";
133         finalmsg.append(log);
134         /* Only opers can receive snotices, so we iterate the oper list */
135         const std::list<User*>& opers = ServerInstance->Users->all_opers;
136         for (std::list<User*>::const_iterator i = opers.begin(); i != opers.end(); ++i)
137         {
138                 User* user = *i;
139                 // IsNoticeMaskSet() returns false for opers who aren't +s, no need to check for it seperately
140                 if (IS_LOCAL(user) && user->IsNoticeMaskSet(letter))
141                         user->WriteNotice(finalmsg);
142         }
143 }
144
145 std::string Snomask::GetDescription(char letter) const
146 {
147         std::string ret;
148         if (isupper(letter))
149                 ret = "REMOTE";
150         if (!Description.empty())
151                 ret += Description;
152         else
153                 ret += std::string("SNO-") + (char)tolower(letter);
154         return ret;
155 }