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