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