1 /***************************************************************************
2 * Copyright (C) 2005-2015 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
47 #if QT_VERSION >= 0x050000
58 //! Check if the storage type is available.
59 /** A storage subclass should return true if it can be successfully used, i.e. if all
60 * prerequisites are in place (e.g. we have an appropriate DB driver etc.).
61 * \return True if and only if the storage class can be successfully used.
63 virtual bool isAvailable() const = 0;
65 //! Returns the display name of the storage backend
66 /** \return A string that can be used by the client to name the storage backend */
67 virtual QString displayName() const = 0;
69 //! Returns a description of this storage backend
70 /** \return A string that can be displayed by the client to describe the storage backend */
71 virtual QString description() const = 0;
73 //! Returns a list of properties required to use the storage backend
74 virtual QStringList setupKeys() const = 0;
76 //! Returns a map where the keys are are properties to use the storage backend
77 /* the values are QVariants with default values */
78 virtual QVariantMap setupDefaults() const = 0;
80 //! Setup the storage provider.
81 /** This prepares the storage provider (e.g. create tables, etc.) for use within Quassel.
82 * \param settings Hostname, port, username, password, ...
83 * \return True if and only if the storage provider was initialized successfully.
85 virtual bool setup(const QVariantMap &settings = QVariantMap()) = 0;
87 //! Initialize the storage provider
88 /** \param settings Hostname, port, username, password, ...
89 * \return the State the storage backend is now in (see Storage::State)
91 virtual State init(const QVariantMap &settings = QVariantMap()) = 0;
93 //! Makes temp data persistent
94 /** This Method is periodically called by the Quassel Core to make temporary
95 * data persistant. This reduces the data loss drastically in the
96 * unlikely case of a Core crash.
98 virtual void sync() = 0;
100 // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
104 //! Add a new core user to the storage.
105 /** \param user The username of the new user
106 * \param password The cleartext password for the new user
107 * \return The new user's UserId
109 virtual UserId addUser(const QString &user, const QString &password) = 0;
111 //! Update a core user's password.
112 /** \param user The user's id
113 * \param password The user's new password
114 * \return true on success.
116 virtual bool updateUser(UserId user, const QString &password) = 0;
119 /** \param user The user's id
120 * \param newName The user's new name
122 virtual void renameUser(UserId user, const QString &newName) = 0;
124 //! Validate a username with a given password.
125 /** \param user The username to validate
126 * \param password The user's alleged password
127 * \return A valid UserId if the password matches the username; 0 else
129 virtual UserId validateUser(const QString &user, const QString &password) = 0;
131 //! Check if a user with given username exists. Do not use for login purposes!
132 /** \param username The username to validate
133 * \return A valid UserId if the user exists; 0 else
135 virtual UserId getUserId(const QString &username) = 0;
137 //! Determine the UserId of the internal user
138 /** \return A valid UserId if the password matches the username; 0 else
140 virtual UserId internalUser() = 0;
142 //! Remove a core user from storage.
143 /** \param user The userid to delete
145 virtual void delUser(UserId user) = 0;
147 //! Store a user setting persistently
149 * \param userId The users Id
150 * \param settingName The Name of the Setting
151 * \param data The Value
153 virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) = 0;
155 //! Retrieve a persistent user setting
157 * \param userId The users Id
158 * \param settingName The Name of the Setting
159 * \param default Value to return in case it's unset.
160 * \return the Value of the Setting or the default value if it is unset.
162 virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant()) = 0;
164 /* Identity handling */
165 virtual IdentityId createIdentity(UserId user, CoreIdentity &identity) = 0;
166 virtual bool updateIdentity(UserId user, const CoreIdentity &identity) = 0;
167 virtual void removeIdentity(UserId user, IdentityId identityId) = 0;
168 virtual QList<CoreIdentity> identities(UserId user) = 0;
170 /* Network handling */
172 //! Create a new Network in the storage backend and return it unique Id
173 /** \param user The core user who owns this network
174 * \param networkInfo The networkInfo holding the network definition
175 * \return the NetworkId of the newly created Network. Possibly invalid.
177 virtual NetworkId createNetwork(UserId user, const NetworkInfo &info) = 0;
179 //! Apply the changes to NetworkInfo info to the storage engine
181 * \param user The core user
182 * \param networkInfo The Updated NetworkInfo
183 * \return true if successfull.
185 virtual bool updateNetwork(UserId user, const NetworkInfo &info) = 0;
187 //! Permanently remove a Network and all the data associated with it.
188 /** \note This method is thredsafe.
190 * \param user The core user
191 * \param networkId The network to delete
192 * \return true if successfull.
194 virtual bool removeNetwork(UserId user, const NetworkId &networkId) = 0;
196 //! Returns a list of all NetworkInfos for the given UserId user
197 /** \note This method is thredsafe.
199 * \param user The core user
200 * \return QList<NetworkInfo>.
202 virtual QList<NetworkInfo> networks(UserId user) = 0;
204 //! Get a list of Networks to restore
205 /** Return a list of networks the user was connected at the time of core shutdown
206 * \note This method is threadsafe.
208 * \param user The User Id in question
210 virtual QList<NetworkId> connectedNetworks(UserId user) = 0;
212 //! Update the connected state of a network
213 /** \note This method is threadsafe
215 * \param user The Id of the networks owner
216 * \param networkId The Id of the network
217 * \param isConnected whether the network is connected or not
219 virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) = 0;
221 //! Get a hash of channels with their channel keys for a given network
222 /** The keys are channel names and values are passwords (possibly empty)
223 * \note This method is threadsafe
225 * \param user The id of the networks owner
226 * \param networkId The Id of the network
228 virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) = 0;
230 //! Update the connected state of a channel
231 /** \note This method is threadsafe
233 * \param user The Id of the networks owner
234 * \param networkId The Id of the network
235 * \param channel The name of the channel
236 * \param isJoined whether the channel is connected or not
238 virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) = 0;
240 //! Update the key 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 key The key of the channel (possibly empty)
248 virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) = 0;
250 //! retrieve last known away message for session restore
251 /** \note This method is threadsafe
253 * \param user The Id of the networks owner
254 * \param networkId The Id of the network
256 virtual QString awayMessage(UserId user, NetworkId networkId) = 0;
258 //! Make away message persistent for session restore
259 /** \note This method is threadsafe
261 * \param user The Id of the networks owner
262 * \param networkId The Id of the network
263 * \param awayMsg The current away message of own user
265 virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) = 0;
267 //! retrieve last known user mode for session restore
268 /** \note This method is threadsafe
270 * \param user The Id of the networks owner
271 * \param networkId The Id of the network
273 virtual QString userModes(UserId user, NetworkId networkId) = 0;
275 //! Make our user modes persistent for session restore
276 /** \note This method is threadsafe
278 * \param user The Id of the networks owner
279 * \param networkId The Id of the network
280 * \param userModes The current user modes of own user
282 virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes) = 0;
284 /* Buffer handling */
286 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
287 /** \param user The core user who owns this buffername
288 * \param networkId The network id
289 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
290 * \param buffer The buffer name (if empty, the net's status buffer is returned)
291 * \param create Whether or not the buffer should be created if it doesnt exist
292 * \return The BufferInfo corresponding to the given network and buffer name, or an invalid BufferInfo if not found
294 virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) = 0;
296 //! Get the unique BufferInfo for a bufferId
297 /** \param user The core user who owns this buffername
298 * \param bufferId The id of the buffer
299 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
301 virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) = 0;
303 //! Request a list of all buffers known to a user.
304 /** This method is used to get a list of all buffers we have stored a backlog from.
305 * \param user The user whose buffers we request
306 * \return A list of the BufferInfos for all buffers as requested
308 virtual QList<BufferInfo> requestBuffers(UserId user) = 0;
310 //! Request a list of BufferIds for a given NetworkId
311 /** \note This method is threadsafe.
313 * \param user The user whose buffers we request
314 * \param networkId The NetworkId of the network in question
315 * \return List of BufferIds belonging to the Network
317 virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) = 0;
319 //! Remove permanently a buffer and it's content from the storage backend
320 /** This call cannot be reverted!
321 * \param user The user who is the owner of the buffer
322 * \param bufferId The bufferId
323 * \return true if successfull
325 virtual bool removeBuffer(const UserId &user, const BufferId &bufferId) = 0;
328 /** \note This method is threadsafe.
329 * \param user The id of the buffer owner
330 * \param bufferId The bufferId
331 * \param newName The new name of the buffer
332 * \return true if successfull
334 virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) = 0;
336 //! Merge the content of two Buffers permanently. This cannot be reversed!
337 /** \note This method is threadsafe.
338 * \param user The id of the buffer owner
339 * \param bufferId1 The bufferId of the remaining buffer
340 * \param bufferId2 The buffer that is about to be removed
341 * \return true if successfull
343 virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) = 0;
345 //! Update the LastSeenDate for a Buffer
346 /** This Method is used to make the LastSeenDate of a Buffer persistent
347 * \param user The Owner of that Buffer
348 * \param bufferId The buffer id
349 * \param MsgId The Message id of the message that has been just seen
351 virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
353 //! Get a Hash of all last seen message ids
354 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
355 * \param user The Owner of the buffers
357 virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) = 0;
359 //! Update the MarkerLineMsgId for a Buffer
360 /** This Method is used to make the marker line position of a Buffer persistent
361 * \note This method is threadsafe.
363 * \param user The Owner of that Buffer
364 * \param bufferId The buffer id
365 * \param MsgId The Message id where the marker line should be placed
367 virtual void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
369 //! Get a Hash of all marker line message ids
370 /** This Method is called when the Quassel Core is started to restore the MarkerLineMsgIds
371 * \note This method is threadsafe.
373 * \param user The Owner of the buffers
375 virtual QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) = 0;
377 /* Message handling */
379 //! Store a Message in the storage backend and set its unique Id.
380 /** \param msg The message object to be stored
381 * \return true on success
383 virtual bool logMessage(Message &msg) = 0;
385 //! Store a list of Messages in the storage backend and set their unique Id.
386 /** \param msgs The list message objects to be stored
387 * \return true on success
389 virtual bool logMessages(MessageList &msgs) = 0;
391 //! Request a certain number messages stored in a given buffer.
392 /** \param buffer The buffer we request messages from
393 * \param first if != -1 return only messages with a MsgId >= first
394 * \param last if != -1 return only messages with a MsgId < last
395 * \param limit if != -1 limit the returned list to a max of \limit entries
396 * \return The requested list of messages
398 virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
400 //! Request a certain number of messages across all buffers
401 /** \param first if != -1 return only messages with a MsgId >= first
402 * \param last if != -1 return only messages with a MsgId < last
403 * \param limit Max amount of messages
404 * \return The requested list of messages
406 virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
409 //! Sent when a new BufferInfo is created, or an existing one changed somehow.
410 void bufferInfoUpdated(UserId user, const BufferInfo &);
411 //! Sent when a Buffer was renamed
412 void bufferRenamed(const QString &newName, const QString &oldName);
413 //! Sent when a new user has been added
414 void userAdded(UserId, const QString &username);
415 //! Sent when a user has been renamed
416 void userRenamed(UserId, const QString &newname);
417 //! Sent when a user has been removed
418 void userRemoved(UserId);
421 QString hashPassword(const QString &password);
422 bool checkHashedPassword(const UserId user, const QString &password, const QString &hashedPassword, const Storage::HashVersion version);
425 QString hashPasswordSha1(const QString &password);
426 bool checkHashedPasswordSha1(const QString &password, const QString &hashedPassword);
428 #if QT_VERSION >= 0x050000
429 QString hashPasswordSha2_512(const QString &password);
430 bool checkHashedPasswordSha2_512(const QString &password, const QString &hashedPassword);