Quassel IRC  Pre-Release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
core.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2015 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  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #ifndef CORE_H
22 #define CORE_H
23 
24 #include <QDateTime>
25 #include <QString>
26 #include <QVariant>
27 #include <QTimer>
28 
29 #ifdef HAVE_SSL
30 # include <QSslSocket>
31 # include "sslserver.h"
32 #else
33 # include <QTcpSocket>
34 # include <QTcpServer>
35 #endif
36 
37 #include "bufferinfo.h"
38 #include "message.h"
39 #include "oidentdconfiggenerator.h"
40 #include "sessionthread.h"
41 #include "storage.h"
42 #include "types.h"
43 
44 class CoreAuthHandler;
45 class CoreSession;
46 struct NetworkInfo;
47 class SessionThread;
48 class SignalProxy;
49 
52 
53 class Core : public QObject
54 {
55  Q_OBJECT
56 
57 public:
58  static Core *instance();
59  static void destroy();
60 
61  static void saveState();
62  static void restoreState();
63 
64  /*** Storage access ***/
65  // These methods are threadsafe.
66 
68 
73  static inline UserId validateUser(const QString &userName, const QString &password) {
74  return instance()->_storage->validateUser(userName, password);
75  }
76 
77 
79 
84  static bool changeUserPassword(UserId userId, const QString &password);
85 
87 
92  static inline void setUserSetting(UserId userId, const QString &settingName, const QVariant &data)
93  {
94  instance()->_storage->setUserSetting(userId, settingName, data);
95  }
96 
97 
99 
105  static inline QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultValue = QVariant())
106  {
107  return instance()->_storage->getUserSetting(userId, settingName, defaultValue);
108  }
109 
110 
111  /* Identity handling */
112  static inline IdentityId createIdentity(UserId user, CoreIdentity &identity)
113  {
114  return instance()->_storage->createIdentity(user, identity);
115  }
116 
117 
118  static bool updateIdentity(UserId user, const CoreIdentity &identity)
119  {
120  return instance()->_storage->updateIdentity(user, identity);
121  }
122 
123 
124  static void removeIdentity(UserId user, IdentityId identityId)
125  {
126  instance()->_storage->removeIdentity(user, identityId);
127  }
128 
129 
130  static QList<CoreIdentity> identities(UserId user)
131  {
132  return instance()->_storage->identities(user);
133  }
134 
135 
137 
143  static bool createNetwork(UserId user, NetworkInfo &info);
144 
146 
152  static inline bool updateNetwork(UserId user, const NetworkInfo &info)
153  {
154  return instance()->_storage->updateNetwork(user, info);
155  }
156 
157 
159 
165  static inline bool removeNetwork(UserId user, const NetworkId &networkId)
166  {
167  return instance()->_storage->removeNetwork(user, networkId);
168  }
169 
170 
172 
177  static inline QList<NetworkInfo> networks(UserId user)
178  {
179  return instance()->_storage->networks(user);
180  }
181 
182 
184 
189  static inline QList<NetworkId> connectedNetworks(UserId user)
190  {
191  return instance()->_storage->connectedNetworks(user);
192  }
193 
194 
196 
202  static inline void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected)
203  {
204  return instance()->_storage->setNetworkConnected(user, networkId, isConnected);
205  }
206 
207 
209 
215  static inline QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId)
216  {
217  return instance()->_storage->persistentChannels(user, networkId);
218  }
219 
220 
222 
229  static inline void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined)
230  {
231  return instance()->_storage->setChannelPersistent(user, networkId, channel, isJoined);
232  }
233 
234 
236 
243  static inline void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key)
244  {
245  return instance()->_storage->setPersistentChannelKey(user, networkId, channel, key);
246  }
247 
248 
250 
255  static inline QString awayMessage(UserId user, NetworkId networkId)
256  {
257  return instance()->_storage->awayMessage(user, networkId);
258  }
259 
260 
262 
268  static inline void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg)
269  {
270  return instance()->_storage->setAwayMessage(user, networkId, awayMsg);
271  }
272 
273 
275 
280  static inline QString userModes(UserId user, NetworkId networkId)
281  {
282  return instance()->_storage->userModes(user, networkId);
283  }
284 
285 
287 
293  static inline void setUserModes(UserId user, NetworkId networkId, const QString &userModes)
294  {
295  return instance()->_storage->setUserModes(user, networkId, userModes);
296  }
297 
298 
300 
309  static inline BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true)
310  {
311  return instance()->_storage->bufferInfo(user, networkId, type, buffer, create);
312  }
313 
314 
316 
321  static inline BufferInfo getBufferInfo(UserId user, const BufferId &bufferId)
322  {
323  return instance()->_storage->getBufferInfo(user, bufferId);
324  }
325 
326 
328 
333  static inline bool storeMessage(Message &message)
334  {
335  return instance()->_storage->logMessage(message);
336  }
337 
338 
340 
345  static inline bool storeMessages(MessageList &messages)
346  {
347  return instance()->_storage->logMessages(messages);
348  }
349 
350 
352 
358  static inline QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1)
359  {
360  return instance()->_storage->requestMsgs(user, bufferId, first, last, limit);
361  }
362 
363 
365 
370  static inline QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1)
371  {
372  return instance()->_storage->requestAllMsgs(user, first, last, limit);
373  }
374 
375 
377 
383  static inline QList<BufferInfo> requestBuffers(UserId user)
384  {
385  return instance()->_storage->requestBuffers(user);
386  }
387 
388 
390 
396  static inline QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId)
397  {
398  return instance()->_storage->requestBufferIdsForNetwork(user, networkId);
399  }
400 
401 
403 
410  static inline bool removeBuffer(const UserId &user, const BufferId &bufferId)
411  {
412  return instance()->_storage->removeBuffer(user, bufferId);
413  }
414 
415 
417 
423  static inline bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName)
424  {
425  return instance()->_storage->renameBuffer(user, bufferId, newName);
426  }
427 
428 
430 
436  static inline bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2)
437  {
438  return instance()->_storage->mergeBuffersPermanently(user, bufferId1, bufferId2);
439  }
440 
441 
443 
450  static inline void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId)
451  {
452  return instance()->_storage->setBufferLastSeenMsg(user, bufferId, msgId);
453  }
454 
455 
457 
462  static inline QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user)
463  {
464  return instance()->_storage->bufferLastSeenMsgIds(user);
465  }
466 
467 
469 
476  static inline void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId)
477  {
478  return instance()->_storage->setBufferMarkerLineMsg(user, bufferId, msgId);
479  }
480 
481 
483 
488  static inline QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user)
489  {
490  return instance()->_storage->bufferMarkerLineMsgIds(user);
491  }
492 
493 
494  static inline QDateTime startTime() { return instance()->_startTime; }
495  static inline bool isConfigured() { return instance()->_configured; }
496  static bool sslSupported();
497  static QVariantList backendInfo();
498 
499  static QString setup(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData);
500 
501  static inline QTimer &syncTimer() { return instance()->_storageSyncTimer; }
502 
504 
505  static const int AddClientEventId;
506 
507 public slots:
509 
511  void syncStorage();
512  void setupInternalClientSession(InternalPeer *clientConnection);
513  QString setupCore(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData);
514 
515 signals:
517  void bufferInfoUpdated(UserId user, const BufferInfo &info);
518 
521 
522 protected:
523  virtual void customEvent(QEvent *event);
524 
525 private slots:
526  bool startListening();
527  void stopListening(const QString &msg = QString());
528  void incomingConnection();
529  void clientDisconnected();
530 
531  bool initStorage(const QString &backend, const QVariantMap &settings, bool setup = false);
532 
533  void socketError(QAbstractSocket::SocketError err, const QString &errorString);
535 
536  void changeUserPass(const QString &username);
537 
538 private:
539  Core();
540  ~Core();
541  void init();
542  static Core *instanceptr;
543 
544  SessionThread *sessionForUser(UserId userId, bool restoreState = false);
545  void addClientHelper(RemotePeer *peer, UserId uid);
546  //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
547  QString setupCoreForInternalUsage();
548 
553  bool selectBackend(const QString &backend);
554  void createUser();
555  void saveBackendSettings(const QString &backend, const QVariantMap &settings);
556  QVariantMap promptForSettings(const Storage *storage);
557 
558 private:
559  QSet<CoreAuthHandler *> _connectingClients;
560  QHash<UserId, SessionThread *> _sessions;
563 
564 #ifdef HAVE_SSL
565  SslServer _server, _v6server;
566 #else
567  QTcpServer _server, _v6server;
568 #endif
569 
571 
572  QHash<QString, Storage *> _storageBackends;
573 
574  QDateTime _startTime;
575 
577 
580  static void stdInEcho(bool on);
581  static inline void enableStdInEcho() { stdInEcho(true); }
582  static inline void disableStdInEcho() { stdInEcho(false); }
583 };
584 
585 
586 #endif