mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
added irc commands
This commit is contained in:
parent
27d6cf2bfd
commit
765a75f158
6 changed files with 132 additions and 8 deletions
|
@ -133,6 +133,7 @@ SOURCES += \
|
||||||
src/providers/irc/Irc2.cpp \
|
src/providers/irc/Irc2.cpp \
|
||||||
src/providers/irc/IrcAccount.cpp \
|
src/providers/irc/IrcAccount.cpp \
|
||||||
src/providers/irc/IrcChannel2.cpp \
|
src/providers/irc/IrcChannel2.cpp \
|
||||||
|
src/providers/irc/IrcCommands.cpp \
|
||||||
src/providers/irc/IrcConnection2.cpp \
|
src/providers/irc/IrcConnection2.cpp \
|
||||||
src/providers/irc/IrcServer.cpp \
|
src/providers/irc/IrcServer.cpp \
|
||||||
src/providers/LinkResolver.cpp \
|
src/providers/LinkResolver.cpp \
|
||||||
|
@ -302,6 +303,7 @@ HEADERS += \
|
||||||
src/providers/irc/Irc2.hpp \
|
src/providers/irc/Irc2.hpp \
|
||||||
src/providers/irc/IrcAccount.hpp \
|
src/providers/irc/IrcAccount.hpp \
|
||||||
src/providers/irc/IrcChannel2.hpp \
|
src/providers/irc/IrcChannel2.hpp \
|
||||||
|
src/providers/irc/IrcCommands.hpp \
|
||||||
src/providers/irc/IrcConnection2.hpp \
|
src/providers/irc/IrcConnection2.hpp \
|
||||||
src/providers/irc/IrcServer.hpp \
|
src/providers/irc/IrcServer.hpp \
|
||||||
src/providers/LinkResolver.hpp \
|
src/providers/LinkResolver.hpp \
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include "debug/AssertInGuiThread.hpp"
|
#include "debug/AssertInGuiThread.hpp"
|
||||||
#include "messages/MessageBuilder.hpp"
|
#include "messages/MessageBuilder.hpp"
|
||||||
|
#include "providers/irc/IrcCommands.hpp"
|
||||||
#include "providers/irc/IrcServer.hpp"
|
#include "providers/irc/IrcServer.hpp"
|
||||||
|
|
||||||
namespace chatterino {
|
namespace chatterino {
|
||||||
|
@ -17,15 +18,26 @@ void IrcChannel::sendMessage(const QString &message)
|
||||||
{
|
{
|
||||||
assertInGuiThread();
|
assertInGuiThread();
|
||||||
|
|
||||||
if (this->server())
|
if (message.startsWith("/"))
|
||||||
this->server()->sendMessage(this->getName(), message);
|
{
|
||||||
|
int index = message.indexOf(' ', 1);
|
||||||
|
QString command = message.mid(1, index - 1);
|
||||||
|
QString params = index == -1 ? "" : message.mid(index + 1);
|
||||||
|
|
||||||
MessageBuilder builder;
|
invokeIrcCommand(command, params, *this);
|
||||||
builder.emplace<TimestampElement>();
|
}
|
||||||
builder.emplace<TextElement>(this->server()->nick() + ":",
|
else
|
||||||
MessageElementFlag::Username);
|
{
|
||||||
builder.emplace<TextElement>(message, MessageElementFlag::Text);
|
if (this->server())
|
||||||
this->addMessage(builder.release());
|
this->server()->sendMessage(this->getName(), message);
|
||||||
|
|
||||||
|
MessageBuilder builder;
|
||||||
|
builder.emplace<TimestampElement>();
|
||||||
|
builder.emplace<TextElement>(this->server()->nick() + ":",
|
||||||
|
MessageElementFlag::Username);
|
||||||
|
builder.emplace<TextElement>(message, MessageElementFlag::Text);
|
||||||
|
this->addMessage(builder.release());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IrcServer *IrcChannel::server()
|
IrcServer *IrcChannel::server()
|
||||||
|
|
76
src/providers/irc/IrcCommands.cpp
Normal file
76
src/providers/irc/IrcCommands.cpp
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
#include "IrcCommands.hpp"
|
||||||
|
|
||||||
|
#include "messages/MessageBuilder.hpp"
|
||||||
|
#include "providers/irc/IrcChannel2.hpp"
|
||||||
|
#include "providers/irc/IrcServer.hpp"
|
||||||
|
#include "util/Overloaded.hpp"
|
||||||
|
#include "util/QStringHash.hpp"
|
||||||
|
|
||||||
|
namespace chatterino {
|
||||||
|
|
||||||
|
Outcome invokeIrcCommand(const QString &commandName, const QString &allParams,
|
||||||
|
IrcChannel &channel)
|
||||||
|
{
|
||||||
|
if (!channel.server())
|
||||||
|
{
|
||||||
|
return Failure;
|
||||||
|
}
|
||||||
|
|
||||||
|
// STATIC MESSAGES
|
||||||
|
static auto staticMessages = std::unordered_map<QString, QString>{
|
||||||
|
{"join", "/join is not supported. Press ctrl+r to change the "
|
||||||
|
"channel. If required use /raw JOIN #channel."},
|
||||||
|
{"part", "/part is not supported. Press ctrl+r to change the "
|
||||||
|
"channel. If required use /raw PART #channel."},
|
||||||
|
};
|
||||||
|
auto cmd = commandName.toLower();
|
||||||
|
|
||||||
|
if (auto it = staticMessages.find(cmd); it != staticMessages.end())
|
||||||
|
{
|
||||||
|
channel.addMessage(makeSystemMessage(it->second));
|
||||||
|
return Success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// CUSTOM COMMANDS
|
||||||
|
auto params = allParams.split(' ');
|
||||||
|
auto paramsAfter = [&](int i) { return params.mid(i + 1).join(' '); };
|
||||||
|
|
||||||
|
auto sendRaw = [&](QString str) { channel.server()->sendRawMessage(str); };
|
||||||
|
|
||||||
|
if (cmd == "msg")
|
||||||
|
{
|
||||||
|
sendRaw("PRIVMSG " + params[0] + " :" + paramsAfter(0));
|
||||||
|
}
|
||||||
|
else if (cmd == "away")
|
||||||
|
{
|
||||||
|
sendRaw("AWAY" + params[0] + " :" + paramsAfter(0));
|
||||||
|
}
|
||||||
|
else if (cmd == "knock")
|
||||||
|
{
|
||||||
|
sendRaw("KNOCK #" + params[0] + " " + paramsAfter(0));
|
||||||
|
}
|
||||||
|
else if (cmd == "kick")
|
||||||
|
{
|
||||||
|
if (paramsAfter(1).isEmpty())
|
||||||
|
sendRaw("KICK " + params[0] + " " + params[1]);
|
||||||
|
else
|
||||||
|
sendRaw("KICK " + params[0] + " " + params[1] + " :" +
|
||||||
|
paramsAfter(1));
|
||||||
|
}
|
||||||
|
else if (cmd == "wallops")
|
||||||
|
{
|
||||||
|
sendRaw("WALLOPS :" + allParams);
|
||||||
|
}
|
||||||
|
else if (cmd == "raw")
|
||||||
|
{
|
||||||
|
sendRaw(allParams);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sendRaw(cmd.toUpper() + " " + allParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Success;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace chatterino
|
12
src/providers/irc/IrcCommands.hpp
Normal file
12
src/providers/irc/IrcCommands.hpp
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "common/Outcome.hpp"
|
||||||
|
|
||||||
|
namespace chatterino {
|
||||||
|
|
||||||
|
class IrcChannel;
|
||||||
|
|
||||||
|
Outcome invokeIrcCommand(const QString &command, const QString ¶ms,
|
||||||
|
IrcChannel &channel);
|
||||||
|
|
||||||
|
} // namespace chatterino
|
|
@ -107,6 +107,25 @@ void IrcServer::initializeConnection(IrcConnection *connection,
|
||||||
this, [](const QString &reserved, QString *result) {
|
this, [](const QString &reserved, QString *result) {
|
||||||
*result = reserved + (std::rand() % 100);
|
*result = reserved + (std::rand() % 100);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QObject::connect(connection, &Communi::IrcConnection::noticeMessageReceived,
|
||||||
|
this, [this](Communi::IrcNoticeMessage *message) {
|
||||||
|
MessageBuilder builder;
|
||||||
|
|
||||||
|
builder.emplace<TimestampElement>();
|
||||||
|
builder.emplace<TextElement>(
|
||||||
|
message->nick(), MessageElementFlag::Username);
|
||||||
|
builder.emplace<TextElement>(
|
||||||
|
"-> you:", MessageElementFlag::Username);
|
||||||
|
builder.emplace<TextElement>(message->content(),
|
||||||
|
MessageElementFlag::Text);
|
||||||
|
|
||||||
|
auto msg = builder.release();
|
||||||
|
|
||||||
|
for (auto &&weak : this->channels)
|
||||||
|
if (auto shared = weak.lock())
|
||||||
|
shared->addMessage(msg);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Channel> IrcServer::createChannel(const QString &channelName)
|
std::shared_ptr<Channel> IrcServer::createChannel(const QString &channelName)
|
||||||
|
@ -211,6 +230,7 @@ void IrcServer::readConnectionMessageReceived(Communi::IrcMessage *message)
|
||||||
}
|
}
|
||||||
|
|
||||||
case Communi::IrcMessage::Pong:
|
case Communi::IrcMessage::Pong:
|
||||||
|
case Communi::IrcMessage::Notice:
|
||||||
return;
|
return;
|
||||||
|
|
||||||
default:;
|
default:;
|
||||||
|
|
|
@ -294,6 +294,8 @@ void SelectChannelDialog::setSelectedChannel(IndirectChannel _channel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this->ui_.irc.channel->setFocus();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in a new issue