1 /***************************************************************************
2 * Copyright (C) 2005-08 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
31 # include <QSslSocket>
32 # include "sslserver.h"
34 # include <QTcpSocket>
35 # include <QTcpServer>
38 #include "bufferinfo.h"
40 #include "sessionthread.h"
48 class Core : public QObject {
52 static Core * instance();
53 static void destroy();
55 static void saveState();
56 static void restoreState();
58 /*** Storage access ***/
59 // These methods are threadsafe.
61 //! Store a user setting persistently
63 * \param userId The users Id
64 * \param settingName The Name of the Setting
65 * \param data The Value
67 static void setUserSetting(UserId userId, const QString &settingName, const QVariant &data);
69 //! Retrieve a persistent user setting
71 * \param userId The users Id
72 * \param settingName The Name of the Setting
73 * \param default Value to return in case it's unset.
74 * \return the Value of the Setting or the default value if it is unset.
76 static QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant());
79 //! Create a Network in the Storage and store it's Id in the given NetworkInfo
80 /** \note This method is thredsafe.
82 * \param user The core user
83 * \param networkInfo a NetworkInfo definition to store the newly created ID in
84 * \return true if successfull.
86 static bool createNetwork(UserId user, NetworkInfo &info);
88 //! Apply the changes to NetworkInfo info to the storage engine
89 /** \note This method is thredsafe.
91 * \param user The core user
92 * \param networkInfo The Updated NetworkInfo
93 * \return true if successfull.
95 static bool updateNetwork(UserId user, const NetworkInfo &info);
97 //! Permanently remove a Network and all the data associated with it.
98 /** \note This method is thredsafe.
100 * \param user The core user
101 * \param networkId The network to delete
102 * \return true if successfull.
104 static bool removeNetwork(UserId user, const NetworkId &networkId);
106 //! Returns a list of all NetworkInfos for the given UserId user
107 /** \note This method is thredsafe.
109 * \param user The core user
110 * \return QList<NetworkInfo>.
112 static QList<NetworkInfo> networks(UserId user);
114 //! Get the NetworkId for a network name.
115 /** \note This method is threadsafe.
117 * \param user The core user
118 * \param network The name of the network
119 * \return The NetworkId corresponding to the given network.
121 static NetworkId networkId(UserId user, const QString &network);
123 //! Get a list of Networks to restore
124 /** Return a list of networks the user was connected at the time of core shutdown
125 * \note This method is threadsafe.
127 * \param user The User Id in question
129 static QList<NetworkId> connectedNetworks(UserId user);
131 //! Update the connected state of a network
132 /** \note This method is threadsafe
134 * \param user The Id of the networks owner
135 * \param networkId The Id of the network
136 * \param isConnected whether the network is connected or not
138 static void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected);
140 //! Get a hash of channels with their channel keys for a given network
141 /** The keys are channel names and values are passwords (possibly empty)
142 * \note This method is threadsafe
144 * \param user The id of the networks owner
145 * \param networkId The Id of the network
147 static QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId);
149 //! Update the connected state of a channel
150 /** \note This method is threadsafe
152 * \param user The Id of the networks owner
153 * \param networkId The Id of the network
154 * \param channel The name of the channel
155 * \param isJoined whether the channel is connected or not
157 static void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined);
159 //! Update the key of a channel
160 /** \note This method is threadsafe
162 * \param user The Id of the networks owner
163 * \param networkId The Id of the network
164 * \param channel The name of the channel
165 * \param key The key of the channel (possibly empty)
167 static void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key);
169 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
170 /** \note This method is threadsafe.
172 * \param user The core user who owns this buffername
173 * \param networkId The network id
174 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
175 * \param buffer The buffer name (if empty, the net's status buffer is returned)
176 * \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
178 static BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type, const QString &buffer = "");
180 //! Get the unique BufferInfo for a bufferId
181 /** \note This method is threadsafe
182 * \param user The core user who owns this buffername
183 * \param bufferId The id of the buffer
184 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
186 static BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
189 //! Store a Message in the backlog.
190 /** \note This method is threadsafe.
192 * \param msg The message object to be stored
193 * \return The globally unique id for the stored message
195 static MsgId storeMessage(const Message &message);
197 //! Request a certain number (or all) messages stored in a given buffer.
198 /** \note This method is threadsafe.
200 * \param buffer The buffer we request messages from
201 * \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
202 * \param offset Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
203 * \return The requested list of messages
205 static QList<Message> requestMsgs(UserId user, BufferId buffer, int lastmsgs = -1, int offset = -1);
207 //! Request messages stored in a given buffer since a certain point in time.
208 /** \note This method is threadsafe.
210 * \param buffer The buffer we request messages from
211 * \param since Only return messages newer than this point in time
212 * \param offset Do not return messages with MsgId >= offset, if offset >= 0
213 * \return The requested list of messages
215 static QList<Message> requestMsgs(UserId user, BufferId buffer, QDateTime since, int offset = -1);
217 //! Request a range of messages stored in a given buffer.
218 /** \note This method is threadsafe.
220 * \param buffer The buffer we request messages from
221 * \param first Return messages with first <= MsgId <= last
222 * \param last Return messages with first <= MsgId <= last
223 * \return The requested list of messages
225 static QList<Message> requestMsgRange(UserId user, BufferId buffer, int first, int last);
227 //! Request a list of all buffers known to a user.
228 /** This method is used to get a list of all buffers we have stored a backlog from.
229 * \note This method is threadsafe.
231 * \param user The user whose buffers we request
232 * \return A list of the BufferInfos for all buffers as requested
234 static QList<BufferInfo> requestBuffers(UserId user);
237 //! Request a list of BufferIds for a given NetworkId
238 /** \note This method is threadsafe.
240 * \param user The user whose buffers we request
241 * \param networkId The NetworkId of the network in question
242 * \return List of BufferIds belonging to the Network
244 static QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId);
246 //! Remove permanently a buffer and it's content from the storage backend
247 /** This call cannot be reverted!
248 * \note This method is threadsafe.
250 * \param user The user who is the owner of the buffer
251 * \param bufferId The bufferId
252 * \return true if successfull
254 static bool removeBuffer(const UserId &user, const BufferId &bufferId);
257 /** \note This method is threadsafe.
258 * \param user The id of the buffer owner
259 * \param networkId The id of the network the buffer belongs to
260 * \param newName The new name of the buffer
261 * \param oldName The previous name of the buffer
262 * \return the BufferId of the affected buffer or an invalid BufferId if not successfull
264 static BufferId renameBuffer(const UserId &user, const NetworkId &networkId, const QString &newName, const QString &oldName);
266 //! Update the LastSeenDate for a Buffer
267 /** This Method is used to make the LastSeenDate of a Buffer persistent
268 * \note This method is threadsafe.
270 * \param user The Owner of that Buffer
271 * \param bufferId The buffer id
272 * \param MsgId The Message id of the message that has been just seen
274 static void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
276 //! Get a Hash of all last seen message ids
277 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
278 * \note This method is threadsafe.
280 * \param user The Owner of the buffers
282 static QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user);
284 const QDateTime &startTime() const { return _startTime; }
287 //! Make storage data persistent
288 /** \note This method is threadsafe.
293 //! Sent when a BufferInfo is updated in storage.
294 void bufferInfoUpdated(UserId user, const BufferInfo &info);
297 bool startListening();
298 void stopListening();
299 void incomingConnection();
300 void clientHasData();
301 void clientDisconnected();
303 bool initStorage(QVariantMap dbSettings, bool setup = false);
306 void sslErrors(const QList<QSslError> &errors);
308 void socketError(QAbstractSocket::SocketError);
314 static Core *instanceptr;
316 SessionThread *createSession(UserId userId, bool restoreState = false);
317 void setupClientSession(QTcpSocket *socket, UserId uid);
318 void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
319 //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
320 QString setupCore(const QVariant &setupData);
322 bool registerStorageBackend(Storage *);
323 void unregisterStorageBackend(Storage *);
325 QHash<UserId, SessionThread *> sessions;
327 QTimer _storageSyncTimer;
330 SslServer _server, _v6server;
332 QTcpServer _server, _v6server;
335 QHash<QTcpSocket *, quint32> blocksizes;
336 QHash<QTcpSocket *, QVariantMap> clientInfo;
338 QHash<QString, Storage *> _storageBackends;
340 QDateTime _startTime;