1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5  *   Copyright (C) 2013-2016, 2018 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2013 Adam <Adam@anope.org>
8  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
9  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11  *   Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 2007, 2009-2010 Craig Edwards <brain@inspircd.org>
13  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
14  *
15  * This file is part of InspIRCd.  InspIRCd is free software: you can
16  * redistribute it and/or modify it under the terms of the GNU General Public
17  * License as published by the Free Software Foundation, version 2.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 
29 #pragma once
30 
31 #include "inspircd.h"
32 #include "event.h"
33 #include "modules/dns.h"
34 #include "modules/ssl.h"
35 #include "modules/stats.h"
36 #include "modules/ctctags.h"
37 #include "modules/server.h"
38 #include "servercommand.h"
39 #include "commands.h"
40 #include "protocolinterface.h"
41 #include "tags.h"
42 
43 /** An enumeration of all known protocol versions.
44  *
45  * If you introduce new protocol versions please document them here:
46  * https://docs.inspircd.org/spanningtree/changes
47  */
48 enum ProtocolVersion
49 {
50 	/** The linking protocol version introduced in InspIRCd v2.0. */
51 	PROTO_INSPIRCD_20 = 1202,
52 
53 	/** The linking protocol version introduced in InspIRCd v2.1 alpha 0. */
54 	PROTO_INSPIRCD_21_A0 = 1203,
55 
56 	/** The linking protocol version introduced in InspIRCd v2.1 beta 2. */
57 	PROTO_INSPIRCD_21_B2 = 1204,
58 
59 	/** The linking protocol version introduced in InspIRCd v3.0. */
60 	PROTO_INSPIRCD_30 = 1205,
61 
62 	/** The oldest version of the protocol that we support. */
63 	PROTO_OLDEST = PROTO_INSPIRCD_20,
64 
65 	/** The newest version of the protocol that we support. */
66 	PROTO_NEWEST = PROTO_INSPIRCD_30
67 };
68 
69 /** Forward declarations
70  */
71 class SpanningTreeUtilities;
72 class CacheRefreshTimer;
73 class TreeServer;
74 class Link;
75 class Autoconnect;
76 
77 /** This is the main class for the spanningtree module
78  */
79 class ModuleSpanningTree
80 	: public Module
81 	, public Away::EventListener
82 	, public Stats::EventListener
83 	, public CTCTags::EventListener
84 {
85 	/** Client to server commands, registered in the core
86 	 */
87 	CommandRConnect rconnect;
88 	CommandRSQuit rsquit;
89 	CommandMap map;
90 
91 	/** Server to server only commands, not registered in the core
92 	 */
93 	SpanningTreeCommands commands;
94 
95 	/** Next membership id assigned when a local user joins a channel
96 	 */
97 	Membership::Id currmembid;
98 
99 	/** The specialized ProtocolInterface that is assigned to ServerInstance->PI on load
100 	 */
101 	SpanningTreeProtocolInterface protocolinterface;
102 
103 	/** Event provider for our broadcast events. */
104 	Events::ModuleEventProvider broadcasteventprov;
105 
106 	/** Event provider for our link events. */
107 	Events::ModuleEventProvider linkeventprov;
108 
109 	/** Event provider for our message events. */
110 	Events::ModuleEventProvider messageeventprov;
111 
112 	/** Event provider for our sync events. */
113 	Events::ModuleEventProvider synceventprov;
114 
115 	/** API for accessing user SSL certificates. */
116 	UserCertificateAPI sslapi;
117 
118 	/** Tag for marking services pseudoclients. */
119 	ServiceTag servicetag;
120 
121  public:
122 	dynamic_reference<DNS::Manager> DNS;
123 
124 	/** Event provider for message tags. */
125 	ClientProtocol::MessageTagEvent tagevprov;
126 
127 	ServerCommandManager CmdManager;
128 
129 	/** Set to true if inside a spanningtree call, to prevent sending
130 	 * xlines and other things back to their source
131 	 */
132 	bool loopCall;
133 
134 	/** Constructor
135 	 */
136 	ModuleSpanningTree();
137 	void init() CXX11_OVERRIDE;
138 
139 	/** Shows /LINKS
140 	 */
141 	void ShowLinks(TreeServer* Current, User* user, int hops);
142 
143 	/** Handle LINKS command
144 	 */
145 	void HandleLinks(const CommandBase::Params& parameters, User* user);
146 
147 	/** Handle SQUIT
148 	 */
149 	ModResult HandleSquit(const CommandBase::Params& parameters, User* user);
150 
151 	/** Handle remote WHOIS
152 	 */
153 	ModResult HandleRemoteWhois(const CommandBase::Params& parameters, User* user);
154 
155 	/** Connect a server locally
156 	 */
157 	void ConnectServer(Link* x, Autoconnect* y = NULL);
158 
159 	/** Connect the next autoconnect server
160 	 */
161 	void ConnectServer(Autoconnect* y, bool on_timer);
162 
163 	/** Check if any servers are due to be autoconnected
164 	 */
165 	void AutoConnectServers(time_t curtime);
166 
167 	/** Check if any connecting servers should timeout
168 	 */
169 	void DoConnectTimeout(time_t curtime);
170 
171 	/** Handle remote VERSION
172 	 */
173 	ModResult HandleVersion(const CommandBase::Params& parameters, User* user);
174 
175 	/** Handle CONNECT
176 	 */
177 	ModResult HandleConnect(const CommandBase::Params& parameters, User* user);
178 
179 	/** Retrieves the event provider for broadcast events. */
GetBroadcastEventProvider()180 	const Events::ModuleEventProvider& GetBroadcastEventProvider() const { return broadcasteventprov; }
181 
182 	/** Retrieves the event provider for link events. */
GetLinkEventProvider()183 	const Events::ModuleEventProvider& GetLinkEventProvider() const { return linkeventprov; }
184 
185 	/** Retrieves the event provider for message events. */
GetMessageEventProvider()186 	const Events::ModuleEventProvider& GetMessageEventProvider() const { return messageeventprov; }
187 
188 	/** Retrieves the event provider for sync events. */
GetSyncEventProvider()189 	const Events::ModuleEventProvider& GetSyncEventProvider() const { return synceventprov; }
190 
191 	/**
192 	 ** *** MODULE EVENTS ***
193 	 **/
194 
195 	ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE;
196 	void OnPostCommand(Command*, const CommandBase::Params& parameters, LocalUser* user, CmdResult result, bool loop) CXX11_OVERRIDE;
197 	void OnUserConnect(LocalUser* source) CXX11_OVERRIDE;
198 	void OnUserInvite(User* source, User* dest, Channel* channel, time_t timeout, unsigned int notifyrank, CUList& notifyexcepts) CXX11_OVERRIDE;
199 	ModResult OnPreTopicChange(User* user, Channel* chan, const std::string& topic) CXX11_OVERRIDE;
200 	void OnPostTopicChange(User* user, Channel* chan, const std::string &topic) CXX11_OVERRIDE;
201 	void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE;
202 	void OnUserPostTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details) CXX11_OVERRIDE;
203 	void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE;
204 	void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE;
205 	void OnChangeHost(User* user, const std::string &newhost) CXX11_OVERRIDE;
206 	void OnChangeRealName(User* user, const std::string& real) CXX11_OVERRIDE;
207 	void OnChangeIdent(User* user, const std::string &ident) CXX11_OVERRIDE;
208 	void OnUserPart(Membership* memb, std::string &partmessage, CUList& excepts) CXX11_OVERRIDE;
209 	void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message) CXX11_OVERRIDE;
210 	void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE;
211 	void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts) CXX11_OVERRIDE;
212 	void OnPreRehash(User* user, const std::string &parameter) CXX11_OVERRIDE;
213 	void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE;
214 	void OnOper(User* user, const std::string &opertype) CXX11_OVERRIDE;
215 	void OnAddLine(User *u, XLine *x) CXX11_OVERRIDE;
216 	void OnDelLine(User *u, XLine *x) CXX11_OVERRIDE;
217 	ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE;
218 	void OnUserAway(User* user) CXX11_OVERRIDE;
219 	void OnUserBack(User* user) CXX11_OVERRIDE;
220 	void OnLoadModule(Module* mod) CXX11_OVERRIDE;
221 	void OnUnloadModule(Module* mod) CXX11_OVERRIDE;
222 	ModResult OnAcceptConnection(int newsock, ListenSocket* from, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) CXX11_OVERRIDE;
223 	void OnMode(User* source, User* u, Channel* c, const Modes::ChangeList& modes, ModeParser::ModeProcessFlag processflags) CXX11_OVERRIDE;
224 	void OnShutdown(const std::string& reason) CXX11_OVERRIDE;
225 	CullResult cull() CXX11_OVERRIDE;
226 	~ModuleSpanningTree();
227 	Version GetVersion() CXX11_OVERRIDE;
228 	void Prioritize() CXX11_OVERRIDE;
229 };
230