modernize: Prefer default member init over ctor init
[quassel.git] / src / core / coresession.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 <QHash>
24 #include <QSet>
25 #include <QString>
26 #include <QVariant>
27 #include <utility>
28
29 #include "coreinfo.h"
30 #include "corealiasmanager.h"
31 #include "corehighlightrulemanager.h"
32 #include "coreignorelistmanager.h"
33 #include "peer.h"
34 #include "protocol.h"
35 #include "message.h"
36 #include "storage.h"
37
38 class CoreBacklogManager;
39 class CoreBufferSyncer;
40 class CoreBufferViewManager;
41 class CoreDccConfig;
42 class CoreIdentity;
43 class CoreIrcListHelper;
44 class CoreNetwork;
45 class CoreNetworkConfig;
46 class CoreSessionEventProcessor;
47 class CoreTransferManager;
48 class CtcpParser;
49 class EventManager;
50 class EventStringifier;
51 class InternalPeer;
52 class IrcParser;
53 class MessageEvent;
54 class NetworkConnection;
55 class RemotePeer;
56 class SignalProxy;
57
58 struct NetworkInfo;
59
60 class QScriptEngine;
61
62 class CoreSession : public QObject
63 {
64     Q_OBJECT
65
66 public:
67     CoreSession(UserId, bool restoreState, bool strictIdentEnabled, QObject *parent = nullptr);
68
69     QList<BufferInfo> buffers() const;
70     inline UserId user() const { return _user; }
71     CoreNetwork *network(NetworkId) const;
72     CoreIdentity *identity(IdentityId) const;
73
74     /**
75      * Returns the optionally strict-compliant ident for the given user identity
76      *
77      * If strict mode is enabled, this will return the user's Quassel username for any identity,
78      * otherwise this will return the given identity's ident, whatever it may be.
79      *
80      * @return The user's ident, compliant with strict mode (when enabled)
81      */
82     const QString strictCompliantIdent(const CoreIdentity *identity);
83
84     inline CoreNetworkConfig *networkConfig() const { return _networkConfig; }
85     NetworkConnection *networkConnection(NetworkId) const;
86
87     Protocol::SessionState sessionState() const;
88
89     inline SignalProxy *signalProxy() const { return _signalProxy; }
90
91     const AliasManager &aliasManager() const { return _aliasManager; }
92     AliasManager &aliasManager() { return _aliasManager; }
93
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; }
99
100     inline CoreIrcListHelper *ircListHelper() const { return _ircListHelper; }
101
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; }
106
107 //   void attachNetworkConnection(NetworkConnection *conn);
108
109     //! Return necessary data for restoring the session after restarting the core
110     void restoreSessionState();
111
112 public slots:
113     void addClient(RemotePeer *peer);
114     void addClient(InternalPeer *peer);
115
116     /**
117      * Shuts down the session and deletes itself afterwards.
118      */
119     void shutdown();
120
121     void msgFromClient(BufferInfo, QString message);
122
123     //! Create an identity and propagate the changes to the clients.
124     /** \param identity The identity to be created.
125      */
126     void createIdentity(const Identity &identity, const QVariantMap &additional);
127     void createIdentity(const CoreIdentity &identity);
128
129     //! Remove identity and propagate that fact to the clients.
130     /** \param identity The identity to be removed.
131      */
132     void removeIdentity(IdentityId identity);
133
134     //! Create a network and propagate the changes to the clients.
135     /** \param info The network's settings.
136      */
137     void createNetwork(const NetworkInfo &info, const QStringList &persistentChannels = QStringList());
138
139     //! Remove network and propagate that fact to the clients.
140     /** \param network The id of the network to be removed.
141      */
142     void removeNetwork(NetworkId network);
143
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
148      */
149     void renameBuffer(const NetworkId &networkId, const QString &newName, const QString &oldName);
150
151     void changePassword(PeerPtr peer, const QString &userName, const QString &oldPassword, const QString &newPassword);
152
153     void kickClient(int peerId);
154
155     QHash<QString, QString> persistentChannels(NetworkId) const;
156
157     QHash<QString, QByteArray> bufferCiphers(NetworkId id) const;
158     void setBufferCipher(NetworkId id, const QString &bufferName, const QByteArray &cipher) const;
159
160     /**
161      * Marks us away (or unaway) on all networks
162      *
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)
165      */
166     void globalAway(const QString &msg = QString(), const bool skipFormatting = false);
167
168 signals:
169     void initialized();
170     void sessionState(const Protocol::SessionState &sessionState);
171
172     //void msgFromGui(uint netid, QString buf, QString message);
173     void displayMsg(Message message);
174     void displayStatusMsg(QString, QString);
175
176     void scriptResult(QString result);
177
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.
181      */
182     void identityCreated(const Identity &identity);
183
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.
187      */
188     void identityRemoved(IdentityId identity);
189
190     void networkCreated(NetworkId);
191     void networkRemoved(NetworkId);
192     void networkDisconnected(NetworkId);
193
194     void passwordChanged(PeerPtr peer, bool success);
195
196     void disconnectFromCore();
197
198 protected:
199     void customEvent(QEvent *event) override;
200
201 private slots:
202     void removeClient(Peer *peer);
203
204     void recvStatusMsgFromServer(QString msg);
205     void recvMessageFromServer(NetworkId networkId, Message::Type, BufferInfo::Type, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None);
206
207     void destroyNetwork(NetworkId);
208
209     void scriptRequest(QString script);
210
211     void clientsConnected();
212     void clientsDisconnected();
213
214     void updateIdentityBySender();
215
216     void saveSessionState() const;
217
218     void onNetworkDisconnected(NetworkId networkId);
219
220 private:
221     void processMessages();
222
223     void loadSettings();
224     void initScriptEngine();
225
226     /// Hook for converting events to the old displayMsg() handlers
227     Q_INVOKABLE void processMessageEvent(MessageEvent *event);
228
229     UserId _user;
230
231     /// Whether or not strict ident mode is enabled, locking users' idents to Quassel username
232     bool _strictIdentEnabled;
233
234     SignalProxy *_signalProxy;
235     CoreAliasManager _aliasManager;
236
237     QHash<IdentityId, CoreIdentity *> _identities;
238     QHash<NetworkId, CoreNetwork *> _networks;
239     QSet<NetworkId> _networksPendingDisconnect;
240
241     CoreBufferSyncer *_bufferSyncer;
242     CoreBacklogManager *_backlogManager;
243     CoreBufferViewManager *_bufferViewManager;
244     CoreDccConfig *_dccConfig;
245     CoreIrcListHelper *_ircListHelper;
246     CoreNetworkConfig *_networkConfig;
247     CoreInfo *_coreInfo;
248     CoreTransferManager *_transferManager;
249
250     EventManager *_eventManager;
251     EventStringifier *_eventStringifier; // should eventually move into client
252     CoreSessionEventProcessor *_sessionEventProcessor;
253     CtcpParser *_ctcpParser;
254     IrcParser *_ircParser;
255
256     QScriptEngine *scriptEngine;
257
258     /**
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
263      */
264     QString senderPrefixes(const QString &sender, const BufferInfo &bufferInfo) const;
265
266     /**
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
270      */
271     QString realName(const QString &sender, NetworkId networkId) const;
272
273     /**
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
277      */
278     QString avatarUrl(const QString &sender, NetworkId networkId) const;
279     QList<RawMessage> _messageQueue;
280     bool _processMessages;
281     CoreIgnoreListManager _ignoreListManager;
282     CoreHighlightRuleManager _highlightRuleManager;
283 };
284
285
286 struct RawMessage {
287     NetworkId networkId;
288     Message::Type type;
289     BufferInfo::Type bufferType;
290     QString target;
291     QString text;
292     QString sender;
293     Message::Flags flags;
294     RawMessage(NetworkId networkId, Message::Type type, BufferInfo::Type bufferType, QString target, QString text, QString sender, Message::Flags flags)
295         : networkId(networkId), type(type), bufferType(bufferType), target(std::move(target)), text(std::move(text)), sender(std::move(sender)), flags(flags) {}
296 };