1 /***************************************************************************
2 * Copyright (C) 2005-2022 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 ***************************************************************************/
27 #include <QProcessEnvironment>
30 #include <QVariantList>
32 #include "coreidentity.h"
37 class Storage : public QObject
42 Storage(QObject* parent = nullptr);
46 IsReady, // ready to go
47 NeedsSetup, // need basic setup (ask the user for input)
48 NotAvailable // remove the storage backend from the list of available backends
61 //! Check if the storage type is available.
62 /** A storage subclass should return true if it can be successfully used, i.e. if all
63 * prerequisites are in place (e.g. we have an appropriate DB driver etc.).
64 * \return True if and only if the storage class can be successfully used.
66 virtual bool isAvailable() const = 0;
68 //! Returns the identifier of the authenticator backend
69 /** \return A string that can be used by the client to identify the authenticator backend */
70 virtual QString backendId() const = 0;
72 //! Returns the display name of the storage backend
73 /** \return A string that can be used by the client to name the storage backend */
74 virtual QString displayName() const = 0;
76 //! Returns a description of this storage backend
77 /** \return A string that can be displayed by the client to describe the storage backend */
78 virtual QString description() const = 0;
80 //! Returns data required to configure the authenticator backend
82 * A list of flattened triples for each field: {key, translated field name, default value}
83 * The default value's type determines the kind of input widget to be shown
84 * (int -> QSpinBox; QString -> QLineEdit)
85 * \return A list of triples defining the data to be shown in the configuration dialog
87 virtual QVariantList setupData() const = 0;
89 //! Setup the storage provider.
90 /** This prepares the storage provider (e.g. create tables, etc.) for use within Quassel.
91 * \param settings Hostname, port, username, password, ...
92 * \return True if and only if the storage provider was initialized successfully.
94 virtual bool setup(const QVariantMap& settings = QVariantMap(),
95 const QProcessEnvironment& environment = {},
96 bool loadFromEnvironment = false)
99 //! Initialize the storage provider
100 /** \param settings Hostname, port, username, password, ...
101 * \return the State the storage backend is now in (see Storage::State)
103 virtual State init(const QVariantMap& settings = QVariantMap(),
104 const QProcessEnvironment& environment = {},
105 bool loadFromEnvironment = false)
108 //! Makes temp data persistent
109 /** This Method is periodically called by the Quassel Core to make temporary
110 * data persistent. This reduces the data loss drastically in the
111 * unlikely case of a Core crash.
113 virtual void sync() = 0;
115 // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
119 //! Add a new core user to the storage.
120 /** \param user The username of the new user
121 * \param password The cleartext password for the new user
122 * \return The new user's UserId
124 virtual UserId addUser(const QString& user, const QString& password, const QString& authenticator = "Database") = 0;
126 //! Update a core user's password.
127 /** \param user The user's id
128 * \param password The user's new password
129 * \return true on success.
131 virtual bool updateUser(UserId user, const QString& password) = 0;
134 /** \param user The user's id
135 * \param newName The user's new name
137 virtual void renameUser(UserId user, const QString& newName) = 0;
139 //! Validate a username with a given password.
140 /** \param user The username to validate
141 * \param password The user's alleged password
142 * \return A valid UserId if the password matches the username; 0 else
144 virtual UserId validateUser(const QString& user, const QString& password) = 0;
146 //! Check if a user with given username exists. Do not use for login purposes!
147 /** \param username The username to validate
148 * \return A valid UserId if the user exists; 0 else
150 virtual UserId getUserId(const QString& username) = 0;
152 //! Get the authentication provider for a given user.
153 /** \param username The username to validate
154 * \return The name of the auth provider if the UserId exists, "" otherwise.
156 virtual QString getUserAuthenticator(const UserId userid) = 0;
158 //! Determine the UserId of the internal user
159 /** \return A valid UserId if the password matches the username; 0 else
161 virtual UserId internalUser() = 0;
163 //! Remove a core user from storage.
164 /** \param user The userid to delete
166 virtual void delUser(UserId user) = 0;
168 //! Store a user setting persistently
170 * \param userId The users Id
171 * \param settingName The Name of the Setting
172 * \param data The Value
174 virtual void setUserSetting(UserId userId, const QString& settingName, const QVariant& data) = 0;
176 //! Retrieve a persistent user setting
178 * \param userId The users Id
179 * \param settingName The Name of the Setting
180 * \param default Value to return in case it's unset.
181 * \return the Value of the Setting or the default value if it is unset.
183 virtual QVariant getUserSetting(UserId userId, const QString& settingName, const QVariant& data = QVariant()) = 0;
187 * \param data Active Sessions
189 virtual void setCoreState(const QVariantList& data) = 0;
191 //! Retrieve core state
193 * \param default Value to return in case it's unset.
194 * \return Active Sessions
196 virtual QVariantList getCoreState(const QVariantList& data = QVariantList()) = 0;
198 /* Identity handling */
199 virtual IdentityId createIdentity(UserId user, CoreIdentity& identity) = 0;
200 virtual bool updateIdentity(UserId user, const CoreIdentity& identity) = 0;
201 virtual void removeIdentity(UserId user, IdentityId identityId) = 0;
202 virtual std::vector<CoreIdentity> identities(UserId user) = 0;
204 /* Network handling */
206 //! Create a new Network in the storage backend and return it unique Id
207 /** \param user The core user who owns this network
208 * \param networkInfo The networkInfo holding the network definition
209 * \return the NetworkId of the newly created Network. Possibly invalid.
211 virtual NetworkId createNetwork(UserId user, const NetworkInfo& info) = 0;
213 //! Apply the changes to NetworkInfo info to the storage engine
215 * \param user The core user
216 * \param networkInfo The Updated NetworkInfo
217 * \return true if successful.
219 virtual bool updateNetwork(UserId user, const NetworkInfo& info) = 0;
221 //! Permanently remove a Network and all the data associated with it.
222 /** \note This method is thredsafe.
224 * \param user The core user
225 * \param networkId The network to delete
226 * \return true if successful.
228 virtual bool removeNetwork(UserId user, const NetworkId& networkId) = 0;
230 //! Returns a list of all NetworkInfos for the given UserId user
231 /** \note This method is thredsafe.
233 * \param user The core user
234 * \return QList<NetworkInfo>.
236 virtual std::vector<NetworkInfo> networks(UserId user) = 0;
238 //! Get a list of Networks to restore
239 /** Return a list of networks the user was connected at the time of core shutdown
240 * \note This method is threadsafe.
242 * \param user The User Id in question
244 virtual std::vector<NetworkId> connectedNetworks(UserId user) = 0;
246 //! Update the connected state of a network
247 /** \note This method is threadsafe
249 * \param user The Id of the networks owner
250 * \param networkId The Id of the network
251 * \param isConnected whether the network is connected or not
253 virtual void setNetworkConnected(UserId user, const NetworkId& networkId, bool isConnected) = 0;
255 //! Get a hash of channels with their channel keys for a given network
256 /** The keys are channel names and values are passwords (possibly empty)
257 * \note This method is threadsafe
259 * \param user The id of the networks owner
260 * \param networkId The Id of the network
262 virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId& networkId) = 0;
264 //! Update the connected state of a channel
265 /** \note This method is threadsafe
267 * \param user The Id of the networks owner
268 * \param networkId The Id of the network
269 * \param channel The name of the channel
270 * \param isJoined whether the channel is connected or not
272 virtual void setChannelPersistent(UserId user, const NetworkId& networkId, const QString& channel, bool isJoined) = 0;
274 //! Update the key of a channel
275 /** \note This method is threadsafe
277 * \param user The Id of the networks owner
278 * \param networkId The Id of the network
279 * \param channel The name of the channel
280 * \param key The key of the channel (possibly empty)
282 virtual void setPersistentChannelKey(UserId user, const NetworkId& networkId, const QString& channel, const QString& key) = 0;
284 //! retrieve last known away message for session restore
285 /** \note This method is threadsafe
287 * \param user The Id of the networks owner
288 * \param networkId The Id of the network
290 virtual QString awayMessage(UserId user, NetworkId networkId) = 0;
292 //! Make away message persistent for session restore
293 /** \note This method is threadsafe
295 * \param user The Id of the networks owner
296 * \param networkId The Id of the network
297 * \param awayMsg The current away message of own user
299 virtual void setAwayMessage(UserId user, NetworkId networkId, const QString& awayMsg) = 0;
301 //! retrieve last known user mode for session restore
302 /** \note This method is threadsafe
304 * \param user The Id of the networks owner
305 * \param networkId The Id of the network
307 virtual QString userModes(UserId user, NetworkId networkId) = 0;
309 //! Make our user modes persistent for session restore
310 /** \note This method is threadsafe
312 * \param user The Id of the networks owner
313 * \param networkId The Id of the network
314 * \param userModes The current user modes of own user
316 virtual void setUserModes(UserId user, NetworkId networkId, const QString& userModes) = 0;
318 /* Buffer handling */
320 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
321 /** \param user The core user who owns this buffername
322 * \param networkId The network id
323 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
324 * \param buffer The buffer name (if empty, the net's status buffer is returned)
325 * \param create Whether or not the buffer should be created if it doesn't exist
326 * \return The BufferInfo corresponding to the given network and buffer name, or an invalid BufferInfo if not found
328 virtual BufferInfo bufferInfo(UserId user, const NetworkId& networkId, BufferInfo::Type type, const QString& buffer = "", bool create = true)
331 //! Get the unique BufferInfo for a bufferId
332 /** \param user The core user who owns this buffername
333 * \param bufferId The id of the buffer
334 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
336 virtual BufferInfo getBufferInfo(UserId user, const BufferId& bufferId) = 0;
338 //! Request a list of all buffers known to a user.
339 /** This method is used to get a list of all buffers we have stored a backlog from.
340 * \param user The user whose buffers we request
341 * \return A list of the BufferInfos for all buffers as requested
343 virtual std::vector<BufferInfo> requestBuffers(UserId user) = 0;
345 //! Request a list of BufferIds for a given NetworkId
346 /** \note This method is threadsafe.
348 * \param user The user whose buffers we request
349 * \param networkId The NetworkId of the network in question
350 * \return List of BufferIds belonging to the Network
352 virtual std::vector<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) = 0;
354 //! Remove permanently a buffer and it's content from the storage backend
355 /** This call cannot be reverted!
356 * \param user The user who is the owner of the buffer
357 * \param bufferId The bufferId
358 * \return true if successful
360 virtual bool removeBuffer(const UserId& user, const BufferId& bufferId) = 0;
363 /** \note This method is threadsafe.
364 * \param user The id of the buffer owner
365 * \param bufferId The bufferId
366 * \param newName The new name of the buffer
367 * \return true if successful
369 virtual bool renameBuffer(const UserId& user, const BufferId& bufferId, const QString& newName) = 0;
371 //! Merge the content of two Buffers permanently. This cannot be reversed!
372 /** \note This method is threadsafe.
373 * \param user The id of the buffer owner
374 * \param bufferId1 The bufferId of the remaining buffer
375 * \param bufferId2 The buffer that is about to be removed
376 * \return true if successful
378 virtual bool mergeBuffersPermanently(const UserId& user, const BufferId& bufferId1, const BufferId& bufferId2) = 0;
380 //! Update the LastSeenDate for a Buffer
381 /** This Method is used to make the LastSeenDate of a Buffer persistent
382 * \param user The Owner of that Buffer
383 * \param bufferId The buffer id
384 * \param MsgId The Message id of the message that has been just seen
386 virtual void setBufferLastSeenMsg(UserId user, const BufferId& bufferId, const MsgId& msgId) = 0;
388 //! Get a Hash of all last message ids
389 /** This Method is called when the Quassel Core is started to restore the lastMsgIds
390 * \param user The Owner of the buffers
392 virtual QHash<BufferId, MsgId> bufferLastMsgIds(UserId user) = 0;
394 //! Get a Hash of all last seen message ids
395 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
396 * \param user The Owner of the buffers
398 virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) = 0;
400 //! Update the MarkerLineMsgId for a Buffer
401 /** This Method is used to make the marker line position of a Buffer persistent
402 * \note This method is threadsafe.
404 * \param user The Owner of that Buffer
405 * \param bufferId The buffer id
406 * \param MsgId The Message id where the marker line should be placed
408 virtual void setBufferMarkerLineMsg(UserId user, const BufferId& bufferId, const MsgId& msgId) = 0;
410 //! Get a Hash of all marker line message ids
411 /** This Method is called when the Quassel Core is started to restore the MarkerLineMsgIds
412 * \note This method is threadsafe.
414 * \param user The Owner of the buffers
416 virtual QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) = 0;
418 //! Update the BufferActivity for a Buffer
419 /** This Method is used to make the activity state of a Buffer persistent
420 * \note This method is threadsafe.
422 * \param user The Owner of that Buffer
423 * \param bufferId The buffer id
424 * \param MsgId The Message id where the marker line should be placed
426 virtual void setBufferActivity(UserId id, BufferId bufferId, Message::Types type) = 0;
428 //! Get a Hash of all buffer activity states
429 /** This Method is called when the Quassel Core is started to restore the BufferActivities
430 * \note This method is threadsafe.
432 * \param user The Owner of the buffers
434 virtual QHash<BufferId, Message::Types> bufferActivities(UserId id) = 0;
436 //! Get the bitset of buffer activity states for a buffer
437 /** This method is used to load the activity state of a buffer when its last seen message changes.
438 * \note This method is threadsafe.
440 * \param bufferId The buffer
441 * \param lastSeenMsgId The last seen message
443 virtual Message::Types bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) = 0;
445 //! Get a hash of buffers with their ciphers for a given network
446 /** The keys are channel names and values are ciphers (possibly empty)
447 * \note This method is threadsafe
449 * \param user The id of the networks owner
450 * \param networkId The Id of the network
452 virtual QHash<QString, QByteArray> bufferCiphers(UserId user, const NetworkId& networkId) = 0;
454 //! Update the cipher of a buffer
455 /** \note This method is threadsafe
457 * \param user The Id of the networks owner
458 * \param networkId The Id of the network
459 * \param bufferName The Cname of the buffer
460 * \param cipher The cipher for the buffer
462 virtual void setBufferCipher(UserId user, const NetworkId& networkId, const QString& bufferName, const QByteArray& cipher) = 0;
464 //! Update the highlight count for a Buffer
465 /** This Method is used to make the activity state of a Buffer persistent
466 * \note This method is threadsafe.
468 * \param user The Owner of that Buffer
469 * \param bufferId The buffer id
470 * \param MsgId The Message id where the marker line should be placed
472 virtual void setHighlightCount(UserId id, BufferId bufferId, int count) = 0;
474 //! Get a Hash of all highlight count states
475 /** This Method is called when the Quassel Core is started to restore the HighlightCounts
476 * \note This method is threadsafe.
478 * \param user The Owner of the buffers
480 virtual QHash<BufferId, int> highlightCounts(UserId id) = 0;
482 //! Get the highlight count states for a buffer
483 /** This method is used to load the activity state of a buffer when its last seen message changes.
484 * \note This method is threadsafe.
486 * \param bufferId The buffer
487 * \param lastSeenMsgId The last seen message
489 virtual int highlightCount(BufferId bufferId, MsgId lastSeenMsgId) = 0;
491 /* Message handling */
493 //! Store a Message in the storage backend and set its unique Id.
494 /** \param msg The message object to be stored
495 * \return true on success
497 virtual bool logMessage(Message& msg) = 0;
499 //! Store a list of Messages in the storage backend and set their unique Id.
500 /** \param msgs The list message objects to be stored
501 * \return true on success
503 virtual bool logMessages(MessageList& msgs) = 0;
505 //! Request a certain number messages stored in a given buffer.
506 /** \param buffer The buffer we request messages from
507 * \param first if != -1 return only messages with a MsgId >= first
508 * \param last if != -1 return only messages with a MsgId < last
509 * \param limit if != -1 limit the returned list to a max of \limit entries
510 * \return The requested list of messages
512 virtual std::vector<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
514 //! Request a certain number messages stored in a given buffer, matching certain filters
515 /** \param buffer The buffer we request messages from
516 * \param first if != -1 return only messages with a MsgId >= first
517 * \param last if != -1 return only messages with a MsgId < last
518 * \param limit if != -1 limit the returned list to a max of \limit entries
519 * \param type The Message::Types that should be returned
520 * \return The requested list of messages
522 virtual std::vector<Message> requestMsgsFiltered(UserId user,
527 Message::Types type = Message::Types{-1},
528 Message::Flags flags = Message::Flags{-1}) = 0;
530 //! Request a certain number messages stored in a given buffer, matching certain filters, ascending
531 /** \param buffer The buffer we request messages from
532 * \param first if != -1 return only messages with a MsgId >= first
533 * \param last if != -1 return only messages with a MsgId < last
534 * \param limit if != -1 limit the returned list to a max of \limit entries
535 * \param type The Message::Types that should be returned
536 * \param flags The Message::Flags that should be returned
537 * \return The requested list of messages
539 virtual std::vector<Message> requestMsgsForward(UserId user,
544 Message::Types type = Message::Types{-1},
545 Message::Flags flags = Message::Flags{-1}) = 0;
547 //! Request a certain number of messages across all buffers
548 /** \param first if != -1 return only messages with a MsgId >= first
549 * \param last if != -1 return only messages with a MsgId < last
550 * \param limit Max amount of messages
551 * \return The requested list of messages
553 virtual std::vector<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
555 //! Request a certain number of messages across all buffers, matching certain filters
556 /** \param first if != -1 return only messages with a MsgId >= first
557 * \param last if != -1 return only messages with a MsgId < last
558 * \param limit Max amount of messages
559 * \param type The Message::Types that should be returned
560 * \return The requested list of messages
562 virtual std::vector<Message> requestAllMsgsFiltered(UserId user,
566 Message::Types type = Message::Types{-1},
567 Message::Flags flags = Message::Flags{-1}) = 0;
569 //! Fetch all authusernames
570 /** \return Map of all current UserIds to permitted idents
572 virtual QMap<UserId, QString> getAllAuthUserNames() = 0;
575 //! Sent when a new BufferInfo is created, or an existing one changed somehow.
576 void bufferInfoUpdated(UserId user, const BufferInfo&);
577 //! Sent when a Buffer was renamed
578 void bufferRenamed(const QString& newName, const QString& oldName);
579 //! Sent when a new user has been added
580 void userAdded(UserId, const QString& username);
581 //! Sent when a user has been renamed
582 void userRenamed(UserId, const QString& newname);
583 //! Sent when a user has been removed
584 void userRemoved(UserId);
586 //! Emitted when database schema upgrade starts or ends
587 void dbUpgradeInProgress(bool inProgress);
590 QString hashPassword(const QString& password);
591 bool checkHashedPassword(const UserId user, const QString& password, const QString& hashedPassword, const Storage::HashVersion version);
594 QString hashPasswordSha1(const QString& password);
595 bool checkHashedPasswordSha1(const QString& password, const QString& hashedPassword);
597 QString hashPasswordSha2_512(const QString& password);
598 bool checkHashedPasswordSha2_512(const QString& password, const QString& hashedPassword);
599 QString sha2_512(const QString& input);