3e08eacce252b9e8512a6e99c5810fb63948b4c8
[quassel.git] / src / core / coresession.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #pragma once
22
23 #include <utility>
24
25 #include <QHash>
26 #include <QSet>
27 #include <QString>
28 #include <QVariant>
29
30 #include "corealiasmanager.h"
31 #include "corehighlightrulemanager.h"
32 #include "coreignorelistmanager.h"
33 #include "coreinfo.h"
34 #include "message.h"
35 #include "metricsserver.h"
36 #include "peer.h"
37 #include "protocol.h"
38 #include "storage.h"
39
40 class CoreBacklogManager;
41 class CoreBufferSyncer;
42 class CoreBufferViewManager;
43 class CoreDccConfig;
44 class CoreIdentity;
45 class CoreIrcListHelper;
46 class CoreNetwork;
47 class CoreNetworkConfig;
48 class CoreSessionEventProcessor;
49 class CoreTransferManager;
50 class CtcpParser;
51 class EventManager;
52 class EventStringifier;
53 class InternalPeer;
54 class IrcParser;
55 class MessageEvent;
56 class RemotePeer;
57 class SignalProxy;
58
59 struct NetworkInfo;
60
61 class CoreSession : public QObject
62 {
63     Q_OBJECT
64
65 public:
66     CoreSession(UserId, bool restoreState, bool strictIdentEnabled, QObject* parent = nullptr);
67
68     QList<BufferInfo> buffers() const;
69     inline UserId user() const { return _user; }
70     CoreNetwork* network(NetworkId) const;
71     CoreIdentity* identity(IdentityId) const;
72
73     /**
74      * Returns the optionally strict-compliant ident for the given user identity
75      *
76      * If strict mode is enabled, this will return the user's Quassel username for any identity,
77      * otherwise this will return the given identity's ident, whatever it may be.
78      *
79      * @return The user's ident, compliant with strict mode (when enabled)
80      */
81     const QString strictCompliantIdent(const CoreIdentity* identity);
82
83     inline CoreNetworkConfig* networkConfig() const { return _networkConfig; }
84
85     Protocol::SessionState sessionState() const;
86
87     inline SignalProxy* signalProxy() const { return _signalProxy; }
88
89     const AliasManager& aliasManager() const { return _aliasManager; }
90     AliasManager& aliasManager() { return _aliasManager; }
91
92     inline EventManager* eventManager() const { return _eventManager; }
93     inline EventStringifier* eventStringifier() const { return _eventStringifier; }
94     inline CoreSessionEventProcessor* sessionEventProcessor() const { return _sessionEventProcessor; }
95     inline CtcpParser* ctcpParser() const { return _ctcpParser; }
96     inline IrcParser* ircParser() const { return _ircParser; }
97
98     inline CoreIrcListHelper* ircListHelper() const { return _ircListHelper; }
99
100     inline CoreIgnoreListManager* ignoreListManager() { return &_ignoreListManager; }
101     inline HighlightRuleManager* highlightRuleManager() { return &_highlightRuleManager; }
102     inline CoreTransferManager* transferManager() const { return _transferManager; }
103     inline CoreDccConfig* dccConfig() const { return _dccConfig; }
104
105     //   void attachNetworkConnection(NetworkConnection *conn);
106
107     //! Return necessary data for restoring the session after restarting the core
108     void restoreSessionState();
109
110 public slots:
111     void addClient(RemotePeer* peer);
112     void addClient(InternalPeer* peer);
113
114     /**
115      * Shuts down the session and deletes itself afterwards.
116      */
117     void shutdown();
118
119     void msgFromClient(BufferInfo, QString message);
120
121     //! Create an identity and propagate the changes to the clients.
122     /** \param identity The identity to be created.
123      */
124     void createIdentity(const Identity& identity, const QVariantMap& additional);
125     void createIdentity(const CoreIdentity& identity);
126
127     //! Remove identity and propagate that fact to the clients.
128     /** \param identity The identity to be removed.
129      */
130     void removeIdentity(IdentityId identity);
131
132     //! Create a network and propagate the changes to the clients.
133     /** \param info The network's settings.
134      */
135     void createNetwork(const NetworkInfo& info, const QStringList& persistentChannels = QStringList());
136
137     //! Remove network and propagate that fact to the clients.
138     /** \param network The id of the network to be removed.
139      */
140     void removeNetwork(NetworkId network);
141
142     //! Rename a Buffer for a given network
143     /* \param networkId The id of the network the buffer belongs to
144      * \param newName   The new name of the buffer
145      * \param oldName   The old name of the buffer
146      */
147     void renameBuffer(const NetworkId& networkId, const QString& newName, const QString& oldName);
148
149     void changePassword(PeerPtr peer, const QString& userName, const QString& oldPassword, const QString& newPassword);
150
151     void kickClient(int peerId);
152
153     QHash<QString, QString> persistentChannels(NetworkId) const;
154
155     QHash<QString, QByteArray> bufferCiphers(NetworkId id) const;
156     void setBufferCipher(NetworkId id, const QString& bufferName, const QByteArray& cipher) const;
157
158     /**
159      * Marks us away (or unaway) on all networks
160      *
161      * @param[in] msg             Away message, or blank to set unaway
162      * @param[in] skipFormatting  If true, skip timestamp formatting codes (e.g. if already done)
163      */
164     void globalAway(const QString& msg = QString(), bool skipFormatting = false);
165
166 signals:
167     void initialized();
168     void sessionStateReceived(const Protocol::SessionState& sessionState);
169
170     // void msgFromGui(uint netid, QString buf, QString message);
171     void displayMsg(Message message);
172     void displayStatusMsg(QString, QString);
173
174     //! Identity has been created.
175     /** This signal is propagated to the clients to tell them that the given identity has been created.
176      *  \param identity The new identity.
177      */
178     void identityCreated(const Identity& identity);
179
180     //! Identity has been removed.
181     /** This signal is propagated to the clients to inform them about the removal of the given identity.
182      *  \param identity The identity that has been removed.
183      */
184     void identityRemoved(IdentityId identity);
185
186     void networkCreated(NetworkId);
187     void networkRemoved(NetworkId);
188     void networkDisconnected(NetworkId);
189
190     void passwordChanged(PeerPtr peer, bool success);
191
192     void disconnectFromCore();
193
194 protected:
195     void customEvent(QEvent* event) override;
196
197 private slots:
198     void removeClient(Peer* peer);
199
200     void recvStatusMsgFromServer(QString msg);
201     void recvMessageFromServer(RawMessage msg);
202
203     void destroyNetwork(NetworkId);
204
205     void clientsConnected();
206     void clientsDisconnected();
207
208     void updateIdentityBySender();
209
210     void saveSessionState() const;
211
212     void onNetworkDisconnected(NetworkId networkId);
213
214 private:
215     void processMessages();
216
217     void loadSettings();
218
219     /// Hook for converting events to the old displayMsg() handlers
220     Q_INVOKABLE void processMessageEvent(MessageEvent* event);
221
222     UserId _user;
223
224     /// Whether or not strict ident mode is enabled, locking users' idents to Quassel username
225     bool _strictIdentEnabled;
226
227     SignalProxy* _signalProxy;
228     CoreAliasManager _aliasManager;
229
230     QHash<IdentityId, CoreIdentity*> _identities;
231     QHash<NetworkId, CoreNetwork*> _networks;
232     QSet<NetworkId> _networksPendingDisconnect;
233
234     CoreBufferSyncer* _bufferSyncer;
235     CoreBacklogManager* _backlogManager;
236     CoreBufferViewManager* _bufferViewManager;
237     CoreDccConfig* _dccConfig;
238     CoreIrcListHelper* _ircListHelper;
239     CoreNetworkConfig* _networkConfig;
240     CoreInfo* _coreInfo;
241     CoreTransferManager* _transferManager;
242
243     EventManager* _eventManager;
244     EventStringifier* _eventStringifier;  // should eventually move into client
245     CoreSessionEventProcessor* _sessionEventProcessor;
246     CtcpParser* _ctcpParser;
247     IrcParser* _ircParser;
248
249     /**
250      * This method obtains the prefixes of the message's sender within a channel, by looking up their channelmodes, and
251      * processing them to prefixes based on the network's settings.
252      * @param sender The hostmask of the sender
253      * @param bufferInfo The BufferInfo object of the buffer
254      */
255     QString senderPrefixes(const QString& sender, const BufferInfo& bufferInfo) const;
256
257     /**
258      * This method obtains the realname of the message's sender.
259      * @param sender The hostmask of the sender
260      * @param networkId The network the user is on
261      */
262     QString realName(const QString& sender, NetworkId networkId) const;
263
264     /**
265      * This method obtains the avatar of the message's sender.
266      * @param sender The hostmask of the sender
267      * @param networkId The network the user is on
268      */
269     QString avatarUrl(const QString& sender, NetworkId networkId) const;
270     QList<RawMessage> _messageQueue;
271     bool _processMessages;
272     CoreIgnoreListManager _ignoreListManager;
273     CoreHighlightRuleManager _highlightRuleManager;
274     MetricsServer* _metricsServer{nullptr};
275 };
276
277 struct NetworkInternalMessage
278 {
279     Message::Type type;
280     BufferInfo::Type bufferType;
281     QString target;
282     QString text;
283     QString sender;
284     Message::Flags flags;
285     NetworkInternalMessage(Message::Type type,
286                            BufferInfo::Type bufferType,
287                            QString target,
288                            QString text,
289                            QString sender = "",
290                            Message::Flags flags = Message::None)
291         : type(type)
292         , bufferType(bufferType)
293         , target(std::move(target))
294         , text(std::move(text))
295         , sender(std::move(sender))
296         , flags(flags)
297     {}
298 };
299
300 struct RawMessage
301 {
302     QDateTime timestamp;
303     NetworkId networkId;
304     Message::Type type;
305     BufferInfo::Type bufferType;
306     QString target;
307     QString text;
308     QString sender;
309     Message::Flags flags;
310
311     RawMessage(QDateTime timestamp,
312                NetworkId networkId,
313                Message::Type type,
314                BufferInfo::Type bufferType,
315                QString target,
316                QString text,
317                QString sender,
318                Message::Flags flags)
319         : timestamp(std::move(timestamp))
320         , networkId(networkId)
321         , type(type)
322         , bufferType(bufferType)
323         , target(std::move(target))
324         , text(std::move(text))
325         , sender(std::move(sender))
326         , flags(flags)
327     {}
328
329     RawMessage(NetworkId networkId,
330                const NetworkInternalMessage& msg)
331         : timestamp(QDateTime::currentDateTimeUtc())
332         , networkId(networkId)
333         , type(msg.type)
334         , bufferType(msg.bufferType)
335         , target(msg.target)
336         , text(msg.text)
337         , sender(msg.sender)
338         , flags(msg.flags)
339     {}
340 };