1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5  *   Copyright (C) 2018 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
8  *   Copyright (C) 2013 Adam <Adam@anope.org>
9  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
10  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
11  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
12  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
13  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
14  *   Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
15  *
16  * This file is part of InspIRCd.  InspIRCd is free software: you can
17  * redistribute it and/or modify it under the terms of the GNU General Public
18  * License as published by the Free Software Foundation, version 2.
19  *
20  * This program is distributed in the hope that it will be useful, but WITHOUT
21  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  */
28 
29 
30 #include "inspircd.h"
31 
FlushSnotices()32 void SnomaskManager::FlushSnotices()
33 {
34 	for (int i=0; i < 26; i++)
35 		masks[i].Flush();
36 }
37 
EnableSnomask(char letter,const std::string & type)38 void SnomaskManager::EnableSnomask(char letter, const std::string &type)
39 {
40 	if (letter >= 'a' && letter <= 'z')
41 		masks[letter - 'a'].Description = type;
42 }
43 
WriteToSnoMask(char letter,const std::string & text)44 void SnomaskManager::WriteToSnoMask(char letter, const std::string &text)
45 {
46 	if (letter >= 'a' && letter <= 'z')
47 		masks[letter - 'a'].SendMessage(text, letter);
48 	if (letter >= 'A' && letter <= 'Z')
49 		masks[letter - 'A'].SendMessage(text, letter);
50 }
51 
WriteGlobalSno(char letter,const std::string & text)52 void SnomaskManager::WriteGlobalSno(char letter, const std::string& text)
53 {
54 	WriteToSnoMask(letter, text);
55 	letter = toupper(letter);
56 	ServerInstance->PI->SendSNONotice(letter, text);
57 }
58 
WriteToSnoMask(char letter,const char * text,...)59 void SnomaskManager::WriteToSnoMask(char letter, const char* text, ...)
60 {
61 	std::string textbuffer;
62 	VAFORMAT(textbuffer, text, text);
63 	this->WriteToSnoMask(letter, textbuffer);
64 }
65 
WriteGlobalSno(char letter,const char * text,...)66 void SnomaskManager::WriteGlobalSno(char letter, const char* text, ...)
67 {
68 	std::string textbuffer;
69 	VAFORMAT(textbuffer, text, text);
70 	this->WriteGlobalSno(letter, textbuffer);
71 }
72 
SnomaskManager()73 SnomaskManager::SnomaskManager()
74 {
75 	EnableSnomask('c',"CONNECT");			/* Local connect notices */
76 	EnableSnomask('q',"QUIT");			/* Local quit notices */
77 	EnableSnomask('k',"KILL");			/* Kill notices */
78 	EnableSnomask('o',"OPER");			/* Oper up/down notices */
79 	EnableSnomask('a',"ANNOUNCEMENT");		/* formerly WriteOpers() - generic notices to all opers */
80 	EnableSnomask('x',"XLINE");			/* X-line notices (G/Z/Q/K/E/R/SHUN/CBan) */
81 	EnableSnomask('t',"STATS");			/* Local or remote stats request */
82 }
83 
IsSnomaskUsable(char ch) const84 bool SnomaskManager::IsSnomaskUsable(char ch) const
85 {
86 	return ((isalpha(ch)) && (!masks[tolower(ch) - 'a'].Description.empty()));
87 }
88 
Snomask()89 Snomask::Snomask()
90 	: Count(0)
91 {
92 }
93 
SendMessage(const std::string & message,char letter)94 void Snomask::SendMessage(const std::string& message, char letter)
95 {
96 	if ((!ServerInstance->Config->NoSnoticeStack) && (message == LastMessage) && (letter == LastLetter))
97 	{
98 		Count++;
99 		return;
100 	}
101 
102 	this->Flush();
103 
104 	std::string desc = GetDescription(letter);
105 	ModResult MOD_RESULT;
106 	FIRST_MOD_RESULT(OnSendSnotice, MOD_RESULT, (letter, desc, message));
107 	if (MOD_RESULT == MOD_RES_DENY)
108 		return;
109 
110 	Snomask::Send(letter, desc, message);
111 	LastMessage = message;
112 	LastLetter = letter;
113 	Count++;
114 }
115 
Flush()116 void Snomask::Flush()
117 {
118 	if (Count > 1)
119 	{
120 		std::string desc = GetDescription(LastLetter);
121 		std::string msg = "(last message repeated " + ConvToStr(Count) + " times)";
122 
123 		FOREACH_MOD(OnSendSnotice, (LastLetter, desc, msg));
124 		Snomask::Send(LastLetter, desc, msg);
125 	}
126 
127 	LastMessage.clear();
128 	Count = 0;
129 }
130 
Send(char letter,const std::string & desc,const std::string & msg)131 void Snomask::Send(char letter, const std::string& desc, const std::string& msg)
132 {
133 	ServerInstance->Logs->Log(desc, LOG_DEFAULT, msg);
134 	const std::string finalmsg = InspIRCd::Format("*** %s: %s", desc.c_str(), msg.c_str());
135 
136 	/* Only opers can receive snotices, so we iterate the oper list */
137 	const UserManager::OperList& opers = ServerInstance->Users->all_opers;
138 	for (UserManager::OperList::const_iterator i = opers.begin(); i != opers.end(); ++i)
139 	{
140 		User* user = *i;
141 		// IsNoticeMaskSet() returns false for opers who aren't +s, no need to check for it separately
142 		if (IS_LOCAL(user) && user->IsNoticeMaskSet(letter))
143 			user->WriteNotice(finalmsg);
144 	}
145 }
146 
GetDescription(char letter) const147 std::string Snomask::GetDescription(char letter) const
148 {
149 	std::string ret;
150 	if (isupper(letter))
151 		ret = "REMOTE";
152 	if (!Description.empty())
153 		ret += Description;
154 	else
155 		ret += std::string("SNO-") + (char)tolower(letter);
156 	return ret;
157 }
158