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 display name of the storage backend
63 /** \return A string that can be used by the client to name the storage backend */
64 virtual QString displayName() const = 0;
66 //! Returns a description of this storage backend
67 /** \return A string that can be displayed by the client to describe the storage backend */
68 virtual QString description() const = 0;
70 //! Returns a list of properties required to use the storage backend
71 virtual QStringList setupKeys() const = 0;
73 //! Returns a map where the keys are are properties to use the storage backend
74 /* the values are QVariants with default values */
75 virtual QVariantMap setupDefaults() const = 0;
77 //! Setup the storage provider.
78 /** This prepares the storage provider (e.g. create tables, etc.) for use within Quassel.
79 * \param settings Hostname, port, username, password, ...
80 * \return True if and only if the storage provider was initialized successfully.
82 virtual bool setup(const QVariantMap &settings = QVariantMap()) = 0;
84 //! Initialize the storage provider
85 /** \param settings Hostname, port, username, password, ...
86 * \return the State the storage backend is now in (see Storage::State)
88 virtual State init(const QVariantMap &settings = QVariantMap()) = 0;
90 //! Makes temp data persistent
91 /** This Method is periodically called by the Quassel Core to make temporary
92 * data persistant. This reduces the data loss drastically in the
93 * unlikely case of a Core crash.
95 virtual void sync() = 0;
97 // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
101 //! Add a new core user to the storage.
102 /** \param user The username of the new user
103 * \param password The cleartext password for the new user
104 * \return The new user's UserId
106 virtual UserId addUser(const QString &user, const QString &password, const QString &authenticator = "Database") = 0;
108 //! Update a core user's password.
109 /** \param user The user's id
110 * \param password The user's new password
111 * \return true on success.
113 virtual bool updateUser(UserId user, const QString &password) = 0;
116 /** \param user The user's id
117 * \param newName The user's new name
119 virtual void renameUser(UserId user, const QString &newName) = 0;
121 //! Validate a username with a given password.
122 /** \param user The username to validate
123 * \param password The user's alleged password
124 * \return A valid UserId if the password matches the username; 0 else
126 virtual UserId validateUser(const QString &user, const QString &password) = 0;
128 //! Check if a user with given username exists. Do not use for login purposes!
129 /** \param username The username to validate
130 * \return A valid UserId if the user exists; 0 else
132 virtual UserId getUserId(const QString &username) = 0;
134 //! Get the authentication provider for a given user.
135 /** \param username The username to validate
136 * \return The name of the auth provider if the UserId exists, "" otherwise.
138 virtual QString getUserAuthenticator(const UserId userid) = 0;
141 //! Determine the UserId of the internal user
142 /** \return A valid UserId if the password matches the username; 0 else
144 virtual UserId internalUser() = 0;
146 //! Remove a core user from storage.
147 /** \param user The userid to delete
149 virtual void delUser(UserId user) = 0;
151 //! Store a user setting persistently
153 * \param userId The users Id
154 * \param settingName The Name of the Setting
155 * \param data The Value
157 virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) = 0;
159 //! Retrieve a persistent user setting
161 * \param userId The users Id
162 * \param settingName The Name of the Setting
163 * \param default Value to return in case it's unset.
164 * \return the Value of the Setting or the default value if it is unset.
166 virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant()) = 0;
168 /* Identity handling */
169 virtual IdentityId createIdentity(UserId user, CoreIdentity &identity) = 0;
170 virtual bool updateIdentity(UserId user, const CoreIdentity &identity) = 0;
171 virtual void removeIdentity(UserId user, IdentityId identityId) = 0;
172 virtual QList<CoreIdentity> identities(UserId user) = 0;
174 /* Network handling */
176 //! Create a new Network in the storage backend and return it unique Id
177 /** \param user The core user who owns this network
178 * \param networkInfo The networkInfo holding the network definition
179 * \return the NetworkId of the newly created Network. Possibly invalid.
181 virtual NetworkId createNetwork(UserId user, const NetworkInfo &info) = 0;
183 //! Apply the changes to NetworkInfo info to the storage engine
185 * \param user The core user
186 * \param networkInfo The Updated NetworkInfo
187 * \return true if successfull.
189 virtual bool updateNetwork(UserId user, const NetworkInfo &info) = 0;
191 //! Permanently remove a Network and all the data associated with it.
192 /** \note This method is thredsafe.
194 * \param user The core user
195 * \param networkId The network to delete
196 * \return true if successfull.
198 virtual bool removeNetwork(UserId user, const NetworkId &networkId) = 0;
200 //! Returns a list of all NetworkInfos for the given UserId user
201 /** \note This method is thredsafe.
203 * \param user The core user
204 * \return QList<NetworkInfo>.
206 virtual QList<NetworkInfo> networks(UserId user) = 0;
208 //! Get a list of Networks to restore
209 /** Return a list of networks the user was connected at the time of core shutdown
210 * \note This method is threadsafe.
212 * \param user The User Id in question
214 virtual QList<NetworkId> connectedNetworks(UserId user) = 0;
216 //! Update the connected state of a network
217 /** \note This method is threadsafe
219 * \param user The Id of the networks owner
220 * \param networkId The Id of the network
221 * \param isConnected whether the network is connected or not
223 virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) = 0;
225 //! Get a hash of channels with their channel keys for a given network
226 /** The keys are channel names and values are passwords (possibly empty)
227 * \note This method is threadsafe
229 * \param user The id of the networks owner
230 * \param networkId The Id of the network
232 virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) = 0;
234 //! Update the connected state of a channel
235 /** \note This method is threadsafe
237 * \param user The Id of the networks owner
238 * \param networkId The Id of the network
239 * \param channel The name of the channel
240 * \param isJoined whether the channel is connected or not
242 virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) = 0;
244 //! Update the key of a channel
245 /** \note This method is threadsafe
247 * \param user The Id of the networks owner
248 * \param networkId The Id of the network
249 * \param channel The name of the channel
250 * \param key The key of the channel (possibly empty)
252 virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) = 0;
254 //! retrieve last known away message for session restore
255 /** \note This method is threadsafe
257 * \param user The Id of the networks owner
258 * \param networkId The Id of the network
260 virtual QString awayMessage(UserId user, NetworkId networkId) = 0;
262 //! Make away message persistent for session restore
263 /** \note This method is threadsafe
265 * \param user The Id of the networks owner
266 * \param networkId The Id of the network
267 * \param awayMsg The current away message of own user
269 virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) = 0;
271 //! retrieve last known user mode for session restore
272 /** \note This method is threadsafe
274 * \param user The Id of the networks owner
275 * \param networkId The Id of the network
277 virtual QString userModes(UserId user, NetworkId networkId) = 0;
279 //! Make our user modes persistent for session restore
280 /** \note This method is threadsafe
282 * \param user The Id of the networks owner
283 * \param networkId The Id of the network
284 * \param userModes The current user modes of own user
286 virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes) = 0;
288 /* Buffer handling */
290 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
291 /** \param user The core user who owns this buffername
292 * \param networkId The network id
293 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
294 * \param buffer The buffer name (if empty, the net's status buffer is returned)
295 * \param create Whether or not the buffer should be created if it doesnt exist
296 * \return The BufferInfo corresponding to the given network and buffer name, or an invalid BufferInfo if not found
298 virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) = 0;
300 //! Get the unique BufferInfo for a bufferId
301 /** \param user The core user who owns this buffername
302 * \param bufferId The id of the buffer
303 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
305 virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) = 0;
307 //! Request a list of all buffers known to a user.
308 /** This method is used to get a list of all buffers we have stored a backlog from.
309 * \param user The user whose buffers we request
310 * \return A list of the BufferInfos for all buffers as requested
312 virtual QList<BufferInfo> requestBuffers(UserId user) = 0;
314 //! Request a list of BufferIds for a given NetworkId
315 /** \note This method is threadsafe.
317 * \param user The user whose buffers we request
318 * \param networkId The NetworkId of the network in question
319 * \return List of BufferIds belonging to the Network
321 virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) = 0;
323 //! Remove permanently a buffer and it's content from the storage backend
324 /** This call cannot be reverted!
325 * \param user The user who is the owner of the buffer
326 * \param bufferId The bufferId
327 * \return true if successfull
329 virtual bool removeBuffer(const UserId &user, const BufferId &bufferId) = 0;
332 /** \note This method is threadsafe.
333 * \param user The id of the buffer owner
334 * \param bufferId The bufferId
335 * \param newName The new name of the buffer
336 * \return true if successfull
338 virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) = 0;
340 //! Merge the content of two Buffers permanently. This cannot be reversed!
341 /** \note This method is threadsafe.
342 * \param user The id of the buffer owner
343 * \param bufferId1 The bufferId of the remaining buffer
344 * \param bufferId2 The buffer that is about to be removed
345 * \return true if successfull
347 virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) = 0;
349 //! Update the LastSeenDate for a Buffer
350 /** This Method is used to make the LastSeenDate of a Buffer persistent
351 * \param user The Owner of that Buffer
352 * \param bufferId The buffer id
353 * \param MsgId The Message id of the message that has been just seen
355 virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
357 //! Get a Hash of all last seen message ids
358 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
359 * \param user The Owner of the buffers
361 virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) = 0;
363 //! Update the MarkerLineMsgId for a Buffer
364 /** This Method is used to make the marker line position of a Buffer persistent
365 * \note This method is threadsafe.
367 * \param user The Owner of that Buffer
368 * \param bufferId The buffer id
369 * \param MsgId The Message id where the marker line should be placed
371 virtual void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
373 //! Get a Hash of all marker line message ids
374 /** This Method is called when the Quassel Core is started to restore the MarkerLineMsgIds
375 * \note This method is threadsafe.
377 * \param user The Owner of the buffers
379 virtual QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) = 0;
381 /* Message handling */
383 //! Store a Message in the storage backend and set its unique Id.
384 /** \param msg The message object to be stored
385 * \return true on success
387 virtual bool logMessage(Message &msg) = 0;
389 //! Store a list of Messages in the storage backend and set their unique Id.
390 /** \param msgs The list message objects to be stored
391 * \return true on success
393 virtual bool logMessages(MessageList &msgs) = 0;
395 //! Request a certain number messages stored in a given buffer.
396 /** \param buffer The buffer we request messages from
397 * \param first if != -1 return only messages with a MsgId >= first
398 * \param last if != -1 return only messages with a MsgId < last
399 * \param limit if != -1 limit the returned list to a max of \limit entries
400 * \return The requested list of messages
402 virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
404 //! Request a certain number of messages across all buffers
405 /** \param first if != -1 return only messages with a MsgId >= first
406 * \param last if != -1 return only messages with a MsgId < last
407 * \param limit Max amount of messages
408 * \return The requested list of messages
410 virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
413 //! Sent when a new BufferInfo is created, or an existing one changed somehow.
414 void bufferInfoUpdated(UserId user, const BufferInfo &);
415 //! Sent when a Buffer was renamed
416 void bufferRenamed(const QString &newName, const QString &oldName);
417 //! Sent when a new user has been added
418 void userAdded(UserId, const QString &username);
419 //! Sent when a user has been renamed
420 void userRenamed(UserId, const QString &newname);
421 //! Sent when a user has been removed
422 void userRemoved(UserId);
425 QString hashPassword(const QString &password);
426 bool checkHashedPassword(const UserId user, const QString &password, const QString &hashedPassword, const Storage::HashVersion version);
429 QString hashPasswordSha1(const QString &password);
430 bool checkHashedPasswordSha1(const QString &password, const QString &hashedPassword);
432 QString hashPasswordSha2_512(const QString &password);
433 bool checkHashedPasswordSha2_512(const QString &password, const QString &hashedPassword);
434 QString sha2_512(const QString &input);