The complete source code of IdealIRC http://www.idealirc.org/
 
 
 
 
idealirc/IdealIRC/InputHandler.cpp

71 lines
2.0 KiB

/*
* IdealIRC - Internet Relay Chat client
* Copyright (C) 2021 Tom-Andre Barstad.
* This software is licensed under the Software Attribution License.
* See LICENSE for more information.
*/
#include "InputHandler.h"
#include "IWin/IWin.h"
#include "ICommand/ICommand.h"
#include "ICommand/Commands.h"
#include "IRC.h"
#include "ConfigMgr.h"
#include "IRCClient/IRCMember.h"
#include "IRCClient/IRCChannel.h"
#include "IRCClient/Commands.h"
#include <QDebug>
InputHandler::InputHandler(IRC& connection_)
: connection(connection_)
, cmdHndl(connection_)
{}
void InputHandler::parse(IWin& sender, const QString& text, bool handleCommand)
{
if (text.isEmpty())
return;
if (text[0] == '/' && handleCommand)
cmdHndl.parse(text);
else {
if (sender.getType() == IWin::Type::Status) {
sender.print(PrintType::ProgramInfo, "You're not in a chat window!");
return;
}
else if (!connection.isOnline())
sender.print(PrintType::ProgramInfo, "Not connected to a server");
else {
const std::string target = sender.getButtonText().toStdString();
std::string me = connection.getNickname();
connection.command(Command::IRC::PRIVMSG, { target }, text.toStdString());
if (ConfigMgr::instance().common("ShowModeInMessage") == "1") do {
const auto& myChannels = connection.channels();
auto chanIt = std::find_if(myChannels.begin(), myChannels.end(),
[&target](std::shared_ptr<IRCChannel> ptr) {
return target == ptr->name();
});
if (chanIt == myChannels.end()) {
qWarning() << "Could not find channel '" << target.c_str() << "' internally, although it seems we're on it anyway.";
break;
}
const auto& member = (*chanIt)->getMember(me)->get();
const auto prefixes = connection.toMemberPrefix(member.modes());
if (!prefixes.empty())
me.insert(me.begin(), prefixes[0]);
} while(0);
sender.print(PrintType::OwnText, QStringLiteral("<%1> %2")
.arg(me.c_str())
.arg(text));
}
}
}
void InputHandler::parseCommand(const QString& text)
{
cmdHndl.parse(text);
}