Testing the CIA bot.
[quassel.git] / core / core.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005/06 by The Quassel Team                             *
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) any later version.                                   *
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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "core.h"
22 #include "server.h"
23 #include "global.h"
24 #include "util.h"
25 #include "coreproxy.h"
26
27 #include <QtSql>
28 #include <QSettings>
29
30 Core::Core() {
31   if(core) qFatal("Trying to instantiate more than one Core object!");
32
33   connect(coreProxy, SIGNAL(requestServerStates()), this, SIGNAL(serverStateRequested()));
34   connect(coreProxy, SIGNAL(gsRequestConnect(QStringList)), this, SLOT(connectToIrc(QStringList)));
35   connect(coreProxy, SIGNAL(gsUserInput(BufferId, QString)), this, SLOT(msgFromGUI(BufferId, QString)));
36   connect(coreProxy, SIGNAL(gsImportBacklog()), &backlog, SLOT(importOldBacklog()));
37   connect(coreProxy, SIGNAL(gsRequestBacklog(BufferId, QVariant, QVariant)), this, SLOT(sendBacklog(BufferId, QVariant, QVariant)));
38   connect(this, SIGNAL(displayMsg(Message)), coreProxy, SLOT(csDisplayMsg(Message)));
39   connect(this, SIGNAL(displayStatusMsg(QString, QString)), coreProxy, SLOT(csDisplayStatusMsg(QString, QString)));
40   connect(this, SIGNAL(backlogData(BufferId, QList<QVariant>, bool)), coreProxy, SLOT(csBacklogData(BufferId, QList<QVariant>, bool)));
41   connect(&backlog, SIGNAL(bufferIdUpdated(BufferId)), coreProxy, SLOT(csUpdateBufferId(BufferId)));
42   connect(this, SIGNAL(bufferIdUpdated(BufferId)), coreProxy, SLOT(csUpdateBufferId(BufferId)));
43   // Read global settings from config file
44   QSettings s;
45   s.beginGroup("Global");
46   QString key;
47   foreach(key, s.childKeys()) {
48     global->updateData(key, s.value(key));
49   }
50   backlog.init("Default"); // FIXME
51   global->updateData("CoreReady", true);
52   // Now that we are in sync, we can connect signals to automatically store further updates.
53   // I don't think we care if global data changed locally or if it was updated by a client. 
54   connect(global, SIGNAL(dataUpdatedRemotely(QString)), SLOT(globalDataUpdated(QString)));
55   connect(global, SIGNAL(dataPutLocally(QString)), SLOT(globalDataUpdated(QString)));
56
57 }
58
59 Core::~Core() {
60   //foreach(Server *s, servers) {
61   //  delete s;
62   //}
63 }
64
65 void Core::globalDataUpdated(QString key) {
66   QVariant data = global->getData(key);
67   QSettings s;
68   s.setValue(QString("Global/")+key, data);
69 }
70
71 void Core::connectToIrc(QStringList networks) {
72   foreach(QString net, networks) {
73     if(servers.contains(net)) {
74
75     } else {
76       Server *server = new Server(net);
77       connect(this, SIGNAL(serverStateRequested()), server, SLOT(sendState()));
78       connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
79       connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
80       connect(this, SIGNAL(msgFromGUI(QString, QString, QString)), server, SLOT(userInput(QString, QString, QString)));
81       connect(server, SIGNAL(serverState(QString, VarMap)), coreProxy, SLOT(csServerState(QString, VarMap)));
82       //connect(server, SIGNAL(displayMsg(Message)), this, SLOT(recvMessageFromServer(Message)));
83       connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
84       connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
85       connect(server, SIGNAL(modeSet(QString, QString, QString)), coreProxy, SLOT(csModeSet(QString, QString, QString)));
86       connect(server, SIGNAL(topicSet(QString, QString, QString)), coreProxy, SLOT(csTopicSet(QString, QString, QString)));
87       connect(server, SIGNAL(nickAdded(QString, QString, VarMap)), coreProxy, SLOT(csNickAdded(QString, QString, VarMap)));
88       connect(server, SIGNAL(nickRenamed(QString, QString, QString)), coreProxy, SLOT(csNickRenamed(QString, QString, QString)));
89       connect(server, SIGNAL(nickRemoved(QString, QString)), coreProxy, SLOT(csNickRemoved(QString, QString)));
90       connect(server, SIGNAL(nickUpdated(QString, QString, VarMap)), coreProxy, SLOT(csNickUpdated(QString, QString, VarMap)));
91       connect(server, SIGNAL(ownNickSet(QString, QString)), coreProxy, SLOT(csOwnNickSet(QString, QString)));
92       connect(server, SIGNAL(queryRequested(QString, QString)), coreProxy, SLOT(csQueryRequested(QString, QString)));
93       // add error handling
94       connect(server, SIGNAL(connected(QString)), coreProxy, SLOT(csServerConnected(QString)));
95       connect(server, SIGNAL(disconnected(QString)), this, SLOT(serverDisconnected(QString)));
96
97       server->start();
98       servers[net] = server;
99     }
100     emit connectToIrc(net);
101   }
102 }
103
104 void Core::serverDisconnected(QString net) {
105   delete servers[net];
106   servers.remove(net);
107   coreProxy->csServerDisconnected(net);
108 }
109
110 void Core::msgFromGUI(BufferId bufid, QString msg) {
111   emit msgFromGUI(bufid.network(), bufid.buffer(), msg);
112 }
113
114 // ALL messages coming pass through these functions before going to the GUI.
115 // So this is the perfect place for storing the backlog and log stuff.
116
117 void Core::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
118   Server *s = qobject_cast<Server*>(this->sender());
119   Q_ASSERT(s);
120   BufferId buf;
121   if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
122     buf = backlog.getBufferId(s->getNetwork(), nickFromMask(sender));
123   } else {
124     buf = backlog.getBufferId(s->getNetwork(), target);
125   }
126   Message msg(buf, type, text, sender, flags);
127   msg.msgId = backlog.logMessage(msg);
128   emit displayMsg(msg);
129 }
130 /*
131 void Core::recvMessageFromServer(Message msg) {
132   Server *s = qobject_cast<Server*>(sender());
133   Q_ASSERT(s);
134   logMessage(s->getNetwork(), msg);
135   emit displayMsg(s->getNetwork(), msg);
136 }
137 */
138
139 void Core::recvStatusMsgFromServer(QString msg) {
140   Server *s = qobject_cast<Server*>(sender());
141   Q_ASSERT(s);
142   emit displayStatusMsg(s->getNetwork(), msg);
143 }
144
145 QList<BufferId> Core::getBuffers() {
146   return backlog.requestBuffers();
147 }
148
149 void Core::sendBacklog(BufferId id, QVariant v1, QVariant v2) {
150   QList<QVariant> log;
151   QList<Message> msglist;
152   if(v1.type() == QVariant::DateTime) {
153
154
155   } else {
156     msglist = backlog.requestMsgs(id, v1.toInt(), v2.toInt());
157   }
158
159   // Send messages out in smaller packages - we don't want to make the signal data too large!
160   for(int i = 0; i < msglist.count(); i++) {
161     log.append(QVariant::fromValue(msglist[i]));
162     if(log.count() >= 5) {
163       emit backlogData(id, log, i >= msglist.count() - 1);
164       log.clear();
165     }
166   }
167   if(log.count() > 0) emit backlogData(id, log, true);
168 }
169
170
171 Core *core = 0;