Add support for SHA2-512 hash on Qt4 builds
[quassel.git] / src / core / storage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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 #ifndef STORAGE_H
22 #define STORAGE_H
23
24 #include <QtCore>
25
26 #include "types.h"
27 #include "coreidentity.h"
28 #include "message.h"
29 #include "network.h"
30
31 class Storage : public QObject
32 {
33     Q_OBJECT
34
35 public:
36     Storage(QObject *parent = 0);
37     virtual ~Storage() {};
38
39     enum State {
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
43     };
44
45     enum HashVersion {
46         Sha1,
47         Sha2_512,
48         Latest=Sha2_512
49         
50     };
51
52 public slots:
53     /* General */
54
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.
59      */
60     virtual bool isAvailable() const = 0;
61
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;
65
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;
69
70     //! Returns a list of properties required to use the storage backend
71     virtual QStringList setupKeys() const = 0;
72
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;
76
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.
81      */
82     virtual bool setup(const QVariantMap &settings = QVariantMap()) = 0;
83
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)
87      */
88     virtual State init(const QVariantMap &settings = QVariantMap()) = 0;
89
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.
94      */
95     virtual void sync() = 0;
96
97     // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
98
99     /* User handling */
100
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
105      */
106     virtual UserId addUser(const QString &user, const QString &password) = 0;
107
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.
112      */
113     virtual bool updateUser(UserId user, const QString &password) = 0;
114
115     //! Rename a user
116     /** \param user     The user's id
117      *  \param newName  The user's new name
118      */
119     virtual void renameUser(UserId user, const QString &newName) = 0;
120
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
125      */
126     virtual UserId validateUser(const QString &user, const QString &password) = 0;
127
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
131      */
132     virtual UserId getUserId(const QString &username) = 0;
133
134     //! Determine the UserId of the internal user
135     /** \return A valid UserId if the password matches the username; 0 else
136      */
137     virtual UserId internalUser() = 0;
138
139     //! Remove a core user from storage.
140     /** \param user     The userid to delete
141      */
142     virtual void delUser(UserId user) = 0;
143
144     //! Store a user setting persistently
145     /**
146      * \param userId       The users Id
147      * \param settingName  The Name of the Setting
148      * \param data         The Value
149      */
150     virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) = 0;
151
152     //! Retrieve a persistent user setting
153     /**
154      * \param userId       The users Id
155      * \param settingName  The Name of the Setting
156      * \param default      Value to return in case it's unset.
157      * \return the Value of the Setting or the default value if it is unset.
158      */
159     virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant()) = 0;
160
161     /* Identity handling */
162     virtual IdentityId createIdentity(UserId user, CoreIdentity &identity) = 0;
163     virtual bool updateIdentity(UserId user, const CoreIdentity &identity) = 0;
164     virtual void removeIdentity(UserId user, IdentityId identityId) = 0;
165     virtual QList<CoreIdentity> identities(UserId user) = 0;
166
167     /* Network handling */
168
169     //! Create a new Network in the storage backend and return it unique Id
170     /** \param user        The core user who owns this network
171      *  \param networkInfo The networkInfo holding the network definition
172      *  \return the NetworkId of the newly created Network. Possibly invalid.
173      */
174     virtual NetworkId createNetwork(UserId user, const NetworkInfo &info) = 0;
175
176     //! Apply the changes to NetworkInfo info to the storage engine
177     /**
178      *  \param user        The core user
179      *  \param networkInfo The Updated NetworkInfo
180      *  \return true if successfull.
181      */
182     virtual bool updateNetwork(UserId user, const NetworkInfo &info) = 0;
183
184     //! Permanently remove a Network and all the data associated with it.
185     /** \note This method is thredsafe.
186      *
187      *  \param user        The core user
188      *  \param networkId   The network to delete
189      *  \return true if successfull.
190      */
191     virtual bool removeNetwork(UserId user, const NetworkId &networkId) = 0;
192
193     //! Returns a list of all NetworkInfos for the given UserId user
194     /** \note This method is thredsafe.
195      *
196      *  \param user        The core user
197      *  \return QList<NetworkInfo>.
198      */
199     virtual QList<NetworkInfo> networks(UserId user) = 0;
200
201     //! Get a list of Networks to restore
202     /** Return a list of networks the user was connected at the time of core shutdown
203      *  \note This method is threadsafe.
204      *
205      *  \param user  The User Id in question
206      */
207     virtual QList<NetworkId> connectedNetworks(UserId user) = 0;
208
209     //! Update the connected state of a network
210     /** \note This method is threadsafe
211      *
212      *  \param user        The Id of the networks owner
213      *  \param networkId   The Id of the network
214      *  \param isConnected whether the network is connected or not
215      */
216     virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) = 0;
217
218     //! Get a hash of channels with their channel keys for a given network
219     /** The keys are channel names and values are passwords (possibly empty)
220      *  \note This method is threadsafe
221      *
222      *  \param user       The id of the networks owner
223      *  \param networkId  The Id of the network
224      */
225     virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) = 0;
226
227     //! Update the connected state of a channel
228     /** \note This method is threadsafe
229      *
230      *  \param user       The Id of the networks owner
231      *  \param networkId  The Id of the network
232      *  \param channel    The name of the channel
233      *  \param isJoined   whether the channel is connected or not
234      */
235     virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) = 0;
236
237     //! Update the key of a channel
238     /** \note This method is threadsafe
239      *
240      *  \param user       The Id of the networks owner
241      *  \param networkId  The Id of the network
242      *  \param channel    The name of the channel
243      *  \param key        The key of the channel (possibly empty)
244      */
245     virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) = 0;
246
247     //! retrieve last known away message for session restore
248     /** \note This method is threadsafe
249      *
250      *  \param user       The Id of the networks owner
251      *  \param networkId  The Id of the network
252      */
253     virtual QString awayMessage(UserId user, NetworkId networkId) = 0;
254
255     //! Make away message persistent for session restore
256     /** \note This method is threadsafe
257      *
258      *  \param user       The Id of the networks owner
259      *  \param networkId  The Id of the network
260      *  \param awayMsg    The current away message of own user
261      */
262     virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) = 0;
263
264     //! retrieve last known user mode for session restore
265     /** \note This method is threadsafe
266      *
267      *  \param user       The Id of the networks owner
268      *  \param networkId  The Id of the network
269      */
270     virtual QString userModes(UserId user, NetworkId networkId) = 0;
271
272     //! Make our user modes persistent for session restore
273     /** \note This method is threadsafe
274      *
275      *  \param user       The Id of the networks owner
276      *  \param networkId  The Id of the network
277      *  \param userModes  The current user modes of own user
278      */
279     virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes) = 0;
280
281     /* Buffer handling */
282
283     //! Get the unique BufferInfo for the given combination of network and buffername for a user.
284     /** \param user      The core user who owns this buffername
285      *  \param networkId The network id
286      *  \param type      The type of the buffer (StatusBuffer, Channel, etc.)
287      *  \param buffer  The buffer name (if empty, the net's status buffer is returned)
288      *  \param create    Whether or not the buffer should be created if it doesnt exist
289      *  \return The BufferInfo corresponding to the given network and buffer name, or an invalid BufferInfo if not found
290      */
291     virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) = 0;
292
293     //! Get the unique BufferInfo for a bufferId
294     /** \param user      The core user who owns this buffername
295      *  \param bufferId  The id of the buffer
296      *  \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
297      */
298     virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) = 0;
299
300     //! Request a list of all buffers known to a user.
301     /** This method is used to get a list of all buffers we have stored a backlog from.
302      *  \param user  The user whose buffers we request
303      *  \return A list of the BufferInfos for all buffers as requested
304      */
305     virtual QList<BufferInfo> requestBuffers(UserId user) = 0;
306
307     //! Request a list of BufferIds for a given NetworkId
308     /** \note This method is threadsafe.
309      *
310      *  \param user  The user whose buffers we request
311      *  \param networkId  The NetworkId of the network in question
312      *  \return List of BufferIds belonging to the Network
313      */
314     virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) = 0;
315
316     //! Remove permanently a buffer and it's content from the storage backend
317     /** This call cannot be reverted!
318      *  \param user      The user who is the owner of the buffer
319      *  \param bufferId  The bufferId
320      *  \return true if successfull
321      */
322     virtual bool removeBuffer(const UserId &user, const BufferId &bufferId) = 0;
323
324     //! Rename a Buffer
325     /** \note This method is threadsafe.
326      *  \param user      The id of the buffer owner
327      *  \param bufferId  The bufferId
328      *  \param newName   The new name of the buffer
329      *  \return true if successfull
330      */
331     virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) = 0;
332
333     //! Merge the content of two Buffers permanently. This cannot be reversed!
334     /** \note This method is threadsafe.
335      *  \param user      The id of the buffer owner
336      *  \param bufferId1 The bufferId of the remaining buffer
337      *  \param bufferId2 The buffer that is about to be removed
338      *  \return true if successfull
339      */
340     virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) = 0;
341
342     //! Update the LastSeenDate for a Buffer
343     /** This Method is used to make the LastSeenDate of a Buffer persistent
344      * \param user      The Owner of that Buffer
345      * \param bufferId  The buffer id
346      * \param MsgId     The Message id of the message that has been just seen
347      */
348     virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
349
350     //! Get a Hash of all last seen message ids
351     /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
352      * \param user      The Owner of the buffers
353      */
354     virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) = 0;
355
356     //! Update the MarkerLineMsgId for a Buffer
357     /** This Method is used to make the marker line position of a Buffer persistent
358      *  \note This method is threadsafe.
359      *
360      * \param user      The Owner of that Buffer
361      * \param bufferId  The buffer id
362      * \param MsgId     The Message id where the marker line should be placed
363      */
364     virtual void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
365
366     //! Get a Hash of all marker line message ids
367     /** This Method is called when the Quassel Core is started to restore the MarkerLineMsgIds
368      *  \note This method is threadsafe.
369      *
370      * \param user      The Owner of the buffers
371      */
372     virtual QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) = 0;
373
374     /* Message handling */
375
376     //! Store a Message in the storage backend and set its unique Id.
377     /** \param msg  The message object to be stored
378      *  \return true on success
379      */
380     virtual bool logMessage(Message &msg) = 0;
381
382     //! Store a list of Messages in the storage backend and set their unique Id.
383     /** \param msgs The list message objects to be stored
384      *  \return true on success
385      */
386     virtual bool logMessages(MessageList &msgs) = 0;
387
388     //! Request a certain number messages stored in a given buffer.
389     /** \param buffer   The buffer we request messages from
390      *  \param first    if != -1 return only messages with a MsgId >= first
391      *  \param last     if != -1 return only messages with a MsgId < last
392      *  \param limit    if != -1 limit the returned list to a max of \limit entries
393      *  \return The requested list of messages
394      */
395     virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
396
397     //! Request a certain number of messages across all buffers
398     /** \param first    if != -1 return only messages with a MsgId >= first
399      *  \param last     if != -1 return only messages with a MsgId < last
400      *  \param limit    Max amount of messages
401      *  \return The requested list of messages
402      */
403     virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
404
405 signals:
406     //! Sent when a new BufferInfo is created, or an existing one changed somehow.
407     void bufferInfoUpdated(UserId user, const BufferInfo &);
408     //! Sent when a Buffer was renamed
409     void bufferRenamed(const QString &newName, const QString &oldName);
410     //! Sent when a new user has been added
411     void userAdded(UserId, const QString &username);
412     //! Sent when a user has been renamed
413     void userRenamed(UserId, const QString &newname);
414     //! Sent when a user has been removed
415     void userRemoved(UserId);
416
417 protected:
418     QString hashPassword(const QString &password);
419     bool checkHashedPassword(const UserId user, const QString &password, const QString &hashedPassword, const Storage::HashVersion version);
420
421 private:
422     QString hashPasswordSha1(const QString &password);
423     bool checkHashedPasswordSha1(const QString &password, const QString &hashedPassword);
424
425     QString hashPasswordSha2_512(const QString &password);
426     bool checkHashedPasswordSha2_512(const QString &password, const QString &hashedPassword);
427     QString sha2_512(const QString &input);
428 };
429
430
431 #endif