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 "bufferinfo.h"
35 #include "sessionthread.h"
36 #include "sslserver.h"
44 class Core : public QObject {
48 static Core * instance();
49 static void destroy();
51 static void saveState();
52 static void restoreState();
54 /*** Storage access ***/
55 // These methods are threadsafe.
57 //! Create a Network in the Storage and store it's Id in the given NetworkInfo
58 /** \note This method is thredsafe.
60 * \param user The core user
61 * \param networkInfo a NetworkInfo definition to store the newly created ID in
62 * \return true if successfull.
64 static bool createNetwork(UserId user, NetworkInfo &info);
66 //! Apply the changes to NetworkInfo info to the storage engine
67 /** \note This method is thredsafe.
69 * \param user The core user
70 * \param networkInfo The Updated NetworkInfo
71 * \return true if successfull.
73 static bool updateNetwork(UserId user, const NetworkInfo &info);
75 //! Permanently remove a Network and all the data associated with it.
76 /** \note This method is thredsafe.
78 * \param user The core user
79 * \param networkId The network to delete
80 * \return true if successfull.
82 static bool removeNetwork(UserId user, const NetworkId &networkId);
84 //! Returns a list of all NetworkInfos for the given UserId user
85 /** \note This method is thredsafe.
87 * \param user The core user
88 * \return QList<NetworkInfo>.
90 static QList<NetworkInfo> networks(UserId user);
92 //! Get the NetworkId for a network name.
93 /** \note This method is threadsafe.
95 * \param user The core user
96 * \param network The name of the network
97 * \return The NetworkId corresponding to the given network.
99 static NetworkId networkId(UserId user, const QString &network);
101 //! Get a list of Networks to restore
102 /** Return a list of networks the user was connected at the time of core shutdown
103 * \note This method is threadsafe.
105 * \param user The User Id in question
107 static QList<NetworkId> connectedNetworks(UserId user);
109 //! Update the connected state of a network
110 /** \note This method is threadsafe
112 * \param user The Id of the networks owner
113 * \param networkId The Id of the network
114 * \param isConnected whether the network is connected or not
116 static void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected);
118 //! Get a hash of channels with their channel keys for a given network
119 /** The keys are channel names and values are passwords (possibly empty)
120 * \note This method is threadsafe
122 * \param user The id of the networks owner
123 * \param networkId The Id of the network
125 static QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId);
127 //! Update the connected state of a channel
128 /** \note This method is threadsafe
130 * \param user The Id of the networks owner
131 * \param networkId The Id of the network
132 * \param channel The name of the channel
133 * \param isJoined whether the channel is connected or not
135 static void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined);
137 //! Update the key of a channel
138 /** \note This method is threadsafe
140 * \param user The Id of the networks owner
141 * \param networkId The Id of the network
142 * \param channel The name of the channel
143 * \param key The key of the channel (possibly empty)
145 static void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key);
147 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
148 /** \note This method is threadsafe.
150 * \param user The core user who owns this buffername
151 * \param networkId The network id
152 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
153 * \param buffer The buffer name (if empty, the net's status buffer is returned)
154 * \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
156 static BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type, const QString &buffer = "");
158 //! Get the unique BufferInfo for a bufferId
159 /** \note This method is threadsafe
160 * \param user The core user who owns this buffername
161 * \param bufferId The id of the buffer
162 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
164 static BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
167 //! Store a Message in the backlog.
168 /** \note This method is threadsafe.
170 * \param msg The message object to be stored
171 * \return The globally unique id for the stored message
173 static MsgId storeMessage(const Message &message);
175 //! Request a certain number (or all) messages stored in a given buffer.
176 /** \note This method is threadsafe.
178 * \param buffer The buffer we request messages from
179 * \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
180 * \param offset Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
181 * \return The requested list of messages
183 static QList<Message> requestMsgs(UserId user, BufferId buffer, int lastmsgs = -1, int offset = -1);
185 //! Request messages stored in a given buffer since a certain point in time.
186 /** \note This method is threadsafe.
188 * \param buffer The buffer we request messages from
189 * \param since Only return messages newer than this point in time
190 * \param offset Do not return messages with MsgId >= offset, if offset >= 0
191 * \return The requested list of messages
193 static QList<Message> requestMsgs(UserId user, BufferId buffer, QDateTime since, int offset = -1);
195 //! Request a range of messages stored in a given buffer.
196 /** \note This method is threadsafe.
198 * \param buffer The buffer we request messages from
199 * \param first Return messages with first <= MsgId <= last
200 * \param last Return messages with first <= MsgId <= last
201 * \return The requested list of messages
203 static QList<Message> requestMsgRange(UserId user, BufferId buffer, int first, int last);
205 //! Request a list of all buffers known to a user since a certain point in time.
206 /** This method is used to get a list of all buffers we have stored a backlog from.
207 * Optionally, a QDateTime can be given, so that only buffers are listed that were active
208 * since that point in time.
209 * \note This method is threadsafe.
211 * \param user The user whose buffers we request
212 * \param since If this is defined, older buffers will be ignored
213 * \return A list of the BufferInfos for all buffers as requested
215 static QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime());
217 //! Remove permanently a buffer and it's content from the storage backend
218 /** This call cannot be reverted!
219 * \note This method is threadsafe.
221 * \param user The user who is the owner of the buffer
222 * \param bufferId The bufferId
223 * \return true if successfull
225 static bool removeBuffer(const UserId &user, const BufferId &bufferId);
228 /** \note This method is threadsafe.
229 * \param user The id of the buffer owner
230 * \param networkId The id of the network the buffer belongs to
231 * \param newName The new name of the buffer
232 * \param oldName The previous name of the buffer
233 * \return the BufferId of the affected buffer or an invalid BufferId if not successfull
235 static BufferId renameBuffer(const UserId &user, const NetworkId &networkId, const QString &newName, const QString &oldName);
237 //! Update the LastSeenDate for a Buffer
238 /** This Method is used to make the LastSeenDate of a Buffer persistent
239 * \note This method is threadsafe.
241 * \param user The Owner of that Buffer
242 * \param bufferId The buffer id
243 * \param MsgId The Message id of the message that has been just seen
245 static void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
247 //! Get a Hash of all last seen message ids
248 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
249 * \note This method is threadsafe.
251 * \param user The Owner of the buffers
253 static QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user);
256 //! Make storage data persistent
257 /** \note This method is threadsafe.
262 //! Sent when a BufferInfo is updated in storage.
263 void bufferInfoUpdated(UserId user, const BufferInfo &info);
266 bool startListening(uint port = Global::defaultPort);
267 void stopListening();
268 void incomingConnection();
269 void clientHasData();
270 void clientDisconnected();
272 bool initStorage(QVariantMap dbSettings, bool setup = false);
274 void sslErrors(const QList<QSslError> &errors);
275 void socketError(QAbstractSocket::SocketError);
281 static Core *instanceptr;
283 SessionThread *createSession(UserId userId, bool restoreState = false);
284 void setupClientSession(QTcpSocket *socket, UserId uid);
285 void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
286 //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
287 QString setupCore(const QVariant &setupData);
289 bool registerStorageBackend(Storage *);
290 void unregisterStorageBackend(Storage *);
292 QHash<UserId, SessionThread *> sessions;
294 QTimer _storageSyncTimer;
296 #ifndef QT_NO_OPENSSL
302 QHash<QTcpSocket *, quint32> blocksizes;
303 QHash<QTcpSocket *, QVariantMap> clientInfo;
305 QHash<QString, Storage *> _storageBackends;