properly identifying the internal/admin user
[quassel.git] / src / core / storage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef _STORAGE_H_
22 #define _STORAGE_H_
23
24 #include <QtCore>
25
26 #include "types.h"
27 #include "message.h"
28 #include "network.h"
29
30 class Storage : public QObject {
31   Q_OBJECT
32
33   public:
34     Storage(QObject *parent = 0);
35     virtual ~Storage() {};
36
37   public slots:
38     /* General */
39
40     //! Check if the storage type is available.
41     /** A storage subclass should return true if it can be successfully used, i.e. if all
42      *  prerequisites are in place (e.g. we have an appropriate DB driver etc.).
43      * \return True if and only if the storage class can be successfully used.
44      */
45     virtual bool isAvailable() const = 0;
46
47     //! Returns the display name of the storage backend
48     /** \return A string that can be used by the client to name the storage backend */
49     virtual QString displayName() const = 0;
50
51     //! Returns a description of this storage backend
52     /** \return A string that can be displayed by the client to describe the storage backend */
53     virtual QString description() const = 0;
54
55     //! Setup the storage provider.
56     /** This prepares the storage provider (e.g. create tables, etc.) for use within Quassel.
57      *  \param settings   Hostname, port, username, password, ...
58      *  \return True if and only if the storage provider was initialized successfully.
59      */
60     virtual bool setup(const QVariantMap &settings = QVariantMap()) = 0;
61
62     //! Initialize the storage provider
63     /** \param settings   Hostname, port, username, password, ...  
64      *  \return True if and only if the storage provider was initialized successfully.
65      */
66     virtual bool init(const QVariantMap &settings = QVariantMap()) = 0;
67
68     //! Makes temp data persistent
69     /** This Method is periodically called by the Quassel Core to make temporary
70     *  data persistant. This reduces the data loss drastically in the
71     *  unlikely case of a Core crash.
72     */
73     virtual void sync() = 0;
74
75     // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
76
77     /* User handling */
78
79     //! Add a new core user to the storage.
80     /** \param user     The username of the new user
81      *  \param password The cleartext password for the new user
82      *  \return The new user's UserId
83      */
84     virtual UserId addUser(const QString &user, const QString &password) = 0;
85
86     //! Update a core user's password.
87     /** \param user     The user's id
88      *  \param password The user's new password
89      */
90     virtual void updateUser(UserId user, const QString &password) = 0;
91
92     //! Rename a user
93     /** \param user     The user's id
94      *  \param newName  The user's new name
95      */
96     virtual void renameUser(UserId user, const QString &newName) = 0;
97
98     //! Validate a username with a given password.
99     /** \param user     The username to validate
100      *  \param password The user's alleged password
101      *  \return A valid UserId if the password matches the username; 0 else
102      */
103     virtual UserId validateUser(const QString &user, const QString &password) = 0;
104
105     //! Determine the UserId of the internal user
106     /** \return A valid UserId if the password matches the username; 0 else
107      */
108     virtual UserId internalUser() = 0;
109
110     //! Remove a core user from storage.
111     /** \param user     The userid to delete
112      */
113     virtual void delUser(UserId user) = 0;
114
115     //! Store a user setting persistently
116     /**
117      * \param userId       The users Id
118      * \param settingName  The Name of the Setting
119      * \param data         The Value
120      */
121     virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) = 0;
122   
123     //! Retrieve a persistent user setting
124     /**
125      * \param userId       The users Id
126      * \param settingName  The Name of the Setting
127      * \param default      Value to return in case it's unset.
128      * \return the Value of the Setting or the default value if it is unset.
129      */
130     virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant()) = 0;
131   
132     /* Network handling */
133
134     //! Create a new Network in the storage backend and return it unique Id
135     /** \param user        The core user who owns this network
136      *  \param networkInfo The networkInfo holding the network definition
137      *  \return the NetworkId of the newly created Network. Possibly invalid.
138      */
139     virtual NetworkId createNetwork(UserId user, const NetworkInfo &info) = 0;
140
141     //! Apply the changes to NetworkInfo info to the storage engine
142     /**
143      *  \param user        The core user
144      *  \param networkInfo The Updated NetworkInfo
145      *  \return true if successfull.
146      */
147     virtual bool updateNetwork(UserId user, const NetworkInfo &info) = 0;
148
149     //! Permanently remove a Network and all the data associated with it.
150     /** \note This method is thredsafe.
151      *
152      *  \param user        The core user
153      *  \param networkId   The network to delete
154      *  \return true if successfull.
155      */
156     virtual bool removeNetwork(UserId user, const NetworkId &networkId) = 0;
157
158     //! Returns a list of all NetworkInfos for the given UserId user
159     /** \note This method is thredsafe.
160      *
161      *  \param user        The core user
162      *  \return QList<NetworkInfo>.
163      */
164     virtual QList<NetworkInfo> networks(UserId user) = 0;
165   
166     //! Get the unique NetworkId of the network for a user.
167     /** \param user    The core user who owns this network
168      *  \param network The network name
169      *  \return The NetworkId corresponding to the given network, or 0 if not found
170      */
171     virtual NetworkId getNetworkId(UserId user, const QString &network) = 0;
172
173     //! Get a list of Networks to restore
174     /** Return a list of networks the user was connected at the time of core shutdown
175      *  \note This method is threadsafe.
176      *
177      *  \param user  The User Id in question
178      */
179     virtual QList<NetworkId> connectedNetworks(UserId user) = 0;
180
181     //! Update the connected state of a network
182     /** \note This method is threadsafe
183      *
184      *  \param user        The Id of the networks owner
185      *  \param networkId   The Id of the network
186      *  \param isConnected whether the network is connected or not
187      */
188     virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) = 0;
189
190     //! Get a hash of channels with their channel keys for a given network
191     /** The keys are channel names and values are passwords (possibly empty)
192      *  \note This method is threadsafe
193      *
194      *  \param user       The id of the networks owner
195      *  \param networkId  The Id of the network
196      */
197     virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) = 0;
198
199     //! Update the connected state of a channel
200     /** \note This method is threadsafe
201      *
202      *  \param user       The Id of the networks owner
203      *  \param networkId  The Id of the network
204      *  \param channel    The name of the channel
205      *  \param isJoined   whether the channel is connected or not
206      */
207     virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) = 0;
208
209     //! Update the key of a channel
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 channel    The name of the channel
215      *  \param key        The key of the channel (possibly empty)
216      */
217     virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) = 0;
218   
219     /* Buffer handling */
220
221     //! Get the unique BufferInfo for the given combination of network and buffername for a user.
222     /** \param user      The core user who owns this buffername
223      *  \param networkId The network id
224      *  \param type      The type of the buffer (StatusBuffer, Channel, etc.)
225      *  \param buffer  The buffer name (if empty, the net's status buffer is returned)
226      *  \return The BufferInfo corresponding to the given network and buffer name, or an invalid BufferInfo if not found
227      */
228     virtual BufferInfo getBufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "") = 0;
229
230     //! Get the unique BufferInfo for a bufferId
231     /** \param user      The core user who owns this buffername
232      *  \param bufferId  The id of the buffer
233      *  \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
234      */
235     virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) = 0;
236
237     //! Request a list of all buffers known to a user.
238     /** This method is used to get a list of all buffers we have stored a backlog from.
239      *  \param user  The user whose buffers we request
240      *  \return A list of the BufferInfos for all buffers as requested
241      */
242     virtual QList<BufferInfo> requestBuffers(UserId user) = 0;
243
244     //! Request a list of BufferIds for a given NetworkId
245     /** \note This method is threadsafe.
246      *
247      *  \param user  The user whose buffers we request
248      *  \param networkId  The NetworkId of the network in question
249      *  \return List of BufferIds belonging to the Network
250      */
251     virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) = 0;
252
253     //! Remove permanently a buffer and it's content from the storage backend
254     /** This call cannot be reverted!
255      *  \param user      The user who is the owner of the buffer
256      *  \param bufferId  The bufferId
257      *  \return true if successfull
258      */
259     virtual bool removeBuffer(const UserId &user, const BufferId &bufferId) = 0;
260
261     //! Rename a Buffer
262     /** \param user      The id of the buffer owner
263      *  \param networkId The id of the network the buffer belongs to
264      *  \param newName   The new name of the buffer
265      *  \param oldName   The previous name of the buffer
266      *  \return the BufferId of the affected buffer or an invalid BufferId if not successfull
267      */
268     virtual BufferId renameBuffer(const UserId &user, const NetworkId &networkId, const QString &newName, const QString &oldName) = 0;
269   
270     //! Update the LastSeenDate for a Buffer
271     /** This Method is used to make the LastSeenDate of a Buffer persistent
272      * \param user      The Owner of that Buffer
273      * \param bufferId  The buffer id
274      * \param MsgId     The Message id of the message that has been just seen
275      */
276     virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
277
278     //! Get a Hash of all last seen message ids
279     /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
280      * \param user      The Owner of the buffers
281      */
282     virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) = 0;
283
284   
285     /* Message handling */
286
287     //! Store a Message in the backlog.
288     /** \param msg  The message object to be stored
289      *  \return The globally unique id for the stored message
290      */
291     virtual MsgId logMessage(Message msg) = 0;
292
293     //! Request a certain number (or all) messages stored in a given buffer.
294     /** \param buffer   The buffer we request messages from
295      *  \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
296      *  \param offset   Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
297      *  \return The requested list of messages
298      */
299     virtual QList<Message> requestMsgs(UserId user, BufferId buffer, int lastmsgs = -1, int offset = -1) = 0;
300
301     //! Request messages stored in a given buffer since a certain point in time.
302     /** \param buffer   The buffer we request messages from
303      *  \param since    Only return messages newer than this point in time
304      *  \param offset   Do not return messages with MsgId >= offset, if offset >= 0
305      *  \return The requested list of messages
306      */
307     virtual QList<Message> requestMsgs(UserId user, BufferId buffer, QDateTime since, int offset = -1) = 0;
308
309     //! Request a range of messages stored in a given buffer.
310     /** \param buffer   The buffer we request messages from
311      *  \param first    Return messages with first <= MsgId <= last
312      *  \param last     Return messages with first <= MsgId <= last
313      *  \return The requested list of messages
314      */
315     virtual QList<Message> requestMsgRange(UserId user, BufferId buffer, int first, int last) = 0;
316
317   signals:
318     //! Sent when a new BufferInfo is created, or an existing one changed somehow.
319     void bufferInfoUpdated(UserId user, const BufferInfo &);
320     //! Sent when a Buffer was renamed
321     void bufferRenamed(const QString &newName, const QString &oldName);
322     //! Sent when a new user has been added
323     void userAdded(UserId, const QString &username);
324     //! Sent when a user has been renamed
325     void userRenamed(UserId, const QString &newname);
326     //! Sent when a user has been removed
327     void userRemoved(UserId);
328
329   protected:
330     //! when implementing a storage handler, use this method to crypt user passwords.
331     /**  This guarantees compatibility with other storage handlers and allows easy migration
332      */
333     QString cryptedPassword(const QString &password);
334 };
335
336
337 #endif