1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 "coreidentity.h"
31 class Storage : public QObject
36 Storage(QObject *parent = 0);
37 virtual ~Storage() {};
40 IsReady, // ready to go
41 NeedsSetup, // need basic setup (ask the user for input)
42 NotAvailable // remove the storage backend from the list of avaliable backends
55 //! Check if the storage type is available.
56 /** A storage subclass should return true if it can be successfully used, i.e. if all
57 * prerequisites are in place (e.g. we have an appropriate DB driver etc.).
58 * \return True if and only if the storage class can be successfully used.
60 virtual bool isAvailable() const = 0;
62 //! Returns the identifier of the authenticator backend
63 /** \return A string that can be used by the client to identify the authenticator backend */
64 virtual QString backendId() const = 0;
66 //! Returns the display name of the storage backend
67 /** \return A string that can be used by the client to name the storage backend */
68 virtual QString displayName() const = 0;
70 //! Returns a description of this storage backend
71 /** \return A string that can be displayed by the client to describe the storage backend */
72 virtual QString description() const = 0;
74 //! Returns data required to configure the authenticator backend
76 * A list of flattened triples for each field: {key, translated field name, default value}
77 * The default value's type determines the kind of input widget to be shown
78 * (int -> QSpinBox; QString -> QLineEdit)
79 * \return A list of triples defining the data to be shown in the configuration dialog
81 virtual QVariantList setupData() const = 0;
83 //! Setup the storage provider.
84 /** This prepares the storage provider (e.g. create tables, etc.) for use within Quassel.
85 * \param settings Hostname, port, username, password, ...
86 * \return True if and only if the storage provider was initialized successfully.
88 virtual bool setup(const QVariantMap &settings = QVariantMap()) = 0;
90 //! Initialize the storage provider
91 /** \param settings Hostname, port, username, password, ...
92 * \return the State the storage backend is now in (see Storage::State)
94 virtual State init(const QVariantMap &settings = QVariantMap()) = 0;
96 //! Makes temp data persistent
97 /** This Method is periodically called by the Quassel Core to make temporary
98 * data persistant. This reduces the data loss drastically in the
99 * unlikely case of a Core crash.
101 virtual void sync() = 0;
103 // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
107 //! Add a new core user to the storage.
108 /** \param user The username of the new user
109 * \param password The cleartext password for the new user
110 * \return The new user's UserId
112 virtual UserId addUser(const QString &user, const QString &password, const QString &authenticator = "Database") = 0;
114 //! Update a core user's password.
115 /** \param user The user's id
116 * \param password The user's new password
117 * \return true on success.
119 virtual bool updateUser(UserId user, const QString &password) = 0;
122 /** \param user The user's id
123 * \param newName The user's new name
125 virtual void renameUser(UserId user, const QString &newName) = 0;
127 //! Validate a username with a given password.
128 /** \param user The username to validate
129 * \param password The user's alleged password
130 * \return A valid UserId if the password matches the username; 0 else
132 virtual UserId validateUser(const QString &user, const QString &password) = 0;
134 //! Check if a user with given username exists. Do not use for login purposes!
135 /** \param username The username to validate
136 * \return A valid UserId if the user exists; 0 else
138 virtual UserId getUserId(const QString &username) = 0;
140 //! Get the authentication provider for a given user.
141 /** \param username The username to validate
142 * \return The name of the auth provider if the UserId exists, "" otherwise.
144 virtual QString getUserAuthenticator(const UserId userid) = 0;
147 //! Determine the UserId of the internal user
148 /** \return A valid UserId if the password matches the username; 0 else
150 virtual UserId internalUser() = 0;
152 //! Remove a core user from storage.
153 /** \param user The userid to delete
155 virtual void delUser(UserId user) = 0;
157 //! Store a user setting persistently
159 * \param userId The users Id
160 * \param settingName The Name of the Setting
161 * \param data The Value
163 virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) = 0;
165 //! Retrieve a persistent user setting
167 * \param userId The users Id
168 * \param settingName The Name of the Setting
169 * \param default Value to return in case it's unset.
170 * \return the Value of the Setting or the default value if it is unset.
172 virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant()) = 0;
174 /* Identity handling */
175 virtual IdentityId createIdentity(UserId user, CoreIdentity &identity) = 0;
176 virtual bool updateIdentity(UserId user, const CoreIdentity &identity) = 0;
177 virtual void removeIdentity(UserId user, IdentityId identityId) = 0;
178 virtual QList<CoreIdentity> identities(UserId user) = 0;
180 /* Network handling */
182 //! Create a new Network in the storage backend and return it unique Id
183 /** \param user The core user who owns this network
184 * \param networkInfo The networkInfo holding the network definition
185 * \return the NetworkId of the newly created Network. Possibly invalid.
187 virtual NetworkId createNetwork(UserId user, const NetworkInfo &info) = 0;
189 //! Apply the changes to NetworkInfo info to the storage engine
191 * \param user The core user
192 * \param networkInfo The Updated NetworkInfo
193 * \return true if successfull.
195 virtual bool updateNetwork(UserId user, const NetworkInfo &info) = 0;
197 //! Permanently remove a Network and all the data associated with it.
198 /** \note This method is thredsafe.
200 * \param user The core user
201 * \param networkId The network to delete
202 * \return true if successfull.
204 virtual bool removeNetwork(UserId user, const NetworkId &networkId) = 0;
206 //! Returns a list of all NetworkInfos for the given UserId user
207 /** \note This method is thredsafe.
209 * \param user The core user
210 * \return QList<NetworkInfo>.
212 virtual QList<NetworkInfo> networks(UserId user) = 0;
214 //! Get a list of Networks to restore
215 /** Return a list of networks the user was connected at the time of core shutdown
216 * \note This method is threadsafe.
218 * \param user The User Id in question
220 virtual QList<NetworkId> connectedNetworks(UserId user) = 0;
222 //! Update the connected state of a network
223 /** \note This method is threadsafe
225 * \param user The Id of the networks owner
226 * \param networkId The Id of the network
227 * \param isConnected whether the network is connected or not
229 virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) = 0;
231 //! Get a hash of channels with their channel keys for a given network
232 /** The keys are channel names and values are passwords (possibly empty)
233 * \note This method is threadsafe
235 * \param user The id of the networks owner
236 * \param networkId The Id of the network
238 virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) = 0;
240 //! Update the connected state of a channel
241 /** \note This method is threadsafe
243 * \param user The Id of the networks owner
244 * \param networkId The Id of the network
245 * \param channel The name of the channel
246 * \param isJoined whether the channel is connected or not
248 virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) = 0;
250 //! Update the key of a channel
251 /** \note This method is threadsafe
253 * \param user The Id of the networks owner
254 * \param networkId The Id of the network
255 * \param channel The name of the channel
256 * \param key The key of the channel (possibly empty)
258 virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) = 0;
260 //! retrieve last known away message for session restore
261 /** \note This method is threadsafe
263 * \param user The Id of the networks owner
264 * \param networkId The Id of the network
266 virtual QString awayMessage(UserId user, NetworkId networkId) = 0;
268 //! Make away message persistent for session restore
269 /** \note This method is threadsafe
271 * \param user The Id of the networks owner
272 * \param networkId The Id of the network
273 * \param awayMsg The current away message of own user
275 virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) = 0;
277 //! retrieve last known user mode for session restore
278 /** \note This method is threadsafe
280 * \param user The Id of the networks owner
281 * \param networkId The Id of the network
283 virtual QString userModes(UserId user, NetworkId networkId) = 0;
285 //! Make our user modes persistent for session restore
286 /** \note This method is threadsafe
288 * \param user The Id of the networks owner
289 * \param networkId The Id of the network
290 * \param userModes The current user modes of own user
292 virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes) = 0;
294 /* Buffer handling */
296 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
297 /** \param user The core user who owns this buffername
298 * \param networkId The network id
299 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
300 * \param buffer The buffer name (if empty, the net's status buffer is returned)
301 * \param create Whether or not the buffer should be created if it doesnt exist
302 * \return The BufferInfo corresponding to the given network and buffer name, or an invalid BufferInfo if not found
304 virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) = 0;
306 //! Get the unique BufferInfo for a bufferId
307 /** \param user The core user who owns this buffername
308 * \param bufferId The id of the buffer
309 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
311 virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) = 0;
313 //! Request a list of all buffers known to a user.
314 /** This method is used to get a list of all buffers we have stored a backlog from.
315 * \param user The user whose buffers we request
316 * \return A list of the BufferInfos for all buffers as requested
318 virtual QList<BufferInfo> requestBuffers(UserId user) = 0;
320 //! Request a list of BufferIds for a given NetworkId
321 /** \note This method is threadsafe.
323 * \param user The user whose buffers we request
324 * \param networkId The NetworkId of the network in question
325 * \return List of BufferIds belonging to the Network
327 virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) = 0;
329 //! Remove permanently a buffer and it's content from the storage backend
330 /** This call cannot be reverted!
331 * \param user The user who is the owner of the buffer
332 * \param bufferId The bufferId
333 * \return true if successfull
335 virtual bool removeBuffer(const UserId &user, const BufferId &bufferId) = 0;
338 /** \note This method is threadsafe.
339 * \param user The id of the buffer owner
340 * \param bufferId The bufferId
341 * \param newName The new name of the buffer
342 * \return true if successfull
344 virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) = 0;
346 //! Merge the content of two Buffers permanently. This cannot be reversed!
347 /** \note This method is threadsafe.
348 * \param user The id of the buffer owner
349 * \param bufferId1 The bufferId of the remaining buffer
350 * \param bufferId2 The buffer that is about to be removed
351 * \return true if successfull
353 virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) = 0;
355 //! Update the LastSeenDate for a Buffer
356 /** This Method is used to make the LastSeenDate of a Buffer persistent
357 * \param user The Owner of that Buffer
358 * \param bufferId The buffer id
359 * \param MsgId The Message id of the message that has been just seen
361 virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
363 //! Get a Hash of all last seen message ids
364 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
365 * \param user The Owner of the buffers
367 virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) = 0;
369 //! Update the MarkerLineMsgId for a Buffer
370 /** This Method is used to make the marker line position of a Buffer persistent
371 * \note This method is threadsafe.
373 * \param user The Owner of that Buffer
374 * \param bufferId The buffer id
375 * \param MsgId The Message id where the marker line should be placed
377 virtual void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
379 //! Get a Hash of all marker line message ids
380 /** This Method is called when the Quassel Core is started to restore the MarkerLineMsgIds
381 * \note This method is threadsafe.
383 * \param user The Owner of the buffers
385 virtual QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) = 0;
387 //! Update the BufferActivity for a Buffer
388 /** This Method is used to make the activity state of a Buffer persistent
389 * \note This method is threadsafe.
391 * \param user The Owner of that Buffer
392 * \param bufferId The buffer id
393 * \param MsgId The Message id where the marker line should be placed
395 virtual void setBufferActivity(UserId id, BufferId bufferId, Message::Types type) = 0;
397 //! Get a Hash of all buffer activity states
398 /** This Method is called when the Quassel Core is started to restore the BufferActivities
399 * \note This method is threadsafe.
401 * \param user The Owner of the buffers
403 virtual QHash<BufferId, Message::Types> bufferActivities(UserId id) = 0;
405 //! Get the bitset of buffer activity states for a buffer
406 /** This method is used to load the activity state of a buffer when its last seen message changes.
407 * \note This method is threadsafe.
409 * \param bufferId The buffer
410 * \param lastSeenMsgId The last seen message
412 virtual Message::Types bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) = 0;
414 /* Message handling */
416 //! Store a Message in the storage backend and set its unique Id.
417 /** \param msg The message object to be stored
418 * \return true on success
420 virtual bool logMessage(Message &msg) = 0;
422 //! Store a list of Messages in the storage backend and set their unique Id.
423 /** \param msgs The list message objects to be stored
424 * \return true on success
426 virtual bool logMessages(MessageList &msgs) = 0;
428 //! Request a certain number messages stored in a given buffer.
429 /** \param buffer The buffer we request messages from
430 * \param first if != -1 return only messages with a MsgId >= first
431 * \param last if != -1 return only messages with a MsgId < last
432 * \param limit if != -1 limit the returned list to a max of \limit entries
433 * \return The requested list of messages
435 virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
437 //! Request a certain number of messages across all buffers
438 /** \param first if != -1 return only messages with a MsgId >= first
439 * \param last if != -1 return only messages with a MsgId < last
440 * \param limit Max amount of messages
441 * \return The requested list of messages
443 virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
446 //! Sent when a new BufferInfo is created, or an existing one changed somehow.
447 void bufferInfoUpdated(UserId user, const BufferInfo &);
448 //! Sent when a Buffer was renamed
449 void bufferRenamed(const QString &newName, const QString &oldName);
450 //! Sent when a new user has been added
451 void userAdded(UserId, const QString &username);
452 //! Sent when a user has been renamed
453 void userRenamed(UserId, const QString &newname);
454 //! Sent when a user has been removed
455 void userRemoved(UserId);
458 QString hashPassword(const QString &password);
459 bool checkHashedPassword(const UserId user, const QString &password, const QString &hashedPassword, const Storage::HashVersion version);
462 QString hashPasswordSha1(const QString &password);
463 bool checkHashedPasswordSha1(const QString &password, const QString &hashedPassword);
465 QString hashPasswordSha2_512(const QString &password);
466 bool checkHashedPasswordSha2_512(const QString &password, const QString &hashedPassword);
467 QString sha2_512(const QString &input);