mirror of https://notabug.org/acetone/ircabot.git
159 lines
5.1 KiB
C++
159 lines
5.1 KiB
C++
/*
|
|
This file is part of IRCaBot.
|
|
IRCaBot is IRC logger with features.
|
|
Source code: https://notabug.org/acetone/ircabot.
|
|
Copyright (C) 2023, acetone.
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "ircnetworkclient.h"
|
|
#include "currenttime.h"
|
|
#include "adminircpanel.h"
|
|
|
|
#include <QDateTime>
|
|
#include <QStringList>
|
|
#include <QDebug>
|
|
|
|
#define LOG_WARN qWarning().noquote() << "[" + m_config->networkName + "]"
|
|
#define LOG_INFO qInfo().noquote() << "[" + m_config->networkName + "]"
|
|
|
|
IRCNetworkClient::IRCNetworkClient(QObject *parent) : QObject(parent) {}
|
|
|
|
void IRCNetworkClient::reloadChannelList()
|
|
{
|
|
QStringList connectedChannelsTotalList;
|
|
|
|
// PART
|
|
QStringList channelsToPart;
|
|
QListIterator<libircclient::Channel*> connectedChannelListIterator(m_network->GetChannels());
|
|
while (connectedChannelListIterator.hasNext())
|
|
{
|
|
auto connectedChan = connectedChannelListIterator.next();
|
|
if (not m_config->channels.contains(connectedChan->GetName()))
|
|
{
|
|
channelsToPart.push_back(connectedChan->GetName());
|
|
}
|
|
connectedChannelsTotalList.push_back(connectedChan->GetName());
|
|
}
|
|
|
|
QStringListIterator channelsToPartIter (channelsToPart);
|
|
while (channelsToPartIter.hasNext())
|
|
{
|
|
const auto chanName = channelsToPartIter.next();
|
|
m_network->RequestPart(chanName);
|
|
QThread::msleep(500);
|
|
}
|
|
|
|
// JOIN
|
|
QStringList channelsToJoin;
|
|
QStringListIterator newChannelListIter (m_config->channels);
|
|
while (newChannelListIter.hasNext())
|
|
{
|
|
auto newChannel = newChannelListIter.next();
|
|
if (not connectedChannelsTotalList.contains(newChannel))
|
|
{
|
|
channelsToJoin.push_back(newChannel);
|
|
}
|
|
}
|
|
|
|
QStringListIterator channelsToJoinIter (channelsToJoin);
|
|
while (channelsToJoinIter.hasNext())
|
|
{
|
|
const auto chanName = channelsToJoinIter.next();
|
|
m_network->RequestJoin(chanName);
|
|
QThread::msleep(500);
|
|
}
|
|
}
|
|
|
|
void IRCNetworkClient::start()
|
|
{
|
|
libirc::ServerAddress address(m_config->host);
|
|
address.SetPort(m_config->port);
|
|
address.SetSSL(m_config->ssl);
|
|
address.SetNick(m_config->nickname);
|
|
address.SetPassword(m_config->password);
|
|
address.SetIdent(m_config->ident);
|
|
address.SetRealname(m_config->realName);
|
|
address.SetSuffix(m_config->channels.join(','));
|
|
|
|
m_network = new libircclient::Network(address, m_config->networkName);
|
|
m_network->SetDefaultIdentifyString(m_config->identifyFormat);
|
|
m_network->Connect();
|
|
|
|
connect(m_network, &libircclient::Network::Event_WhoisRegNick, this, &IRCNetworkClient::Slot_adminIdentified);
|
|
connect(m_network, &libircclient::Network::Event_PRIVMSG, this, &IRCNetworkClient::Slot_privMsg);
|
|
}
|
|
|
|
void IRCNetworkClient::Slot_privMsg(libircclient::Parser *p)
|
|
{
|
|
const QString receiver = p->GetParameterLine();
|
|
const QString sender = p->GetSourceUserInfo()->GetNick();
|
|
const QString text = p->GetText();
|
|
|
|
if (receiver == m_network->GetLocalUserInfo()->GetNick())
|
|
{
|
|
emit Event_NewDirectMessage(sender, text);
|
|
}
|
|
else
|
|
{
|
|
emit Event_NewChannelMessage(receiver, sender, text);
|
|
}
|
|
}
|
|
|
|
void IRCNetworkClient::Slot_adminIdentified(libircclient::Parser* p)
|
|
{
|
|
auto params = p->GetParameters();
|
|
if (params.size() < 2)
|
|
{
|
|
LOG_WARN << "Event_WhoisRegNick failure: params.size() < 2, expected 2";
|
|
return;
|
|
}
|
|
if (p->GetParameters().at(1) != m_config->ircabotAdmin)
|
|
{
|
|
LOG_WARN << "Event_WhoisRegNick failure: requested, but admin changed";
|
|
return;
|
|
}
|
|
|
|
LOG_INFO << "Admin authorized:" << m_config->ircabotAdmin;
|
|
AdminIRCPanel::identifiedTimestampUpdate(m_network->GetNetworkName());
|
|
m_network->SendMessage(m_config->ircabotAdmin, "You have been identified as an IRCaBot administrator. "
|
|
"Timeout: " + QString::number(Config::ircAdminAuthorizationTimeout()) + " secs");
|
|
}
|
|
|
|
void IRCNetworkClient::Slot_onDirectMessage(QString sender, QString text)
|
|
{
|
|
if (sender != m_config->ircabotAdmin) return;
|
|
|
|
QStringList answer;
|
|
if (not AdminIRCPanel::parse(m_network->GetNetworkName(), text, answer))
|
|
{
|
|
m_network->RequestWhois(sender, libircclient::Priority_Low);
|
|
}
|
|
|
|
LOG_INFO << "Admin is doing something...";
|
|
|
|
QStringListIterator answerIter (answer);
|
|
while (answerIter.hasNext())
|
|
{
|
|
m_network->SendMessage(sender, answerIter.next(), libircclient::Priority_RealTime);
|
|
if (answerIter.hasNext()) QThread::msleep(100);
|
|
}
|
|
}
|
|
|
|
void IRCNetworkClient::Slot_onChannelMessage(QString channel, QString sender, QString text)
|
|
{
|
|
|
|
}
|