-void CoreSession::createNetwork(const NetworkInfo &_info, bool useId) {
- NetworkInfo info = _info;
- int id;
- if(useId && info.networkId > 0) id = info.networkId.toInt();
- else {
- for(id = 1; id <= _networks.count(); id++) {
- if(!_networks.keys().contains(id)) break;
- }
- //qDebug() << "found free id" << i;
- info.networkId = id;
- }
- Network *net = new Network(id, this);
- connect(net, SIGNAL(connectRequested(NetworkId)), this, SLOT(connectToNetwork(NetworkId)));
- connect(net, SIGNAL(disconnectRequested(NetworkId)), this, SLOT(disconnectFromNetwork(NetworkId)));
- net->setNetworkInfo(info);
- net->setProxy(signalProxy());
- _networks[id] = net;
- signalProxy()->synchronize(net);
- CoreUserSettings s(user());
- s.storeNetworkInfo(info);
- emit networkCreated(id);
-}
-
-void CoreSession::updateNetwork(const NetworkInfo &info) {
- if(!_networks.contains(info.networkId)) {
- qWarning() << "Update request for unknown network received!";
- return;
- }
- _networks[info.networkId]->setNetworkInfo(info);
- CoreUserSettings s(user());
- s.storeNetworkInfo(info);
-}
-
-void CoreSession::removeNetwork(NetworkId id) {
- Network *net = _networks.take(id);
- if(net) {
- emit networkRemoved(id);
- CoreUserSettings s(user());
- s.removeNetworkInfo(id);
- net->deleteLater();
- }
+void CoreSession::createNetwork(const NetworkInfo& info_, const QStringList& persistentChans)
+{
+ NetworkInfo info = info_;
+ int id;
+
+ if (!info.networkId.isValid())
+ Core::createNetwork(user(), info);
+
+ if (!info.networkId.isValid()) {
+ qWarning() << qPrintable(
+ tr("CoreSession::createNetwork(): Got invalid networkId from Core when trying to create network %1!").arg(info.networkName));
+ return;
+ }
+
+ id = info.networkId.toInt();
+ if (!_networks.contains(id)) {
+ // create persistent chans
+ QRegExp rx(R"(\s*(\S+)(?:\s*(\S+))?\s*)");
+ for (const QString& channel : persistentChans) {
+ if (!rx.exactMatch(channel)) {
+ qWarning() << QString("Invalid persistent channel declaration: %1").arg(channel);
+ continue;
+ }
+ Core::bufferInfo(user(), info.networkId, BufferInfo::ChannelBuffer, rx.cap(1), true);
+ Core::setChannelPersistent(user(), info.networkId, rx.cap(1), true);
+ if (!rx.cap(2).isEmpty())
+ Core::setPersistentChannelKey(user(), info.networkId, rx.cap(1), rx.cap(2));
+ }
+
+ CoreNetwork* net = new CoreNetwork(id, this);
+ connect(net, &CoreNetwork::displayMsg, this, &CoreSession::recvMessageFromServer);
+ connect(net, &CoreNetwork::displayStatusMsg, this, &CoreSession::recvStatusMsgFromServer);
+ connect(net, &CoreNetwork::disconnected, this, &CoreSession::networkDisconnected);
+
+ net->setNetworkInfo(info);
+ net->setProxy(signalProxy());
+ _networks[id] = net;
+ signalProxy()->synchronize(net);
+ emit networkCreated(id);
+ }
+ else {
+ qWarning() << qPrintable(tr("CoreSession::createNetwork(): Trying to create a network that already exists, updating instead!"));
+ _networks[info.networkId]->requestSetNetworkInfo(info);
+ }
+}
+
+void CoreSession::removeNetwork(NetworkId id)
+{
+ // Make sure the network is disconnected!
+ CoreNetwork* net = network(id);
+ if (!net)
+ return;
+
+ if (net->connectionState() != Network::Disconnected) {
+ // make sure we no longer receive data from the tcp buffer
+ disconnect(net, &CoreNetwork::displayMsg, this, nullptr);
+ disconnect(net, &CoreNetwork::displayStatusMsg, this, nullptr);
+ connect(net, &CoreNetwork::disconnected, this, &CoreSession::destroyNetwork);
+ net->disconnectFromIrc();
+ }
+ else {
+ destroyNetwork(id);
+ }
+}
+
+void CoreSession::destroyNetwork(NetworkId id)
+{
+ Network* net = _networks.take(id);
+ if (net && Core::removeNetwork(user(), id)) {
+ // make sure that all unprocessed RawMessages from this network are removed
+ QList<RawMessage>::iterator messageIter = _messageQueue.begin();
+ while (messageIter != _messageQueue.end()) {
+ if (messageIter->networkId == id) {
+ messageIter = _messageQueue.erase(messageIter);
+ }
+ else {
+ ++messageIter;
+ }
+ }
+ // remove buffers from syncer
+ for (BufferId bufferId : Core::requestBufferIdsForNetwork(user(), id)) {
+ _bufferSyncer->removeBuffer(bufferId);
+ }
+ emit networkRemoved(id);
+ net->deleteLater();
+ }
+}
+
+void CoreSession::renameBuffer(const NetworkId& networkId, const QString& newName, const QString& oldName)
+{
+ BufferInfo bufferInfo = Core::bufferInfo(user(), networkId, BufferInfo::QueryBuffer, oldName, false);
+ if (bufferInfo.isValid()) {
+ _bufferSyncer->renameBuffer(bufferInfo.bufferId(), newName);
+ }
+}
+
+void CoreSession::clientsConnected()
+{
+ QHash<NetworkId, CoreNetwork*>::iterator netIter = _networks.begin();
+ Identity* identity = nullptr;
+ CoreNetwork* net = nullptr;
+ IrcUser* me = nullptr;
+ while (netIter != _networks.end()) {
+ net = *netIter;
+ ++netIter;
+
+ if (!net->isConnected())
+ continue;
+ identity = net->identityPtr();
+ if (!identity)
+ continue;
+ me = net->me();
+ if (!me)
+ continue;
+
+ if (identity->detachAwayEnabled() && me->isAway()) {
+ net->userInputHandler()->handleAway(BufferInfo(), QString());
+ }
+ }
+}
+
+void CoreSession::clientsDisconnected()
+{
+ QHash<NetworkId, CoreNetwork*>::iterator netIter = _networks.begin();
+ Identity* identity = nullptr;
+ CoreNetwork* net = nullptr;
+ IrcUser* me = nullptr;
+ QString awayReason;
+ while (netIter != _networks.end()) {
+ net = *netIter;
+ ++netIter;
+
+ if (!net->isConnected())
+ continue;
+
+ identity = net->identityPtr();
+ if (!identity)
+ continue;
+ me = net->me();
+ if (!me)
+ continue;
+
+ if (identity->detachAwayEnabled() && !me->isAway()) {
+ if (!identity->detachAwayReason().isEmpty())
+ awayReason = identity->detachAwayReason();
+ net->setAutoAwayActive(true);
+ // Allow handleAway() to format the current date/time in the string.
+ net->userInputHandler()->handleAway(BufferInfo(), awayReason);
+ }
+ }
+}
+
+void CoreSession::globalAway(const QString& msg, const bool skipFormatting)
+{
+ QHash<NetworkId, CoreNetwork*>::iterator netIter = _networks.begin();
+ CoreNetwork* net = nullptr;
+ while (netIter != _networks.end()) {
+ net = *netIter;
+ ++netIter;
+
+ if (!net->isConnected())
+ continue;
+
+ net->userInputHandler()->issueAway(msg, false /* no force away */, skipFormatting);
+ }
+}
+
+void CoreSession::changePassword(PeerPtr peer, const QString& userName, const QString& oldPassword, const QString& newPassword)
+{
+ Q_UNUSED(peer);
+
+ bool success = false;
+ UserId uid = Core::validateUser(userName, oldPassword);
+ if (uid.isValid() && uid == user())
+ success = Core::changeUserPassword(uid, newPassword);
+
+ signalProxy()->restrictTargetPeers(signalProxy()->sourcePeer(), [&] { emit passwordChanged(nullptr, success); });
+}
+
+void CoreSession::kickClient(int peerId)
+{
+ auto peer = signalProxy()->peerById(peerId);
+ if (peer == nullptr) {
+ qWarning() << "Invalid peer Id: " << peerId;
+ return;
+ }
+ signalProxy()->restrictTargetPeers(peer, [&] { emit disconnectFromCore(); });