1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
30 #include "corealiasmanager.h"
31 #include "corehighlightrulemanager.h"
32 #include "coreignorelistmanager.h"
35 #include "metricsserver.h"
40 class CoreBacklogManager;
41 class CoreBufferSyncer;
42 class CoreBufferViewManager;
45 class CoreIrcListHelper;
47 class CoreNetworkConfig;
48 class CoreSessionEventProcessor;
49 class CoreTransferManager;
52 class EventStringifier;
63 class CoreSession : public QObject
68 CoreSession(UserId, bool restoreState, bool strictIdentEnabled, QObject* parent = nullptr);
70 QList<BufferInfo> buffers() const;
71 inline UserId user() const { return _user; }
72 CoreNetwork* network(NetworkId) const;
73 CoreIdentity* identity(IdentityId) const;
76 * Returns the optionally strict-compliant ident for the given user identity
78 * If strict mode is enabled, this will return the user's Quassel username for any identity,
79 * otherwise this will return the given identity's ident, whatever it may be.
81 * @return The user's ident, compliant with strict mode (when enabled)
83 const QString strictCompliantIdent(const CoreIdentity* identity);
85 inline CoreNetworkConfig* networkConfig() const { return _networkConfig; }
87 Protocol::SessionState sessionState() const;
89 inline SignalProxy* signalProxy() const { return _signalProxy; }
91 const AliasManager& aliasManager() const { return _aliasManager; }
92 AliasManager& aliasManager() { return _aliasManager; }
94 inline EventManager* eventManager() const { return _eventManager; }
95 inline EventStringifier* eventStringifier() const { return _eventStringifier; }
96 inline CoreSessionEventProcessor* sessionEventProcessor() const { return _sessionEventProcessor; }
97 inline CtcpParser* ctcpParser() const { return _ctcpParser; }
98 inline IrcParser* ircParser() const { return _ircParser; }
100 inline CoreIrcListHelper* ircListHelper() const { return _ircListHelper; }
102 inline CoreIgnoreListManager* ignoreListManager() { return &_ignoreListManager; }
103 inline HighlightRuleManager* highlightRuleManager() { return &_highlightRuleManager; }
104 inline CoreTransferManager* transferManager() const { return _transferManager; }
105 inline CoreDccConfig* dccConfig() const { return _dccConfig; }
107 // void attachNetworkConnection(NetworkConnection *conn);
109 //! Return necessary data for restoring the session after restarting the core
110 void restoreSessionState();
113 void addClient(RemotePeer* peer);
114 void addClient(InternalPeer* peer);
117 * Shuts down the session and deletes itself afterwards.
121 void msgFromClient(BufferInfo, QString message);
123 //! Create an identity and propagate the changes to the clients.
124 /** \param identity The identity to be created.
126 void createIdentity(const Identity& identity, const QVariantMap& additional);
127 void createIdentity(const CoreIdentity& identity);
129 //! Remove identity and propagate that fact to the clients.
130 /** \param identity The identity to be removed.
132 void removeIdentity(IdentityId identity);
134 //! Create a network and propagate the changes to the clients.
135 /** \param info The network's settings.
137 void createNetwork(const NetworkInfo& info, const QStringList& persistentChannels = QStringList());
139 //! Remove network and propagate that fact to the clients.
140 /** \param network The id of the network to be removed.
142 void removeNetwork(NetworkId network);
144 //! Rename a Buffer for a given network
145 /* \param networkId The id of the network the buffer belongs to
146 * \param newName The new name of the buffer
147 * \param oldName The old name of the buffer
149 void renameBuffer(const NetworkId& networkId, const QString& newName, const QString& oldName);
151 void changePassword(PeerPtr peer, const QString& userName, const QString& oldPassword, const QString& newPassword);
153 void kickClient(int peerId);
155 QHash<QString, QString> persistentChannels(NetworkId) const;
157 QHash<QString, QByteArray> bufferCiphers(NetworkId id) const;
158 void setBufferCipher(NetworkId id, const QString& bufferName, const QByteArray& cipher) const;
161 * Marks us away (or unaway) on all networks
163 * @param[in] msg Away message, or blank to set unaway
164 * @param[in] skipFormatting If true, skip timestamp formatting codes (e.g. if already done)
166 void globalAway(const QString& msg = QString(), bool skipFormatting = false);
170 void sessionStateReceived(const Protocol::SessionState& sessionState);
172 // void msgFromGui(uint netid, QString buf, QString message);
173 void displayMsg(Message message);
174 void displayStatusMsg(QString, QString);
176 void scriptResult(QString result);
178 //! Identity has been created.
179 /** This signal is propagated to the clients to tell them that the given identity has been created.
180 * \param identity The new identity.
182 void identityCreated(const Identity& identity);
184 //! Identity has been removed.
185 /** This signal is propagated to the clients to inform them about the removal of the given identity.
186 * \param identity The identity that has been removed.
188 void identityRemoved(IdentityId identity);
190 void networkCreated(NetworkId);
191 void networkRemoved(NetworkId);
192 void networkDisconnected(NetworkId);
194 void passwordChanged(PeerPtr peer, bool success);
196 void disconnectFromCore();
199 void customEvent(QEvent* event) override;
202 void removeClient(Peer* peer);
204 void recvStatusMsgFromServer(QString msg);
205 void recvMessageFromServer(RawMessage msg);
207 void destroyNetwork(NetworkId);
209 void scriptRequest(QString script);
211 void clientsConnected();
212 void clientsDisconnected();
214 void updateIdentityBySender();
216 void saveSessionState() const;
218 void onNetworkDisconnected(NetworkId networkId);
221 void processMessages();
224 void initScriptEngine();
226 /// Hook for converting events to the old displayMsg() handlers
227 Q_INVOKABLE void processMessageEvent(MessageEvent* event);
231 /// Whether or not strict ident mode is enabled, locking users' idents to Quassel username
232 bool _strictIdentEnabled;
234 SignalProxy* _signalProxy;
235 CoreAliasManager _aliasManager;
237 QHash<IdentityId, CoreIdentity*> _identities;
238 QHash<NetworkId, CoreNetwork*> _networks;
239 QSet<NetworkId> _networksPendingDisconnect;
241 CoreBufferSyncer* _bufferSyncer;
242 CoreBacklogManager* _backlogManager;
243 CoreBufferViewManager* _bufferViewManager;
244 CoreDccConfig* _dccConfig;
245 CoreIrcListHelper* _ircListHelper;
246 CoreNetworkConfig* _networkConfig;
248 CoreTransferManager* _transferManager;
250 EventManager* _eventManager;
251 EventStringifier* _eventStringifier; // should eventually move into client
252 CoreSessionEventProcessor* _sessionEventProcessor;
253 CtcpParser* _ctcpParser;
254 IrcParser* _ircParser;
256 QScriptEngine* scriptEngine;
259 * This method obtains the prefixes of the message's sender within a channel, by looking up their channelmodes, and
260 * processing them to prefixes based on the network's settings.
261 * @param sender The hostmask of the sender
262 * @param bufferInfo The BufferInfo object of the buffer
264 QString senderPrefixes(const QString& sender, const BufferInfo& bufferInfo) const;
267 * This method obtains the realname of the message's sender.
268 * @param sender The hostmask of the sender
269 * @param networkId The network the user is on
271 QString realName(const QString& sender, NetworkId networkId) const;
274 * This method obtains the avatar of the message's sender.
275 * @param sender The hostmask of the sender
276 * @param networkId The network the user is on
278 QString avatarUrl(const QString& sender, NetworkId networkId) const;
279 QList<RawMessage> _messageQueue;
280 bool _processMessages;
281 CoreIgnoreListManager _ignoreListManager;
282 CoreHighlightRuleManager _highlightRuleManager;
283 MetricsServer* _metricsServer{nullptr};
286 struct NetworkInternalMessage
289 BufferInfo::Type bufferType;
293 Message::Flags flags;
294 NetworkInternalMessage(Message::Type type,
295 BufferInfo::Type bufferType,
299 Message::Flags flags = Message::None)
301 , bufferType(bufferType)
302 , target(std::move(target))
303 , text(std::move(text))
304 , sender(std::move(sender))
314 BufferInfo::Type bufferType;
318 Message::Flags flags;
320 RawMessage(QDateTime timestamp,
323 BufferInfo::Type bufferType,
327 Message::Flags flags)
328 : timestamp(std::move(timestamp))
329 , networkId(networkId)
331 , bufferType(bufferType)
332 , target(std::move(target))
333 , text(std::move(text))
334 , sender(std::move(sender))
338 RawMessage(NetworkId networkId,
339 const NetworkInternalMessage& msg)
340 : timestamp(QDateTime::currentDateTimeUtc())
341 , networkId(networkId)
343 , bufferType(msg.bufferType)