1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2017-2019, 2021 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2007-2008, 2010 Craig Edwards <brain@inspircd.org>
10  *   Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 
26 #pragma once
27 
28 #include "inspircd.h"
29 #include "cachetimer.h"
30 
31 class TreeServer;
32 class TreeSocket;
33 class Link;
34 class Autoconnect;
35 class ModuleSpanningTree;
36 class SpanningTreeUtilities;
37 class CmdBuilder;
38 
39 extern SpanningTreeUtilities* Utils;
40 
41 /** Associative container type, mapping server names/ids to TreeServers
42  */
43 typedef TR1NS::unordered_map<std::string, TreeServer*, irc::insensitive, irc::StrHashComp> server_hash;
44 
45 /** Contains helper functions and variables for this module,
46  * and keeps them out of the global namespace
47  */
48 class SpanningTreeUtilities : public classbase
49 {
50 	CacheRefreshTimer RefreshTimer;
51 
52  public:
53 	typedef std::set<TreeSocket*> TreeSocketSet;
54 	typedef std::map<TreeSocket*, std::pair<std::string, unsigned int> > TimeoutList;
55 
56 	/** Creator module
57 	 */
58 	ModuleSpanningTree* Creator;
59 
60 	/** Flatten links and /MAP for non-opers
61 	 */
62 	bool FlatLinks;
63 
64 	/** True if we're going to hide netsplits as *.net *.split for non-opers
65 	 */
66 	bool HideSplits;
67 
68 	/** Hide U-Lined servers in /MAP and /LINKS
69 	 */
70 	bool HideULines;
71 	/** Announce TS changes to channels on merge
72 	 */
73 	bool AnnounceTSChange;
74 
75 	/** Allow modules marked as VF_OPTCOMMON to be mismatched when linking
76 	 */
77 	bool AllowOptCommon;
78 
79 	/** Make snomasks +CQ quiet during bursts and splits
80 	 */
81 	bool quiet_bursts;
82 
83 	/* Number of seconds that a server can go without ping
84 	 * before opers are warned of high latency.
85 	 */
86 	unsigned int PingWarnTime;
87 	/** This variable represents the root of the server tree
88 	 */
89 	TreeServer *TreeRoot;
90 	/** IPs allowed to link to us
91 	 */
92 	std::vector<std::string> ValidIPs;
93 	/** Hash of currently connected servers by name
94 	 */
95 	server_hash serverlist;
96 	/** Hash of currently known server ids
97 	 */
98 	server_hash sidlist;
99 	/** List of all outgoing sockets and their timeouts
100 	 */
101 	TimeoutList timeoutlist;
102 	/** Holds the data from the <link> tags in the conf
103 	 */
104 	std::vector<reference<Link> > LinkBlocks;
105 	/** Holds the data from the <autoconnect> tags in the conf
106 	 */
107 	std::vector<reference<Autoconnect> > AutoconnectBlocks;
108 
109 	/** Ping frequency of server to server links
110 	 */
111 	unsigned int PingFreq;
112 
113 	/** Initialise utility class
114 	 */
115 	SpanningTreeUtilities(ModuleSpanningTree* Creator);
116 
117 	/** Prepare for class destruction
118 	 */
119 	CullResult cull() CXX11_OVERRIDE;
120 
121 	/** Destroy class and free listeners etc
122 	 */
123 	~SpanningTreeUtilities();
124 
125 	void RouteCommand(TreeServer* origin, CommandBase* cmd, const CommandBase::Params& parameters, User* user);
126 
127 	/** Send a message from this server to one other local or remote
128 	 */
129 	void DoOneToOne(const CmdBuilder& params, Server* target);
130 
131 	/** Send a message from this server to all but one other, local or remote
132 	 */
133 	void DoOneToAllButSender(const CmdBuilder& params, TreeServer* omit);
134 
135 	/** Send a message from this server to all others
136 	 */
137 	void DoOneToMany(const CmdBuilder& params);
138 
139 	/** Read the spanningtree module's tags from the config file
140 	 */
141 	void ReadConfiguration();
142 
143 	/** Handle nick collision
144 	 */
145 	bool DoCollision(User* u, TreeServer* server, time_t remotets, const std::string& remoteident, const std::string& remoteip, const std::string& remoteuid, const char* collidecmd);
146 
147 	/** Compile a list of servers which contain members of channel c
148 	 */
149 	void GetListOfServersForChannel(Channel* c, TreeSocketSet& list, char status, const CUList& exempt_list);
150 
151 	/** Find a server by name or SID
152 	 */
153 	TreeServer* FindServer(const std::string &ServerName);
154 
155 	/** Find server by SID
156 	 */
157 	TreeServer* FindServerID(const std::string &id);
158 
159 	/** Find a server based on a target string.
160 	 * @param target Target string where a command should be routed to. May be a server name, a sid, a nickname or a uuid.
161 	 */
162 	TreeServer* FindRouteTarget(const std::string& target);
163 
164 	/** Find a server by glob mask
165 	 */
166 	TreeServer* FindServerMask(const std::string &ServerName);
167 
168 	/** Find a link tag from a server name
169 	 */
170 	Link* FindLink(const std::string& name);
171 
172 	/** Refresh the IP cache used for allowing inbound connections
173 	 */
174 	void RefreshIPCache();
175 
176 	/** Sends a PRIVMSG or a NOTICE to a channel obeying an exempt list and an optional prefix
177 	 */
178 	void SendChannelMessage(User* source, Channel* target, const std::string& text, char status, const ClientProtocol::TagMap& tags, const CUList& exempt_list, const char* message_type, TreeSocket* omit = NULL);
179 
180 	/** Send the channel list mode limits to either the specified server or all servers if NULL. */
181 	void SendListLimits(Channel* chan, TreeSocket* sock = NULL);
182 };
183 
DoOneToMany(const CmdBuilder & params)184 inline void SpanningTreeUtilities::DoOneToMany(const CmdBuilder& params)
185 {
186 	DoOneToAllButSender(params, NULL);
187 }
188