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 //! Create a Network in the Storage and store it's Id in the given NetworkInfo
63 /** \note This method is thredsafe.
65 * \param user The core user
66 * \param networkInfo a NetworkInfo definition to store the newly created ID in
67 * \return true if successfull.
69 static bool createNetwork(UserId user, NetworkInfo &info);
71 //! Apply the changes to NetworkInfo info to the storage engine
72 /** \note This method is thredsafe.
74 * \param user The core user
75 * \param networkInfo The Updated NetworkInfo
76 * \return true if successfull.
78 static bool updateNetwork(UserId user, const NetworkInfo &info);
80 //! Permanently remove a Network and all the data associated with it.
81 /** \note This method is thredsafe.
83 * \param user The core user
84 * \param networkId The network to delete
85 * \return true if successfull.
87 static bool removeNetwork(UserId user, const NetworkId &networkId);
89 //! Returns a list of all NetworkInfos for the given UserId user
90 /** \note This method is thredsafe.
92 * \param user The core user
93 * \return QList<NetworkInfo>.
95 static QList<NetworkInfo> networks(UserId user);
97 //! Get the NetworkId for a network name.
98 /** \note This method is threadsafe.
100 * \param user The core user
101 * \param network The name of the network
102 * \return The NetworkId corresponding to the given network.
104 static NetworkId networkId(UserId user, const QString &network);
106 //! Get a list of Networks to restore
107 /** Return a list of networks the user was connected at the time of core shutdown
108 * \note This method is threadsafe.
110 * \param user The User Id in question
112 static QList<NetworkId> connectedNetworks(UserId user);
114 //! Update the connected state of a network
115 /** \note This method is threadsafe
117 * \param user The Id of the networks owner
118 * \param networkId The Id of the network
119 * \param isConnected whether the network is connected or not
121 static void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected);
123 //! Get a hash of channels with their channel keys for a given network
124 /** The keys are channel names and values are passwords (possibly empty)
125 * \note This method is threadsafe
127 * \param user The id of the networks owner
128 * \param networkId The Id of the network
130 static QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId);
132 //! Update the connected state of a channel
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 channel The name of the channel
138 * \param isJoined whether the channel is connected or not
140 static void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined);
142 //! Update the key of a channel
143 /** \note This method is threadsafe
145 * \param user The Id of the networks owner
146 * \param networkId The Id of the network
147 * \param channel The name of the channel
148 * \param key The key of the channel (possibly empty)
150 static void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key);
152 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
153 /** \note This method is threadsafe.
155 * \param user The core user who owns this buffername
156 * \param networkId The network id
157 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
158 * \param buffer The buffer name (if empty, the net's status buffer is returned)
159 * \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
161 static BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type, const QString &buffer = "");
163 //! Get the unique BufferInfo for a bufferId
164 /** \note This method is threadsafe
165 * \param user The core user who owns this buffername
166 * \param bufferId The id of the buffer
167 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
169 static BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
172 //! Store a Message in the backlog.
173 /** \note This method is threadsafe.
175 * \param msg The message object to be stored
176 * \return The globally unique id for the stored message
178 static MsgId storeMessage(const Message &message);
180 //! Request a certain number (or all) messages stored in a given buffer.
181 /** \note This method is threadsafe.
183 * \param buffer The buffer we request messages from
184 * \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
185 * \param offset Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
186 * \return The requested list of messages
188 static QList<Message> requestMsgs(UserId user, BufferId buffer, int lastmsgs = -1, int offset = -1);
190 //! Request messages stored in a given buffer since a certain point in time.
191 /** \note This method is threadsafe.
193 * \param buffer The buffer we request messages from
194 * \param since Only return messages newer than this point in time
195 * \param offset Do not return messages with MsgId >= offset, if offset >= 0
196 * \return The requested list of messages
198 static QList<Message> requestMsgs(UserId user, BufferId buffer, QDateTime since, int offset = -1);
200 //! Request a range of messages stored in a given buffer.
201 /** \note This method is threadsafe.
203 * \param buffer The buffer we request messages from
204 * \param first Return messages with first <= MsgId <= last
205 * \param last Return messages with first <= MsgId <= last
206 * \return The requested list of messages
208 static QList<Message> requestMsgRange(UserId user, BufferId buffer, int first, int last);
210 //! Request a list of all buffers known to a user since a certain point in time.
211 /** This method is used to get a list of all buffers we have stored a backlog from.
212 * Optionally, a QDateTime can be given, so that only buffers are listed that were active
213 * since that point in time.
214 * \note This method is threadsafe.
216 * \param user The user whose buffers we request
217 * \param since If this is defined, older buffers will be ignored
218 * \return A list of the BufferInfos for all buffers as requested
220 static QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime());
222 //! Remove permanently a buffer and it's content from the storage backend
223 /** This call cannot be reverted!
224 * \note This method is threadsafe.
226 * \param user The user who is the owner of the buffer
227 * \param bufferId The bufferId
228 * \return true if successfull
230 static bool removeBuffer(const UserId &user, const BufferId &bufferId);
233 /** \note This method is threadsafe.
234 * \param user The id of the buffer owner
235 * \param networkId The id of the network the buffer belongs to
236 * \param newName The new name of the buffer
237 * \param oldName The previous name of the buffer
238 * \return the BufferId of the affected buffer or an invalid BufferId if not successfull
240 static BufferId renameBuffer(const UserId &user, const NetworkId &networkId, const QString &newName, const QString &oldName);
242 //! Update the LastSeenDate for a Buffer
243 /** This Method is used to make the LastSeenDate of a Buffer persistent
244 * \note This method is threadsafe.
246 * \param user The Owner of that Buffer
247 * \param bufferId The buffer id
248 * \param MsgId The Message id of the message that has been just seen
250 static void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
252 //! Get a Hash of all last seen message ids
253 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
254 * \note This method is threadsafe.
256 * \param user The Owner of the buffers
258 static QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user);
261 //! Make storage data persistent
262 /** \note This method is threadsafe.
267 //! Sent when a BufferInfo is updated in storage.
268 void bufferInfoUpdated(UserId user, const BufferInfo &info);
271 bool startListening(uint port = Global::defaultPort);
272 void stopListening();
273 void incomingConnection();
274 void clientHasData();
275 void clientDisconnected();
277 bool initStorage(QVariantMap dbSettings, bool setup = false);
279 #ifndef QT_NO_OPENSSL
280 void sslErrors(const QList<QSslError> &errors);
282 void socketError(QAbstractSocket::SocketError);
288 static Core *instanceptr;
290 SessionThread *createSession(UserId userId, bool restoreState = false);
291 void setupClientSession(QTcpSocket *socket, UserId uid);
292 void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
293 //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
294 QString setupCore(const QVariant &setupData);
296 bool registerStorageBackend(Storage *);
297 void unregisterStorageBackend(Storage *);
299 QHash<UserId, SessionThread *> sessions;
301 QTimer _storageSyncTimer;
303 #ifndef QT_NO_OPENSSL
309 QHash<QTcpSocket *, quint32> blocksizes;
310 QHash<QTcpSocket *, QVariantMap> clientInfo;
312 QHash<QString, Storage *> _storageBackends;