]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/snomasks.cpp
Merge the latest changes from insp20 into master.
[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
25 void SnomaskManager::FlushSnotices()
26 {
27         for (int i=0; i < 26; i++)
28                 masks[i].Flush();
29 }
30
31 void SnomaskManager::EnableSnomask(char letter, const std::string &type)
32 {
33         if (letter >= 'a' && letter <= 'z')
34                 masks[letter - 'a'].Description = type;
35 }
36
37 void SnomaskManager::WriteToSnoMask(char letter, const std::string &text)
38 {
39         if (letter >= 'a' && letter <= 'z')
40                 masks[letter - 'a'].SendMessage(text, letter);
41         if (letter >= 'A' && letter <= 'Z')
42                 masks[letter - 'A'].SendMessage(text, letter);
43 }
44
45 void SnomaskManager::WriteGlobalSno(char letter, const std::string& text)
46 {
47         WriteToSnoMask(letter, text);
48         letter = toupper(letter);
49         ServerInstance->PI->SendSNONotice(letter, text);
50 }
51
52 void SnomaskManager::WriteToSnoMask(char letter, const char* text, ...)
53 {
54         std::string textbuffer;
55         VAFORMAT(textbuffer, text, text);
56         this->WriteToSnoMask(letter, textbuffer);
57 }
58
59 void SnomaskManager::WriteGlobalSno(char letter, const char* text, ...)
60 {
61         std::string textbuffer;
62         VAFORMAT(textbuffer, text, text);
63         this->WriteGlobalSno(letter, textbuffer);
64 }
65
66 SnomaskManager::SnomaskManager()
67 {
68         EnableSnomask('c',"CONNECT");                   /* Local connect notices */
69         EnableSnomask('q',"QUIT");                      /* Local quit notices */
70         EnableSnomask('k',"KILL");                      /* Kill notices */
71         EnableSnomask('o',"OPER");                      /* Oper up/down notices */
72         EnableSnomask('a',"ANNOUNCEMENT");      /* formerly WriteOpers() - generic notices to all opers */
73         EnableSnomask('d',"DEBUG");                     /* Debug notices */
74         EnableSnomask('x',"XLINE");                     /* Xline notice (g/z/q/k/e) */
75         EnableSnomask('t',"STATS");                     /* Local or remote stats request */
76 }
77
78 bool SnomaskManager::IsSnomaskUsable(char ch) const
79 {
80         return ((isalpha(ch)) && (!masks[tolower(ch) - 'a'].Description.empty()));
81 }
82
83 Snomask::Snomask()
84         : Count(0)
85 {
86 }
87
88 void Snomask::SendMessage(const std::string& message, char letter)
89 {
90         if ((!ServerInstance->Config->NoSnoticeStack) && (message == LastMessage) && (letter == LastLetter))
91         {
92                 Count++;
93                 return;
94         }
95
96         this->Flush();
97
98         std::string desc = GetDescription(letter);
99         ModResult MOD_RESULT;
100         FIRST_MOD_RESULT(OnSendSnotice, MOD_RESULT, (letter, desc, message));
101         if (MOD_RESULT == MOD_RES_DENY)
102                 return;
103
104         Snomask::Send(letter, desc, message);
105         LastMessage = message;
106         LastLetter = letter;
107         Count++;
108 }
109
110 void Snomask::Flush()
111 {
112         if (Count > 1)
113         {
114                 std::string desc = GetDescription(LastLetter);
115                 std::string msg = "(last message repeated " + ConvToStr(Count) + " times)";
116
117                 FOREACH_MOD(OnSendSnotice, (LastLetter, desc, msg));
118                 Snomask::Send(LastLetter, desc, msg);
119         }
120
121         LastMessage.clear();
122         Count = 0;
123 }
124
125 void Snomask::Send(char letter, const std::string& desc, const std::string& msg)
126 {
127         std::string log = desc;
128         log.append(": ").append(msg);
129         ServerInstance->Logs->Log("snomask", LOG_DEFAULT, log);
130
131         std::string finalmsg = "*** ";
132         finalmsg.append(log);
133         /* Only opers can receive snotices, so we iterate the oper list */
134         const UserManager::OperList& opers = ServerInstance->Users->all_opers;
135         for (UserManager::OperList::const_iterator i = opers.begin(); i != opers.end(); ++i)
136         {
137                 User* user = *i;
138                 // IsNoticeMaskSet() returns false for opers who aren't +s, no need to check for it seperately
139                 if (IS_LOCAL(user) && user->IsNoticeMaskSet(letter))
140                         user->WriteNotice(finalmsg);
141         }
142 }
143
144 std::string Snomask::GetDescription(char letter) const
145 {
146         std::string ret;
147         if (isupper(letter))
148                 ret = "REMOTE";
149         if (!Description.empty())
150                 ret += Description;
151         else
152                 ret += std::string("SNO-") + (char)tolower(letter);
153         return ret;
154 }