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 ***************************************************************************/
32 #include "sslserver.h"
38 #include "bufferinfo.h"
41 #include "sessionthread.h"
49 class Core : public QObject {
53 static Core * instance();
54 static void destroy();
56 static void saveState();
57 static void restoreState();
59 /*** Storage access ***/
60 // These methods are threadsafe.
62 //! Store a user setting persistently
64 * \param userId The users Id
65 * \param settingName The Name of the Setting
66 * \param data The Value
68 static void setUserSetting(UserId userId, const QString &settingName, const QVariant &data);
70 //! Retrieve a persistent user setting
72 * \param userId The users Id
73 * \param settingName The Name of the Setting
74 * \param default Value to return in case it's unset.
75 * \return the Value of the Setting or the default value if it is unset.
77 static QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant());
80 //! Create a Network in the Storage and store it's Id in the given NetworkInfo
81 /** \note This method is thredsafe.
83 * \param user The core user
84 * \param networkInfo a NetworkInfo definition to store the newly created ID in
85 * \return true if successfull.
87 static bool createNetwork(UserId user, NetworkInfo &info);
89 //! Apply the changes to NetworkInfo info to the storage engine
90 /** \note This method is thredsafe.
92 * \param user The core user
93 * \param networkInfo The Updated NetworkInfo
94 * \return true if successfull.
96 static bool updateNetwork(UserId user, const NetworkInfo &info);
98 //! Permanently remove a Network and all the data associated with it.
99 /** \note This method is thredsafe.
101 * \param user The core user
102 * \param networkId The network to delete
103 * \return true if successfull.
105 static bool removeNetwork(UserId user, const NetworkId &networkId);
107 //! Returns a list of all NetworkInfos for the given UserId user
108 /** \note This method is thredsafe.
110 * \param user The core user
111 * \return QList<NetworkInfo>.
113 static QList<NetworkInfo> networks(UserId user);
115 //! Get the NetworkId for a network name.
116 /** \note This method is threadsafe.
118 * \param user The core user
119 * \param network The name of the network
120 * \return The NetworkId corresponding to the given network.
122 static NetworkId networkId(UserId user, const QString &network);
124 //! Get a list of Networks to restore
125 /** Return a list of networks the user was connected at the time of core shutdown
126 * \note This method is threadsafe.
128 * \param user The User Id in question
130 static QList<NetworkId> connectedNetworks(UserId user);
132 //! Update the connected state of a network
133 /** \note This method is threadsafe
135 * \param user The Id of the networks owner
136 * \param networkId The Id of the network
137 * \param isConnected whether the network is connected or not
139 static void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected);
141 //! Get a hash of channels with their channel keys for a given network
142 /** The keys are channel names and values are passwords (possibly empty)
143 * \note This method is threadsafe
145 * \param user The id of the networks owner
146 * \param networkId The Id of the network
148 static QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId);
150 //! Update the connected state of a channel
151 /** \note This method is threadsafe
153 * \param user The Id of the networks owner
154 * \param networkId The Id of the network
155 * \param channel The name of the channel
156 * \param isJoined whether the channel is connected or not
158 static void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined);
160 //! Update the key of a channel
161 /** \note This method is threadsafe
163 * \param user The Id of the networks owner
164 * \param networkId The Id of the network
165 * \param channel The name of the channel
166 * \param key The key of the channel (possibly empty)
168 static void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key);
170 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
171 /** \note This method is threadsafe.
173 * \param user The core user who owns this buffername
174 * \param networkId The network id
175 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
176 * \param buffer The buffer name (if empty, the net's status buffer is returned)
177 * \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
179 static BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type, const QString &buffer = "");
181 //! Get the unique BufferInfo for a bufferId
182 /** \note This method is threadsafe
183 * \param user The core user who owns this buffername
184 * \param bufferId The id of the buffer
185 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
187 static BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
190 //! Store a Message in the backlog.
191 /** \note This method is threadsafe.
193 * \param msg The message object to be stored
194 * \return The globally unique id for the stored message
196 static MsgId storeMessage(const Message &message);
198 //! Request a certain number (or all) messages stored in a given buffer.
199 /** \note This method is threadsafe.
201 * \param buffer The buffer we request messages from
202 * \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
203 * \param offset Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
204 * \return The requested list of messages
206 static QList<Message> requestMsgs(UserId user, BufferId buffer, int lastmsgs = -1, int offset = -1);
208 //! Request messages stored in a given buffer since a certain point in time.
209 /** \note This method is threadsafe.
211 * \param buffer The buffer we request messages from
212 * \param since Only return messages newer than this point in time
213 * \param offset Do not return messages with MsgId >= offset, if offset >= 0
214 * \return The requested list of messages
216 static QList<Message> requestMsgs(UserId user, BufferId buffer, QDateTime since, int offset = -1);
218 //! Request a range of messages stored in a given buffer.
219 /** \note This method is threadsafe.
221 * \param buffer The buffer we request messages from
222 * \param first Return messages with first <= MsgId <= last
223 * \param last Return messages with first <= MsgId <= last
224 * \return The requested list of messages
226 static QList<Message> requestMsgRange(UserId user, BufferId buffer, int first, int last);
228 //! Request a list of all buffers known to a user.
229 /** This method is used to get a list of all buffers we have stored a backlog from.
230 * \note This method is threadsafe.
232 * \param user The user whose buffers we request
233 * \return A list of the BufferInfos for all buffers as requested
235 static QList<BufferInfo> requestBuffers(UserId user);
237 //! Remove permanently a buffer and it's content from the storage backend
238 /** This call cannot be reverted!
239 * \note This method is threadsafe.
241 * \param user The user who is the owner of the buffer
242 * \param bufferId The bufferId
243 * \return true if successfull
245 static bool removeBuffer(const UserId &user, const BufferId &bufferId);
248 /** \note This method is threadsafe.
249 * \param user The id of the buffer owner
250 * \param networkId The id of the network the buffer belongs to
251 * \param newName The new name of the buffer
252 * \param oldName The previous name of the buffer
253 * \return the BufferId of the affected buffer or an invalid BufferId if not successfull
255 static BufferId renameBuffer(const UserId &user, const NetworkId &networkId, const QString &newName, const QString &oldName);
257 //! Update the LastSeenDate for a Buffer
258 /** This Method is used to make the LastSeenDate of a Buffer persistent
259 * \note This method is threadsafe.
261 * \param user The Owner of that Buffer
262 * \param bufferId The buffer id
263 * \param MsgId The Message id of the message that has been just seen
265 static void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
267 //! Get a Hash of all last seen message ids
268 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
269 * \note This method is threadsafe.
271 * \param user The Owner of the buffers
273 static QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user);
276 //! Make storage data persistent
277 /** \note This method is threadsafe.
282 //! Sent when a BufferInfo is updated in storage.
283 void bufferInfoUpdated(UserId user, const BufferInfo &info);
286 bool startListening(uint port = Global::defaultPort);
287 void stopListening();
288 void incomingConnection();
289 void clientHasData();
290 void clientDisconnected();
292 bool initStorage(QVariantMap dbSettings, bool setup = false);
294 #ifndef QT_NO_OPENSSL
295 void sslErrors(const QList<QSslError> &errors);
297 void socketError(QAbstractSocket::SocketError);
303 static Core *instanceptr;
305 SessionThread *createSession(UserId userId, bool restoreState = false);
306 void setupClientSession(QTcpSocket *socket, UserId uid);
307 void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
308 //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
309 QString setupCore(const QVariant &setupData);
311 bool registerStorageBackend(Storage *);
312 void unregisterStorageBackend(Storage *);
314 QHash<UserId, SessionThread *> sessions;
316 QTimer _storageSyncTimer;
318 #ifndef QT_NO_OPENSSL
324 QHash<QTcpSocket *, quint32> blocksizes;
325 QHash<QTcpSocket *, QVariantMap> clientInfo;
327 QHash<QString, Storage *> _storageBackends;