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